isomorfeus-speednode 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,2 +0,0 @@
1
- require 'bundler/setup'
2
- require 'isomorfeus-speednode'
@@ -1,456 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- require "minitest/autorun"
3
- require "execjs/module"
4
- require "json"
5
-
6
- begin
7
- require "execjs"
8
- rescue ExecJS::RuntimeUnavailable => e
9
- warn e
10
- exit 2
11
- end
12
-
13
- if defined? Minitest::Test
14
- Test = Minitest::Test
15
- elsif defined? MiniTest::Unit::TestCase
16
- Test = MiniTest::Unit::TestCase
17
- end
18
-
19
- class TestExecJS < Test
20
- def test_runtime_available
21
- runtime = ExecJS::ExternalRuntime.new(command: "nonexistent")
22
- assert !runtime.available?
23
-
24
- runtime = ExecJS::ExternalRuntime.new(command: "ruby")
25
- assert runtime.available?
26
- end
27
-
28
- def test_runtime_assignment
29
- original_runtime = ExecJS.runtime
30
- runtime = ExecJS::ExternalRuntime.new(command: "nonexistent")
31
- assert_raises(ExecJS::RuntimeUnavailable) { ExecJS.runtime = runtime }
32
- assert_equal original_runtime, ExecJS.runtime
33
-
34
- runtime = ExecJS::ExternalRuntime.new(command: "ruby")
35
- ExecJS.runtime = runtime
36
- assert_equal runtime, ExecJS.runtime
37
- ensure
38
- ExecJS.runtime = original_runtime
39
- end
40
-
41
- def test_context_call
42
- context = ExecJS.compile("id = function(v) { return v; }")
43
- assert_equal "bar", context.call("id", "bar")
44
- end
45
-
46
- def test_nested_context_call
47
- context = ExecJS.compile("a = {}; a.b = {}; a.b.id = function(v) { return v; }")
48
- assert_equal "bar", context.call("a.b.id", "bar")
49
- end
50
-
51
- def test_call_with_complex_properties
52
- context = ExecJS.compile("")
53
- assert_equal 2, context.call("function(a, b) { return a + b }", 1, 1)
54
-
55
- context = ExecJS.compile("foo = 1")
56
- assert_equal 2, context.call("(function(bar) { return foo + bar })", 1)
57
- end
58
-
59
- def test_call_with_this
60
- # Known bug: https://github.com/cowboyd/therubyrhino/issues/39
61
- skip if ExecJS.runtime.is_a?(ExecJS::RubyRhinoRuntime)
62
-
63
- # Make sure that `this` is indeed the global scope
64
- context = ExecJS.compile(<<-EOF)
65
- name = 123;
66
-
67
- function Person(name) {
68
- this.name = name;
69
- }
70
-
71
- Person.prototype.getThis = function() {
72
- return this.name;
73
- }
74
- EOF
75
-
76
- assert_equal 123, context.call("(new Person('Bob')).getThis")
77
- end
78
-
79
- def test_context_call_missing_function
80
- context = ExecJS.compile("")
81
- assert_raises ExecJS::ProgramError do
82
- context.call("missing")
83
- end
84
- end
85
-
86
- {
87
- "function() {}" => nil,
88
- "0" => 0,
89
- "null" => nil,
90
- "undefined" => nil,
91
- "true" => true,
92
- "false" => false,
93
- "[1, 2]" => [1, 2],
94
- "[1, function() {}]" => [1, nil],
95
- "'hello'" => "hello",
96
- "'red yellow blue'.split(' ')" => ["red", "yellow", "blue"],
97
- "{a:1,b:2}" => {"a"=>1,"b"=>2},
98
- "{a:true,b:function (){}}" => {"a"=>true},
99
- "'café'" => "café",
100
- '"☃"' => "☃",
101
- '"\u2603"' => "☃",
102
- "'\u{1f604}'".encode("UTF-8") => "\u{1f604}".encode("UTF-8"), # Smiling emoji
103
- "'\u{1f1fa}\u{1f1f8}'".encode("UTF-8") => "\u{1f1fa}\u{1f1f8}".encode("UTF-8"), # US flag
104
- '"\\\\"' => "\\"
105
- }.each_with_index do |(input, output), index|
106
- define_method("test_exec_string_#{index}") do
107
- if output == nil
108
- assert_nil output, ExecJS.exec("return #{input}")
109
- else
110
- assert_equal output, ExecJS.exec("return #{input}")
111
- end
112
- end
113
-
114
- define_method("test_eval_string_#{index}") do
115
- if output == nil
116
- assert_nil output, ExecJS.eval(input)
117
- else
118
- assert_equal output, ExecJS.eval(input)
119
- end
120
- end
121
-
122
- define_method("test_compile_return_string_#{index}") do
123
- context = ExecJS.compile("var a = #{input};")
124
- if output == nil
125
- assert_nil output, context.eval("a")
126
- else
127
- assert_equal output, context.eval("a")
128
- end
129
- end
130
-
131
- define_method("test_compile_call_string_#{index}") do
132
- context = ExecJS.compile("function a() { return #{input}; }")
133
- if output == nil
134
- assert_nil output, context.call("a")
135
- else
136
- assert_equal output, context.call("a")
137
- end
138
- end
139
- end
140
-
141
- [
142
- nil,
143
- true,
144
- false,
145
- 1,
146
- 3.14,
147
- "hello",
148
- "\\",
149
- "café",
150
- "☃",
151
- "\u{1f604}".encode("UTF-8"), # Smiling emoji
152
- "\u{1f1fa}\u{1f1f8}".encode("UTF-8"), # US flag
153
- [1, 2, 3],
154
- [1, [2, 3]],
155
- [1, [2, [3]]],
156
- ["red", "yellow", "blue"],
157
- { "a" => 1, "b" => 2},
158
- { "a" => 1, "b" => [2, 3]},
159
- { "a" => true }
160
- ].each_with_index do |value, index|
161
- json_value = JSON.generate(value, quirks_mode: true)
162
-
163
- define_method("test_json_value_#{index}") do
164
- if value == nil
165
- assert_nil value, JSON.parse(json_value, quirks_mode: true)
166
- else
167
- assert_equal value, JSON.parse(json_value, quirks_mode: true)
168
- end
169
- end
170
-
171
- define_method("test_exec_value_#{index}") do
172
- if value == nil
173
- assert_nil value, ExecJS.exec("return #{json_value}")
174
- else
175
- assert_equal value, ExecJS.exec("return #{json_value}")
176
- end
177
- end
178
-
179
- define_method("test_eval_value_#{index}") do
180
- if value == nil
181
- assert_nil value, ExecJS.eval("#{json_value}")
182
- else
183
- assert_equal value, ExecJS.eval("#{json_value}")
184
- end
185
- end
186
-
187
- define_method("test_strinigfy_value_#{index}") do
188
- context = ExecJS.compile("function json(obj) { return JSON.stringify(obj); }")
189
- assert_equal json_value, context.call("json", value)
190
- end
191
-
192
- define_method("test_call_value_#{index}") do
193
- context = ExecJS.compile("function id(obj) { return obj; }")
194
- if value == nil
195
- assert_nil value, context.call("id", value)
196
- else
197
- assert_equal value, context.call("id", value)
198
- end
199
- end
200
- end
201
-
202
- def test_additional_options
203
- assert ExecJS.eval("true", :foo => true)
204
- assert ExecJS.exec("return true", :foo => true)
205
-
206
- context = ExecJS.compile("foo = true", :foo => true)
207
- assert context.eval("foo", :foo => true)
208
- assert context.exec("return foo", :foo => true)
209
- end
210
-
211
- def test_eval_blank
212
- assert_nil ExecJS.eval("")
213
- assert_nil ExecJS.eval(" ")
214
- assert_nil ExecJS.eval(" ")
215
- end
216
-
217
- def test_exec_return
218
- assert_nil ExecJS.exec("return")
219
- end
220
-
221
- def test_exec_no_return
222
- assert_nil ExecJS.exec("1")
223
- end
224
-
225
- def test_encoding
226
- utf8 = Encoding.find('UTF-8')
227
-
228
- assert_equal utf8, ExecJS.exec("return 'hello'").encoding
229
- assert_equal utf8, ExecJS.eval("'☃'").encoding
230
-
231
- ascii = "'hello'".encode('US-ASCII')
232
- result = ExecJS.eval(ascii)
233
- assert_equal "hello", result
234
- assert_equal utf8, result.encoding
235
-
236
- assert_raises Encoding::UndefinedConversionError do
237
- binary = "\xde\xad\xbe\xef".force_encoding("BINARY")
238
- ExecJS.eval(binary)
239
- end
240
- end
241
-
242
- def test_encoding_compile
243
- utf8 = Encoding.find('UTF-8')
244
-
245
- context = ExecJS.compile("foo = function(v) { return '¶' + v; }".encode("ISO8859-15"))
246
-
247
- assert_equal utf8, context.exec("return foo('hello')").encoding
248
- assert_equal utf8, context.eval("foo('☃')").encoding
249
-
250
- ascii = "foo('hello')".encode('US-ASCII')
251
- result = context.eval(ascii)
252
- assert_equal "¶hello", result
253
- assert_equal utf8, result.encoding
254
-
255
- assert_raises Encoding::UndefinedConversionError do
256
- binary = "\xde\xad\xbe\xef".force_encoding("BINARY")
257
- context.eval(binary)
258
- end
259
- end
260
-
261
- def test_surrogate_pairs
262
- # Smiling emoji
263
- str = "\u{1f604}".encode("UTF-8")
264
- assert_equal 2, ExecJS.eval("'#{str}'.length")
265
- assert_equal str, ExecJS.eval("'#{str}'")
266
-
267
- # US flag emoji
268
- str = "\u{1f1fa}\u{1f1f8}".encode("UTF-8")
269
- assert_equal 4, ExecJS.eval("'#{str}'.length")
270
- assert_equal str, ExecJS.eval("'#{str}'")
271
- end
272
-
273
- def test_compile_anonymous_function
274
- context = ExecJS.compile("foo = function() { return \"bar\"; }")
275
- assert_equal "bar", context.exec("return foo()")
276
- assert_equal "bar", context.eval("foo()")
277
- assert_equal "bar", context.call("foo")
278
- end
279
-
280
- def test_compile_named_function
281
- context = ExecJS.compile("function foo() { return \"bar\"; }")
282
- assert_equal "bar", context.exec("return foo()")
283
- assert_equal "bar", context.eval("foo()")
284
- assert_equal "bar", context.call("foo")
285
- end
286
-
287
- def test_this_is_global_scope
288
- assert_equal true, ExecJS.eval("this === (function() {return this})()")
289
- assert_equal true, ExecJS.exec("return this === (function() {return this})()")
290
- end
291
-
292
- def test_browser_self_is_undefined
293
- assert ExecJS.eval("typeof self == 'undefined'")
294
- end
295
-
296
- def test_node_global_is_undefined
297
- assert ExecJS.eval("typeof global == 'undefined'")
298
- end
299
-
300
- def test_node_process_is_undefined
301
- assert ExecJS.eval("typeof process == 'undefined'")
302
- refute ExecJS.eval("'process' in this")
303
- end
304
-
305
- def test_commonjs_vars_are_undefined
306
- assert ExecJS.eval("typeof module == 'undefined'")
307
- assert ExecJS.eval("typeof exports == 'undefined'")
308
- assert ExecJS.eval("typeof require == 'undefined'")
309
-
310
- refute ExecJS.eval("'module' in this")
311
- refute ExecJS.eval("'exports' in this")
312
- refute ExecJS.eval("'require' in this")
313
- end
314
-
315
- def test_console_is_undefined
316
- assert ExecJS.eval("typeof console == 'undefined'")
317
- refute ExecJS.eval("'console' in this")
318
- end
319
-
320
- def test_timers_are_undefined
321
- assert ExecJS.eval("typeof setTimeout == 'undefined'")
322
- assert ExecJS.eval("typeof setInterval == 'undefined'")
323
- assert ExecJS.eval("typeof clearTimeout == 'undefined'")
324
- assert ExecJS.eval("typeof clearInterval == 'undefined'")
325
- assert ExecJS.eval("typeof setImmediate == 'undefined'")
326
- assert ExecJS.eval("typeof clearImmediate == 'undefined'")
327
-
328
- refute ExecJS.eval("'setTimeout' in this")
329
- refute ExecJS.eval("'setInterval' in this")
330
- refute ExecJS.eval("'clearTimeout' in this")
331
- refute ExecJS.eval("'clearInterval' in this")
332
- refute ExecJS.eval("'setImmediate' in this")
333
- refute ExecJS.eval("'clearImmediate' in this")
334
- end
335
-
336
- def test_compile_large_scripts
337
- body = "var foo = 'bar';\n" * 100_000
338
- assert ExecJS.exec("function foo() {\n#{body}\n};\nreturn true")
339
- end
340
-
341
- def test_exec_syntax_error
342
- begin
343
- ExecJS.exec(")")
344
- flunk
345
- rescue ExecJS::RuntimeError => e
346
- assert e
347
- assert e.backtrace[0].include?("(execjs):1"), e.backtrace.join("\n")
348
- end
349
- end
350
-
351
- def test_eval_syntax_error
352
- begin
353
- ExecJS.eval(")")
354
- flunk
355
- rescue ExecJS::RuntimeError => e
356
- assert e
357
- assert e.backtrace[0].include?("(execjs):1"), e.backtrace.join("\n")
358
- end
359
- end
360
-
361
- def test_compile_syntax_error
362
- begin
363
- ExecJS.compile(")")
364
- flunk
365
- rescue ExecJS::RuntimeError => e
366
- assert e
367
- assert e.backtrace[0].include?("(execjs):1"), e.backtrace.join("\n")
368
- end
369
- end
370
-
371
- def test_exec_thrown_error
372
- begin
373
- ExecJS.exec("throw new Error('hello')")
374
- flunk
375
- rescue ExecJS::ProgramError => e
376
- assert e
377
- assert e.backtrace[0].include?("(execjs):1"), e.backtrace.join("\n")
378
- end
379
- end
380
-
381
- def test_eval_thrown_error
382
- begin
383
- ExecJS.eval("(function(){ throw new Error('hello') })()")
384
- flunk
385
- rescue ExecJS::ProgramError => e
386
- assert e
387
- assert e.backtrace[0].include?("(execjs):1"), e.backtrace.join("\n")
388
- end
389
- end
390
-
391
- def test_compile_thrown_error
392
- begin
393
- ExecJS.compile("throw new Error('hello')")
394
- flunk
395
- rescue ExecJS::ProgramError => e
396
- assert e
397
- assert e.backtrace[0].include?("(execjs):1"), e.backtrace.join("\n")
398
- end
399
- end
400
-
401
- def test_exec_thrown_string
402
- assert_raises ExecJS::ProgramError do
403
- ExecJS.exec("throw 'hello'")
404
- end
405
- end
406
-
407
- def test_eval_thrown_string
408
- assert_raises ExecJS::ProgramError do
409
- ExecJS.eval("(function(){ throw 'hello' })()")
410
- end
411
- end
412
-
413
- def test_compile_thrown_string
414
- assert_raises ExecJS::ProgramError do
415
- ExecJS.compile("throw 'hello'")
416
- end
417
- end
418
-
419
- def test_babel
420
- skip if ExecJS.runtime.is_a?(ExecJS::RubyRhinoRuntime)
421
-
422
- assert source = File.read(File.expand_path("../fixtures/babel.js", __FILE__))
423
- source = <<-JS
424
- var self = this;
425
- #{source}
426
- babel.eval = function(code) {
427
- return eval(babel.transform(code)["code"]);
428
- }
429
- JS
430
- context = ExecJS.compile(source)
431
- assert_equal 64, context.call("babel.eval", "((x) => x * x)(8)")
432
- end
433
-
434
- def test_coffeescript
435
- assert source = File.read(File.expand_path("../fixtures/coffee-script.js", __FILE__))
436
- context = ExecJS.compile(source)
437
- assert_equal 64, context.call("CoffeeScript.eval", "((x) -> x * x)(8)")
438
- end
439
-
440
- def test_uglify
441
- assert source = File.read(File.expand_path("../fixtures/uglify.js", __FILE__))
442
- source = <<-JS
443
- #{source}
444
-
445
- function uglify(source) {
446
- var ast = UglifyJS.parse(source);
447
- var stream = UglifyJS.OutputStream();
448
- ast.print(stream);
449
- return stream.toString();
450
- }
451
- JS
452
- context = ExecJS.compile(source)
453
- assert_equal "function foo(bar){return bar}",
454
- context.call("uglify", "function foo(bar) {\n return bar;\n}")
455
- end
456
- end