-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
Expand file tree
/
Copy pathtest-diff.js
More file actions
80 lines (65 loc) · 1.92 KB
/
test-diff.js
File metadata and controls
80 lines (65 loc) · 1.92 KB
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
'use strict';
require('../common');
const { describe, it } = require('node:test');
const assert = require('node:assert');
const { diff } = require('util');
describe('diff', () => {
it('throws because actual is nor an array nor a string', () => {
const actual = {};
const expected = 'foo';
assert.throws(() => diff(actual, expected), {
message: 'The "actual" argument must be of type string. Received an instance of Object'
});
});
it('throws because expected is nor an array nor a string', () => {
const actual = 'foo';
const expected = {};
assert.throws(() => diff(actual, expected), {
message: 'The "expected" argument must be of type string. Received an instance of Object'
});
});
it('throws because the actual array does not only contain string', () => {
const actual = ['1', { b: 2 }];
const expected = ['1', '2'];
assert.throws(() => diff(actual, expected), {
message: 'The "actual[1]" property must be of type string. Received an instance of Object'
});
});
it('returns an empty array because actual and expected are the same', () => {
const actual = 'foo';
const expected = 'foo';
const result = diff(actual, expected);
assert.deepStrictEqual(result, []);
});
it('returns the diff for strings', () => {
const actual = '12345678';
const expected = '12!!5!7!';
const result = diff(actual, expected);
assert.deepStrictEqual(result, [
[0, '1'],
[0, '2'],
[1, '3'],
[1, '4'],
[-1, '!'],
[-1, '!'],
[0, '5'],
[1, '6'],
[-1, '!'],
[0, '7'],
[1, '8'],
[-1, '!'],
]);
});
it('returns the diff for arrays', () => {
const actual = ['1', '2', '3'];
const expected = ['1', '3', '4'];
const result = diff(actual, expected);
assert.deepStrictEqual(result, [
[0, '1'],
[1, '2'],
[0, '3'],
[-1, '4'],
]
);
});
});