Skip to content

Commit

Permalink
Improve tests and add famous examples
Browse files Browse the repository at this point in the history
  • Loading branch information
ygormutti committed Sep 4, 2018
1 parent edd577b commit 2b29ca5
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 47 deletions.
16 changes: 5 additions & 11 deletions diffless/languages/json/tests/json.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import { readTextFile } from '../../../cli';
import { Document } from '../../../model';
import { announceHtml, fixture } from '../../../tests/test-util';
import { announceHtml, fixtureDocument } from '../../../tests/test-util';
import { jsonDiff, jsonLexicalDiff } from '../index';

describe('JSON support', () => {
const mapReorderleftPath = fixture('single_map_reorder/before.json');
const mapReorderRightPath = fixture('single_map_reorder/after.json');
const mapReorderleft = new Document('file://' + mapReorderleftPath, readTextFile(mapReorderleftPath));
const mapReorderRight = new Document('file://' + mapReorderRightPath, readTextFile(mapReorderRightPath));
const mapReorderleft = fixtureDocument('single_map_reorder/before.json');
const mapReorderRight = fixtureDocument('single_map_reorder/after.json');

describe('jsonLexicalDiff', () => {
it('should match snapshot', () => {
Expand All @@ -27,10 +23,8 @@ describe('JSON support', () => {
});

it('should match snapshot', () => {
const leftPath = fixture('textual_changes/before.json');
const rightPath = fixture('textual_changes/after.json');
const left = new Document('file://' + leftPath, readTextFile(leftPath));
const right = new Document('file://' + rightPath, readTextFile(rightPath));
const left = fixtureDocument('textual_changes/before.json');
const right = fixtureDocument('textual_changes/after.json');

const diff = jsonDiff(left, right);

Expand Down
12 changes: 3 additions & 9 deletions diffless/languages/json/tests/lexer.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
import { readTextFile } from '../../../cli';
import { Document } from '../../../model';
import { fixture } from '../../../tests/test-util';
import { fixtureDocument } from '../../../tests/test-util';
import { tokenize } from '../lexer';

describe('lexer', () => {
describe('tokenize', () => {
it('should match snapshot', () => {
const path = fixture('textual_changes/before.json');
const document = new Document('file://' + path, readTextFile(path));

const document = fixtureDocument('textual_changes/before.json');
expect(tokenize(document)).toMatchSnapshot();
});

it('should match snapshot', () => {
const path = fixture('textual_changes/after.json');
const document = new Document('file://' + path, readTextFile(path));

const document = fixtureDocument('textual_changes/after.json');
expect(tokenize(document)).toMatchSnapshot();
});
});
Expand Down
49 changes: 49 additions & 0 deletions diffless/tests/__snapshots__/diffless.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,41 @@ Array [
]
`;
exports[`array/diff lineDiff should be able to compare strings directly 2`] = `
Array [
Object {
"left": Location {
"range": [4:1; 7:1),
"uri": "string:left",
},
"level": "Textual",
"right": Location {
"range": [4:1; 7:1),
"uri": "string:right",
},
"type": "Move",
},
Object {
"left": Location {
"range": [7:1; 10:1),
"uri": "string:left",
},
"level": "Textual",
"right": undefined,
"type": "Delete",
},
Object {
"left": undefined,
"level": "Textual",
"right": Location {
"range": [1:1; 4:1),
"uri": "string:right",
},
"type": "Add",
},
]
`;
exports[`array/diff lineDiff should be possible to use no weight 1`] = `
Array [
Object {
Expand Down Expand Up @@ -298,3 +333,17 @@ Array [
},
]
`;
exports[`array/diff lineDiff should produce good quality diff 1`] = `
Array [
Object {
"left": undefined,
"level": "Textual",
"right": Location {
"range": [3:1; 7:1),
"uri": "file://diffless/tests/fixtures/patience/after.c",
},
"type": "Add",
},
]
`;
106 changes: 80 additions & 26 deletions diffless/tests/diffless.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ArrayDiffTool } from '../array/diff';
import { characterDiff, lineDiff } from '../index';
import { DiffLevel, Document } from '../model';
import { announceHtml } from '../tests/test-util';
import { announceHtml, fixtureDocument } from '../tests/test-util';
import { stripMargin } from '../util';

describe('array/diff', () => {
Expand Down Expand Up @@ -75,24 +75,35 @@ describe('array/diff', () => {
});

describe('lineDiff', () => {
const left = new Document('string:left', stripMargin
`abc
|def
|ghijklmn`,
);
const right = new Document('string:right', stripMargin
`abc
|ghijklmn
|def`,
);

it('should consider line weight', () => {
const left = new Document('string:left', stripMargin
`abc
|def
|ghijklmn`,
);
const right = new Document('string:right', stripMargin
`abc
|ghijklmn
|def`,
);

const diff = lineDiff(left, right);
expect(diff.edits).toMatchSnapshot();
announceHtml(left, right, diff.edits, 'move_lines_weighed');
});

it('should be possible to use no weight', () => {
const left = new Document('string:left', stripMargin
`abc
|def
|ghijklmn`,
);
const right = new Document('string:right', stripMargin
`abc
|ghijklmn
|def`,
);

const tool = new ArrayDiffTool({
level: DiffLevel.Textual,
similarityThreshold: 0,
Expand All @@ -106,27 +117,70 @@ describe('array/diff', () => {

it('should be able to compare strings directly', () => {
const otherLeft = stripMargin
`"m"
|"z"
|"j"
|"a"
|"w"
|"x"
|"u"`;
`"m"
|"z"
|"j"
|"a"
|"w"
|"x"
|"u"`;

const otherRight = stripMargin
`"a"
|"j"
|"m"
|"u"
|"w"
|"x"
|"z"`;
`"a"
|"j"
|"m"
|"u"
|"w"
|"x"
|"z"`;

const diff = lineDiff(otherLeft, otherRight);

expect(diff.edits).toMatchSnapshot();
announceHtml(diff.left, diff.right, diff.edits, 'strings');
});

it('should be able to compare strings directly', () => {
// from: https://news.ycombinator.com/item?id=13987217
const otherLeft = stripMargin
`a
|b
|c
|1
|2
|3
|x
|y
|z
|`;

const otherRight = stripMargin
`x
|y
|z
|1
|2
|3
|a
|b
|c
|`;

const diff = lineDiff(otherLeft, otherRight);

expect(diff.edits).toMatchSnapshot(); // FIXME result is wrong
announceHtml(diff.left, diff.right, diff.edits, 'move-fail');
});

it('should produce good quality diff', () => {
// from: https://bramcohen.livejournal.com/73318.html
const left = fixtureDocument('patience/before.c');
const right = fixtureDocument('patience/after.c');

const diff = lineDiff(left, right);

expect(diff.edits).toMatchSnapshot(); // FIXME result is poor
announceHtml(diff.left, diff.right, diff.edits, 'patience');
});
});
});
11 changes: 11 additions & 0 deletions diffless/tests/fixtures/patience/after.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
void func1() {
x += 1
}

void functhreehalves() {
x += 1.5
}

void func2() {
x += 2
}
7 changes: 7 additions & 0 deletions diffless/tests/fixtures/patience/before.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
void func1() {
x += 1
}

void func2() {
x += 2
}
7 changes: 6 additions & 1 deletion diffless/tests/test-util.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { resolve } from 'path';

import { saveAnnotatedHtml } from '../cli';
import { readTextFile, saveAnnotatedHtml } from '../cli';
import { Document, Edit } from '../model';

export function fixture(path: string) {
return `diffless/tests/fixtures/${path}`;
}

export function fixtureDocument(path: string): Document {
path = fixture(path);
return new Document('file://' + path, readTextFile(path));
}

let seed = 0;
export function announceHtml(left: Document, right: Document, edits: Edit[], name?: string) {
const outputPath = `dist/tmp_${name || seed++}.html`;
Expand Down

0 comments on commit 2b29ca5

Please sign in to comment.