1 |
|
2 |
|
3 |
|
4 |
|
5 | import parse from './parse';
|
6 | import template from 'babel-template';
|
7 | import traverse from 'babel-traverse';
|
8 | import generateSpec from './mocha-template';
|
9 |
|
10 | import {
|
11 | build as buildLiteralAssertion,
|
12 | test as literalAssertionTest
|
13 | } from './assertions/literal';
|
14 |
|
15 | const assertionStatementTemplate = template(`
|
16 | return Promise.resolve(ORIGINAL)
|
17 | .then(function(result) {
|
18 | ASSERTIONS
|
19 | })
|
20 | `);
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | export default function transform({
|
31 | comment, name, filename, type
|
32 | }) {
|
33 | const ast = parse(comment);
|
34 |
|
35 | traverse(ast, {
|
36 | enter(path) {
|
37 | if (path.node.trailingComments) {
|
38 | const assertions = path.node.trailingComments.map(makeAsserter);
|
39 | if (assertions.length) {
|
40 | Reflect.deleteProperty(path.node, 'trailingComments');
|
41 |
|
42 | path.replaceWith(assertionStatementTemplate({
|
43 | ORIGINAL: path.node,
|
44 | ASSERTIONS: assertions
|
45 | }));
|
46 | }
|
47 | }
|
48 | }
|
49 | });
|
50 |
|
51 | return generateSpec({
|
52 | testCase: ast,
|
53 | name,
|
54 | filename,
|
55 | type
|
56 | });
|
57 | }
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 | function makeAsserter(comment) {
|
65 | if (literalAssertionTest(comment.value)) {
|
66 | return buildLiteralAssertion(comment.value);
|
67 | }
|
68 |
|
69 | return null;
|
70 | }
|