Skip to content

Commit b642ec7

Browse files
aautioMalte Legenhausen
authored andcommitted
Added tests for the cli arguments (html-to-text#153)
1 parent 4e6127d commit b642ec7

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

test/test-cli.js

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
var expect = require('chai').expect;
2+
var fs = require('fs');
3+
4+
var exec = require('child_process').exec;
5+
6+
function runWithInputAndExpect(input, args, expectedOutput, done) {
7+
exec('echo "' + input.replace(/"/g, '\\"') + '" | node bin/cli.js ' + args, function callback(error, stdout, stderr) {
8+
expect(error).to.be.a('null');
9+
expect(stderr).to.equal('');
10+
expect(stdout).to.equal(expectedOutput + '\n');
11+
done(error);
12+
});
13+
}
14+
15+
describe('cli arguments', function() {
16+
it('should output nothing with empty input', function(done) {
17+
runWithInputAndExpect('', '', '', done);
18+
});
19+
20+
it('should not ignore images by default', function (done) {
21+
runWithInputAndExpect(
22+
'Hello <img alt="alt text" src="http://my.img/here.jpg">!',
23+
'',
24+
'Hello alt text [http://my.img/here.jpg]!',
25+
done);
26+
});
27+
28+
it('should ignore images with --ignore-image=true', function (done) {
29+
runWithInputAndExpect(
30+
'Hello <img alt="alt text" src="http://my.img/here.jpg">!',
31+
'--ignore-image=true',
32+
'Hello !',
33+
done);
34+
});
35+
36+
it('should not ignore href by default', function (done) {
37+
runWithInputAndExpect(
38+
'<a href="http://my.link">test</a>',
39+
'',
40+
'test [http://my.link]',
41+
done);
42+
});
43+
44+
it('should ignore href with --ignore-href=true', function (done) {
45+
runWithInputAndExpect(
46+
'<a href="http://my.link">test</a>',
47+
'--ignore-href=true',
48+
'test',
49+
done);
50+
});
51+
52+
it('should wordwrap at 80 characters by default', function (done) {
53+
runWithInputAndExpect(
54+
' 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789',
55+
'',
56+
' 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789\n123456789',
57+
done);
58+
});
59+
60+
it('should wordwrap at 40 with --wordwrap=40', function (done) {
61+
runWithInputAndExpect(
62+
' 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789',
63+
'--wordwrap=40',
64+
' 123456789 123456789 123456789 123456789\n123456789 123456789 123456789 123456789\n123456789',
65+
done);
66+
});
67+
68+
it('should return link with brackets by default', function (done) {
69+
runWithInputAndExpect(
70+
'<a href="http://my.link">test</a>',
71+
'',
72+
'test [http://my.link]',
73+
done);
74+
});
75+
76+
it('should return link without brackets with --noLinkBrackets=true', function (done) {
77+
runWithInputAndExpect(
78+
'<a href="http://my.link">test</a>',
79+
'--noLinkBrackets=true',
80+
'test http://my.link',
81+
done);
82+
});
83+
84+
it('should support --tables definitions with commas', function(done) {
85+
var expectedTxt = fs.readFileSync('test/test.txt', 'utf8');
86+
87+
function runWithArgs(args, callback) {
88+
exec('cat test/test.html | node bin/cli.js ' + args, callback);
89+
}
90+
91+
runWithArgs('--tables=#invoice,.address', function callback(error, stdout, stderr) {
92+
expect(error).to.be.a('null');
93+
expect(stderr).to.equal('');
94+
expect(stdout).to.equal(expectedTxt + '\n');
95+
done(error);
96+
});
97+
});
98+
});

0 commit comments

Comments
 (0)