-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexpl.test.js
216 lines (171 loc) · 9.89 KB
/
expl.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
206
207
208
209
210
211
212
213
214
215
(function (global) {
global.test = test;
function test()
{
// Miscellaneous tests
//#BEGIN_TEST_DEV_ONLY
// Make sure function calls are also renamed
if (!/_b_\s*\(\s*_a_\s*\+\s*_c_\s*\)/.test( inlineCode('function f(a,b,c) { b(a+c); } inline f(d,e,f);') ))
throw new Error( 'inline failure: test #123' );
// https://github.com/glathoud/js.metaret/issues/6
// detect and forbid inline cycles
ensure_error( 'inlineCode( "inline f(); function f() { inline g(); } function g() { inline f(); }" );'
, function (error)
{
var msg = ('' + error).toLowerCase();
return [ 'error', 'inline', 'infinite', 'recursion', 'metafun', 'metaret', 'instead' ].every( function (word) { return -1 < msg.lastIndexOf( word ); } );
}
);
// https://github.com/glathoud/js.metaret/issues/7
// Permit inlining across files.
var workspace = {}
, first_file = 'function f() { "blah" }'
, second_file = 'inline f();'
, first_file_inlined = inlineCode( first_file, workspace, { path : "first.file.js" } )
, second_file_inlined = inlineCode( second_file, workspace, { path : "second.file.js" } )
;
assert( 'v.first_file === v.first_file_inlined', { first_file : first_file, first_file_inlined : first_file_inlined } );
assert( 'v.second_file !== v.second_file_inlined', { second_file : second_file, second_file_inlined : second_file_inlined } );
assert( '!/\\binline\\b/.test( v )', second_file_inlined.replace( /^\/\/#INLINE_.*$/gm, '' ) );
assert( ' /"blah"/.test( v )', second_file_inlined );
// https://github.com/glathoud/js.metaret/issues/7
// Permit inlining across files including access to some globals
var workspace = {}
, first_file = 'var SOME_CONST = "blah"; function f() { return SOME_CONST; }'
, second_file = 'inline f();'
, first_file_inlined = inlineCode( first_file, workspace, { path : "first.file.js" } )
, second_file_inlined = inlineCode( second_file, workspace, { path : "second.file.js" } )
;
assert( 'v.first_file === v.first_file_inlined', { first_file : first_file, first_file_inlined : first_file_inlined } );
assert( 'v.second_file !== v.second_file_inlined', { second_file : second_file, second_file_inlined : second_file_inlined } );
assert( '!/\\binline\\b/.test( v )', second_file_inlined.replace( /^\/\/#INLINE_.*$/gm, '' ) );
assert( ' /\\bSOME_CONST\\b/.test( v )', second_file_inlined );
// https://github.com/glathoud/js.metaret/issues/7
// Permit inlining across files but forbid access to locally bound externals (closure).
var workspace = {}
, first_file = '(function () { var SOME_CONST = "blah"; function f() { return SOME_CONST; } })()'
, second_file = 'inline f();'
, first_file_inlined = inlineCode( first_file, workspace, { path : "first.file.js" } )
;
assert( 'v.first_file === v.first_file_inlined', { first_file : first_file, first_file_inlined : first_file_inlined } );
assert( '!/\\binline\\b/.test( v )', second_file_inlined.replace( /^\/\/#INLINE_.*$/gm, '' ) );
ensure_error( 'inlineCode( this.second_file, this.workspace, { path : "second.file.js" } )'
, function (error)
{
var msg = ('' + error).toLowerCase();
return [ 'error', 'inline', 'across', 'files', 'local', 'closure', 'global' ].every( function (word) { return -1 < msg.lastIndexOf( word ); } );
}
, { second_file : second_file, workspace : workspace }
);
// https://github.com/glathoud/js.metaret/issues/7
//
// Inlining within a file. The source body must be visible
// to the target inline location.
//
ensure_error(
"inlineCode( 'function f() { var a,b; return g(); function g() { inline var ret = i(); return ret; function h() { function i() { return a+b; } } } }' )"
, function (error)
{
var msg = ('' + error).toLowerCase();
return [ 'error', 'inline', 'within', 'file', 'source', 'body', 'must be', 'visible', 'target', 'inline', 'location' ].every( function (word) { return -1 < msg.lastIndexOf( word ); } );
}
);
// https://github.com/glathoud/js.metaret/issues/7
//
// Inlining within a file. Permit:
//
// * to inline any function that does not have any closure.
//
assert( "2 === inlineCode( 'function f() { return g(); function g() { inline var ret = i(); return ret; } } function h() { function i() { \"i-body\"; } }' ).match( /i-body/g ).length" );
// * to share bound variables as long as they are defined
// within a scope shared by the source body and the target
// inline location.
inlineCode( 'function f() { var a,b; return g(); function g() { inline var ret = h(); return ret; function h() { return a+b; } } }' );
ensure_error(
"inlineCode( 'function f() { return g(); function g() { inline var ret = i(); } function h() { var a,b; function i() { return a+b; } } }' )"
, function (error)
{
var msg = ('' + error).toLowerCase();
return [ 'error', 'inline', 'within', 'file', 'source', 'body', 'must be', 'visible', 'target', 'inline', 'location' ].every( function (word) { return -1 < msg.lastIndexOf( word ); } );
}
);
ensure_error(
"inlineCode( 'function f() { var a,b; inline var ret = g(); } var a,b; function g() { return a+b; }' )"
, function (error)
{
var msg = ('' + error).toLowerCase();
return [ 'error', 'inline', 'within', 'file', 'source', 'body', 'target', 'inline', 'location', 'must', 'share', 'bound', 'variables' ].every( function (word) { return -1 < msg.lastIndexOf( word ); } );
}
);
// https://github.com/glathoud/js.metaret/issues/9
// error messages for beginner
ensure_error( 'var jscode = jsm2js("function f() { metaret; }");'
, function (error)
{
var msg = '' + error;
return /\bsyntaxerror\b/i.test( msg ) && /\bmetaret\b/i.test( msg ) &&
/\boutside\b/i.test( msg ) && /\bmetafun\b/i.test( msg );
}
);
ensure_error( 'inlineCode( "inline f(); function f() { console.log( arguments ); }" )'
, function (error)
{
var msg = '' + error;
return /\binline\b/i.test( msg ) && /\berror\b/i.test( msg ) && /\barguments\b/i.test( msg ) && /\bbody\b/i.test( msg );
}
);
// https://github.com/glathoud/js.metaret/issues/11
// forbid `arguments` use within metafun body because the body will be inlined.
ensure_error( 'var jscode = jsm2js("metafun f( self ) { arguments; }");'
, function (error)
{
var msg = ('' + error).toLowerCase();
return [ 'error', 'metafun', 'arguments', 'forbidden' ].every( function (word) { return -1 < msg.lastIndexOf( word ); } );
}
);
// https://github.com/glathoud/js.metaret/issues/13
// mutual recursion: any bound parameters must be shared.
ensure_error(
'jsm2js("function f(x){var y;return a(x); metafun a(self,x) { if (x > 0) metaret b,x-1; else return x+y; } } metafun b(self,x) {return y*x;}")'
, function (error)
{
var msg = ('' + error).toLowerCase();
return [ 'error', 'metafun', 'mutual', 'recursion', 'bound', 'variable', 'must', 'share' ].every( function (word) { return -1 < msg.lastIndexOf( word ); } );
}
);
need$( 'minify.js' );
assert( '"function f(){}"===minify("function f() { function g() { } }")' );
//#END_TEST_DEV_ONLY
// https://github.com/glathoud/js.metaret/issues/16
// RegExp parsing should work fine based on acorn.
var cp = codeparse( 'var a = b / c; var d = e / f;' );
if (0 !== cp.regexpArr.length)
throw new Error( 'RegExp: false positive!' );
var cp = codeparse( 'a = b' + '\n' + '/hi/g.exec(c).map(d);' ); // Based on the remarks at the beginning of section 7 of the ECMAscript 5 spec.
if (0 !== cp.regexpArr.length)
throw new Error( 'RegExp: false positive!' );
// ---
'undefined' !== typeof console && console.log('All tests passed.');
return true;
}
// ---
function ensure_error(codestring, testfun, /*?object?*/thisObj)
{
var success = false;
try {
new Function( codestring ).call( thisObj || global );
} catch (e) {
if (testfun( e ))
success = true;
else
throw new Error( 'Failed test: Caught an error as expected, but could not validate the error object. Test code: ' + codestring + ', error: ' + e );
}
if (!success)
throw new Error( 'Failed test: Expected an error on ' + codestring );
}
function assert(codestring, /*?optional?*/v)
{
if (!new Function( 'v', 'return ' + codestring + ';' )( v ))
throw new Error( 'Failed test: ' + codestring );
}
})(this);