-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathDiff.swift
617 lines (571 loc) · 19.3 KB
/
Diff.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
/// Detects differences between two given values by comparing their mirrors and optionally returns
/// a formatted string describing it.
///
/// This can be a great tool to use for building debug tools for applications and libraries. For
/// example, this library uses ``diff(_:_:format:)`` to implement
/// ``XCTAssertNoDifference(_:_:_:file:line:)``, which asserts that two values are equal, and
/// if they are not the failure message is a nicely formatted diff showing exactly what part of the
/// values are not equal.
///
/// Further, the
/// [Composable Architecture](https://www.github.com/pointfreeco/swift-composable-architecture) uses
/// ``diff(_:_:format:)`` in a couple different ways:
///
/// * It is used to implement a tool that prints changes to application state over time as diffs
/// between the previous state and the current state whenever an action is sent to the store.
/// * It is also used in a testing tool so that when one fails to assert for how state may have
/// changed after sending an action, it can display a concise message showing the exact difference
/// in state.
///
/// - Parameters:
/// - lhs: An expression of type `T`.
/// - rhs: A second expression of type `T`.
/// - format: A format to use for the diff. By default it uses ASCII characters typically
/// associated with the "diff" format: "-" for removals, "+" for additions, and " " for
/// unchanged lines.
/// - Returns: A string describing any difference detected between values, or `nil` if no difference
/// is detected.
public func diff<T>(_ lhs: T, _ rhs: T, format: DiffFormat = .default,
excludedNoChangedProperty: Bool = false) -> String? {
var visitedItems: Set<ObjectIdentifier> = []
func diffHelp(
_ lhs: Any,
_ rhs: Any,
lhsName: String?,
rhsName: String?,
separator: String,
indent: Int
) -> String {
let rhsName = rhsName ?? lhsName
guard !isMirrorEqual(lhs, rhs) else {
return _customDump(lhs, name: rhsName, indent: indent, maxDepth: 0)
.appending(separator)
.indenting(with: format.both + " ")
}
let lhsMirror = Mirror(customDumpReflecting: lhs)
let rhsMirror = Mirror(customDumpReflecting: rhs)
var out = ""
func diffEverything() {
var lhs = _customDump(lhs, name: lhsName, indent: indent, maxDepth: .max)
var rhs = _customDump(rhs, name: rhsName, indent: indent, maxDepth: .max)
if lhs == rhs {
if lhsMirror.subjectType != rhsMirror.subjectType {
lhs.append(" as \(typeName(lhsMirror.subjectType))")
rhs.append(" as \(typeName(rhsMirror.subjectType))")
}
}
lhs.append(separator)
rhs.append(separator)
print(
lhs.indenting(with: format.first + " "),
to: &out
)
print(
rhs.indenting(with: format.second + " "),
terminator: "",
to: &out
)
}
guard lhsMirror.subjectType == rhsMirror.subjectType
else {
diffEverything()
return out
}
func diffChildren(
_ lhsMirror: Mirror,
_ rhsMirror: Mirror,
prefix: String,
suffix: String,
elementIndent: Int,
elementSeparator: String,
collapseUnchanged: Bool,
areEquivalent: (Mirror.Child, Mirror.Child) -> Bool = { $0.label == $1.label },
areInIncreasingOrder: ((Mirror.Child, Mirror.Child) -> Bool)? = nil,
_ transform: (inout Mirror.Child, Int) -> Void = { _, _ in }
) {
var lhsChildren = Array(lhsMirror.children)
var rhsChildren = Array(rhsMirror.children)
guard !isMirrorEqual(lhsChildren, rhsChildren)
else {
print(
"// Not equal but no difference detected:"
.indenting(by: indent)
.indenting(with: format.both + " "),
to: &out
)
print(
_customDump(
lhs,
name: lhsName,
indent: indent,
maxDepth: 0
)
.indenting(with: format.first + " "),
to: &out
)
print(
_customDump(
rhs,
name: rhsName,
indent: indent,
maxDepth: 0
)
.indenting(with: format.second + " "),
terminator: "",
to: &out
)
return
}
guard !lhsMirror.isSingleValueContainer && !rhsMirror.isSingleValueContainer
else {
print(
_customDump(
lhs,
name: lhsName,
indent: indent,
maxDepth: .max
)
.indenting(with: format.first + " "),
to: &out
)
print(
_customDump(
rhs,
name: rhsName,
indent: indent,
maxDepth: .max
)
.indenting(with: format.second + " "),
terminator: "",
to: &out
)
return
}
let name = rhsName.map { "\($0): " } ?? ""
print(
name
.appending(prefix)
.indenting(by: indent)
.indenting(with: format.both + " "),
to: &out
)
if let areInIncreasingOrder = areInIncreasingOrder {
lhsChildren.sort(by: areInIncreasingOrder)
rhsChildren.sort(by: areInIncreasingOrder)
}
let difference = rhsChildren.difference(from: lhsChildren, by: areEquivalent)
var lhsOffset = 0
var rhsOffset = 0
var unchangedBuffer: [Mirror.Child] = []
func flushUnchanged() {
guard collapseUnchanged else { return }
if areInIncreasingOrder == nil && unchangedBuffer.count == 1 {
let child = unchangedBuffer[0]
print(
_customDump(
child.value,
name: child.label,
indent: indent + elementIndent,
maxDepth: 0
)
.indenting(with: format.both + " "),
terminator: rhsOffset - 1 == rhsChildren.count - 1 ? "\n" : "\(elementSeparator)\n",
to: &out
)
} else if areInIncreasingOrder != nil && unchangedBuffer.count == 1
|| unchangedBuffer.count > 1
{
print(
"… (\(unchangedBuffer.count) unchanged)"
.indenting(by: indent + elementIndent)
.indenting(with: format.both + " "),
terminator: rhsOffset - 1 == rhsChildren.count - 1 ? "\n" : "\(elementSeparator)\n",
to: &out
)
}
unchangedBuffer.removeAll()
}
while lhsOffset < lhsChildren.count || rhsOffset < rhsChildren.count {
let isRemoval = difference.removals.contains(where: { $0.offset == lhsOffset })
let isInsertion = difference.insertions.contains(where: { $0.offset == rhsOffset })
if collapseUnchanged,
!isRemoval,
!isInsertion,
isMirrorEqual(lhsChildren[lhsOffset], rhsChildren[rhsOffset])
{
var child = rhsChildren[rhsOffset]
transform(&child, rhsOffset)
unchangedBuffer.append(child)
lhsOffset += 1
rhsOffset += 1
continue
}
if areInIncreasingOrder == nil {
flushUnchanged()
}
switch (isRemoval, isInsertion) {
case (true, true), (false, false):
var lhsChild = lhsChildren[lhsOffset]
var rhsChild = rhsChildren[rhsOffset]
transform(&lhsChild, isRemoval ? lhsOffset : rhsOffset)
transform(&rhsChild, rhsOffset)
print(
diffHelp(
lhsChild.value,
rhsChild.value,
lhsName: lhsChild.label,
rhsName: rhsChild.label,
separator: lhsOffset == lhsChildren.count - 1 && rhsOffset == rhsChildren.count - 1
? ""
: elementSeparator,
indent: indent + elementIndent
),
to: &out
)
lhsOffset += 1
rhsOffset += 1
continue
case (true, false):
var lhsChild = lhsChildren[lhsOffset]
transform(&lhsChild, lhsOffset)
print(
_customDump(
lhsChild.value,
name: lhsChild.label,
indent: indent + elementIndent,
maxDepth: .max
)
.indenting(with: format.first + " "),
terminator: lhsOffset == lhsChildren.count - 1 ? "\n" : "\(elementSeparator)\n",
to: &out
)
lhsOffset += 1
case (false, true):
var rhsChild = rhsChildren[rhsOffset]
transform(&rhsChild, rhsOffset)
print(
_customDump(
rhsChild.value,
name: rhsChild.label,
indent: indent + elementIndent,
maxDepth: .max
)
.indenting(with: format.second + " "),
terminator: rhsOffset == rhsChildren.count - 1 && unchangedBuffer.isEmpty
? "\n"
: "\(elementSeparator)\n",
to: &out
)
rhsOffset += 1
}
}
flushUnchanged()
print(
suffix
.indenting(by: indent)
.indenting(with: format.both + " "),
terminator: separator,
to: &out
)
}
switch (lhs, lhsMirror.displayStyle, rhs, rhsMirror.displayStyle) {
case (is CustomDumpStringConvertible, _, is CustomDumpStringConvertible, _):
diffEverything()
case let (lhs as CustomDumpRepresentable, _, rhs as CustomDumpRepresentable, _):
out.write(
diffHelp(
lhs.customDumpValue,
rhs.customDumpValue,
lhsName: lhsName,
rhsName: rhsName,
separator: separator,
indent: indent
)
)
case let (lhs as AnyObject, .class?, rhs as AnyObject, .class?):
let lhsItem = ObjectIdentifier(lhs)
let rhsItem = ObjectIdentifier(rhs)
let subjectType = typeName(lhsMirror.subjectType)
if visitedItems.contains(lhsItem) || visitedItems.contains(rhsItem) {
print(
"\(lhsName.map { "\($0): " } ?? "")\(subjectType)(↩︎)"
.indenting(by: indent)
.indenting(with: format.first + " "),
to: &out
)
print(
"\(rhsName.map { "\($0): " } ?? "")\(subjectType)(↩︎)"
.indenting(by: indent)
.indenting(with: format.second + " "),
terminator: "",
to: &out
)
} else {
let showObjectIdentifiers =
lhsItem != rhsItem
&& isMirrorEqual(Array(lhsMirror.children), Array(rhsMirror.children))
let lhsMirror =
showObjectIdentifiers
? Mirror(lhs, children: [("_", lhsItem)] + lhsMirror.children, displayStyle: .class)
: lhsMirror
let rhsMirror =
showObjectIdentifiers
? Mirror(rhs, children: [("_", rhsItem)] + rhsMirror.children, displayStyle: .class)
: rhsMirror
visitedItems.insert(lhsItem)
diffChildren(
lhsMirror,
rhsMirror,
prefix: "\(subjectType)(",
suffix: ")",
elementIndent: 2,
elementSeparator: ",",
collapseUnchanged: false
)
}
case (_, .collection?, _, .collection?):
diffChildren(
lhsMirror,
rhsMirror,
prefix: "[",
suffix: "]",
elementIndent: 2,
elementSeparator: ",",
collapseUnchanged: true,
areEquivalent: {
isIdentityEqual($0.value, $1.value) || isMirrorEqual($0.value, $1.value)
},
{ $0.label = "[\($1)]" }
)
case (_, .dictionary?, _, .dictionary?):
diffChildren(
lhsMirror,
rhsMirror,
prefix: "[",
suffix: "]",
elementIndent: 2,
elementSeparator: ",",
collapseUnchanged: true,
areEquivalent: {
guard
let lhs = $0.value as? (key: AnyHashable, value: Any),
let rhs = $1.value as? (key: AnyHashable, value: Any)
else {
return isMirrorEqual($0.value, $1.value)
}
return lhs.key == rhs.key
},
areInIncreasingOrder: {
guard
let lhs = $0.value as? (key: AnyHashable, value: Any),
let rhs = $1.value as? (key: AnyHashable, value: Any)
else {
return _customDump($0.value, name: nil, indent: 0, maxDepth: 1)
< _customDump($1.value, name: nil, indent: 0, maxDepth: 1)
}
return _customDump(lhs.key.base, name: nil, indent: 0, maxDepth: 1)
< _customDump(rhs.key.base, name: nil, indent: 0, maxDepth: 1)
}
) { child, _ in
guard let pair = child.value as? (key: AnyHashable, value: Any) else { return }
child = (
_customDump(pair.key.base, name: nil, indent: 0, maxDepth: 1),
pair.value
)
}
case (_, .enum?, _, .enum?):
guard
let lhsChild = lhsMirror.children.first,
let rhsChild = rhsMirror.children.first,
let caseName = lhsChild.label,
caseName == rhsChild.label
else {
diffEverything()
break
}
let lhsChildMirror = Mirror(customDumpReflecting: lhsChild.value)
let rhsChildMirror = Mirror(customDumpReflecting: rhsChild.value)
let lhsAssociatedValuesMirror =
lhsChildMirror.displayStyle == .tuple
? lhsChildMirror
: Mirror(lhs, unlabeledChildren: [lhsChild.value], displayStyle: .tuple)
let rhsAssociatedValuesMirror =
rhsChildMirror.displayStyle == .tuple
? rhsChildMirror
: Mirror(rhs, unlabeledChildren: [rhsChild.value], displayStyle: .tuple)
let subjectType = typeName(lhsMirror.subjectType)
diffChildren(
lhsAssociatedValuesMirror,
rhsAssociatedValuesMirror,
prefix: "\(subjectType).\(caseName)(",
suffix: ")",
elementIndent: 2,
elementSeparator: ",",
collapseUnchanged: false,
{ child, _ in
if child.label?.first == "." {
child.label = nil
}
}
)
case (_, .optional?, _, .optional?):
guard
let lhsValue = lhsMirror.children.first?.value,
let rhsValue = rhsMirror.children.first?.value
else {
diffEverything()
break
}
out.write(
diffHelp(
lhsValue,
rhsValue,
lhsName: lhsName,
rhsName: rhsName,
separator: separator,
indent: indent
)
)
case (_, .set?, _, .set?):
diffChildren(
lhsMirror,
rhsMirror,
prefix: "Set([",
suffix: "])",
elementIndent: 2,
elementSeparator: ",",
collapseUnchanged: true,
areEquivalent: {
isIdentityEqual($0.value, $1.value) || isMirrorEqual($0.value, $1.value)
},
areInIncreasingOrder: {
_customDump($0.value, name: nil, indent: 0, maxDepth: 1)
< _customDump($1.value, name: nil, indent: 0, maxDepth: 1)
}
)
case (_, .struct?, _, .struct?):
diffChildren(
lhsMirror,
rhsMirror,
prefix: "\(typeName(lhsMirror.subjectType))(",
suffix: ")",
elementIndent: 2,
elementSeparator: ",",
collapseUnchanged: excludedNoChangedProperty,
areInIncreasingOrder:
excludedNoChangedProperty
? { ($0.label ?? "") > ($1.label ?? "") }
: nil
)
case (_, .tuple?, _, .tuple?):
diffChildren(
lhsMirror,
rhsMirror,
prefix: "(",
suffix: ")",
elementIndent: 2,
elementSeparator: ",",
collapseUnchanged: false,
{ child, _ in
if child.label?.first == "." {
child.label = nil
}
}
)
default:
if let lhs = stringFromStringProtocol(lhs),
let rhs = stringFromStringProtocol(rhs),
lhs.contains(where: \.isNewline) || rhs.contains(where: \.isNewline)
{
let lhsMirror = Mirror(
customDumpReflecting:
lhs.isEmpty
? []
: lhs
.split(separator: "\n", omittingEmptySubsequences: false)
.map(Line.init(rawValue:))
)
let rhsMirror = Mirror(
customDumpReflecting:
rhs.isEmpty
? []
: rhs
.split(separator: "\n", omittingEmptySubsequences: false)
.map(Line.init(rawValue:))
)
let hashes = String(repeating: "#", count: max(lhs.hashCount, rhs.hashCount))
diffChildren(
lhsMirror,
rhsMirror,
prefix: "\(hashes)\"\"\"",
suffix: rhsName != nil ? " \"\"\"\(hashes)" : "\"\"\"\(hashes)",
elementIndent: rhsName != nil ? 2 : 0,
elementSeparator: "",
collapseUnchanged: false,
areEquivalent: {
isIdentityEqual($0.value, $1.value) || isMirrorEqual($0.value, $1.value)
}
)
} else {
diffEverything()
}
}
return out
}
guard !isMirrorEqual(lhs, rhs) else { return nil }
var diff = diffHelp(lhs, rhs, lhsName: nil, rhsName: nil, separator: "", indent: 0)
if diff.last == "\n" { diff.removeLast() }
return diff
}
/// Describes how to format a difference between two values when using ``diff(_:_:format:)``.
///
/// Typically one simply wants to use "-" to denote removals, "+" to denote additions, and " " for
/// spacing. However, in some contexts, such as in `XCTest` failures, messages are displayed in a
/// non-monospaced font. In those times the simple "-" and " " characters do not properly line up
/// visually, and so you need to use different characters that visually look similar to "-" and " "
/// but have the proper widths.
///
/// This type comes with two pre-configured formats that you will probably want to use for most
/// situations: ``DiffFormat/default`` and ``DiffFormat/proportional``.
public struct DiffFormat {
/// A string prepended to lines that only appear in the string representation of the first value,
/// e.g. a "removal."
public var first: String
/// A string prepended to lines that only appear in the string representation of the second value,
/// e.g. an "insertion."
public var second: String
/// A string prepended to lines that appear in the string representation of both values, e.g.
/// something "unchanged."
public var both: String
public init(
first: String,
second: String,
both: String
) {
self.first = first
self.second = second
self.both = both
}
/// The default format for ``diff(_:_:format:)`` output, appropriate for where monospaced fonts
/// are used, e.g. console output.
///
/// Uses ascii characters for removals (hyphen "-"), insertions (plus "+"), and unchanged (space
/// " ").
public static let `default` = Self(first: "-", second: "+", both: " ")
/// A diff format appropriate for where proportional (non-monospaced) fonts are used, e.g. Xcode's
/// failure overlays.
///
/// Uses ascii plus ("+") for insertions, unicode minus sign ("−") for removals, and unicode
/// figure space (" ") for unchanged. These three characters are more likely to render with equal
/// widths in proportional fonts.
public static let proportional = Self(first: "\u{2212}", second: "+", both: "\u{2007}")
}
private struct Line: CustomDumpStringConvertible, Identifiable {
var rawValue: Substring
var id: Substring {
self.rawValue
}
var customDumpDescription: String {
.init(self.rawValue)
}
}