Skip to content

Commit

Permalink
chore: show templates diff in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nekrich committed Aug 2, 2024
1 parent b075441 commit f54feed
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions Templates/Tests/TemplatesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,39 @@ class TemplatesTests: QuickSpec {
fatalError("Template \(name) can not be checked as the expected file can not be read")
}

let emptyLinesFilter: (String) -> Bool = { line in return !line.isEmpty }
let commentLinesFilter: (String) -> Bool = { line in return !line.hasPrefix("//") }
let generatedFileLines = generatedFileString.components(separatedBy: .newlines).filter(emptyLinesFilter)
let generatedFileFilteredLines = generatedFileLines.filter(emptyLinesFilter).filter(commentLinesFilter)
let generatedFileLines = generatedFileString.components(separatedBy: .newlines)
let expectedFileLines = expectedFileString.components(separatedBy: .newlines)
let expectedFileFilteredLines = expectedFileLines.filter(emptyLinesFilter).filter(commentLinesFilter)
expect(generatedFileFilteredLines).to(equal(expectedFileFilteredLines))

/// String normalization.
/// Transformations:
/// * Trim all whitespaces, tabs and new lines.
/// * If this line is comment (starts with `//`) treat is as an empty line.
let normalizeString: (String) -> String = {
let string = $0.trimmingCharacters(in: .whitespacesAndNewlines)
if string.hasPrefix("//") {
return ""
}
return string
}

// Allow test to produce all failures. So the diff is clearly visible.
self.continueAfterFailure = true

// Get the full diff.
let diff: CollectionDifference<String> = expectedFileLines.difference(from: generatedFileLines) { (lhs: String, rhs: String) -> Bool in
normalizeString(lhs) == normalizeString(rhs)
}

for diffLine in diff {
switch diffLine {
case let .insert(offset: offset, element: element, associatedWith: associatedWith) where !normalizeString(element).isEmpty:
fail("Inserted line: \(element) at offset: \(offset) associated with: \(String(describing: associatedWith))")
case let .remove(offset: offset, element: element, associatedWith: associatedWith) where !normalizeString(element).isEmpty:
fail("Removed line: \(element) at offset: \(offset) associated with: \(String(describing: associatedWith))")
default:
continue
}
}
}

#if !canImport(ObjectiveC)
Expand Down

0 comments on commit f54feed

Please sign in to comment.