-
Notifications
You must be signed in to change notification settings - Fork 5
/
extraction-test.js
206 lines (152 loc) · 4.73 KB
/
extraction-test.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
var extraction = require('nodeunit').testCase;
var Codesurgeon = require('../lib/codesurgeon').Codesurgeon;
var uglify = require('uglify-js');
var vm = require('vm');
module.exports = extraction({
setUp: function (test) {
if (typeof test === 'function') {
test();
}
else {
test.done();
}
},
tearDown: function (test) {
if (typeof test === 'function') {
test();
}
else {
test.done();
}
},
'1. Extract a method by `simple` name.': function (test) {
var surgeon = new Codesurgeon;
var sandbox = {};
surgeon
//
// if we want to see logging information or not.
//
.configure({ quiet: true })
//
// read one, or a couple of files
//
.read(__dirname + '/fixture1.js')
//
// get one or more methods from the code that we've read in.
//
.extract('test5');
vm.runInNewContext(surgeon.output, sandbox, 'sandbox.vm');
test.ok(sandbox.test5(), 'The function was extracted and executed.')
test.expect(1);
test.done();
},
'2. Extract a method by dot notation.': function (test) {
var surgeon = new Codesurgeon;
var sandbox = {
NODEJITSU: {}
};
surgeon
//
// if we want to see logging information or not.
//
.configure({ quiet: true })
//
// read one, or a couple of files
//
.read(__dirname + '/fixture1.js')
//
// get one or more methods from the code that we've read in.
//
.extract(
'NODEJITSU.B',
'NODEJITSU.B.prototype.p'
);
vm.runInNewContext(surgeon.output, sandbox, 'sandbox.vm');
test.ok(sandbox.NODEJITSU.B, 'The function was extracted and executed.')
test.ok(sandbox.NODEJITSU.B.prototype.p, 'The function was extracted and executed.')
test.expect(2);
test.done();
},
'3. Extract a variable.': function (test) {
var surgeon = new Codesurgeon;
var sandbox = {};
surgeon
//
// if we want to see logging information or not.
//
.configure({ quiet: true })
//
// read one, or a couple of files
//
.read(__dirname + '/fixture1.js')
//
// get one or more methods from the code that we've read in.
//
.extract('test1');
vm.runInNewContext(surgeon.output, sandbox, 'sandbox.vm');
test.equal(sandbox.test1, 20, 'The variable was extracted and evaluated correctly.');
test.expect(1);
test.done();
},
'4. Extract a variable from the result of reading in multiple source files.': function (test) {
var surgeon = new Codesurgeon;
var sandbox = {};
surgeon
.configure({ quiet: true })
.read(__dirname + '/fixture1.js', __dirname + '/fixture2.js')
.extract('test1', 'test12');
vm.runInNewContext(surgeon.output, sandbox, 'sandbox.vm');
test.equal(sandbox.test1, 20, 'The variable was extracted and evaluated correctly.');
test.expect(1);
test.done();
},
'5. Extract a variable from the result of reading in multiple source files asyncronously.': function (test) {
var surgeon = new Codesurgeon;
var sandbox = { // fake node environment.
module: { exports: {} },
exports: {}
};
surgeon
.configure({ quiet: true })
.read(__dirname + '/fixture1.js', __dirname + '/fixture2.js', function() {
this.extract('test1', 'test12');
vm.runInNewContext(this.output, sandbox, 'sandbox.vm');
test.equal(sandbox.test1, 20, 'The variable was extracted and evaluated correctly.');
test.equal(sandbox.test12(), 12, 'The function was extracted and evaluated correctly.');
test.expect(2);
test.done();
})
;
},
'6. Extract-As for `simple` and dot notated items.': function (test) {
var surgeon = new Codesurgeon;
var sandbox = {
exports: {}
};
surgeon
.configure({ quiet: true })
.read(__dirname + '/fixture1.js', __dirname + '/fixture2.js')
.extract(
'test1',
['test12', 'bazz12'],
['exports.hello', 'exports.foo']
);
vm.runInNewContext(surgeon.output, sandbox, 'sandbox.vm');
test.equal(sandbox.bazz12(), 12, 'The variable was extracted and evaluated correctly.');
test.equal(sandbox.exports.foo, 'Hello, World.', 'The variable was extracted and evaluated correctly.');
test.expect(2);
test.done();
},
'7. Extract the value of an arbitrary key from an object literal.': function(test) {
var surgeon = new Codesurgeon;
var file = __dirname + '/fixture6.js';
surgeon
.configure({ quiet: true })
.read(file)
.extract('b')
;
test.equal(surgeon.output, 200);
test.expect(1);
test.done();
}
});