177 lines
5.1 KiB
Dart
177 lines
5.1 KiB
Dart
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:pipelines_app/features/changes/domain/unified_patch.dart';
|
|
|
|
void main() {
|
|
test('a blank or absent patch parses to an empty result', () {
|
|
for (final blank in <dynamic>['', ' ', '\n', null]) {
|
|
final parsed = parseUnifiedPatch(blank);
|
|
expect(parsed.raw, '');
|
|
expect(parsed.headerLines, isEmpty);
|
|
expect(parsed.hunks, isEmpty);
|
|
expect(parsed.added, 0);
|
|
expect(parsed.removed, 0);
|
|
}
|
|
});
|
|
|
|
test('a single hunk is numbered and counted', () {
|
|
final patch = [
|
|
'--- "a/x.txt"',
|
|
'+++ "b/x.txt"',
|
|
'@@ -1,3 +1,3 @@',
|
|
' ctx',
|
|
'-old',
|
|
'+new',
|
|
' tail',
|
|
].join('\n');
|
|
final parsed = parseUnifiedPatch(patch);
|
|
expect(parsed.raw, patch);
|
|
expect(parsed.headerLines, ['--- "a/x.txt"', '+++ "b/x.txt"']);
|
|
expect(parsed.hunks, hasLength(1));
|
|
final hunk = parsed.hunks.single;
|
|
expect(hunk.header, '@@ -1,3 +1,3 @@');
|
|
expect(
|
|
[hunk.oldStart, hunk.oldCount, hunk.newStart, hunk.newCount],
|
|
[1, 3, 1, 3],
|
|
);
|
|
expect(
|
|
[
|
|
for (final line in hunk.lines)
|
|
[line.kind, line.text, line.oldLine, line.newLine],
|
|
],
|
|
[
|
|
[ChangeLineKind.context, 'ctx', 1, 1],
|
|
[ChangeLineKind.remove, 'old', 2, 0],
|
|
[ChangeLineKind.add, 'new', 0, 2],
|
|
[ChangeLineKind.context, 'tail', 3, 3],
|
|
],
|
|
);
|
|
expect(parsed.added, 1);
|
|
expect(parsed.removed, 1);
|
|
});
|
|
|
|
test('a missing count defaults to 1', () {
|
|
final hunk = parseUnifiedPatch('@@ -5 +7 @@\n-old\n+new').hunks.single;
|
|
expect(
|
|
[hunk.oldStart, hunk.oldCount, hunk.newStart, hunk.newCount],
|
|
[5, 1, 7, 1],
|
|
);
|
|
});
|
|
|
|
test('a multi-hunk patch keeps each hunk header and its own numbering', () {
|
|
final parsed = parseUnifiedPatch(
|
|
[
|
|
'--- "a/lib/x.dart"',
|
|
'+++ "b/lib/x.dart"',
|
|
'@@ -1,2 +1,3 @@',
|
|
' one',
|
|
'+two',
|
|
' three',
|
|
'@@ -40,3 +41,2 @@',
|
|
' forty',
|
|
'-forty-one',
|
|
' forty-two',
|
|
].join('\n'),
|
|
);
|
|
expect(parsed.hunks, hasLength(2));
|
|
expect(parsed.hunks[0].lines.map((line) => line.newLine), [1, 2, 3]);
|
|
expect(parsed.hunks[1].lines.map((line) => line.oldLine), [40, 41, 42]);
|
|
expect(parsed.added, 1);
|
|
expect(parsed.removed, 1);
|
|
expect(parsed.headerLines, ['--- "a/lib/x.dart"', '+++ "b/lib/x.dart"']);
|
|
});
|
|
|
|
test('a no-newline marker is shown verbatim and never numbered', () {
|
|
final lines = parseUnifiedPatch(
|
|
[
|
|
'@@ -1 +1 @@',
|
|
'-one',
|
|
r'\ No newline at end of file',
|
|
'+two',
|
|
].join('\n'),
|
|
).hunks.single.lines;
|
|
expect(lines[1].noNewlineMarker, isTrue);
|
|
expect(lines[1].text, r'\ No newline at end of file');
|
|
expect(lines[1].oldLine, 0);
|
|
expect(lines[1].newLine, 0);
|
|
expect(lines[1].kind, ChangeLineKind.context);
|
|
});
|
|
|
|
test('a second file header closes the current hunk', () {
|
|
final parsed = parseUnifiedPatch(
|
|
[
|
|
'--- "a/x"',
|
|
'+++ "b/x"',
|
|
'@@ -1 +1 @@',
|
|
'-old',
|
|
'+new',
|
|
'--- "a/y"',
|
|
'+++ "b/y"',
|
|
'@@ -1 +1 @@',
|
|
'-alt',
|
|
'+neu',
|
|
].join('\n'),
|
|
);
|
|
expect(parsed.hunks, hasLength(2));
|
|
expect(parsed.hunks[0].lines.map((line) => line.text), ['old', 'new']);
|
|
expect(parsed.hunks[1].lines.map((line) => line.text), ['alt', 'neu']);
|
|
expect(
|
|
parsed.headerLines.where((line) => line.startsWith('--- ')),
|
|
hasLength(2),
|
|
);
|
|
expect(parsed.added, 2);
|
|
expect(parsed.removed, 2);
|
|
});
|
|
|
|
test('metadata before a hunk is kept; a truncated tail is unnumbered', () {
|
|
final parsed = parseUnifiedPatch(
|
|
[
|
|
'index abc..def 100644',
|
|
'--- "a/x"',
|
|
'+++ "b/x"',
|
|
'@@ -1 +1 @@',
|
|
'-old',
|
|
'+new',
|
|
'trailing trunc',
|
|
].join('\n'),
|
|
);
|
|
expect(parsed.headerLines.first, 'index abc..def 100644');
|
|
final tail = parsed.hunks.single.lines.last;
|
|
expect(tail.kind, ChangeLineKind.context);
|
|
expect(tail.text, 'trailing trunc');
|
|
expect([tail.oldLine, tail.newLine], [0, 0]);
|
|
});
|
|
|
|
test('a trailing newline becomes a numbered empty context line', () {
|
|
final lines = parseUnifiedPatch('@@ -1 +1 @@\n ctx\n').hunks.single.lines;
|
|
expect(lines, hasLength(2));
|
|
expect([lines[1].text, lines[1].oldLine, lines[1].newLine], ['', 2, 2]);
|
|
});
|
|
|
|
test('gutter width, gutter text, symbols and stat match the web wording', () {
|
|
expect(
|
|
gutterWidth(
|
|
const PatchHunk(
|
|
header: 'h',
|
|
oldStart: 0,
|
|
oldCount: 0,
|
|
newStart: 0,
|
|
newCount: 0,
|
|
lines: [],
|
|
),
|
|
),
|
|
1,
|
|
);
|
|
final hunk = parseUnifiedPatch('@@ -9,1 +12,1 @@\n-a\n+b').hunks.single;
|
|
expect(gutterWidth(hunk), 2);
|
|
expect(gutterText(0, 2), ' ');
|
|
expect(gutterText(7, 2), ' 7');
|
|
expect(diffLineSymbol(ChangeLineKind.add), '+');
|
|
expect(diffLineSymbol(ChangeLineKind.remove), '-');
|
|
expect(diffLineSymbol(ChangeLineKind.context), ' ');
|
|
expect(changeStat(3, 2), '+3 -2');
|
|
expect(changeStat(3, 0), '+3');
|
|
expect(changeStat(0, 2), '-2');
|
|
expect(changeStat(0, 0), '');
|
|
});
|
|
}
|