diff --git a/.gitignore b/.gitignore index 2731938e..f027186d 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,6 @@ Pods #Carthage Carthage/Build + +#SPM +.build/ diff --git a/SwiftyJSON.xcodeproj/project.pbxproj b/SwiftyJSON.xcodeproj/project.pbxproj index 4bddfa14..13fc56b1 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 /* SwiftyJSONTests */ = { isa = PBXGroup; children = ( + A869FA631DD02CFF00DBCBEF /* Helpers */, A885D1DA19CFCFF0002FD4C3 /* Tests.json */, A86BAA0D19EBC32B009EAAEB /* PerformanceTests.swift */, A885D1D119CF1EE6002FD4C3 /* BaseTests.swift */, @@ -257,6 +262,14 @@ name = "Supporting Files"; sourceTree = ""; }; + A869FA631DD02CFF00DBCBEF /* Helpers */ = { + isa = PBXGroup; + children = ( + A869FA641DD02D2200DBCBEF /* XCTestCaseProvider.swift */, + ); + name = Helpers; + sourceTree = ""; + }; /* End PBXGroup section */ /* Begin PBXHeadersBuildPhase section */ @@ -551,6 +564,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 */, @@ -588,6 +602,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 */, @@ -609,6 +624,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/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/SwiftyJSONTests/ArrayTests.swift b/Tests/SwiftyJSONTests/ArrayTests.swift index e2d471a6..b543b241 100644 --- a/Tests/SwiftyJSONTests/ArrayTests.swift +++ b/Tests/SwiftyJSONTests/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/SwiftyJSONTests/BaseTests.swift b/Tests/SwiftyJSONTests/BaseTests.swift index 0ec670ed..be6904d4 100644 --- a/Tests/SwiftyJSONTests/BaseTests.swift +++ b/Tests/SwiftyJSONTests/BaseTests.swift @@ -23,21 +23,43 @@ 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() { 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 { + #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") + } } - + override func tearDown() { super.tearDown() } diff --git a/Tests/SwiftyJSONTests/ComparableTests.swift b/Tests/SwiftyJSONTests/ComparableTests.swift index c027b07a..8c754dbd 100644 --- a/Tests/SwiftyJSONTests/ComparableTests.swift +++ b/Tests/SwiftyJSONTests/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/SwiftyJSONTests/DictionaryTests.swift b/Tests/SwiftyJSONTests/DictionaryTests.swift index 13208ded..dceecbf7 100644 --- a/Tests/SwiftyJSONTests/DictionaryTests.swift +++ b/Tests/SwiftyJSONTests/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/SwiftyJSONTests/LiteralConvertibleTests.swift b/Tests/SwiftyJSONTests/LiteralConvertibleTests.swift index c55f2516..e1d14e84 100644 --- a/Tests/SwiftyJSONTests/LiteralConvertibleTests.swift +++ b/Tests/SwiftyJSONTests/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/SwiftyJSONTests/NestedJSONTests.swift b/Tests/SwiftyJSONTests/NestedJSONTests.swift index 5c75def7..e4b7d971 100644 --- a/Tests/SwiftyJSONTests/NestedJSONTests.swift +++ b/Tests/SwiftyJSONTests/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/SwiftyJSONTests/NumberTests.swift b/Tests/SwiftyJSONTests/NumberTests.swift index 828bae68..ad12db05 100644 --- a/Tests/SwiftyJSONTests/NumberTests.swift +++ b/Tests/SwiftyJSONTests/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)) @@ -103,7 +122,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") diff --git a/Tests/SwiftyJSONTests/PerformanceTests.swift b/Tests/SwiftyJSONTests/PerformanceTests.swift index 80b3c028..301d95bb 100644 --- a/Tests/SwiftyJSONTests/PerformanceTests.swift +++ b/Tests/SwiftyJSONTests/PerformanceTests.swift @@ -23,20 +23,37 @@ 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() { 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 { + #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") + } + } + 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/PrintableTests.swift b/Tests/SwiftyJSONTests/PrintableTests.swift index d8361685..c96fae0e 100644 --- a/Tests/SwiftyJSONTests/PrintableTests.swift +++ b/Tests/SwiftyJSONTests/PrintableTests.swift @@ -23,7 +23,23 @@ 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), + ("testArrayWithStrings", testArrayWithStrings), + ("testArrayWithOptionals", testArrayWithOptionals), + ("testDictionaryWithStrings", testDictionaryWithStrings), + ("testDictionaryWithOptionals", testDictionaryWithOptionals) + ] + } + func testNumber() { let json:JSON = 1234567890.876623 XCTAssertEqual(json.description, "1234567890.876623") diff --git a/Tests/SwiftyJSONTests/RawRepresentableTests.swift b/Tests/SwiftyJSONTests/RawRepresentableTests.swift index 342cd782..cefdb828 100644 --- a/Tests/SwiftyJSONTests/RawRepresentableTests.swift +++ b/Tests/SwiftyJSONTests/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/SwiftyJSONTests/RawTests.swift b/Tests/SwiftyJSONTests/RawTests.swift index d45f1cec..f844a64c 100644 --- a/Tests/SwiftyJSONTests/RawTests.swift +++ b/Tests/SwiftyJSONTests/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"] @@ -56,7 +69,6 @@ class RawTests: XCTestCase { let string = json.rawString() XCTAssertTrue (data != nil) XCTAssertTrue (string!.lengthOfBytes(using: String.Encoding.utf8) > 0) - print(string!) } func testDictionary() { @@ -70,7 +82,6 @@ class RawTests: XCTestCase { let string = json.rawString() XCTAssertTrue (data != nil) XCTAssertTrue (string!.lengthOfBytes(using: String.Encoding.utf8) > 0) - print(string!) } func testString() { diff --git a/Tests/SwiftyJSONTests/SequenceTypeTests.swift b/Tests/SwiftyJSONTests/SequenceTypeTests.swift index 62bd7dcf..6acb2573 100644 --- a/Tests/SwiftyJSONTests/SequenceTypeTests.swift +++ b/Tests/SwiftyJSONTests/SequenceTypeTests.swift @@ -24,29 +24,59 @@ import XCTest import SwiftyJSON -class SequenceTypeTests: XCTestCase { +final class SequenceTypeTests: XCTestCase, XCTestCaseProvider { + static var allTests: [(String, (SequenceTypeTests) -> () throws -> Void)] { + return [ + ("testArrayAllNumber", testArrayAllNumber), + ("testArrayAllBool", testArrayAllBool), + ("testArrayAllString", testArrayAllString), + ("testArrayWithNull", testArrayWithNull), + ("testArrayAllDictionary", testArrayAllDictionary), + ("testDictionaryAllNumber", testDictionaryAllNumber), + ("testDictionaryAllBool", testDictionaryAllBool), + ("testDictionaryAllString", testDictionaryAllString), + ("testDictionaryWithNull", testDictionaryWithNull), + ("testDictionaryAllArray", testDictionaryAllArray), + ("testDictionaryIteratingPerformance", testDictionaryIteratingPerformance), + ("testJSONFile", testJSONFile) + ] + } + + var testData: Data! + + override func setUp() { + super.setUp() + + do { + #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") + } + } + 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) diff --git a/Tests/SwiftyJSONTests/StringTests.swift b/Tests/SwiftyJSONTests/StringTests.swift index 9ba2b1df..c78a003b 100644 --- a/Tests/SwiftyJSONTests/StringTests.swift +++ b/Tests/SwiftyJSONTests/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/SwiftyJSONTests/SubscriptTests.swift b/Tests/SwiftyJSONTests/SubscriptTests.swift index e9acc8c1..4310eaa0 100644 --- a/Tests/SwiftyJSONTests/SubscriptTests.swift +++ b/Tests/SwiftyJSONTests/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]) diff --git a/Tests/SwiftyJSONTests/XCTestCaseProvider.swift b/Tests/SwiftyJSONTests/XCTestCaseProvider.swift new file mode 100644 index 00000000..f300d475 --- /dev/null +++ b/Tests/SwiftyJSONTests/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