UNPKG

1.57 kBJavaScriptView Raw
1(function (root, factory) {
2 'use strict';
3
4 var dependencies = [
5 'espower',
6 'esprima',
7 'escodegen'
8 ];
9
10 if (typeof define === 'function' && define.amd) {
11 define(dependencies, factory);
12 } else if (typeof exports === 'object') {
13 module.exports = factory.apply(root, dependencies.map(function (path) { return require(path); }));
14 } else {
15 root.testHelper = factory.apply(root, dependencies.map(function (path) {
16 var tokens = path.split('/');
17 return root[tokens[tokens.length - 1]];
18 }));
19 }
20}(this, function (
21 espower,
22 esprima,
23 escodegen
24) {
25 // see: https://github.com/Constellation/escodegen/issues/115
26 if (typeof define === 'function' && define.amd) {
27 escodegen = window.escodegen;
28 }
29
30function extractBodyOfAssertionAsCode (node) {
31 var expression;
32 if (node.type === 'ExpressionStatement') {
33 expression = node.expression;
34 }
35 return escodegen.generate(expression.arguments[0], {format: {compact: true}});
36}
37
38function applyEspower (line, options) {
39 options = options || {destructive: false, source: line, path: '/path/to/some_test.js', powerAssertVariableName: 'assert'};
40 var tree = esprima.parse(line, {tolerant: true, loc: true, tokens: true});
41 return espower(tree, options);
42}
43
44function weave (line, options) {
45 return escodegen.generate(applyEspower(line, options), {format: {compact: true}});
46}
47
48return {
49 weave: weave,
50 extractBodyOfAssertionAsCode: extractBodyOfAssertionAsCode
51};
52
53}));