From 14a7e7a65a8ec32168d44ce764355494e8ef9bfb Mon Sep 17 00:00:00 2001 From: Zigii Wong Date: Mon, 7 Nov 2016 10:56:46 +0800 Subject: [PATCH 1/8] Remove print functions in test --- Tests/NumberTests.swift | 3 --- Tests/RawTests.swift | 6 ------ 2 files changed, 9 deletions(-) diff --git a/Tests/NumberTests.swift b/Tests/NumberTests.swift index 54c48806..f75e697c 100644 --- a/Tests/NumberTests.swift +++ b/Tests/NumberTests.swift @@ -103,7 +103,6 @@ class NumberTests: XCTestCase { var json = JSON(54321.12345) XCTAssertTrue(json.float! == 54321.12345) XCTAssertTrue(json.floatValue == 54321.12345) - print(json.numberValue.doubleValue) XCTAssertEqual(json.numberValue, 54321.12345) XCTAssertEqual(json.stringValue, "54321.12345") @@ -189,7 +188,6 @@ class NumberTests: XCTestCase { json.int8Value = n0.int8Value XCTAssertTrue(json.int8! == n0.int8Value) XCTAssertTrue(json.int8Value == n0.int8Value) - print(json.number) XCTAssertTrue(json.number! == n0) XCTAssertEqual(json.numberValue, n0) #if (arch(x86_64) || arch(arm64)) @@ -268,7 +266,6 @@ class NumberTests: XCTestCase { json.int16Value = n0.int16Value XCTAssertTrue(json.int16! == n0.int16Value) XCTAssertTrue(json.int16Value == n0.int16Value) - print(json.number) XCTAssertTrue(json.number! == n0) XCTAssertEqual(json.numberValue, n0) XCTAssertEqual(json.stringValue, "0") diff --git a/Tests/RawTests.swift b/Tests/RawTests.swift index a31b84ef..4107c63b 100644 --- a/Tests/RawTests.swift +++ b/Tests/RawTests.swift @@ -56,7 +56,6 @@ class RawTests: XCTestCase { let string = json.rawString() XCTAssertTrue (data != nil) XCTAssertTrue (string!.lengthOfBytes(using: String.Encoding.utf8) > 0) - print(string!) } func testDictionary() { @@ -70,30 +69,25 @@ class RawTests: XCTestCase { let string = json.rawString() XCTAssertTrue (data != nil) XCTAssertTrue (string!.lengthOfBytes(using: String.Encoding.utf8) > 0) - print(string!) } func testString() { let json:JSON = "I'm a json" - print(json.rawString()) XCTAssertTrue(json.rawString() == "I'm a json") } func testNumber() { let json:JSON = 123456789.123 - print(json.rawString()) XCTAssertTrue(json.rawString() == "123456789.123") } func testBool() { let json:JSON = true - print(json.rawString()) XCTAssertTrue(json.rawString() == "true") } func testNull() { let json:JSON = JSON.null - print(json.rawString()) XCTAssertTrue(json.rawString() == "null") } } From f4b832ac9b79d0058b9c06d3bd322d3ea3c0f86b Mon Sep 17 00:00:00 2001 From: Zigii Wong Date: Mon, 7 Nov 2016 18:34:03 +0800 Subject: [PATCH 2/8] Add XCTestCaseProvider to help out swift build in Linux --- SwiftyJSON.xcodeproj/project.pbxproj | 16 +++++++++ Tests/XCTestCaseProvider.swift | 52 ++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 Tests/XCTestCaseProvider.swift diff --git a/SwiftyJSON.xcodeproj/project.pbxproj b/SwiftyJSON.xcodeproj/project.pbxproj index d940cb1e..03a724f5 100644 --- a/SwiftyJSON.xcodeproj/project.pbxproj +++ b/SwiftyJSON.xcodeproj/project.pbxproj @@ -52,6 +52,9 @@ A8580F8B1BCF69A000DA927B /* DictionaryTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A8B66C8B19E51D6500540692 /* DictionaryTests.swift */; }; A8580F8C1BCF69A000DA927B /* ArrayTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A8B66C8D19E52F4200540692 /* ArrayTests.swift */; }; A863BE2819EED46F0092A41F /* RawTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A863BE2719EED46F0092A41F /* RawTests.swift */; }; + A869FA651DD02D2200DBCBEF /* XCTestCaseProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = A869FA641DD02D2200DBCBEF /* XCTestCaseProvider.swift */; }; + A869FA661DD02D2200DBCBEF /* XCTestCaseProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = A869FA641DD02D2200DBCBEF /* XCTestCaseProvider.swift */; }; + A869FA671DD02D2200DBCBEF /* XCTestCaseProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = A869FA641DD02D2200DBCBEF /* XCTestCaseProvider.swift */; }; A86BAA0E19EBC32B009EAAEB /* PerformanceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A86BAA0D19EBC32B009EAAEB /* PerformanceTests.swift */; }; A87080E419E3C2A600CDE086 /* SequenceTypeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A87080E319E3C2A600CDE086 /* SequenceTypeTests.swift */; }; A87080E619E3DF7800CDE086 /* ComparableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A87080E519E3DF7800CDE086 /* ComparableTests.swift */; }; @@ -110,6 +113,7 @@ A8580F741BCF5C5B00DA927B /* SwiftyJSON tvOS Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "SwiftyJSON tvOS Tests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; }; A8580F781BCF5C5B00DA927B /* Info-tvOS.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "Info-tvOS.plist"; sourceTree = ""; }; A863BE2719EED46F0092A41F /* RawTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RawTests.swift; sourceTree = ""; }; + A869FA641DD02D2200DBCBEF /* XCTestCaseProvider.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = XCTestCaseProvider.swift; sourceTree = ""; }; A86BAA0D19EBC32B009EAAEB /* PerformanceTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PerformanceTests.swift; sourceTree = ""; }; A87080E319E3C2A600CDE086 /* SequenceTypeTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SequenceTypeTests.swift; sourceTree = ""; }; A87080E519E3DF7800CDE086 /* ComparableTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ComparableTests.swift; sourceTree = ""; }; @@ -226,6 +230,7 @@ 2E4FEFEA19575BE100351305 /* Tests */ = { isa = PBXGroup; children = ( + A869FA631DD02CFF00DBCBEF /* Helpers */, A885D1DA19CFCFF0002FD4C3 /* Tests.json */, A86BAA0D19EBC32B009EAAEB /* PerformanceTests.swift */, A885D1D119CF1EE6002FD4C3 /* BaseTests.swift */, @@ -256,6 +261,14 @@ name = "Supporting Files"; sourceTree = ""; }; + A869FA631DD02CFF00DBCBEF /* Helpers */ = { + isa = PBXGroup; + children = ( + A869FA641DD02D2200DBCBEF /* XCTestCaseProvider.swift */, + ); + name = Helpers; + sourceTree = ""; + }; /* End PBXGroup section */ /* Begin PBXHeadersBuildPhase section */ @@ -550,6 +563,7 @@ buildActionMask = 2147483647; files = ( A87080E819E439DA00CDE086 /* NumberTests.swift in Sources */, + A869FA651DD02D2200DBCBEF /* XCTestCaseProvider.swift in Sources */, A87080E419E3C2A600CDE086 /* SequenceTypeTests.swift in Sources */, 5DD502911D9B21810004C112 /* NestedJSONTests.swift in Sources */, A86BAA0E19EBC32B009EAAEB /* PerformanceTests.swift in Sources */, @@ -587,6 +601,7 @@ buildActionMask = 2147483647; files = ( 9C459EFB1A9103C1008C9A41 /* SequenceTypeTests.swift in Sources */, + A869FA661DD02D2200DBCBEF /* XCTestCaseProvider.swift in Sources */, 9C459F001A9103C1008C9A41 /* ComparableTests.swift in Sources */, 5DD502921D9B21810004C112 /* NestedJSONTests.swift in Sources */, 9C459F021A9103C1008C9A41 /* NumberTests.swift in Sources */, @@ -608,6 +623,7 @@ buildActionMask = 2147483647; files = ( A8580F801BCF69A000DA927B /* PerformanceTests.swift in Sources */, + A869FA671DD02D2200DBCBEF /* XCTestCaseProvider.swift in Sources */, A8580F811BCF69A000DA927B /* BaseTests.swift in Sources */, 5DD502931D9B21810004C112 /* NestedJSONTests.swift in Sources */, A8580F821BCF69A000DA927B /* SequenceTypeTests.swift in Sources */, diff --git a/Tests/XCTestCaseProvider.swift b/Tests/XCTestCaseProvider.swift new file mode 100644 index 00000000..f300d475 --- /dev/null +++ b/Tests/XCTestCaseProvider.swift @@ -0,0 +1,52 @@ +import Foundation +import XCTest + +// XCTestCaseProvider should be adopted by all XCTestCase subclasses. It provides a +// mechanism for us to fail tests in Xcode which haven't been included in the `allTests` +// list for swift-corelibs-xctest which is unable to dynamically discover tests. Note +// that only `static var allTests` needs to be explicitly implemented, as `allTestNames` +// has a default implementation provided by a protocol extension. + +// Implementation note: This is broken down into two separate protocols because we need a +// protocol with no Self references to which we can cast XCTestCase instances in a non-generic context. + +public protocol XCTestCaseProviderStatic { + // This should be explicitly implemented by XCTestCase subclasses + static var allTests: [(String, (Self) -> () throws -> Void)] { get } +} + +public protocol XCTestCaseNameProvider { + // This does not need to be explicitly implemented because of the protocol extension below + var allTestNames: [String] { get } +} + +public protocol XCTestCaseProvider: XCTestCaseProviderStatic, XCTestCaseNameProvider {} + +extension XCTestCaseProvider where Self: XCTestCaseProviderStatic { + var allTestNames: [String] { + return type(of: self).allTests.map({ name, test in + return name + }) + } +} + +#if os(OSX) || os(iOS) || os(watchOS) || os(tvOS) + +extension XCTestCase { + override open func tearDown() { + if let provider = self as? XCTestCaseNameProvider { + provider.assertContainsTest(invocation!.selector.description) + } + + super.tearDown() + } +} + +extension XCTestCaseNameProvider { + fileprivate func assertContainsTest(_ name: String) { + let contains = self.allTestNames.contains(name) + XCTAssert(contains, "Test '\(name)' is missing from the allTests array") + } +} + +#endif From 777ba81c782d976a1db3d8167791afe16b847236 Mon Sep 17 00:00:00 2001 From: Zigii Wong Date: Mon, 7 Nov 2016 18:36:08 +0800 Subject: [PATCH 3/8] Make Tests conform XCTestCaseProvider --- Tests/ArrayTests.swift | 11 +++++++++-- Tests/BaseTests.swift | 20 ++++++++++++++++++-- Tests/ComparableTests.swift | 28 +++++++++++++++++++++++++++- Tests/DictionaryTests.swift | 11 +++++++++-- Tests/LiteralConvertibleTests.swift | 13 ++++++++++++- Tests/NestedJSONTests.swift | 10 +++++++++- Tests/NumberTests.swift | 23 +++++++++++++++++++++-- Tests/PerformanceTests.swift | 15 +++++++++++++-- Tests/PrintableTests.swift | 14 +++++++++++++- Tests/RawRepresentableTests.swift | 13 ++++++++++++- Tests/RawTests.swift | 15 ++++++++++++++- Tests/SequenceTypeTests.swift | 18 +++++++++++++++++- Tests/StringTests.swift | 13 ++++++++++++- Tests/SubscriptTests.swift | 23 ++++++++++++++++++++++- 14 files changed, 208 insertions(+), 19 deletions(-) diff --git a/Tests/ArrayTests.swift b/Tests/ArrayTests.swift index e2d471a6..b543b241 100644 --- a/Tests/ArrayTests.swift +++ b/Tests/ArrayTests.swift @@ -23,8 +23,15 @@ import XCTest import SwiftyJSON -class ArrayTests: XCTestCase { - +final class ArrayTests: XCTestCase, XCTestCaseProvider { + + static var allTests: [(String, (ArrayTests) -> () throws -> Void)] { + return [ + ("testSingleDimensionalArraysGetter", testSingleDimensionalArraysGetter), + ("testSingleDimensionalArraysSetter", testSingleDimensionalArraysSetter) + ] + } + func testSingleDimensionalArraysGetter() { let array = ["1","2", "a", "B", "D"] let json = JSON(array) diff --git a/Tests/BaseTests.swift b/Tests/BaseTests.swift index 0ec670ed..f4d39e69 100644 --- a/Tests/BaseTests.swift +++ b/Tests/BaseTests.swift @@ -23,8 +23,24 @@ import XCTest @testable import SwiftyJSON -class BaseTests: XCTestCase { - +final class BaseTests: XCTestCase, XCTestCaseProvider { + + static var allTests: [(String, (BaseTests) -> () throws -> Void)] { + return [ + ("testInit", testInit), + ("testCompare", testCompare), + ("testJSONDoesProduceValidWithCorrectKeyPath", testJSONDoesProduceValidWithCorrectKeyPath), + ("testJSONNumberCompare", testJSONNumberCompare), + ("testNumberConvertToString", testNumberConvertToString), + ("testNumberPrint", testNumberPrint), + ("testNullJSON", testNullJSON), + ("testExistance", testExistance), + ("testErrorHandle", testErrorHandle), + ("testReturnObject", testReturnObject), + ("testNumberCompare", testNumberCompare) + ] + } + var testData: Data! override func setUp() { diff --git a/Tests/ComparableTests.swift b/Tests/ComparableTests.swift index c027b07a..8c754dbd 100644 --- a/Tests/ComparableTests.swift +++ b/Tests/ComparableTests.swift @@ -23,8 +23,34 @@ import XCTest import SwiftyJSON -class ComparableTests: XCTestCase { +final class ComparableTests: XCTestCase, XCTestCaseProvider { + static var allTests: [(String, (ComparableTests) -> () throws -> Void)] { + return [ + ("testNumberEqual", testNumberEqual), + ("testNumberNotEqual", testNumberNotEqual), + ("testNumberGreaterThanOrEqual", testNumberGreaterThanOrEqual), + ("testNumberLessThanOrEqual", testNumberLessThanOrEqual), + ("testNumberGreaterThan", testNumberGreaterThan), + ("testNumberLessThan", testNumberLessThan), + ("testBoolEqual", testBoolEqual), + ("testBoolNotEqual", testBoolNotEqual), + ("testBoolGreaterThanOrEqual", testBoolGreaterThanOrEqual), + ("testBoolLessThanOrEqual", testBoolLessThanOrEqual), + ("testBoolGreaterThan", testBoolGreaterThan), + ("testBoolLessThan", testBoolLessThan), + ("testStringEqual", testStringEqual), + ("testStringNotEqual", testStringNotEqual), + ("testStringGreaterThanOrEqual", testStringGreaterThanOrEqual), + ("testStringLessThanOrEqual", testStringLessThanOrEqual), + ("testStringGreaterThan", testStringGreaterThan), + ("testStringLessThan", testStringLessThan), + ("testNil", testNil), + ("testArray", testArray), + ("testDictionary", testDictionary) + ] + } + func testNumberEqual() { let jsonL1:JSON = 1234567890.876623 let jsonR1:JSON = JSON(1234567890.876623) diff --git a/Tests/DictionaryTests.swift b/Tests/DictionaryTests.swift index 13208ded..dceecbf7 100644 --- a/Tests/DictionaryTests.swift +++ b/Tests/DictionaryTests.swift @@ -23,8 +23,15 @@ import XCTest import SwiftyJSON -class DictionaryTests: XCTestCase { - +final class DictionaryTests: XCTestCase, XCTestCaseProvider { + + static var allTests: [(String, (DictionaryTests) -> () throws -> Void)] { + return [ + ("testGetter", testGetter), + ("testSetter", testSetter) + ] + } + func testGetter() { let dictionary = ["number":9823.212, "name":"NAME", "list":[1234, 4.212], "object":["sub_number":877.2323, "sub_name":"sub_name"], "bool":true] as [String : Any] let json = JSON(dictionary) diff --git a/Tests/LiteralConvertibleTests.swift b/Tests/LiteralConvertibleTests.swift index c55f2516..e1d14e84 100644 --- a/Tests/LiteralConvertibleTests.swift +++ b/Tests/LiteralConvertibleTests.swift @@ -23,8 +23,19 @@ import XCTest import SwiftyJSON -class LiteralConvertibleTests: XCTestCase { +final class LiteralConvertibleTests: XCTestCase, XCTestCaseProvider { + static var allTests: [(String, (LiteralConvertibleTests) -> () throws -> Void)] { + return [ + ("testNumber", testNumber), + ("testBool", testBool), + ("testString", testString), + ("testNil", testNil), + ("testArray", testArray), + ("testDictionary", testDictionary) + ] + } + func testNumber() { var json:JSON = 1234567890.876623 XCTAssertEqual(json.int!, 1234567890) diff --git a/Tests/NestedJSONTests.swift b/Tests/NestedJSONTests.swift index 5c75def7..e4b7d971 100644 --- a/Tests/NestedJSONTests.swift +++ b/Tests/NestedJSONTests.swift @@ -9,7 +9,15 @@ import XCTest import SwiftyJSON -class NestedJSONTests: XCTestCase { +final class NestedJSONTests: XCTestCase, XCTestCaseProvider { + + static var allTests: [(String, (NestedJSONTests) -> () throws -> Void)] { + return [ + ("testTopLevelNestedJSON", testTopLevelNestedJSON), + ("testDeeplyNestedJSON", testDeeplyNestedJSON) + ] + } + let family: JSON = [ "names" : [ "Brooke Abigail Matos", diff --git a/Tests/NumberTests.swift b/Tests/NumberTests.swift index f75e697c..6e159102 100644 --- a/Tests/NumberTests.swift +++ b/Tests/NumberTests.swift @@ -23,8 +23,27 @@ import XCTest import SwiftyJSON -class NumberTests: XCTestCase { - +final class NumberTests: XCTestCase, XCTestCaseProvider { + + static var allTests: [(String, (NumberTests) -> () throws -> Void)] { + return [ + ("testNumber", testNumber), + ("testBool", testBool), + ("testDouble", testDouble), + ("testFloat", testFloat), + ("testInt", testInt), + ("testUInt", testUInt), + ("testInt8", testInt8), + ("testUInt8", testUInt8), + ("testInt16", testInt16), + ("testUInt16", testUInt16), + ("testInt32", testInt32), + ("testUInt32", testUInt32), + ("testInt64", testInt64), + ("testUInt64", testUInt64) + ] + } + func testNumber() { //getter var json = JSON(NSNumber(value: 9876543210.123456789)) diff --git a/Tests/PerformanceTests.swift b/Tests/PerformanceTests.swift index 80b3c028..91b4f993 100644 --- a/Tests/PerformanceTests.swift +++ b/Tests/PerformanceTests.swift @@ -23,8 +23,19 @@ import XCTest import SwiftyJSON -class PerformanceTests: XCTestCase { - +final class PerformanceTests: XCTestCase, XCTestCaseProvider { + + static var allTests: [(String, (PerformanceTests) -> () throws -> Void)] { + return [ + ("testInitPerformance", testInitPerformance), + ("testObjectMethodPerformance", testObjectMethodPerformance), + ("testArrayMethodPerformance", testArrayMethodPerformance), + ("testDictionaryMethodPerformance", testDictionaryMethodPerformance), + ("testRawStringMethodPerformance", testRawStringMethodPerformance), + ("testLargeDictionaryMethodPerformance", testLargeDictionaryMethodPerformance) + ] + } + var testData: Data! override func setUp() { diff --git a/Tests/PrintableTests.swift b/Tests/PrintableTests.swift index 5672c13a..86feaf95 100644 --- a/Tests/PrintableTests.swift +++ b/Tests/PrintableTests.swift @@ -23,7 +23,19 @@ import XCTest import SwiftyJSON -class PrintableTests: XCTestCase { +final class PrintableTests: XCTestCase, XCTestCaseProvider { + + static var allTests: [(String, (PrintableTests) -> () throws -> Void)] { + return [ + ("testNumber", testNumber), + ("testBool", testBool), + ("testString", testString), + ("testNil", testNil), + ("testArray", testArray), + ("testDictionary", testDictionary) + ] + } + func testNumber() { let json:JSON = 1234567890.876623 XCTAssertEqual(json.description, "1234567890.876623") diff --git a/Tests/RawRepresentableTests.swift b/Tests/RawRepresentableTests.swift index 342cd782..cefdb828 100644 --- a/Tests/RawRepresentableTests.swift +++ b/Tests/RawRepresentableTests.swift @@ -23,8 +23,19 @@ import XCTest import SwiftyJSON -class RawRepresentableTests: XCTestCase { +final class RawRepresentableTests: XCTestCase, XCTestCaseProvider { + static var allTests: [(String, (RawRepresentableTests) -> () throws -> Void)] { + return [ + ("testNumber", testNumber), + ("testBool", testBool), + ("testString", testString), + ("testNil", testNil), + ("testArray", testArray), + ("testDictionary", testDictionary) + ] + } + func testNumber() { var json:JSON = JSON(rawValue: 948394394.347384 as NSNumber)! XCTAssertEqual(json.int!, 948394394) diff --git a/Tests/RawTests.swift b/Tests/RawTests.swift index 4107c63b..a229c5f6 100644 --- a/Tests/RawTests.swift +++ b/Tests/RawTests.swift @@ -23,7 +23,20 @@ import XCTest import SwiftyJSON -class RawTests: XCTestCase { +final class RawTests: XCTestCase, XCTestCaseProvider { + + static var allTests: [(String, (RawTests) -> () throws -> Void)] { + return [ + ("testRawData", testRawData), + ("testInvalidJSONForRawData", testInvalidJSONForRawData), + ("testArray", testArray), + ("testDictionary", testDictionary), + ("testString", testString), + ("testNumber", testNumber), + ("testBool", testBool), + ("testNull", testNull) + ] + } func testRawData() { let json: JSON = ["somekey" : "some string value"] diff --git a/Tests/SequenceTypeTests.swift b/Tests/SequenceTypeTests.swift index a9ff6cf6..c28f4c5d 100644 --- a/Tests/SequenceTypeTests.swift +++ b/Tests/SequenceTypeTests.swift @@ -24,8 +24,24 @@ import XCTest import SwiftyJSON -class SequenceTypeTests: XCTestCase { +final class SequenceTypeTests: XCTestCase, XCTestCaseProvider { + static var allTests: [(String, (SequenceTypeTests) -> () throws -> Void)] { + return [ + ("testJSONFile", testJSONFile), + ("testArrayAllNumber", testArrayAllNumber), + ("testArrayAllBool", testArrayAllBool), + ("testArrayAllString", testArrayAllString), + ("testArrayWithNull", testArrayWithNull), + ("testArrayAllDictionary", testArrayAllDictionary), + ("testDictionaryAllNumber", testDictionaryAllNumber), + ("testDictionaryAllBool", testDictionaryAllBool), + ("testDictionaryAllString", testDictionaryAllString), + ("testDictionaryWithNull", testDictionaryWithNull), + ("testDictionaryAllArray", testDictionaryAllArray) + ] + } + func testJSONFile() { if let file = Bundle(for:BaseTests.self).path(forResource: "Tests", ofType: "json") { let testData = try? Data(contentsOf: URL(fileURLWithPath: file)) diff --git a/Tests/StringTests.swift b/Tests/StringTests.swift index fd787927..59f01b17 100644 --- a/Tests/StringTests.swift +++ b/Tests/StringTests.swift @@ -23,8 +23,19 @@ import XCTest import SwiftyJSON -class StringTests: XCTestCase { +final class StringTests: XCTestCase, XCTestCaseProvider { + static var allTests: [(String, (StringTests) -> () throws -> Void)] { + return [ + ("testString", testString), + ("testURL", testURL), + ("testBool", testBool), + ("testBoolWithY", testBoolWithY), + ("testBoolWithT", testBoolWithT), + ("testURLPercentEscapes", testURLPercentEscapes) + ] + } + func testString() { //getter var json = JSON("abcdefg hijklmn;opqrst.?+_()") diff --git a/Tests/SubscriptTests.swift b/Tests/SubscriptTests.swift index e9acc8c1..4310eaa0 100644 --- a/Tests/SubscriptTests.swift +++ b/Tests/SubscriptTests.swift @@ -23,8 +23,29 @@ import XCTest import SwiftyJSON -class SubscriptTests: XCTestCase { +final class SubscriptTests: XCTestCase, XCTestCaseProvider { + static var allTests: [(String, (SubscriptTests) -> () throws -> Void)] { + return [ + ("testArrayAllNumber", testArrayAllNumber), + ("testArrayAllBool", testArrayAllBool), + ("testArrayAllString", testArrayAllString), + ("testArrayWithNull", testArrayWithNull), + ("testArrayAllDictionary", testArrayAllDictionary), + ("testDictionaryAllNumber", testDictionaryAllNumber), + ("testDictionaryAllBool", testDictionaryAllBool), + ("testDictionaryAllString", testDictionaryAllString), + ("testDictionaryWithNull", testDictionaryWithNull), + ("testDictionaryAllArray", testDictionaryAllArray), + ("testOutOfBounds", testOutOfBounds), + ("testErrorWrongType", testErrorWrongType), + ("testErrorNotExist", testErrorNotExist), + ("testMultilevelGetter", testMultilevelGetter), + ("testMultilevelSetter1", testMultilevelSetter1), + ("testMultilevelSetter2", testMultilevelSetter2) + ] + } + func testArrayAllNumber() { var json:JSON = [1,2.0,3.3,123456789,987654321.123456789] XCTAssertTrue(json == [1,2.0,3.3,123456789,987654321.123456789]) From c27542107d71056a48b5faa33e0724ed857faa8c Mon Sep 17 00:00:00 2001 From: Zigii Wong Date: Mon, 7 Nov 2016 20:54:10 +0800 Subject: [PATCH 4/8] Add LinuxMain.swift for entry on Linux --- Tests/LinuxMain.swift | 21 +++++++++++++++++++ .../XCTestCaseProvider.swift | 0 2 files changed, 21 insertions(+) create mode 100644 Tests/LinuxMain.swift rename Tests/{ => SwiftyJSONTests}/XCTestCaseProvider.swift (100%) diff --git a/Tests/LinuxMain.swift b/Tests/LinuxMain.swift new file mode 100644 index 00000000..95779e6b --- /dev/null +++ b/Tests/LinuxMain.swift @@ -0,0 +1,21 @@ +import XCTest +@testable import SwiftyJSONTests + +// This is the entry point for SwiftyJSONTests on Linux + +XCTMain([ + testCase(PerformanceTests.allTests), + testCase(BaseTests.allTests), + testCase(NestedJSONTests.allTests), + testCase(SequenceTypeTests.allTests), + testCase(PrintableTests.allTests), + testCase(SubscriptTests.allTests), + testCase(LiteralConvertibleTests.allTests), + testCase(RawRepresentableTests.allTests), + testCase(ComparableTests.allTests), + testCase(StringTests.allTests), + testCase(NumberTests.allTests), + testCase(RawTests.allTests), + testCase(DictionaryTests.allTests), + testCase(ArrayTests.allTests) +]) diff --git a/Tests/XCTestCaseProvider.swift b/Tests/SwiftyJSONTests/XCTestCaseProvider.swift similarity index 100% rename from Tests/XCTestCaseProvider.swift rename to Tests/SwiftyJSONTests/XCTestCaseProvider.swift From 04ed3887fb62b9934873b22cc58a35ea50f1dcf4 Mon Sep 17 00:00:00 2001 From: Zigii Wong Date: Tue, 22 Nov 2016 23:23:56 +0800 Subject: [PATCH 5/8] Make swift test works --- Tests/SwiftyJSONTests/BaseTests.swift | 14 ++--- Tests/SwiftyJSONTests/PerformanceTests.swift | 16 +++--- Tests/SwiftyJSONTests/SequenceTypeTests.swift | 51 +++++++++++-------- 3 files changed, 47 insertions(+), 34 deletions(-) diff --git a/Tests/SwiftyJSONTests/BaseTests.swift b/Tests/SwiftyJSONTests/BaseTests.swift index f4d39e69..40af663d 100644 --- a/Tests/SwiftyJSONTests/BaseTests.swift +++ b/Tests/SwiftyJSONTests/BaseTests.swift @@ -47,13 +47,15 @@ final class BaseTests: XCTestCase, XCTestCaseProvider { super.setUp() - if let file = Bundle(for:BaseTests.self).path(forResource: "Tests", ofType: "json") { - self.testData = try? Data(contentsOf: URL(fileURLWithPath: file)) - } else { - XCTFail("Can't find the test JSON file") - } + do { + self.testData = try Data(contentsOf: URL(fileURLWithPath: "Tests/SwiftyJSONTests/Tests.json")) + } + catch { + XCTFail("Failed to read in the test data") + exit(1) + } } - + override func tearDown() { super.tearDown() } diff --git a/Tests/SwiftyJSONTests/PerformanceTests.swift b/Tests/SwiftyJSONTests/PerformanceTests.swift index 91b4f993..3d244f57 100644 --- a/Tests/SwiftyJSONTests/PerformanceTests.swift +++ b/Tests/SwiftyJSONTests/PerformanceTests.swift @@ -41,13 +41,15 @@ final class PerformanceTests: XCTestCase, XCTestCaseProvider { override func setUp() { super.setUp() - if let file = Bundle(for:PerformanceTests.self).path(forResource: "Tests", ofType: "json") { - self.testData = try? Data(contentsOf: URL(fileURLWithPath: file)) - } else { - XCTFail("Can't find the test JSON file") - } - } - + do { + self.testData = try Data(contentsOf: URL(fileURLWithPath: "Tests/SwiftyJSONTests/Tests.json")) + } + catch { + XCTFail("Failed to read in the test data") + exit(1) + } + } + override func tearDown() { // Put teardown code here. This method is called after the invocation of each test method in the class. super.tearDown() diff --git a/Tests/SwiftyJSONTests/SequenceTypeTests.swift b/Tests/SwiftyJSONTests/SequenceTypeTests.swift index c28f4c5d..abb5ebff 100644 --- a/Tests/SwiftyJSONTests/SequenceTypeTests.swift +++ b/Tests/SwiftyJSONTests/SequenceTypeTests.swift @@ -28,7 +28,6 @@ final class SequenceTypeTests: XCTestCase, XCTestCaseProvider { static var allTests: [(String, (SequenceTypeTests) -> () throws -> Void)] { return [ - ("testJSONFile", testJSONFile), ("testArrayAllNumber", testArrayAllNumber), ("testArrayAllBool", testArrayAllBool), ("testArrayAllString", testArrayAllString), @@ -38,31 +37,41 @@ final class SequenceTypeTests: XCTestCase, XCTestCaseProvider { ("testDictionaryAllBool", testDictionaryAllBool), ("testDictionaryAllString", testDictionaryAllString), ("testDictionaryWithNull", testDictionaryWithNull), - ("testDictionaryAllArray", testDictionaryAllArray) + ("testDictionaryAllArray", testDictionaryAllArray), + ("testJSONFile", testJSONFile) ] } + var testData: Data! + + override func setUp() { + super.setUp() + + do { + self.testData = try Data(contentsOf: URL(fileURLWithPath: "Tests/SwiftyJSONTests/Tests.json")) + } + catch { + XCTFail("Failed to read in the test data") + exit(1) + } + } + func testJSONFile() { - if let file = Bundle(for:BaseTests.self).path(forResource: "Tests", ofType: "json") { - let testData = try? Data(contentsOf: URL(fileURLWithPath: file)) - let json = JSON(data:testData!) - for (index, sub) in json { - switch (index as NSString).integerValue { - case 0: - XCTAssertTrue(sub["id_str"] == "240558470661799936") - case 1: - XCTAssertTrue(sub["id_str"] == "240556426106372096") - case 2: - XCTAssertTrue(sub["id_str"] == "240539141056638977") - default: - continue - } - } - } else { - XCTFail("Can't find the test JSON file") - } + let json = JSON(data:testData!) + for (index, sub) in json { + switch (index as NSString).integerValue { + case 0: + XCTAssertTrue(sub["id_str"] == "240558470661799936") + case 1: + XCTAssertTrue(sub["id_str"] == "240556426106372096") + case 2: + XCTAssertTrue(sub["id_str"] == "240539141056638977") + default: + continue + } + } } - + func testArrayAllNumber() { var json:JSON = [1,2.0,3.3,123456789,987654321.123456789] XCTAssertEqual(json.count, 5) From 18f44feb046b8f2b2cc68018a649e89ac79a6e52 Mon Sep 17 00:00:00 2001 From: Zigii Wong Date: Fri, 25 Nov 2016 11:00:19 +0800 Subject: [PATCH 6/8] Add missing tests --- Tests/SwiftyJSONTests/PrintableTests.swift | 6 +++++- Tests/SwiftyJSONTests/SequenceTypeTests.swift | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/Tests/SwiftyJSONTests/PrintableTests.swift b/Tests/SwiftyJSONTests/PrintableTests.swift index 650b2c35..c96fae0e 100644 --- a/Tests/SwiftyJSONTests/PrintableTests.swift +++ b/Tests/SwiftyJSONTests/PrintableTests.swift @@ -32,7 +32,11 @@ final class PrintableTests: XCTestCase, XCTestCaseProvider { ("testString", testString), ("testNil", testNil), ("testArray", testArray), - ("testDictionary", testDictionary) + ("testDictionary", testDictionary), + ("testArrayWithStrings", testArrayWithStrings), + ("testArrayWithOptionals", testArrayWithOptionals), + ("testDictionaryWithStrings", testDictionaryWithStrings), + ("testDictionaryWithOptionals", testDictionaryWithOptionals) ] } diff --git a/Tests/SwiftyJSONTests/SequenceTypeTests.swift b/Tests/SwiftyJSONTests/SequenceTypeTests.swift index 13a7c4e3..ccbdb8c9 100644 --- a/Tests/SwiftyJSONTests/SequenceTypeTests.swift +++ b/Tests/SwiftyJSONTests/SequenceTypeTests.swift @@ -38,6 +38,7 @@ final class SequenceTypeTests: XCTestCase, XCTestCaseProvider { ("testDictionaryAllString", testDictionaryAllString), ("testDictionaryWithNull", testDictionaryWithNull), ("testDictionaryAllArray", testDictionaryAllArray), + ("testDictionaryIteratingPerformance", testDictionaryIteratingPerformance), ("testJSONFile", testJSONFile) ] } From 06b25183155e7a900f2ed425938ab2c031c7076e Mon Sep 17 00:00:00 2001 From: Zigii Wong Date: Sun, 27 Nov 2016 11:52:53 +0800 Subject: [PATCH 7/8] Add .build to .gitignore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 2731938e..f027186d 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,6 @@ Pods #Carthage Carthage/Build + +#SPM +.build/ From 625e9321c992c0cfea0401ab899c69bf7541afe5 Mon Sep 17 00:00:00 2001 From: Zigii Wong Date: Mon, 28 Nov 2016 22:14:28 +0800 Subject: [PATCH 8/8] Add #if macro to retrieve data on Linux --- Tests/SwiftyJSONTests/BaseTests.swift | 8 ++++++-- Tests/SwiftyJSONTests/PerformanceTests.swift | 8 ++++++-- Tests/SwiftyJSONTests/SequenceTypeTests.swift | 8 ++++++-- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/Tests/SwiftyJSONTests/BaseTests.swift b/Tests/SwiftyJSONTests/BaseTests.swift index 40af663d..be6904d4 100644 --- a/Tests/SwiftyJSONTests/BaseTests.swift +++ b/Tests/SwiftyJSONTests/BaseTests.swift @@ -48,11 +48,15 @@ final class BaseTests: XCTestCase, XCTestCaseProvider { super.setUp() do { - self.testData = try Data(contentsOf: URL(fileURLWithPath: "Tests/SwiftyJSONTests/Tests.json")) + #if os(Linux) + self.testData = try Data(contentsOf: URL(fileURLWithPath: "Tests/SwiftyJSONTests/Tests.json")) + #else + let file = Bundle(for:BaseTests.self).path(forResource: "Tests", ofType: "json") + self.testData = try Data(contentsOf: URL(fileURLWithPath: file!)) + #endif } catch { XCTFail("Failed to read in the test data") - exit(1) } } diff --git a/Tests/SwiftyJSONTests/PerformanceTests.swift b/Tests/SwiftyJSONTests/PerformanceTests.swift index 3d244f57..301d95bb 100644 --- a/Tests/SwiftyJSONTests/PerformanceTests.swift +++ b/Tests/SwiftyJSONTests/PerformanceTests.swift @@ -42,11 +42,15 @@ final class PerformanceTests: XCTestCase, XCTestCaseProvider { super.setUp() do { - self.testData = try Data(contentsOf: URL(fileURLWithPath: "Tests/SwiftyJSONTests/Tests.json")) + #if os(Linux) + self.testData = try Data(contentsOf: URL(fileURLWithPath: "Tests/SwiftyJSONTests/Tests.json")) + #else + let file = Bundle(for:PerformanceTests.self).path(forResource: "Tests", ofType: "json") + self.testData = try Data(contentsOf: URL(fileURLWithPath: file!)) + #endif } catch { XCTFail("Failed to read in the test data") - exit(1) } } diff --git a/Tests/SwiftyJSONTests/SequenceTypeTests.swift b/Tests/SwiftyJSONTests/SequenceTypeTests.swift index ccbdb8c9..6acb2573 100644 --- a/Tests/SwiftyJSONTests/SequenceTypeTests.swift +++ b/Tests/SwiftyJSONTests/SequenceTypeTests.swift @@ -49,11 +49,15 @@ final class SequenceTypeTests: XCTestCase, XCTestCaseProvider { super.setUp() do { - self.testData = try Data(contentsOf: URL(fileURLWithPath: "Tests/SwiftyJSONTests/Tests.json")) + #if os(Linux) + self.testData = try Data(contentsOf: URL(fileURLWithPath: "Tests/SwiftyJSONTests/Tests.json")) + #else + let file = Bundle(for:SequenceTypeTests.self).path(forResource: "Tests", ofType: "json") + self.testData = try Data(contentsOf: URL(fileURLWithPath: file!)) + #endif } catch { XCTFail("Failed to read in the test data") - exit(1) } }