Permalink
Newer
Older
100644 178 lines (114 sloc) 10.3 KB
Nov 23, 2012
1
fs = require 'fs'
2
Path = require 'path'
3
assert = require 'assert'
4
Nov 23, 2012
5
{ diff, diffString } = require "../#{process.env.JSLIB or 'lib'}/index"
6
7
describe 'diff', ->
8
9
describe 'with simple scalar values', ->
11
it "should return undefined for two identical numbers", ->
12
assert.deepEqual undefined, diff(42, 42)
14
it "should return undefined for two identical strings", ->
15
assert.deepEqual undefined, diff("foo", "foo")
17
it "should return { __old: <old value>, __new: <new value> } object for two different numbers", ->
18
assert.deepEqual { __old: 42, __new: 10 }, diff(42, 10)
20
describe 'with objects', ->
22
it "should return undefined for two empty objects", ->
23
assert.deepEqual undefined, diff({ }, { })
24
25
it "should return undefined for two objects with identical contents", ->
26
assert.deepEqual undefined, diff({ foo: 42, bar: 10 }, { foo: 42, bar: 10 })
28
it "should return undefined for two object hierarchies with identical contents", ->
29
assert.deepEqual undefined, diff({ foo: 42, bar: { bbbar: 10, bbboz: 11 } }, { foo: 42, bar: { bbbar: 10, bbboz: 11 } })
31
it "should return { <key>__deleted: <old value> } when the second object is missing a key", ->
32
assert.deepEqual { foo__deleted: 42 }, diff({ foo: 42, bar: 10 }, { bar: 10 })
34
it "should return { <key>__added: <new value> } when the first object is missing a key", ->
35
assert.deepEqual { foo__added: 42 }, diff({ bar: 10 }, { foo: 42, bar: 10 })
36
37
it "should return { <key>: { __old: <old value>, __new: <new value> } } for two objects with diffent scalar values for a key", ->
38
assert.deepEqual { foo: { __old: 42, __new: 10 } }, diff({ foo: 42 }, { foo: 10 })
39
40
it "should return { <key>: <diff> } with a recursive diff for two objects with diffent values for a key", ->
41
assert.deepEqual { bar: { bbboz__deleted: 11, bbbar: { __old: 10, __new: 12 } } }, diff({ foo: 42, bar: { bbbar: 10, bbboz: 11 }}, { foo: 42, bar: { bbbar: 12 }})
42
43
describe 'with arrays of scalars', ->
44
45
it "should return undefined for two arrays with identical contents", ->
46
assert.deepEqual undefined, diff([10, 20, 30], [10, 20, 30])
47
48
it "should return [..., ['-', <removed item>], ...] for two arrays when the second array is missing a value", ->
49
assert.deepEqual [[' ', 10], ['-', 20], [' ', 30]], diff([10, 20, 30], [10, 30])
50
51
it "should return [..., ['+', <added item>], ...] for two arrays when the second one has an extra value", ->
52
assert.deepEqual [[' ', 10], ['+', 20], [' ', 30]], diff([10, 30], [10, 20, 30])
53
54
it "should return [..., ['+', <added item>]] for two arrays when the second one has an extra value at the end (edge case test)", ->
55
assert.deepEqual [[' ', 10], [' ', 20], ['+', 30]], diff([10, 20], [10, 20, 30])
56
57
describe 'with arrays of objects', ->
58
59
it "should return undefined for two arrays with identical contents", ->
60
assert.deepEqual undefined, diff([{ foo: 10 }, { foo: 20 }, { foo: 30 }], [{ foo: 10 }, { foo: 20 }, { foo: 30 }])
61
62
it "should return undefined for two arrays with identical, empty object contents", ->
63
assert.deepEqual undefined, diff([{ }], [{ }])
64
65
it "should return undefined for two arrays with identical, empty array contents", ->
66
assert.deepEqual undefined, diff([[]], [[]])
67
68
it "should return undefined for two arrays with identical, repeated contents", ->
69
assert.deepEqual undefined, diff([{ a: 1, b: 2 }, { a: 1, b: 2 }], [{ a: 1, b: 2 }, { a: 1, b: 2 }])
70
71
it "should return [..., ['-', <removed item>], ...] for two arrays when the second array is missing a value", ->
72
assert.deepEqual [[' '], ['-', { foo: 20 }], [' ']], diff([{ foo: 10 }, { foo: 20 }, { foo: 30 }], [{ foo: 10 }, { foo: 30 }])
73
74
it "should return [..., ['+', <added item>], ...] for two arrays when the second array has an extra value", ->
75
assert.deepEqual [[' '], ['+', { foo: 20 }], [' ']], diff([{ foo: 10 }, { foo: 30 }], [{ foo: 10 }, { foo: 20 }, { foo: 30 }])
77
it "should return [..., ['+', <added item>], ...] for two arrays when the second array has a new but nearly identical object added", ->
78
assert.deepEqual [[' '],[ '+', { name: 'Foo', a: 3, b: 1, c: 1 }], [' ']], diff([{ "name": "Foo", "a": 3, "b": 1 },{ foo: 10 }], [{ "name": "Foo", "a": 3, "b": 1 },{ "name": "Foo", "a": 3, "b": 1, "c": 1 },{ foo: 10 }])
79
80
it "should return [..., ['~', <diff>], ...] for two arrays when an item has been modified (note: involves a crazy heuristic)", ->
81
assert.deepEqual [[' '], ['~', { foo: { __old: 20, __new: 21 } }], [' ']], diff([{ foo: 10, bar: { bbbar: 10, bbboz: 11 } }, { foo: 20, bar: { bbbar: 50, bbboz: 25 } }, { foo: 30, bar: { bbbar: 92, bbboz: 34 } }], [{ foo: 10, bar: { bbbar: 10, bbboz: 11 } }, { foo: 21, bar: { bbbar: 50, bbboz: 25 } }, { foo: 30, bar: { bbbar: 92, bbboz: 34 } }])
Nov 23, 2012
82
83
84
describe 'diff({keysOnly: true})', ->
85
86
describe 'with simple scalar values', ->
87
88
it "should return undefined for two identical numbers", ->
89
assert.deepEqual undefined, diff(42, 42, {keysOnly: true})
90
91
it "should return undefined for two identical strings", ->
92
assert.deepEqual undefined, diff("foo", "foo", {keysOnly: true})
93
94
it "should return undefined object for two different numbers", ->
95
assert.deepEqual undefined, diff(42, 10, {keysOnly: true})
96
97
describe 'with objects', ->
98
99
it "should return undefined for two empty objects", ->
100
assert.deepEqual undefined, diff({ }, { }, {keysOnly: true})
101
102
it "should return undefined for two objects with identical contents", ->
103
assert.deepEqual undefined, diff({ foo: 42, bar: 10 }, { foo: 42, bar: 10 }, {keysOnly: true})
104
105
it "should return undefined for two object hierarchies with identical contents", ->
106
assert.deepEqual undefined, diff({ foo: 42, bar: { bbbar: 10, bbboz: 11 } }, { foo: 42, bar: { bbbar: 10, bbboz: 11 } }, {keysOnly: true})
107
108
it "should return { <key>__deleted: <old value> } when the second object is missing a key", ->
109
assert.deepEqual { foo__deleted: 42 }, diff({ foo: 42, bar: 10 }, { bar: 10 }, {keysOnly: true})
110
111
it "should return { <key>__added: <new value> } when the first object is missing a key", ->
112
assert.deepEqual { foo__added: 42 }, diff({ bar: 10 }, { foo: 42, bar: 10 }, {keysOnly: true})
113
114
it "should return undefined for two objects with diffent scalar values for a key", ->
115
assert.deepEqual undefined, diff({ foo: 42 }, { foo: 10 }, {keysOnly: true})
116
117
it "should return undefined with a recursive diff for two objects with diffent values for a key", ->
118
assert.deepEqual undefined, diff({ foo: 42, bar: { bbbar: 10 }}, { foo: 42, bar: { bbbar: 12 }}, {keysOnly: true})
119
120
it "should return { <key>: <diff> } with a recursive diff when second object is missing a key and two objects with diffent values for a key", ->
121
assert.deepEqual { bar: { bbboz__deleted: 11 } }, diff({ foo: 42, bar: { bbbar: 10, bbboz: 11 }}, { foo: 42, bar: { bbbar: 12 }}, {keysOnly: true})
122
123
describe 'with arrays of scalars', ->
124
125
it "should return undefined for two arrays with identical contents", ->
126
assert.deepEqual undefined, diff([10, 20, 30], [10, 20, 30], {keysOnly: true})
127
128
it "should return undefined for two arrays with when an item has been modified", ->
129
assert.deepEqual undefined, diff([10, 20, 30], [10, 42, 30], {keysOnly: true})
130
131
it "should return [..., ['-', <removed item>], ...] for two arrays when the second array is missing a value", ->
132
assert.deepEqual [[' ', 10], ['-', 20], [' ', 30]], diff([10, 20, 30], [10, 30], {keysOnly: true})
133
134
it "should return [..., ['+', <added item>], ...] for two arrays when the second one has an extra value", ->
135
assert.deepEqual [[' ', 10], ['+', 20], [' ', 30]], diff([10, 30], [10, 20, 30], {keysOnly: true})
136
137
it "should return [..., ['+', <added item>]] for two arrays when the second one has an extra value at the end (edge case test)", ->
138
assert.deepEqual [[' ', 10], [' ', 20], ['+', 30]], diff([10, 20], [10, 20, 30], {keysOnly: true})
139
140
describe 'with arrays of objects', ->
141
142
it "should return undefined for two arrays with identical contents", ->
143
assert.deepEqual undefined, diff([{ foo: 10 }, { foo: 20 }, { foo: 30 }], [{ foo: 10 }, { foo: 20 }, { foo: 30 }], {keysOnly: true})
144
145
it "should return undefined for two arrays with identical, empty object contents", ->
146
assert.deepEqual undefined, diff([{ }], [{ }], {keysOnly: true})
147
148
it "should return undefined for two arrays with identical, empty array contents", ->
149
assert.deepEqual undefined, diff([[]], [[]], {keysOnly: true})
150
151
it "should return undefined for two arrays with identical, repeated contents", ->
152
assert.deepEqual undefined, diff([{ a: 1, b: 2 }, { a: 1, b: 2 }], [{ a: 1, b: 2 }, { a: 1, b: 2 }], {keysOnly: true})
153
154
it "should return [..., ['-', <removed item>], ...] for two arrays when the second array is missing a value", ->
155
assert.deepEqual [[' '], ['-', { foo: 20 }], [' ']], diff([{ foo: 10 }, { foo: 20 }, { foo: 30 }], [{ foo: 10 }, { foo: 30 }], {keysOnly: true})
156
157
it "should return [..., ['+', <added item>], ...] for two arrays when the second array has an extra value", ->
158
assert.deepEqual [[' '], ['+', { foo: 20 }], [' ']], diff([{ foo: 10 }, { foo: 30 }], [{ foo: 10 }, { foo: 20 }, { foo: 30 }], {keysOnly: true})
159
160
it "should return [..., ['~', <diff>], ...] for two arrays when an item has been modified (note: involves a crazy heuristic)", ->
161
assert.deepEqual undefined, diff([{ foo: 10, bar: { bbbar: 10, bbboz: 11 } }, { foo: 20, bar: { bbbar: 50, bbboz: 25 } }, { foo: 30, bar: { bbbar: 92, bbboz: 34 } }], [{ foo: 10, bar: { bbbar: 10, bbboz: 11 } }, { foo: 21, bar: { bbbar: 50, bbboz: 25 } }, { foo: 30, bar: { bbbar: 92, bbboz: 34 } }], {keysOnly: true})
162
163
Nov 23, 2012
164
describe 'diffString', ->
165
166
readExampleFile = (file) -> fs.readFileSync(Path.join(__dirname, '../example', file), 'utf8')
167
a = JSON.parse(readExampleFile('a.json'))
168
b = JSON.parse(readExampleFile('b.json'))
169
170
it "should produce the expected result for the example JSON files", ->
171
assert.equal diffString(a, b, color: no), readExampleFile('result.jsdiff')
172
173
it "should produce the expected colored result for the example JSON files", ->
174
assert.equal diffString(a, b), readExampleFile('result-colored.jsdiff')
175
176
it "return an empty string when no diff found", ->
177
assert.equal diffString(a, a), ''