diff --git a/Templates/Tests/TemplatesTests.swift b/Templates/Tests/TemplatesTests.swift index 46acfd5df..2e226364f 100644 --- a/Templates/Tests/TemplatesTests.swift +++ b/Templates/Tests/TemplatesTests.swift @@ -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 = 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)