|
| 1 | +import Testing |
| 2 | + |
| 3 | +@testable import gitdiff |
| 4 | + |
| 5 | +/// Contract tests for the `DiffParsing` extension point: custom parsers can |
| 6 | +/// construct the library's domain model directly without relying on the |
| 7 | +/// built-in unified-diff parser. |
| 8 | +struct DiffParsingTests { |
| 9 | + |
| 10 | + // MARK: - UnifiedDiffParser |
| 11 | + |
| 12 | + @Test |
| 13 | + func unifiedDiffParserMatchesLegacyStaticParser() async throws { |
| 14 | + let diff = """ |
| 15 | + diff --git a/foo.txt b/foo.txt |
| 16 | + index 1111111..2222222 100644 |
| 17 | + --- a/foo.txt |
| 18 | + +++ b/foo.txt |
| 19 | + @@ -1,2 +1,3 @@ |
| 20 | + line1 |
| 21 | + -line2 |
| 22 | + +line2 changed |
| 23 | + +line3 |
| 24 | + """ |
| 25 | + |
| 26 | + let legacy = try await DiffParser.parse(diff) |
| 27 | + let viaProtocol = try await UnifiedDiffParser().parse(diff) |
| 28 | + |
| 29 | + #expect(legacy.count == viaProtocol.count) |
| 30 | + for (a, b) in zip(legacy, viaProtocol) { |
| 31 | + #expect(a.oldPath == b.oldPath) |
| 32 | + #expect(a.newPath == b.newPath) |
| 33 | + #expect(a.hunks.count == b.hunks.count) |
| 34 | + for (h1, h2) in zip(a.hunks, b.hunks) { |
| 35 | + #expect(h1.lines.count == h2.lines.count) |
| 36 | + for (l1, l2) in zip(h1.lines, h2.lines) { |
| 37 | + #expect(l1.type == l2.type) |
| 38 | + #expect(l1.content == l2.content) |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + // MARK: - Custom parser path |
| 45 | + |
| 46 | + @Test |
| 47 | + func customParserCanBuildDiffFilesDirectly() async throws { |
| 48 | + /// A throwaway parser that ignores its input and produces a single |
| 49 | + /// hand-built `DiffFile`. This exercises the public initialisers on |
| 50 | + /// `DiffFile` / `DiffHunk` / `DiffLine` and the protocol contract. |
| 51 | + struct FixtureParser: DiffParsing { |
| 52 | + let filePath: String |
| 53 | + func parse(_ diffText: String) async throws -> [DiffFile] { |
| 54 | + [ |
| 55 | + DiffFile( |
| 56 | + oldPath: filePath, |
| 57 | + newPath: filePath, |
| 58 | + hunks: [ |
| 59 | + DiffHunk( |
| 60 | + oldStart: 1, |
| 61 | + oldCount: 1, |
| 62 | + newStart: 1, |
| 63 | + newCount: 1, |
| 64 | + header: "fixture", |
| 65 | + lines: [ |
| 66 | + DiffLine(type: .removed, content: "old", oldLineNumber: 1, newLineNumber: nil), |
| 67 | + DiffLine(type: .added, content: "new", oldLineNumber: nil, newLineNumber: 1), |
| 68 | + ] |
| 69 | + ) |
| 70 | + ] |
| 71 | + ) |
| 72 | + ] |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + let files = try await FixtureParser(filePath: "x.swift").parse("anything") |
| 77 | + #expect(files.count == 1) |
| 78 | + #expect(files[0].oldPath == "x.swift") |
| 79 | + #expect(files[0].hunks[0].lines[0].type == .removed) |
| 80 | + #expect(files[0].hunks[0].lines[1].type == .added) |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + func defaultEnvironmentParserIsUnifiedDiffParser() { |
| 85 | + /// Confirms the environment default that `DiffRenderer` reads. |
| 86 | + let key = DiffParserKey.defaultValue |
| 87 | + #expect(key is UnifiedDiffParser) |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + func parserCanReturnEmptyArrayToTriggerNoContentState() async throws { |
| 92 | + struct EmptyParser: DiffParsing { |
| 93 | + func parse(_ diffText: String) async throws -> [DiffFile] { [] } |
| 94 | + } |
| 95 | + let result = try await EmptyParser().parse("ignored") |
| 96 | + #expect(result.isEmpty) |
| 97 | + } |
| 98 | +} |
0 commit comments