@@ -10,16 +10,52 @@ import Foundation
1010
1111private typealias IndentationType = Difference . IndentationType
1212
13+ public struct DifferenceNameLabels {
14+ let expected : String
15+ let received : String
16+ let missing : String
17+ let extra : String
18+
19+ public init ( expected: String , received: String , missing: String , extra: String ) {
20+ self . expected = expected
21+ self . received = received
22+ self . missing = missing
23+ self . extra = extra
24+ }
25+
26+ public static var expectation : Self {
27+ Self (
28+ expected: " Expected " ,
29+ received: " Received " ,
30+ missing: " Missing " ,
31+ extra: " Extra "
32+ )
33+ }
34+
35+ public static var comparing : Self {
36+ Self (
37+ expected: " Previous " ,
38+ received: " Current " ,
39+ missing: " Removed " ,
40+ extra: " Added "
41+ )
42+ }
43+ }
44+
45+
1346private struct Differ {
1447 private let indentationType : IndentationType
1548 private let skipPrintingOnDiffCount : Bool
49+ private let nameLabels : DifferenceNameLabels
1650
1751 init (
1852 indentationType: IndentationType ,
19- skipPrintingOnDiffCount: Bool
53+ skipPrintingOnDiffCount: Bool ,
54+ nameLabels: DifferenceNameLabels
2055 ) {
2156 self . indentationType = indentationType
2257 self . skipPrintingOnDiffCount = skipPrintingOnDiffCount
58+ self . nameLabels = nameLabels
2359 }
2460
2561 func diff< T> ( _ expected: T , _ received: T ) -> [ String ] {
@@ -63,14 +99,14 @@ private struct Differ {
6399 missingKeys. forEach { key in
64100 missingKeyPairs. append ( Line ( contents: " \( key. description) : \( String ( describing: expectedDict [ key] ) ) " , indentationLevel: level + 1 , canBeOrdered: true ) )
65101 }
66- resultLines. append ( Line ( contents: " Missing key pairs:" , indentationLevel: level, canBeOrdered: false , children: missingKeyPairs) )
102+ resultLines. append ( Line ( contents: " \( nameLabels . missing ) key pairs: " , indentationLevel: level, canBeOrdered: false , children: missingKeyPairs) )
67103 }
68104 if ( !extraKeys. isEmpty) {
69105 var extraKeyPairs : [ Line ] = [ ]
70106 extraKeys. forEach { key in
71107 extraKeyPairs. append ( Line ( contents: " \( key. description) : \( String ( describing: receivedDict [ key] ) ) " , indentationLevel: level + 1 , canBeOrdered: true ) )
72108 }
73- resultLines. append ( Line ( contents: " Extra key pairs:" , indentationLevel: level, canBeOrdered: false , children: extraKeyPairs) )
109+ resultLines. append ( Line ( contents: " \( nameLabels . extra ) key pairs: " , indentationLevel: level, canBeOrdered: false , children: extraKeyPairs) )
74110 }
75111 return resultLines
76112 }
@@ -79,11 +115,11 @@ private struct Differ {
79115 let receivedSet = received as? Set < AnyHashable > {
80116 let missing = expectedSet. subtracting ( receivedSet)
81117 . map { unique in
82- Line ( contents: " Missing : \( unique. description) " , indentationLevel: level, canBeOrdered: true )
118+ Line ( contents: " \( nameLabels . missing ) : \( unique. description) " , indentationLevel: level, canBeOrdered: true )
83119 }
84120 let extras = receivedSet. subtracting ( expectedSet)
85121 . map { unique in
86- Line ( contents: " Extra : \( unique. description) " , indentationLevel: level, canBeOrdered: true )
122+ Line ( contents: " \( nameLabels . extra ) : \( unique. description) " , indentationLevel: level, canBeOrdered: true )
87123 }
88124 return missing + extras
89125 }
@@ -192,8 +228,8 @@ private struct Differ {
192228 _ indentationLevel: Int
193229 ) -> [ Line ] {
194230 return [
195- Line ( contents: " Received : \( received) " , indentationLevel: indentationLevel, canBeOrdered: false ) ,
196- Line ( contents: " Expected : \( expected) " , indentationLevel: indentationLevel, canBeOrdered: false )
231+ Line ( contents: " \( nameLabels . received ) : \( received) " , indentationLevel: indentationLevel, canBeOrdered: false ) ,
232+ Line ( contents: " \( nameLabels . expected ) : \( expected) " , indentationLevel: indentationLevel, canBeOrdered: false )
197233 ]
198234 }
199235
@@ -332,9 +368,10 @@ public func diff<T>(
332368 _ expected: T ,
333369 _ received: T ,
334370 indentationType: Difference . IndentationType = . pipe,
335- skipPrintingOnDiffCount: Bool = false
371+ skipPrintingOnDiffCount: Bool = false ,
372+ nameLabels: DifferenceNameLabels = . expectation
336373) -> [ String ] {
337- Differ ( indentationType: indentationType, skipPrintingOnDiffCount: skipPrintingOnDiffCount)
374+ Differ ( indentationType: indentationType, skipPrintingOnDiffCount: skipPrintingOnDiffCount, nameLabels : nameLabels )
338375 . diff ( expected, received)
339376}
340377
0 commit comments