forked from EFForg/https-everywhere
-
Notifications
You must be signed in to change notification settings - Fork 4
/
parse-test.js
169 lines (164 loc) · 6.19 KB
/
parse-test.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright 2017 AJ Jordan, AGPLv3+
'use strict';
const vows = require('perjury'),
assert = vows.assert,
_ = require('lodash');
function assertCorrectBody(text) {
return {
topic: function(parse) {
return parse(text);
},
'it returns an object': function(err, obj) {
assert.ifError(err);
assert.isObject(obj);
},
'the object has the right data': function(err, obj) {
assert.includes(obj, 'domain');
assert.equal(obj.domain, 'example.com');
}
};
}
function assertTypeSucceeds(type) {
return {
topic: function(parse) {
return parse('Type: ' + type);
},
'it returns an object': function(err, obj) {
assert.ifError(err);
assert.isObject(obj);
},
'the object is normal data': function(err, obj) {
assert.equal(obj.type, type);
}
};
}
vows.describe('issue parser module').addBatch({
'When we require the module': {
topic: function() {
return require('../lib/parse');
},
'it works': function(err) {
assert.ifError(err);
},
'it\'s a function': function(err, parse) {
assert.isFunction(parse);
},
'and we pass it a body that\'s a ruleset issue': assertCorrectBody('Type: ruleset issue\nDomain: example.com'),
'and we pass it a body with funky capitalization': {
topic: function(parse) {
return parse('tyPe: ruleset issue\nDoMaIn: example.com');
},
'it returns an object': function(err, obj) {
assert.ifError(err);
assert.isObject(obj);
},
'the capitalization didn\'t matter': function(err, obj) {
assert.includes(obj, 'domain');
assert.equal(obj.domain, 'example.com');
}
},
'and we pass it a body with out-of-order data': assertCorrectBody('Domain: example.com\nType: ruleset issue'),
'and we pass it a body with lots of whitespace': {
topic: function(parse) {
return parse('Type: ruleset issue \nDomain: \t\t example.com \t');
},
'it returns an object': function(err, obj) {
assert.ifError(err);
assert.isObject(obj);
},
'the whitespace was trimmed': function(err, obj) {
assert.includes(obj, 'domain');
assert.equal(obj.domain, 'example.com');
}
},
'and we pass it a body with many newlines': {
topic: function(parse) {
return parse('\n\nType: ruleset issue\n\n\n\nDomain: example.com\n\n');
},
'it returns an object': function(err, obj) {
assert.ifError(err);
assert.isObject(obj);
},
'the newlines were ignored': function(err, obj) {
assert.equal(_.size(obj), 2);
}
},
'and we pass it a body with some Markdown comments': {
topic: function(parse) {
// TODO this should test for comments on the same line too, but see wooorm/strip-markdown#14
return parse('<!-- comment -->\nType: ruleset issue\n<!-- comment after newline -->\nDomain: example.com');
},
'it returns an object': function(err, obj) {
assert.ifError(err);
assert.isObject(obj);
},
'the object has the right data': function(err, obj) {
assert.includes(obj, 'domain');
assert.equal(obj.domain, 'example.com');
},
'the object doesn\'t have a bunch of extra keys': function(err, obj) {
assert.equal(_.size(obj), 2);
}
},
'and we pass it a body that has a full URL in the domain': {
topic: function(parse) {
return parse('Type: ruleset issue\nDomain: http://example.com/index.html');
},
'it returns an object': function(err, obj) {
assert.ifError(err);
assert.isObject(obj);
},
'the URL was converted into a bare domain': function(err, obj) {
assert.includes(obj, 'domain');
assert.equal(obj.domain, 'example.com');
}
},
'and we pass it a body that has a freeform comment': assertCorrectBody('Type: new ruleset\nDomain: example.com\nAnd let me say, what a great GitHub bot HTTPS Everywhere has!'),
'and we pass it a body that has a freeform comment with a Markdown link': assertCorrectBody('Type: new ruleset\nDomain: example.com\nPlease add [example.com][].\n\n [example.com]: http://example.com.'),
'and we pass it a body that has a freeform comment with colons': assertCorrectBody('Type: new ruleset\nDomain: example.com\nHere\'s a secret: I like colons.'),
'and we pass it a body that has a freeform comment with multiple colons': assertCorrectBody('Type: new ruleset\nDomain: example.com\nHere\'s a secret: I like colons. Another secret: I like them a lot.'),
'and we pass it a code issue without any other data': assertTypeSucceeds('code issue'),
'and we pass it a feature request without any other data': assertTypeSucceeds('feature request'),
'and we pass it an issue labeled "other" without any other data': assertTypeSucceeds('other'),
'and we pass it a body that isn\'t a valid issue type': {
topic: function(parse) {
return parse('Type: something else\nDomain: example.com');
},
'it returns the right error': function(err, obj) {
assert.ifError(err);
assert.instanceOf(obj, Error);
assert.equal(obj.message, 'invalid type');
}
},
'and we pass it a body with more than one type': {
topic: function(parse) {
return parse('Type: new ruleset\nType: ruleset issue');
},
'it returns the right error': function(err, obj) {
assert.ifError(err);
assert.instanceOf(obj, Error);
assert.equal(obj.message, 'multiple types');
}
},
'and we pass it a body with no type': {
topic: function(parse) {
return parse('I tried turning it off and on again and it still doesn\'t work');
},
'it returns the right error': function(err, obj) {
assert.ifError(err);
assert.instanceOf(obj, Error);
assert.equal(obj.message, 'no type');
}
},
'and we pass it a null body': {
topic: function(parse) {
return parse('');
},
'it returns the right error': function(err, obj) {
assert.ifError(err);
assert.instanceOf(obj, Error);
assert.equal(obj.message, 'null description');
}
}
}
}).export(module);