Skip to content

Commit

Permalink
Swap tap for node:test
Browse files Browse the repository at this point in the history
Node's built-in test runner works great!
  • Loading branch information
bhousel committed Dec 21, 2023
1 parent f9a7850 commit 618c8bc
Show file tree
Hide file tree
Showing 10 changed files with 288 additions and 334 deletions.
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,15 @@
"build:browser": "esbuild ./index.mjs --platform=browser --format=iife --global-name=Diff3 --bundle --sourcemap --outfile=./dist/index.iife.js",
"build:cjs": "esbuild ./index.mjs --platform=node --format=cjs --sourcemap --outfile=./dist/index.cjs",
"lint": "eslint index.mjs test/*.js",
"tap": "c8 tap --reporter terse --no-cov test/*.js",
"test": "run-s build lint tap"
"test": "run-s build test:node",
"test:node": "c8 node --test test/"
},
"devDependencies": {
"c8": "^8.0.1",
"esbuild": "^0.19.10",
"eslint": "^8.56.0",
"npm-run-all": "^4.1.5",
"shx": "^0.3.4",
"tap": "^16.3.10"
"shx": "^0.3.4"
},
"sideEffects": false,
"publishConfig": {
Expand Down
36 changes: 17 additions & 19 deletions test/LCS.test.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,33 @@
import { test } from 'tap';
import { test } from 'node:test';
import { strict as assert } from 'node:assert';
import * as Diff3 from '../index.mjs';

test('LCS', t => {
test('LCS', async t => {

t.test('returns the LCS of two arrays', t => {
await t.test('returns the LCS of two arrays', t => {
const a = ['AA', 'a', 'b', 'c', 'ZZ', 'new', '00', 'a', 'a', 'M', '99'];
const b = ['AA', 'a', 'd', 'c', 'ZZ', '11', 'M', 'z', 'z', '99'];
const result = Diff3.LCS(a, b);
const NULLRESULT = { buffer1index: -1, buffer2index: -1, chain: null };

t.same(result.buffer1index, 10); // '99'
t.same(result.buffer2index, 9);
assert.deepEqual(result.buffer1index, 10); // '99'
assert.deepEqual(result.buffer2index, 9);

t.same(result.chain.buffer1index, 9); // 'M'
t.same(result.chain.buffer2index, 6);
assert.deepEqual(result.chain.buffer1index, 9); // 'M'
assert.deepEqual(result.chain.buffer2index, 6);

t.same(result.chain.chain.buffer1index, 4); // 'ZZ'
t.same(result.chain.chain.buffer2index, 4);
assert.deepEqual(result.chain.chain.buffer1index, 4); // 'ZZ'
assert.deepEqual(result.chain.chain.buffer2index, 4);

t.same(result.chain.chain.chain.buffer1index, 3); // 'c'
t.same(result.chain.chain.chain.buffer2index, 3);
assert.deepEqual(result.chain.chain.chain.buffer1index, 3); // 'c'
assert.deepEqual(result.chain.chain.chain.buffer2index, 3);

t.same(result.chain.chain.chain.chain.buffer1index, 1); // 'a'
t.same(result.chain.chain.chain.chain.buffer2index, 1);
assert.deepEqual(result.chain.chain.chain.chain.buffer1index, 1); // 'a'
assert.deepEqual(result.chain.chain.chain.chain.buffer2index, 1);

t.same(result.chain.chain.chain.chain.chain.buffer1index, 0); // 'AA'
t.same(result.chain.chain.chain.chain.chain.buffer2index, 0);
t.same(result.chain.chain.chain.chain.chain.chain, NULLRESULT);

t.end();
assert.deepEqual(result.chain.chain.chain.chain.chain.buffer1index, 0); // 'AA'
assert.deepEqual(result.chain.chain.chain.chain.chain.buffer2index, 0);
assert.deepEqual(result.chain.chain.chain.chain.chain.chain, NULLRESULT);
});

t.end();
});
121 changes: 55 additions & 66 deletions test/diff3Merge.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { test } from 'tap';
import { test } from 'node:test';
import { strict as assert } from 'node:assert';
import * as Diff3 from '../index.mjs';

test('diff3Merge', t => {
test('diff3Merge', async t => {

t.test('performs diff3 merge on arrays', t => {
await t.test('performs diff3 merge on arrays', t => {
const o = ['AA', 'ZZ', '00', 'M', '99'];
const a = ['AA', 'a', 'b', 'c', 'ZZ', 'new', '00', 'a', 'a', 'M', '99'];
const b = ['AA', 'a', 'd', 'c', 'ZZ', '11', 'M', 'z', 'z', '99'];
Expand Down Expand Up @@ -38,115 +39,106 @@ test('diff3Merge', t => {
99
*/

t.same(result[0].ok, ['AA']);
t.same(result[0].conflict, undefined);
assert.deepEqual(result[0].ok, ['AA']);
assert.deepEqual(result[0].conflict, undefined);

t.same(result[1].ok, undefined);
t.same(result[1].conflict.o, []);
t.same(result[1].conflict.a, ['a', 'b', 'c']);
t.same(result[1].conflict.b, ['a', 'd', 'c']);
assert.deepEqual(result[1].ok, undefined);
assert.deepEqual(result[1].conflict.o, []);
assert.deepEqual(result[1].conflict.a, ['a', 'b', 'c']);
assert.deepEqual(result[1].conflict.b, ['a', 'd', 'c']);

t.same(result[2].ok, ['ZZ']);
t.same(result[2].conflict, undefined);
assert.deepEqual(result[2].ok, ['ZZ']);
assert.deepEqual(result[2].conflict, undefined);

t.same(result[3].ok, undefined);
t.same(result[3].conflict.o, ['00']);
t.same(result[3].conflict.a, ['new', '00', 'a', 'a']);
t.same(result[3].conflict.b, ['11']);
assert.deepEqual(result[3].ok, undefined);
assert.deepEqual(result[3].conflict.o, ['00']);
assert.deepEqual(result[3].conflict.a, ['new', '00', 'a', 'a']);
assert.deepEqual(result[3].conflict.b, ['11']);

t.same(result[4].ok, ['M', 'z', 'z', '99']);
t.same(result[4].conflict, undefined);

t.end();
assert.deepEqual(result[4].ok, ['M', 'z', 'z', '99']);
assert.deepEqual(result[4].conflict, undefined);
});


t.test('strings split on whitespace by default to avoid surprises - issue #9', t => {
await t.test('strings split on whitespace by default to avoid surprises - issue #9', t => {
const o = 'was touring';
const a = 'was here touring';
const b = 'was into touring';
const result = Diff3.diff3Merge(a, o, b);

t.same(result[0].ok, ['was']);
t.same(result[0].conflict, undefined);

t.same(result[1].ok, undefined);
t.same(result[1].conflict.o, []);
t.same(result[1].conflict.a, ['here']);
t.same(result[1].conflict.b, ['into']);
assert.deepEqual(result[0].ok, ['was']);
assert.deepEqual(result[0].conflict, undefined);

t.same(result[2].ok, ['touring']);
t.same(result[2].conflict, undefined);
assert.deepEqual(result[1].ok, undefined);
assert.deepEqual(result[1].conflict.o, []);
assert.deepEqual(result[1].conflict.a, ['here']);
assert.deepEqual(result[1].conflict.b, ['into']);

t.end();
assert.deepEqual(result[2].ok, ['touring']);
assert.deepEqual(result[2].conflict, undefined);
});

t.test('strings can optionally split on given separator', t => {
await t.test('strings can optionally split on given separator', t => {
const o = 'new hampshire, new mexico, north carolina';
const a = 'new hampshire, new jersey, north carolina';
const b = 'new hampshire, new york, north carolina';
const result = Diff3.diff3Merge(a, o, b, { stringSeparator: /,\s+/ });

t.same(result[0].ok, ['new hampshire']);
t.same(result[0].conflict, undefined);
assert.deepEqual(result[0].ok, ['new hampshire']);
assert.deepEqual(result[0].conflict, undefined);

t.same(result[1].ok, undefined);
t.same(result[1].conflict.o, ['new mexico']);
t.same(result[1].conflict.a, ['new jersey']);
t.same(result[1].conflict.b, ['new york']);
assert.deepEqual(result[1].ok, undefined);
assert.deepEqual(result[1].conflict.o, ['new mexico']);
assert.deepEqual(result[1].conflict.a, ['new jersey']);
assert.deepEqual(result[1].conflict.b, ['new york']);

t.same(result[2].ok, ['north carolina']);
t.same(result[2].conflict, undefined);

t.end();
assert.deepEqual(result[2].ok, ['north carolina']);
assert.deepEqual(result[2].conflict, undefined);
});


t.test('excludes false conflicts by default', t => {
await t.test('excludes false conflicts by default', t => {
const o = 'AA ZZ';
const a = 'AA a b c ZZ';
const b = 'AA a b c ZZ';
const result = Diff3.diff3Merge(a, o, b);

t.same(result[0].ok, ['AA', 'a', 'b', 'c', 'ZZ']);
t.same(result[0].conflict, undefined);
t.end();
assert.deepEqual(result[0].ok, ['AA', 'a', 'b', 'c', 'ZZ']);
assert.deepEqual(result[0].conflict, undefined);
});


t.test('can include false conflicts with option', t => {
await t.test('can include false conflicts with option', t => {
const o = 'AA ZZ';
const a = 'AA a b c ZZ';
const b = 'AA a b c ZZ';
const result = Diff3.diff3Merge(a, o, b, { excludeFalseConflicts: false });

t.same(result[0].ok, ['AA']);
t.same(result[0].conflict, undefined);
assert.deepEqual(result[0].ok, ['AA']);
assert.deepEqual(result[0].conflict, undefined);

t.same(result[1].ok, undefined);
t.same(result[1].conflict.o, []);
t.same(result[1].conflict.a, ['a', 'b', 'c']);
t.same(result[1].conflict.b, ['a', 'b', 'c']);
assert.deepEqual(result[1].ok, undefined);
assert.deepEqual(result[1].conflict.o, []);
assert.deepEqual(result[1].conflict.a, ['a', 'b', 'c']);
assert.deepEqual(result[1].conflict.b, ['a', 'b', 'c']);

t.same(result[2].ok, ['ZZ']);
t.same(result[2].conflict, undefined);
t.end();
assert.deepEqual(result[2].ok, ['ZZ']);
assert.deepEqual(result[2].conflict, undefined);
});


t.test('avoids improper hunk sorting - see openstreetmap/iD#3058', t => {
await t.test('avoids improper hunk sorting - see openstreetmap/iD#3058', t => {
const o = ['n4100522632', 'n4100697091', 'n4100697136', 'n4102671583', 'n4102671584', 'n4102671585', 'n4102671586', 'n4102671587', 'n4102671588', 'n4102677889', 'n4102677890', 'n4094374176'];
const a = ['n4100522632', 'n4100697091', 'n4100697136', 'n-10000', 'n4102671583', 'n4102671584', 'n4102671585', 'n4102671586', 'n4102671587', 'n4102671588', 'n4102677889', 'n4102677890', 'n4094374176'];
const b = ['n4100522632', 'n4100697091', 'n4100697136', 'n4102671583', 'n4102671584', 'n4102671585', 'n4102671586', 'n4102671587', 'n4102671588', 'n4102677889', 'n4105613618', 'n4102677890', 'n4105613617', 'n4094374176'];
const expected = ['n4100522632', 'n4100697091', 'n4100697136', 'n-10000', 'n4102671583', 'n4102671584', 'n4102671585', 'n4102671586', 'n4102671587', 'n4102671588', 'n4102677889', 'n4105613618', 'n4102677890', 'n4105613617', 'n4094374176'];
const result = Diff3.diff3Merge(a, o, b);

t.same(result[0].ok, expected);
t.end();
assert.deepEqual(result[0].ok, expected);
});


t.test('yaml comparison - issue #46', t => {
await t.test('yaml comparison - issue #46', t => {
const o = `title: "title"
description: "description"`;
const a = `title: "title"
Expand All @@ -155,12 +147,9 @@ description: "description changed"`;
description: "description"`;
const result = Diff3.diff3Merge(a, o, b, { stringSeparator: /[\r\n]+/ });

t.same(result[0].ok, undefined);
t.same(result[0].conflict.o, ['title: "title"', 'description: "description"']);
t.same(result[0].conflict.a, ['title: "title"', 'description: "description changed"']);
t.same(result[0].conflict.b, ['title: "title changed"', 'description: "description"']);
t.end();
assert.deepEqual(result[0].ok, undefined);
assert.deepEqual(result[0].conflict.o, ['title: "title"', 'description: "description"']);
assert.deepEqual(result[0].conflict.a, ['title: "title"', 'description: "description changed"']);
assert.deepEqual(result[0].conflict.b, ['title: "title changed"', 'description: "description"']);
});

t.end();
});
100 changes: 49 additions & 51 deletions test/diff3MergeRegions.test.js
Original file line number Diff line number Diff line change
@@ -1,68 +1,66 @@
import { test } from 'tap';
import { test } from 'node:test';
import { strict as assert } from 'node:assert';
import * as Diff3 from '../index.mjs';

test('diff3MergeRegions', t => {
test('diff3MergeRegions', async t => {

t.test('returns results of 3-way diff from o,a,b arrays', t => {
await t.test('returns results of 3-way diff from o,a,b arrays', t => {
const o = ['AA', 'ZZ', '00', 'M', '99'];
const a = ['AA', 'a', 'b', 'c', 'ZZ', 'new', '00', 'a', 'a', 'M', '99'];
const b = ['AA', 'a', 'd', 'c', 'ZZ', '11', 'M', 'z', 'z', '99'];
const result = Diff3.diff3MergeRegions(a, o, b);

t.same(result[0].stable, true);
t.same(result[0].buffer, 'o');
t.same(result[0].bufferStart, 0);
t.same(result[0].bufferLength, 1);
t.same(result[0].bufferContent, ['AA']);
assert.deepEqual(result[0].stable, true);
assert.deepEqual(result[0].buffer, 'o');
assert.deepEqual(result[0].bufferStart, 0);
assert.deepEqual(result[0].bufferLength, 1);
assert.deepEqual(result[0].bufferContent, ['AA']);

t.same(result[1].stable, false);
t.same(result[1].aStart, 1);
t.same(result[1].aLength, 3);
t.same(result[1].aContent, ['a', 'b', 'c']);
t.same(result[1].oStart, 1);
t.same(result[1].oLength, 0);
t.same(result[1].oContent, []);
t.same(result[1].bStart, 1);
t.same(result[1].bLength, 3);
t.same(result[1].bContent, ['a', 'd', 'c']);
assert.deepEqual(result[1].stable, false);
assert.deepEqual(result[1].aStart, 1);
assert.deepEqual(result[1].aLength, 3);
assert.deepEqual(result[1].aContent, ['a', 'b', 'c']);
assert.deepEqual(result[1].oStart, 1);
assert.deepEqual(result[1].oLength, 0);
assert.deepEqual(result[1].oContent, []);
assert.deepEqual(result[1].bStart, 1);
assert.deepEqual(result[1].bLength, 3);
assert.deepEqual(result[1].bContent, ['a', 'd', 'c']);

t.same(result[2].stable, true);
t.same(result[2].buffer, 'o');
t.same(result[2].bufferStart, 1);
t.same(result[2].bufferLength, 1);
t.same(result[2].bufferContent, ['ZZ']);
assert.deepEqual(result[2].stable, true);
assert.deepEqual(result[2].buffer, 'o');
assert.deepEqual(result[2].bufferStart, 1);
assert.deepEqual(result[2].bufferLength, 1);
assert.deepEqual(result[2].bufferContent, ['ZZ']);

t.same(result[3].stable, false);
t.same(result[3].aStart, 5);
t.same(result[3].aLength, 4);
t.same(result[3].aContent, ['new', '00', 'a', 'a']);
t.same(result[3].oStart, 2);
t.same(result[3].oLength, 1);
t.same(result[3].oContent, ['00']);
t.same(result[3].bStart, 5);
t.same(result[3].bLength, 1);
t.same(result[3].bContent, ['11']);
assert.deepEqual(result[3].stable, false);
assert.deepEqual(result[3].aStart, 5);
assert.deepEqual(result[3].aLength, 4);
assert.deepEqual(result[3].aContent, ['new', '00', 'a', 'a']);
assert.deepEqual(result[3].oStart, 2);
assert.deepEqual(result[3].oLength, 1);
assert.deepEqual(result[3].oContent, ['00']);
assert.deepEqual(result[3].bStart, 5);
assert.deepEqual(result[3].bLength, 1);
assert.deepEqual(result[3].bContent, ['11']);

t.same(result[4].stable, true);
t.same(result[4].buffer, 'o');
t.same(result[4].bufferStart, 3);
t.same(result[4].bufferLength, 1);
t.same(result[4].bufferContent, ['M']);
assert.deepEqual(result[4].stable, true);
assert.deepEqual(result[4].buffer, 'o');
assert.deepEqual(result[4].bufferStart, 3);
assert.deepEqual(result[4].bufferLength, 1);
assert.deepEqual(result[4].bufferContent, ['M']);

t.same(result[5].stable, true);
t.same(result[5].buffer, 'b');
t.same(result[5].bufferStart, 7);
t.same(result[5].bufferLength, 2);
t.same(result[5].bufferContent, ['z', 'z']);
assert.deepEqual(result[5].stable, true);
assert.deepEqual(result[5].buffer, 'b');
assert.deepEqual(result[5].bufferStart, 7);
assert.deepEqual(result[5].bufferLength, 2);
assert.deepEqual(result[5].bufferContent, ['z', 'z']);

t.same(result[6].stable, true);
t.same(result[6].buffer, 'o');
t.same(result[6].bufferStart, 4);
t.same(result[6].bufferLength, 1);
t.same(result[6].bufferContent, ['99']);

t.end();
assert.deepEqual(result[6].stable, true);
assert.deepEqual(result[6].buffer, 'o');
assert.deepEqual(result[6].bufferStart, 4);
assert.deepEqual(result[6].bufferLength, 1);
assert.deepEqual(result[6].bufferContent, ['99']);
});

t.end();
});
Loading

0 comments on commit 618c8bc

Please sign in to comment.