-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathnamespace_spec.rb
78 lines (66 loc) · 2.49 KB
/
namespace_spec.rb
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
gem 'minitest'
require 'minitest/autorun'
describe "namespace support" do
def to_js(string)
_(Ruby2JS.convert(string, eslevel: 2017, filters: []).to_s)
end
describe "open modules" do
it "should extend modules" do
to_js( 'module M; def f(); end; end;' +
'module M; def g(); end; end').
must_equal('const M = {f() {}}; ' +
'M.g = function() {}');
end
it "should extend nested modules" do
to_js( 'module M; module N; def f(); end; end; end;' +
'module M::N; def g(); end; end').
must_equal('const M = {N: {f() {}}}; ' +
'M.N.g = function() {}');
end
it "should extend nested modules with getter" do
to_js( 'module M; module N; def f(); end; end; end;' +
'module M::N; def g; end; end').
must_equal('const M = {N: {f() {}}}; ' +
'Object.defineProperties(M.N, ' +
'Object.getOwnPropertyDescriptors({get g() {}}))');
end
end
describe "open classes" do
it "should extend classes" do
to_js( 'class M; def f(); end; end;' +
'class M; def g(); end; end').
must_equal('class M {f() {}}; ' +
'M.prototype.g = function() {}');
end
it "should extend nested modules with getter" do
to_js( 'module M; class N; def f(); end; end; end;' +
'class M::N; def g; end; end').
must_equal('const M = {N: class {f() {}}}; ' +
'Object.defineProperty(M.N.prototype, "g", ' +
'{enumerable: true, configurable: true, get() {}})');
end
it "should bind references to methods defined in original class" do
to_js( 'class C; def f(); end; end' +
'class C; def g; f; end; end').
must_include 'return this.f.bind(this)'
end
it "should call references to methods defined in original class" do
to_js( 'class C; def f(); end; end' +
'class C; def g(); f; end; end').
must_include 'this.f()'
to_js( 'class C; def f(); end; end' +
'class C; def g(); x {f}; end; end').
must_include 'this.f()'
end
it "should bind references to methods defined in parent class" do
to_js( 'class C; def f(); end; end' +
'class D < C; def g; f; end; end').
must_include 'return this.f.bind(this)'
end
it "should bind references to methods defined in included modules" do
to_js( 'class M; def f(); end; end' +
'class C; include M; def g; f; end; end').
must_include 'return this.f.bind(this)'
end
end
end