This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·227 lines (178 loc) · 5.38 KB
/
index.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/env node
var program = require('commander')
, fs = require('fs')
, path = require('path')
, util = require('util')
, extend = require('extend')
, Handlebars = require('handlebars');
program
.version('0.0.0')
.usage('[options] <file ...>')
.option('-o, --output [file]', 'write JSON output to a file')
.parse(process.argv);
var tests = [] // Array containg the actual specs
, indices = [] // Temp array for auto-incrementing test indices
, context = {}; // Current test context
tests.add = function (spec) {
if (!spec || !spec.template) return;
var key = (spec.description + '-' + spec.it).toLowerCase();
for (var i = 0; i < 20; i++) {
var name = key + '-' + ('0' + i).slice(-2);
if (indices.indexOf(name) === -1) {
if (program.output) {
var output = path.resolve(program.output)
, patchName = path.basename(output)
, patchFile = path.dirname(output) + '/../patch/' + patchName;
if (fs.existsSync(path.resolve(patchFile))) {
var patch = require(patchFile);
if (patch.hasOwnProperty(name)) {
spec = extend(true, spec, patch[name]);
}
}
}
tests.push(spec);
indices.push(name);
break;
}
}
};
var isFunction = function (object) {
return !!(object && object.constructor && object.call && object.apply);
};
var isEmptyObject = function (object) {
return !Object.keys(object).length;
};
var extractHelpers = function (data) {
var helpers = {};
if (!data || typeof data !== 'object') {
return false;
}
Object.keys(data).forEach(function (el) {
if (isFunction(data[el])) {
helpers[el] = { javascript: '' + data[el] };
}
});
return isEmptyObject(helpers) ? false : helpers;
};
global.Handlebars = Handlebars;
global.handlebarsEnv = Handlebars.create();
global.beforeEach = function (next) {
next();
};
global.CompilerContext = {
compile: function (template, options) {
// Push template unto context
context.template = template;
return function (data, options) {
if (options && options.hasOwnProperty('data')) {
data = extend(true, data, options.data);
}
// Push template data unto context
context.data = data;
if (options && options.hasOwnProperty('helpers')) {
// Push helpers unto context
context.helpers = options.helpers;
}
};
},
compileWithPartial: function(template, options) {
// Push template unto context
context.template = template;
}
};
global.describe = function (description, next) {
// Push suite description unto context
context.description = description;
next();
};
global.it = function (description, next) {
// Push test spec unto context
context.it = description;
next();
};
global.equal = global.equals = function (actual, expected, message) {
var spec = {
description : context.description
, it : context.it
, template : context.template
, data : context.data
, expected : expected
};
// Remove template and data from context
delete context.template;
delete context.data;
if (message) spec.message = message;
if (context.hasOwnProperty('helpers')) {
spec.helpers = extractHelpers(context.helpers);
// Remove helpder from context
delete context.helpers;
}
// If a template is found in the lexer, use it for the spec. This is true in
// the case of the tokenizer.
if (Handlebars.Parser.lexer.matched) {
spec.template = Handlebars.Parser.lexer.matched;
}
tests.add(spec);
};
global.shouldCompileTo = function (string, hashOrArray, expected) {
shouldCompileToWithPartials(string, hashOrArray, false, expected);
};
global.shouldCompileToWithPartials = function (string, hashOrArray, partials, expected, message) {
compileWithPartials(string, hashOrArray, partials, expected, message);
};
global.compileWithPartials = function (string, hashOrArray, partials, expected, message) {
var helpers = false;
if (util.isArray(hashOrArray)) {
data = hashOrArray[0];
helpers = extractHelpers(hashOrArray[1]);
partials = hashOrArray[2];
} else {
data = hashOrArray;
}
var spec = {
description : context.description
, it : context.it
, template : string
, data : data
};
if (partials) spec.partials = partials;
if (helpers) spec.helpers = helpers;
if (expected) spec.expected = expected;
if (message) spec.message = '' + message;
tests.add(spec);
};
global.shouldThrow = function (callback, error, message) {
try {
callback();
} catch (err) {}
var spec = {
description : context.description
, it : context.it
, template : context.template
, exception : true
};
// Remove template from context
delete context.template;
if (message) spec.message = '' + message;
tests.add(spec);
};
var input = path.resolve(program.args[0]);
fs.exists(input, function (exists) {
if (exists) {
require(input);
var output = JSON.stringify(tests, null, '\t');
if (!program.output) {
return console.log(output);
}
var outputFile = path.resolve(program.output);
fs.writeFile(outputFile, output, function (err) {
if (err) {
console.log(err);
} else {
console.log('JSON saved to ' + program.output);
}
});
} else {
console.log('The input file does not exist');
}
});