-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathes2021_spec.rb
53 lines (43 loc) · 1.79 KB
/
es2021_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
gem 'minitest'
require 'minitest/autorun'
describe "ES2021 support" do
def to_js( string)
_(Ruby2JS.convert(string, eslevel: 2021, filters: []).to_s)
end
def to_js_fn( string)
_(Ruby2JS.convert(string, eslevel: 2021,
filters: [Ruby2JS::Filter::Functions]).to_s)
end
def to_js_nullish( string)
_(Ruby2JS.convert(string, eslevel: 2021, or: :nullish, filters: []).to_s)
end
it "should do short circuit assign - logical (default)" do
to_js( 'a = nil; a ||= 1').must_equal 'let a = null; a ||= 1'
to_js( '@a ||= 1').must_equal 'this._a ||= 1'
to_js( '@@a ||= 1').must_equal 'this.constructor._a ||= 1'
to_js( 'self.p ||= 1').must_equal 'this.p ||= 1'
to_js( 'a[i] ||= 1').must_equal 'a[i] ||= 1'
end
it "should do short circuit assign - nullish" do
to_js_nullish( 'a = nil; a ||= 1').must_equal 'let a = null; a ??= 1'
to_js_nullish( '@a ||= 1').must_equal 'this._a ??= 1'
to_js_nullish( '@@a ||= 1').must_equal 'this.constructor._a ??= 1'
to_js_nullish( 'self.p ||= 1').must_equal 'this.p ??= 1'
to_js_nullish( 'a[i] ||= 1').must_equal 'a[i] ??= 1'
end
it "should do short circuit and" do
to_js( 'a = nil; a &&= 1').must_equal 'let a = null; a &&= 1'
to_js( '@a &&= 1').must_equal 'this._a &&= 1'
to_js( '@@a &&= 1').must_equal 'this.constructor._a &&= 1'
to_js( 'self.p &&= 1').must_equal 'this.p &&= 1'
to_js( 'a[i] &&= 1').must_equal 'a[i] &&= 1'
end
it "should format large numbers with separators" do
to_js( '1000000' ).must_equal '1_000_000'
to_js( '1000000.000001' ).must_equal '1_000_000.000_001'
end
it "should convert gsub to replaceAll" do
to_js_fn( 'x.gsub("a", "b")' ).must_equal 'x.replaceAll("a", "b")'
to_js_fn( 'x.gsub(/a/, "b")' ).must_equal 'x.replaceAll(/a/g, "b")'
end
end