-
Notifications
You must be signed in to change notification settings - Fork 1
/
integration.js
131 lines (110 loc) · 3.41 KB
/
integration.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
var analyse = require("../");
var acorn = require("acorn");
var immutable = require("immutable");
var assert = require("assert");
describe("integration", function() {
it("should be able to get flow for a simple program", function() {
var input = `
while(true) {
console.log("hello world");
}
`;
var ast = acorn.parse(input);
var flow = analyse(ast);
// we'd expect a control flow (roughly) like:
// Program while true { ... } console... ...log()
// 0 | ---> |
// 1 | -> |
// 2 | <- |
// 3 | --------> |
// 4 | ----------> |
// 5 | <-- |
// 6 | --> |
// 7 | <---------- |
// 8 | <-------- |
//
// ( back to step 3 again )
// ( or terminate: )
//
// 9 | <--- |
var visitedNodes = immutable.List();
// given that this should loop forever, we only evaluate 40 control flow
// steps:
var currentExecution = immutable.Set([flow.getStartOfFlow()]);
for(var iteration = 0; iteration < 40; iteration += 1) {
var currentExecution = immutable.Set(
currentExecution.map((flow) => (
immutable.List(flow.getForwardFlows()))).flatten());
visitedNodes = visitedNodes.concat(
currentExecution.filter((flow) => flow.isEnter())
.map((flow) => flow.getNode().type))
if (currentExecution.size == 0) {
assert.fail("Shouldn't terminate - endless program");
}
}
// we'd expect to visit the CallExpression more than once:
assert.ok(visitedNodes.filter((type) => type == "CallExpression").size > 1);
});
it("should work for README example", function() {
var ast = acorn.parse([
"if (x) {",
" hello();",
"} else {",
" world();",
"}"
].join("\n"));
var flow = analyse(ast);
function countBranches(node, visited) {
var nodeId = node.getId();
var outgoing = node.getForwardFlows();
if (visited.indexOf(nodeId) != -1) {
// we've visited this node before - we're in a loop
return Infinity;
}
if (outgoing.length == 0) {
// we've reached the termination of this control flow
return 1;
}
return outgoing.reduce((counter, node) => (
counter + countBranches(node, visited.concat(nodeId))
), 0);
}
assert.equal(2, countBranches(flow.getStartOfFlow(), []));
});
it("should print example output", function() {
var ast = acorn.parse([
"{",
" helloWorld();",
"}"
].join("\n"));
var flow = analyse(ast).getStartOfFlow();
var nodes = [];
while(flow != undefined) {
var type = "";
if (flow.isHoist()) {
type = "hoist";
}
if (flow.isEnter()) {
type = "enter";
}
if (flow.isExit()) {
type = "exit";
}
nodes.push(flow.getNode().type + ": " + type);
flow = flow.getForwardFlows()[0];
}
assert.deepEqual([
"Program: hoist",
"Program: enter",
"BlockStatement: enter",
"ExpressionStatement: enter",
"CallExpression: enter",
"Identifier: enter",
"Identifier: exit",
"CallExpression: exit",
"ExpressionStatement: exit",
"BlockStatement: exit",
"Program: exit"
], nodes);
});
});