-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
82 lines (65 loc) · 1.91 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
const test = require('ava');
const TextFileDiff = require('../dist/index.js').default;
test('test with header', async t => {
const m = new TextFileDiff();
const expected = '+Additional\n+Another\n+Lines\n-Some\n-Simple\n+With\n';
let actual = '';
m.on('-', line => {
actual += '-' + line + '\n';
});
m.on('+', line => {
actual += '+' + line + '\n';
});
await m.diff('tests/file1.txt', 'tests/file2.txt');
t.is(actual, expected);
});
test('test skip header', async t => {
const expected = '+Another\n+File\n+Lines\n-Some\n-Simple\n+With\n';
const m = new TextFileDiff({
skipHeader: true
});
let actual = '';
m.on('-', line => {
actual += '-' + line + '\n';
});
m.on('+', line => {
actual += '+' + line + '\n';
});
await m.diff('tests/file1.txt', 'tests/file2.txt');
t.is(actual, expected);
});
test('test await listener', async t => {
const m = new TextFileDiff();
const expected = 'compared+compared+comparedcompared+compared-compared-comparedcompared+';
let actual = '';
const getListenerFor = event => async () => new Promise(resolve => {
setTimeout(() => {
actual += event;
resolve();
}, 200);
});
m.on('-', getListenerFor('-'));
m.on('+', getListenerFor('+'));
m.on('compared', getListenerFor('compared'));
await m.diff('tests/file1.txt', 'tests/file2.txt');
t.is(actual, expected);
});
test('test against null or empty file', async t => {
let expected = '-some,csv,data\n';
const m = new TextFileDiff({
skipHeader: false
});
let actual = '';
m.on('-', line => {
actual += '-' + line + '\n';
});
m.on('+', line => {
actual += '+' + line + '\n';
});
await m.diff('tests/lowercase.txt', 'tests/empty.txt');
t.is(actual, expected, 'faile test empty');
actual = '';
expected = '+null\n-some,csv,data\n';
await m.diff('tests/lowercase.txt', 'tests/null.txt');
t.is(actual, expected, 'fail test null');
});