-
Notifications
You must be signed in to change notification settings - Fork 35
/
init.js
170 lines (147 loc) · 4.9 KB
/
init.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
170
var Checker = require('jscs/lib/checker');
var parse = require('comment-parser');
var chai = require('chai');
var expect = chai.expect;
chai.use(require('chai-subset'));
global.parse = parse;
global.fnBody = fnBody;
global.checker = rulesChecker;
global.expect = expect;
/**
* Parses body of function
*
* @param {Function} func
* @returns {string} body of func
*/
function fnBody(func) {
var str = func.toString();
var out = str.slice(
str.indexOf('{') + 1,
str.lastIndexOf('}')
);
// strip trailing spaces and tabs
out = out.replace(/^\n*|[ \t]*$/g, '');
// strip preceding indentation
var blockIndent = 0;
out.match(/^([ \t]*)/gm).map(function(v) {
if (!blockIndent || (v.length > 0 && v.length < blockIndent)) {
blockIndent = v.length;
}
});
// rebuild block without inner indent
out = !blockIndent ? out : out.split('\n').map(function(v) {
return v.substr(blockIndent);
}).join('\n');
return out;
}
/**
* @typedef {{it: string, code: Function, rules: ?Object, errors: ?*, skip: ?boolean}} TestCase
*/
/**
* Testing helper object
*
* @param {Object} opts default options for jscs string checker
* @returns {{rules: function(Object), configure: function(Object),
* cases: function(TestCase[]), check: function(string)}}
*/
function rulesChecker(opts) {
var checker;
beforeEach(function() {
checker = new Checker();
checker.registerDefaultRules();
if (opts) {
checker.configure(opts);
}
});
return {
/**
* Set rules for each case (beforeEach wrapper)
*
* @param {Object} rules
*/
rules: function(rules, opts) {
beforeEach(function() {
opts = opts || {};
opts.jsDoc = rules;
checker.configure(opts);
});
},
/**
* Set rules immediately
*
* @param {Object} rules
*/
configure: function(rules) {
checker.configure({jsDoc: rules});
},
/**
* Describe cases (wrapper for mocha it calls)
*
* @param {TestCase[]} items
*/
cases: function(items) {
items = items || [];
items.forEach(function(test) {
(test.only ? it.only : (test.skip ? it.skip : it))(test.it, function() {
if (test.rules) {
checker.configure({ jsDoc: test.rules });
}
var body = test.code.call ? fnBody(test.code) : test.code;
var checked = checker.checkString(body);
var errors = checked.getErrorList();
if (errors.length && errors[0].rule === 'parseError') {
throw new Error(errors[0].message);
}
if (!test.hasOwnProperty('errors') || (typeof test.errors === 'number')) {
expect(checked.getErrorCount())
.to.eq(test.errors || 0, test.errors ?
'Expect ' + test.errors + ' error(s)'
: 'Unexpected error(s)');
} else if (Array.isArray(test.errors)) {
expect(errors.length).to.equal(test.errors.length);
test.errors.forEach(function(err, index) {
for (var i in err) {
if (err.hasOwnProperty(i)) {
expect(errors[index][i]).to.equal(err[i], 'Invalid ' + i);
}
}
});
//expect(errors)
// .to.containSubset(test.errors);
} else {
expect(checked.getErrorCount())
.to.not.eq(0);
for (var i in test.errors) {
if (test.errors.hasOwnProperty(i)) {
expect(errors[0][i]).to.equal(test.errors[i], 'Invalid ' + i);
}
}
}
});
});
},
/**
* @param {string} str
* @returns {Object}
*/
check: function(str) {
return checker.checkString(str);
}
};
}
chai.use(function(chai, utils) {
utils.addMethod(chai.Assertion.prototype, 'similar', method);
/**
* @param {Object} expected
* @throws {Error}
*/
function method(expected) {
var obj = utils.flag(this, 'object');
Object.keys(obj).forEach(function(k) {
if (!expected.hasOwnProperty(k)) {
expected[k] = obj[k];
}
});
new chai.Assertion(obj).to.be.deep.equal(expected);
}
});