-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathbitstring_test.exs
More file actions
95 lines (71 loc) · 4.04 KB
/
Copy pathbitstring_test.exs
File metadata and controls
95 lines (71 loc) · 4.04 KB
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
defmodule ElixirScript.Translator.Bitstring.Test do
use ExUnit.Case
import ElixirScript.TestHelper
test "translate bitstring" do
ex_ast = quote do: <<1, 2, 3>>
assert_translation(ex_ast, "new Bootstrap.Core.BitString(Bootstrap.Core.BitString.integer(1), Bootstrap.Core.BitString.integer(2), Bootstrap.Core.BitString.integer(3))")
ex_ast = quote do: <<1, "foo">>
assert_translation(ex_ast, "new Bootstrap.Core.BitString(Bootstrap.Core.BitString.integer(1), Bootstrap.Core.BitString.binary('foo'))")
ex_ast = quote do: <<1, "foo" :: binary>>
assert_translation(ex_ast, "new Bootstrap.Core.BitString(Bootstrap.Core.BitString.integer(1), Bootstrap.Core.BitString.binary('foo'))")
ex_ast = quote do: <<1, "foo" :: utf8, "bar" :: utf32>>
assert_translation(ex_ast, "new Bootstrap.Core.BitString(Bootstrap.Core.BitString.integer(1), Bootstrap.Core.BitString.utf8('foo'), Bootstrap.Core.BitString.utf32('bar'))")
ex_ast = quote do
rest = "oo"
<<102 :: integer-native, rest :: binary>>
end
assert_translation(ex_ast, "new Bootstrap.Core.BitString(Bootstrap.Core.BitString.native(Bootstrap.Core.BitString.integer(102)), Bootstrap.Core.BitString.binary(rest))")
ex_ast = quote do
rest = "oo"
<<102 :: unsigned-big-integer, rest :: binary>>
end
assert_translation(ex_ast, "new Bootstrap.Core.BitString(Bootstrap.Core.BitString.integer(Bootstrap.Core.BitString.big(Bootstrap.Core.BitString.unsigned(102))), Bootstrap.Core.BitString.binary(rest))")
ex_ast = quote do
rest = 100
<<102, rest :: size(16)>>
end
assert_translation(ex_ast, "new Bootstrap.Core.BitString(Bootstrap.Core.BitString.integer(102), Bootstrap.Core.BitString.size(rest, 16))")
ex_ast = quote do
rest = 100
<<102, rest :: size(16)-unit(4)>>
end
assert_translation(ex_ast, "new Bootstrap.Core.BitString(Bootstrap.Core.BitString.integer(102), Bootstrap.Core.BitString.unit(Bootstrap.Core.BitString.size(rest, 16), 4))")
ex_ast = quote do
rest = 100
<<102, rest :: 16 * 4>>
end
assert_translation(ex_ast, "new Bootstrap.Core.BitString(Bootstrap.Core.BitString.integer(102), Bootstrap.Core.BitString.unit(Bootstrap.Core.BitString.size(rest, 16), 4))")
ex_ast = quote do
rest = 100
<<102, rest :: 16>>
end
assert_translation(ex_ast, "new Bootstrap.Core.BitString(Bootstrap.Core.BitString.integer(102), Bootstrap.Core.BitString.size(rest, 16))")
ex_ast = quote do: << 1, <<2>> >>
assert_translation(ex_ast, "new Bootstrap.Core.BitString(Bootstrap.Core.BitString.integer(1), new Bootstrap.Core.BitString(Bootstrap.Core.BitString.integer(2)))")
end
test "translate pattern matching bitstring" do
ex_ast = quote do: <<name::binary-size(5), " the ", species::binary>> = <<"Frank the Walrus">>
js_code = """
let [name,species] = Bootstrap.Core.Patterns.match(Bootstrap.Core.Patterns.bitStringMatch(Bootstrap.Core.BitString.size(Bootstrap.Core.BitString.binary({
'value': Bootstrap.Core.Patterns.variable()
}),5),Bootstrap.Core.BitString.binary(' the '),Bootstrap.Core.BitString.binary({
'value': Bootstrap.Core.Patterns.variable()
})),'Frank the Walrus');
"""
assert_translation(ex_ast, js_code)
ex_ast = quote do: <<int::integer>> = <<-100>>
js_code = """
let [int] = Bootstrap.Core.Patterns.match(Bootstrap.Core.Patterns.bitStringMatch(Bootstrap.Core.BitString.integer({
'value': Bootstrap.Core.Patterns.variable()
})),new Bootstrap.Core.BitString(Bootstrap.Core.BitString.binary(-100)));
"""
assert_translation(ex_ast, js_code)
ex_ast = quote do: <<-100::signed, _rest::binary>> = <<-100, "foo">>
js_code = """
let [_rest] = Bootstrap.Core.Patterns.match(Bootstrap.Core.Patterns.bitStringMatch(Bootstrap.Core.BitString.signed(-100),Bootstrap.Core.BitString.binary({
'value': Bootstrap.Core.Patterns.variable()
})),new Bootstrap.Core.BitString(Bootstrap.Core.BitString.binary(-100),Bootstrap.Core.BitString.binary('foo')));
"""
assert_translation(ex_ast, js_code)
end
end