master

SwiftyJSON/SwiftyJSON

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

RawTests.swift

TLDR

This file contains unit tests for the Raw functionality in the SwiftyJSON library. It tests various scenarios such as converting JSON data to raw data and raw strings, as well as handling invalid JSON.

Methods

testRawData

This method tests the conversion of JSON data to raw data. It creates a JSON object and an expected raw data representation, and then calls the rawData() method to convert the JSON data to raw data. Finally, it asserts that the converted data matches the expected raw data.

testInvalidJSONForRawData

This method tests the handling of invalid JSON when converting to raw data. It creates a JSON object with invalid JSON data and attempts to convert it to raw data. It expects an error of type SwiftyJSONError.invalidJSON to be thrown.

testArray

This method tests the conversion of an array JSON object to raw data and raw strings. It creates a JSON object representing an array, calls the rawData() method to convert it to raw data, and asserts that the converted data is not nil. It also asserts that the generated raw string has a non-zero length.

testDictionary

This method tests the conversion of a dictionary JSON object to raw data and raw strings. It creates a JSON object representing a dictionary, calls the rawData() method to convert it to raw data, and asserts that the converted data is not nil. It also asserts that the generated raw string has a non-zero length.

testString

This method tests the conversion of a string JSON object to a raw string. It creates a JSON object representing a string and asserts that the generated raw string matches the original string.

testNumber

This method tests the conversion of a number JSON object to a raw string. It creates a JSON object representing a number and asserts that the generated raw string matches the original number.

testBool

This method tests the conversion of a boolean JSON object to a raw string. It creates a JSON object representing a boolean and asserts that the generated raw string matches the original boolean.

testNull

This method tests the conversion of a JSON null object to a raw string. It creates a JSON object representing a null value and asserts that the generated raw string is "null".

testNestedJSON

This method tests the conversion of a nested JSON object to raw data and raw strings. It creates a JSON object with nested objects, calls the rawData() method to convert it to raw data, and asserts that the converted data is not nil. It also asserts that the generated raw string is not nil.

Classes

None

//  RawTests.swift
//
//  Copyright (c) 2014 - 2017 Pinglin Tang
//
//  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 RawTests: XCTestCase {

    func testRawData() {
        let json: JSON = ["somekey": "some string value"]
        let expectedRawData = "{\"somekey\":\"some string value\"}".data(using: String.Encoding.utf8)
        do {
            let data: Data = try json.rawData()
            XCTAssertEqual(expectedRawData, data)
        } catch _ {}
    }

    func testInvalidJSONForRawData() {
        let json: JSON = "...<nonsense>xyz</nonsense>"
        do {
            _ = try json.rawData()
        } catch let error as SwiftyJSONError {
            XCTAssertEqual(error, SwiftyJSONError.invalidJSON)
        } catch _ {}
    }

    func testArray() {
        let json: JSON = [1, "2", 3.12, NSNull(), true, ["name": "Jack"]]
        let data: Data?
        do {
            data = try json.rawData()
        } catch _ {
            data = nil
        }
        let string = json.rawString()
        XCTAssertTrue (data != nil)
        XCTAssertTrue (string!.lengthOfBytes(using: String.Encoding.utf8) > 0)
    }

    func testDictionary() {
        let json: JSON = ["number": 111111.23456789, "name": "Jack", "list": [1, 2, 3, 4], "bool": false, "null": NSNull()]
        let data: Data?
        do {
            data = try json.rawData()
        } catch _ {
            data = nil
        }
        let string = json.rawString()
        XCTAssertTrue (data != nil)
        XCTAssertTrue (string!.lengthOfBytes(using: String.Encoding.utf8) > 0)
    }

    func testString() {
        let json: JSON = "I'm a json"
        XCTAssertEqual(json.rawString(), "I'm a json")
    }

    func testNumber() {
        let json: JSON = 123456789.123
        XCTAssertEqual(json.rawString(), "123456789.123")
    }

    func testBool() {
        let json: JSON = true
        XCTAssertEqual(json.rawString(), "true")
    }

    func testNull() {
        let json: JSON = JSON.null
        XCTAssertEqual(json.rawString(), "null")
    }

    func testNestedJSON() {
        let inner: JSON = ["name": "john doe"]
        let json: JSON = ["level": 1337, "user": inner]
        let data: Data?
        do {
            data = try json.rawData()
        } catch _ {
            data = nil
        }
        let string = json.rawString()
        XCTAssertNotNil(data)
        XCTAssertNotNil(string)
    }
}