master

SwiftyJSON/SwiftyJSON

Last updated at: 29/12/2023 09:36

MergeTests.swift

TLDR

This file contains tests for merging JSON objects using the SwiftyJSON library.

Methods

testDifferingTypes

This method tests the merging of two JSON objects with different types. It asserts that an error with the correct error code, error domain, and error user info is thrown.

testPrimitiveType

This method tests the merging of two JSON objects with primitive types. It asserts that the result of the merge is equal to the second JSON object.

testMergeEqual

This method tests the merging of a JSON object with itself. It asserts that the result of the merge is equal to the original JSON object.

testMergeUnequalValues

This method tests the merging of two JSON objects with different values. It asserts that the result of the merge is equal to the second JSON object.

testMergeUnequalKeysAndValues

This method tests the merging of two JSON objects with different keys and values. It asserts that the result of the merge is equal to a JSON object containing both keys and values.

testMergeFilledAndEmpty

This method tests the merging of a non-empty JSON object with an empty JSON object. It asserts that the result of the merge is equal to the non-empty JSON object.

testMergeEmptyAndFilled

This method tests the merging of an empty JSON object with a non-empty JSON object. It asserts that the result of the merge is equal to the non-empty JSON object.

testMergeArray

This method tests the merging of two JSON arrays. It asserts that the result of the merge is equal to a new JSON array containing the elements from both arrays.

testMergeNestedJSONs

This method tests the merging of two JSON objects with nested JSON objects. It asserts that the result of the merge is equal to the second JSON object.

Classes

Class MergeTests

This class contains test methods for merging JSON objects using the SwiftyJSON library. The methods in this class cover different scenarios such as merging JSON objects with different types, values, keys, and nested JSON objects.

//  MergeTests.swift
//
//  Created by Daniel Kiedrowski on 17.11.16.
//
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.

import XCTest
import SwiftyJSON

class MergeTests: XCTestCase {

    func testDifferingTypes() {
        let A = JSON("a")
        let B = JSON(1)

        do {
            _ = try A.merged(with: B)
        } catch let error as SwiftyJSONError {
            XCTAssertEqual(error.errorCode, SwiftyJSONError.wrongType.rawValue)
            XCTAssertEqual(type(of: error).errorDomain, SwiftyJSONError.errorDomain)
            XCTAssertEqual(error.errorUserInfo as! [String: String], [NSLocalizedDescriptionKey: "Couldn't merge, because the JSONs differ in type on top level."])
        } catch _ {}
    }

    func testPrimitiveType() {
        let A = JSON("a")
        let B = JSON("b")
        XCTAssertEqual(try! A.merged(with: B), B)
    }

    func testMergeEqual() {
        let json = JSON(["a": "A"])
        XCTAssertEqual(try! json.merged(with: json), json)
    }

    func testMergeUnequalValues() {
        let A = JSON(["a": "A"])
        let B = JSON(["a": "B"])
        XCTAssertEqual(try! A.merged(with: B), B)
    }

    func testMergeUnequalKeysAndValues() {
        let A = JSON(["a": "A"])
        let B = JSON(["b": "B"])
        XCTAssertEqual(try! A.merged(with: B), JSON(["a": "A", "b": "B"]))
    }

    func testMergeFilledAndEmpty() {
        let A = JSON(["a": "A"])
        let B = JSON([:])
        XCTAssertEqual(try! A.merged(with: B), A)
    }

    func testMergeEmptyAndFilled() {
        let A = JSON([:])
        let B = JSON(["a": "A"])
        XCTAssertEqual(try! A.merged(with: B), B)
    }

    func testMergeArray() {
        let A = JSON(["a"])
        let B = JSON(["b"])
        XCTAssertEqual(try! A.merged(with: B), JSON(["a", "b"]))
    }

    func testMergeNestedJSONs() {
        let A = JSON([
            "nested": [
                "A": "a"
            ]
        ])

        let B = JSON([
            "nested": [
                "A": "b"
            ]
        ])

        XCTAssertEqual(try! A.merged(with: B), B)
    }
}