Created JSONTests.testDynamicAccessGet and .testDynamicAccessSet test cases

This commit is contained in:
Caleb Kleveter 2019-03-21 09:01:08 -05:00
parent fbed17ab8f
commit 6672b6fc92
No known key found for this signature in database
GPG Key ID: B38DBD5CF2C98D69
3 changed files with 73 additions and 25 deletions

View File

@ -184,10 +184,6 @@ class JSONTests: XCTestCase {
_ = try self.intialize(JSON.self, with: json)
}
func intialize<T>(_ type: T.Type, with json: JSON)throws -> T where T: Decodable {
return try T(json: json)
}
func testSingleValueEncodingFailure()throws {
struct SingleDouble: Codable {
let id: Int
@ -222,23 +218,38 @@ class JSONTests: XCTestCase {
XCTAssertEqual(decoded.name, "same ol'")
}
static var allTests: [(String, (JSONTests) -> ()throws -> ())] = [
("testJSONDecoding", testJSONDecoding),
("testJSONEncoding", testJSONEncoding),
("testDecodingSpeed", testDecodingSpeed),
("testEncodingSpeed", testEncodingSpeed),
("testToJSON", testToJSON),
("testFromJSON", testFromJSON),
("testEncodingNestedJSON", testEncodingNestedJSON),
("testEncodeNestedJSONSpeed", testEncodeNestedJSONSpeed),
("testDecodingNestedJSON", testDecodingNestedJSON),
("testDecodeNestedJSONSpeed", testDecodeNestedJSONSpeed),
("testDefaultJSONEncoding", testDefaultJSONEncoding),
("testDefaultJSONEDecoding", testDefaultJSONEDecoding),
("testSmallData", testSmallData),
("testJSONInitJSON", testJSONInitJSON),
("testSingleValueEncodingFailure", testSingleValueEncodingFailure)
]
func testDynamicAccessGet()throws {
var weather = try JSON(data: Data(json.utf8))
XCTAssertEqual(weather.minutely.data.0.time.int, 1517594040)
measure {
for _ in 0..<10_000 {
_ = weather.minutely.data.0.time
}
}
}
func testDynamicAccessSetSpeed()throws {
var weather = try JSON(data: Data(json.utf8))
weather.minutely.data.0.time.int = 1517594031
XCTAssertEqual(weather.minutely.data.0.time.int, 1517594031)
weather.minutely.data.10.time = 42
XCTAssertEqual(weather.minutely.data.10.time, 42)
measure {
for _ in 0..<10_000 {
weather.minutely.data.time = 39916800
}
}
}
// MARK: - Helpers
func intialize<T>(_ type: T.Type, with json: JSON)throws -> T where T: Decodable {
return try T(json: json)
}
}
fileprivate struct User: Codable {

View File

@ -0,0 +1,35 @@
#if !canImport(ObjectiveC)
import XCTest
extension JSONTests {
// DO NOT MODIFY: This is autogenerated, use:
// `swift test --generate-linuxmain`
// to regenerate.
static let __allTests__JSONTests = [
("testDecodeNestedJSONSpeed", testDecodeNestedJSONSpeed),
("testDecodingNestedJSON", testDecodingNestedJSON),
("testDecodingSpeed", testDecodingSpeed),
("testDefaultJSONEDecoding", testDefaultJSONEDecoding),
("testDefaultJSONEncoding", testDefaultJSONEncoding),
("testDynamicAccessGet", testDynamicAccessGet),
("testDynamicAccessSetSpeed", testDynamicAccessSetSpeed),
("testEncodeNestedJSONSpeed", testEncodeNestedJSONSpeed),
("testEncodingNestedJSON", testEncodingNestedJSON),
("testEncodingSpeed", testEncodingSpeed),
("testFailableJSONSpeed", testFailableJSONSpeed),
("testFromJSON", testFromJSON),
("testJSONDecoding", testJSONDecoding),
("testJSONEncoding", testJSONEncoding),
("testJSONInitJSON", testJSONInitJSON),
("testSingleValueEncodingFailure", testSingleValueEncodingFailure),
("testSmallData", testSmallData),
("testToJSON", testToJSON),
]
}
public func __allTests() -> [XCTestCaseEntry] {
return [
testCase(JSONTests.__allTests__JSONTests),
]
}
#endif

View File

@ -1,6 +1,8 @@
import XCTest
@testable import JSONTests
XCTMain([
testCase(JSONTests.allTests),
])
import JSONTests
var tests = [XCTestCaseEntry]()
tests += JSONTests.__allTests()
XCTMain(tests)