UNPKG

5.25 kBJavaScriptView Raw
1'use strict';
2
3var assert = require('assert');
4var disparity = require('../disparity');
5var cli = require('../disparity-cli');
6var fs = require('fs');
7var path = require('path');
8
9function readFile(name) {
10 var filePath = path.join(__dirname, name);
11 return fs.readFileSync(filePath).toString();
12}
13
14function compare(diff, expected, name) {
15 if (diff !== expected) {
16 // not using assert because it is easier to understand what is wrong
17 process.stderr.write('disparity.' + name + '() failure!\n');
18 process.stderr.write('=== expected result:\n');
19 process.stderr.write(expected);
20 process.stderr.write('=== actual result:\n');
21 process.stderr.write(diff);
22 throw new Error('assertion error');
23 }
24}
25
26// setup
27// =====
28
29var diff, expected;
30var file1 = readFile('file1.js');
31var file2 = readFile('file2.js');
32
33// chars
34// =====
35
36diff = disparity.chars(file1, file2);
37expected = readFile('chars.txt');
38compare(diff, expected, 'chars');
39
40// unified
41// =======
42
43diff = disparity.unified(file1, file2);
44expected = readFile('unified.txt');
45compare(diff, expected, 'unified');
46
47diff = disparity.unified(file1, file2, 'test/file1.js', 'test/file2.js');
48expected = readFile('unified_2.txt');
49compare(diff, expected, 'unified_2');
50
51// unifiedNoColor
52// ==============
53
54diff = disparity.unifiedNoColor(file1, file2, 'test/file1.js', 'test/file2.js');
55expected = readFile('unified_no_color.txt');
56compare(diff, expected, 'unified_no_color');
57
58// custom colors
59// =============
60
61var _oldColors = disparity.colors;
62// wrap blocks into custom tags
63disparity.colors = {
64 // chars diff
65 charsRemoved: { open: '<bggreen>', close: '</bggreen>' },
66 charsAdded: { open: '<bgred>', close: '</bgred>' },
67
68 // unified diff
69 removed: { open: '<red>', close: '</red>' },
70 added: { open: '<green>', close: '</green>' },
71 header: { open: '<yellow>', close: '</yellow>' },
72 section: { open: '<magenta>', close: '</magenta>' }
73};
74
75diff = disparity.chars(file1, file2);
76expected = readFile('chars.html');
77compare(diff, expected, 'chars.html');
78
79diff = disparity.unified(file1, file2, 'test/file1.js', 'test/file2.js');
80expected = readFile('unified.html');
81compare(diff, expected, 'unified.html');
82
83disparity.colors = _oldColors;
84
85// cli.parse
86// =========
87
88var args = cli.parse([]);
89assert.ok(args.help, 'help');
90assert.equal(args.errors.length, 0, 'error 1');
91
92args = cli.parse(['--help']);
93assert.ok(args.help, 'help 2');
94
95args = cli.parse(['-h']);
96assert.ok(args.help, 'help 3');
97
98args = cli.parse(['-v']);
99assert.ok(args.version, 'version');
100assert.equal(args.errors.length, 0, 'error 2');
101
102args = cli.parse(['-u']);
103assert.equal(args.errors[0], 'Error: missing or invalid <file_1> and/or <file_2> path.', 'error 3');
104
105args = cli.parse(['-u', 'foo.js', '--bar']);
106assert.ok(args.unified, '-u');
107assert.equal(args.filePath1, 'foo.js');
108// --bar should cause an error since it's invalid
109assert.equal(args.filePath2, '--bar');
110assert.equal(args.errors[0], 'Error: missing or invalid <file_1> and/or <file_2> path.', 'error 4');
111
112args = cli.parse(['--unified', 'foo.js', 'bar.js']);
113assert.ok(args.unified, '--unified');
114assert.equal(args.filePath1, 'foo.js');
115assert.equal(args.filePath2, 'bar.js');
116assert.equal(args.errors.length, 0, 'error 5');
117
118args = cli.parse(['-c', 'foo.js', 'bar.js']);
119assert.ok(!args.unified, '!--unified');
120assert.ok(args.chars, '-c');
121
122args = cli.parse(['--chars', 'foo.js', 'bar.js']);
123assert.ok(!args.unified, '!--unified');
124assert.ok(args.chars, '--chars');
125
126args = cli.parse(['--no-color', 'foo.js', 'bar.js']);
127assert.ok(args.errors.length, '--no-color errors');
128assert.ok(args.noColor, '--no-color');
129
130args = cli.parse(['--unified', '--no-color', 'foo.js', 'bar.js']);
131assert.ok(!args.errors.length, '--unified --no-color errors');
132assert.ok(args.noColor, '--no-color');
133
134// cli.run
135// =======
136
137function FakeStream(){
138 this.data = '';
139 this.write = function(data) {
140 this.data += data;
141 };
142}
143
144var code;
145var out = new FakeStream();
146var err = new FakeStream();
147
148function run(args) {
149 code = null;
150 out.data = '';
151 err.data = '';
152 return cli.run(args, out, err);
153}
154
155code = run({ help: true });
156assert.ok(!code, 'exit code');
157assert.ok(out.data.length > 100, 'output help');
158
159code = run({ version: true });
160assert.ok(!code, 'exit code');
161assert.equal(out.data, 'disparity v' + require('../package.json').version +'\n', 'version');
162
163code = run({ errors: ['Error: foo bar'] });
164assert.ok(code, 'exit code error');
165assert.equal(err.data, 'Error: foo bar\n\n');
166assert.ok(out.data.search('Options:'));
167
168code = run({
169 chars: true,
170 filePath1: 'test/file1.js',
171 filePath2: 'test/file2.js'
172});
173expected = readFile('chars.txt');
174assert.ok(!code, 'exit code chars');
175assert.equal(out.data, expected);
176assert.equal(err.data, '');
177
178code = run({
179 unified: true,
180 filePath1: 'test/file1.js',
181 filePath2: 'test/file2.js'
182});
183expected = readFile('unified_2.txt');
184assert.ok(!code, 'exit code chars');
185assert.equal(out.data, expected);
186assert.equal(err.data, '');
187
188code = run({
189 unified: true,
190 noColor: true,
191 filePath1: 'test/file1.js',
192 filePath2: 'test/file2.js'
193});
194expected = readFile('unified_no_color.txt');
195assert.ok(!code, 'exit code chars');
196assert.equal(out.data, expected);
197assert.equal(err.data, '');