@@ -3,56 +3,102 @@ import Foundation
33@testable import Generator
44
55@Test
6- func `Track canonical data has nested tests`() {
7- let expectedUUIDs = Set ( [ " first " , " second " , " third " , " fourth " ] )
8- #expect( throws: Never . self) {
9- guard let data = try CanonicalData ( from: validJSONWithNestedTests) else {
10- #expect( Bool ( false ) , " Failed to construct canonical data " )
11- return
12- }
13- #expect( expectedUUIDs == data. uuidSet)
14- }
6+ func `Track canonical data all tests are kept`() throws {
7+ let expectedUUIDs = Set ( [ " a " , " b " , " c " , " d " ] )
8+ let expectedData = try CanonicalData ( from: " valid_config " )
9+ let data = try CanonicalData ( from: " valid_config " )
10+
11+ #expect( data. uuidSet == expectedUUIDs)
12+ #expect( data == expectedData)
1513}
1614
1715@Test
18- func `Track canonical data all tests are kept`() {
19- let expectedUUIDs = Set ( [ " first " , " second " , " third " , " fourth " ] )
20- #expect( throws: Never . self) {
21- guard var data = try CanonicalData ( from: validJSONWithNestedTests) else {
22- #expect( Bool ( false ) , " Failed to construct canonical data " )
23- return
24- }
25- data. whitelistTests ( withUUIDs: expectedUUIDs)
26- #expect( data. uuidSet == expectedUUIDs)
27- }
16+ func `Track canonical data all tests are kept after whitelisting`() throws {
17+ let expectedUUIDs = Set ( [ " a " , " b " , " c " , " d " ] )
18+ let expectedData = try CanonicalData ( from: " valid_config " )
19+ var data = try CanonicalData ( from: " valid_config " )
20+
21+ data. whitelistTests ( withUUIDs: expectedUUIDs)
22+
23+ #expect( data. uuidSet == expectedUUIDs)
24+ #expect( data == expectedData)
2825}
2926
3027@Test
31- func `Track canonical data part of the tests are excluded`() {
32- let expectedUUIDs = Set ( [ " third " , " fourth " ] )
33- #expect( throws: Never . self) {
34- guard var data = try CanonicalData ( from: validJSONWithNestedTests) else {
35- #expect( Bool ( false ) , " Failed to construct canonical data " )
36- return
37- }
38- data. whitelistTests ( withUUIDs: expectedUUIDs)
39- #expect( data. uuidSet == expectedUUIDs)
40- }
28+ func `Track canonical data some tests are filtered`() throws {
29+ let expectedUUIDs = Set ( [ " b " , " c " ] )
30+ let expectedData = try CanonicalData ( from: " valid_config_whitelisted " )
31+ var data = try CanonicalData ( from: " valid_config " )
32+
33+ data. whitelistTests ( withUUIDs: expectedUUIDs)
34+
35+ #expect( data. uuidSet == expectedUUIDs)
36+ #expect( data == expectedData)
37+ }
38+
39+ @Test
40+ func `Track canonical data all tests are filtered`() throws {
41+ let expectedUUIDs = Set < String > ( )
42+ let expectedData = try CanonicalData ( from: " valid_config_empty " )
43+ var data = try CanonicalData ( from: " valid_config " )
44+
45+ data. whitelistTests ( withUUIDs: expectedUUIDs)
46+
47+ #expect( data. uuidSet == expectedUUIDs)
48+ #expect( data == expectedData)
49+ }
50+
51+ @Test
52+ func `Track canonical data all tests are filtered with missing keys`() throws {
53+ let expectedUUIDs = Set < String > ( )
54+ let expectedData = try CanonicalData ( from: " valid_config_empty " )
55+ var data = try CanonicalData ( from: " valid_config " )
56+
57+ data. whitelistTests ( withUUIDs: [ " e " , " f " , " g " , " e " ] )
58+
59+ #expect( data. uuidSet == expectedUUIDs)
60+ #expect( data == expectedData)
61+ }
62+
63+ @Test
64+ func `Track canonical data all tests are kept in nested`() throws {
65+ let expectedUUIDs = Set ( [ " first " , " second " , " third " , " fourth " , " fiths " , " sixth " ] )
66+ var data = try CanonicalData ( from: " valid_nested " )
67+
68+ data. whitelistTests ( withUUIDs: expectedUUIDs)
69+
70+ #expect( data. uuidSet == expectedUUIDs)
71+ }
72+
73+ @Test
74+ func `Track canonical data some tests are kept in nested`() throws {
75+ let expectedUUIDs = Set ( [ " first " , " third " , " fiths " ] )
76+ var data = try CanonicalData ( from: " valid_nested " )
77+
78+ data. whitelistTests ( withUUIDs: expectedUUIDs)
79+
80+ #expect( data. uuidSet == expectedUUIDs)
4181}
4282
4383// MARK: - Helpers
4484
4585extension CanonicalData {
46-
47- init ? ( from string: String ) {
48- guard let data = string. data ( using: . utf8) else { return nil }
49- guard let jsonData = try ? JSONSerialization . jsonObject ( with: data) as? [ String : Any ] else {
50- return nil
86+
87+ fileprivate init ( from fileName: String ) throws {
88+ let url = try Bundle . module. urlForResource ( fileName)
89+ let data = try Data ( contentsOf: url)
90+ let jsonData = try JSONSerialization . jsonObject ( with: data)
91+
92+ guard let jsonDictionary = jsonData as? [ String : Any ] else {
93+ #expect( Bool ( false ) , " Expected json data to be of type [String: Any]. " )
94+ self . init ( dictionary: [ : ] )
95+ return
5196 }
52- self . init ( dictionary: jsonData)
97+
98+ self . init ( dictionary: jsonDictionary)
5399 }
54100
55- var uuidSet : Set < String > {
101+ fileprivate var uuidSet : Set < String > {
56102 var uuids = Set < String > ( )
57103 if let cases = context [ " cases " ] as? [ [ String : Any ] ] {
58104 uuids. formUnion ( collectUUIDs ( from: cases) )
@@ -75,26 +121,43 @@ extension CanonicalData {
75121
76122}
77123
78- fileprivate var validJSONWithNestedTests = """
79- {
80- " exercise " : " acronym " ,
81- " cases " : [
82- { " uuid " : " first " },
83- {
84- " cases " : [
85- { " uuid " : " second " },
86- {
87- " cases " : [
88- { " uuid " : " third " , },
89- {
90- " cases " : [
91- { " uuid " : " fourth " }
92- ]
93- }
94- ]
95- }
96- ]
124+ extension CanonicalData : Equatable {
125+
126+ public static func == ( lhs: Self , rhs: Self ) -> Bool { deepEqual ( lhs. context, rhs. context) }
127+
128+ private static func deepEqual( _ lhs: Any ? , _ rhs: Any ? ) -> Bool {
129+ switch ( lhs, rhs) {
130+ case ( nil , nil ) :
131+ return true
132+
133+ case let ( l as [ String : Any ? ] , r as [ String : Any ? ] ) :
134+ guard Set ( l. keys) == Set ( r. keys) else { return false }
135+ return l. allSatisfy { key, value in deepEqual ( value, r [ key] ?? nil ) }
136+
137+ case let ( l as [ Any ] , r as [ Any ] ) :
138+ guard l. count == r. count else { return false }
139+ return zip ( l, r) . allSatisfy { deepEqual ( $0, $1) }
140+
141+ case let ( l as NSNumber , r as NSNumber ) :
142+ return l == r
143+
144+ case let ( l as Bool , r as Bool ) :
145+ return l == r
146+
147+ case let ( l as String , r as String ) :
148+ return l == r
149+
150+ default :
151+ return false
152+ }
97153 }
98- ]
154+
155+ }
156+
157+ extension Bundle {
158+
159+ fileprivate func urlForResource( _ name: String ) throws -> URL {
160+ return try urlForResource ( name, fileExtension: " json " , subdirectory: " Resources/CanonicalData " )
161+ }
162+
99163}
100- """
0 commit comments