erlang-etf 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (107) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +18 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +4 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE.txt +22 -0
  7. data/README.md +49 -0
  8. data/Rakefile +6 -0
  9. data/erlang-etf.gemspec +30 -0
  10. data/lib/erlang/etf.rb +40 -0
  11. data/lib/erlang/etf/atom.rb +46 -0
  12. data/lib/erlang/etf/atom_utf8.rb +44 -0
  13. data/lib/erlang/etf/bert.rb +74 -0
  14. data/lib/erlang/etf/binary.rb +44 -0
  15. data/lib/erlang/etf/bit_binary.rb +47 -0
  16. data/lib/erlang/etf/export.rb +44 -0
  17. data/lib/erlang/etf/extensions.rb +157 -0
  18. data/lib/erlang/etf/extensions/array.rb +29 -0
  19. data/lib/erlang/etf/extensions/big_decimal.rb +22 -0
  20. data/lib/erlang/etf/extensions/erlang-export.rb +26 -0
  21. data/lib/erlang/etf/extensions/erlang-list.rb +31 -0
  22. data/lib/erlang/etf/extensions/erlang-nil.rb +22 -0
  23. data/lib/erlang/etf/extensions/erlang-pid.rb +22 -0
  24. data/lib/erlang/etf/extensions/erlang-string.rb +22 -0
  25. data/lib/erlang/etf/extensions/erlang-tuple.rb +31 -0
  26. data/lib/erlang/etf/extensions/false_class.rb +28 -0
  27. data/lib/erlang/etf/extensions/float.rb +20 -0
  28. data/lib/erlang/etf/extensions/hash.rb +32 -0
  29. data/lib/erlang/etf/extensions/integer.rb +48 -0
  30. data/lib/erlang/etf/extensions/nil_class.rb +29 -0
  31. data/lib/erlang/etf/extensions/object.rb +24 -0
  32. data/lib/erlang/etf/extensions/regexp.rb +34 -0
  33. data/lib/erlang/etf/extensions/string.rb +35 -0
  34. data/lib/erlang/etf/extensions/symbol.rb +45 -0
  35. data/lib/erlang/etf/extensions/time.rb +29 -0
  36. data/lib/erlang/etf/extensions/true_class.rb +28 -0
  37. data/lib/erlang/etf/float.rb +57 -0
  38. data/lib/erlang/etf/fun.rb +67 -0
  39. data/lib/erlang/etf/integer.rb +29 -0
  40. data/lib/erlang/etf/large_big.rb +53 -0
  41. data/lib/erlang/etf/large_tuple.rb +55 -0
  42. data/lib/erlang/etf/list.rb +50 -0
  43. data/lib/erlang/etf/new_float.rb +33 -0
  44. data/lib/erlang/etf/new_fun.rb +98 -0
  45. data/lib/erlang/etf/new_reference.rb +59 -0
  46. data/lib/erlang/etf/nil.rb +23 -0
  47. data/lib/erlang/etf/pid.rb +45 -0
  48. data/lib/erlang/etf/port.rb +34 -0
  49. data/lib/erlang/etf/reference.rb +41 -0
  50. data/lib/erlang/etf/small_atom.rb +48 -0
  51. data/lib/erlang/etf/small_atom_utf8.rb +44 -0
  52. data/lib/erlang/etf/small_big.rb +59 -0
  53. data/lib/erlang/etf/small_integer.rb +29 -0
  54. data/lib/erlang/etf/small_tuple.rb +56 -0
  55. data/lib/erlang/etf/string.rb +46 -0
  56. data/lib/erlang/etf/term.rb +101 -0
  57. data/lib/erlang/etf/terms.rb +105 -0
  58. data/lib/erlang/etf/version.rb +5 -0
  59. data/spec/erlang/etf/atom_spec.rb +90 -0
  60. data/spec/erlang/etf/atom_utf8_spec.rb +90 -0
  61. data/spec/erlang/etf/binary_spec.rb +90 -0
  62. data/spec/erlang/etf/bit_binary_spec.rb +99 -0
  63. data/spec/erlang/etf/export_spec.rb +58 -0
  64. data/spec/erlang/etf/extensions/array_spec.rb +40 -0
  65. data/spec/erlang/etf/extensions/big_decimal_spec.rb +26 -0
  66. data/spec/erlang/etf/extensions/erlang-export_spec.rb +32 -0
  67. data/spec/erlang/etf/extensions/erlang-list_spec.rb +76 -0
  68. data/spec/erlang/etf/extensions/erlang-nil_spec.rb +24 -0
  69. data/spec/erlang/etf/extensions/erlang-pid_spec.rb +33 -0
  70. data/spec/erlang/etf/extensions/erlang-string_spec.rb +26 -0
  71. data/spec/erlang/etf/extensions/erlang-tuple_spec.rb +56 -0
  72. data/spec/erlang/etf/extensions/false_class_spec.rb +29 -0
  73. data/spec/erlang/etf/extensions/float_spec.rb +24 -0
  74. data/spec/erlang/etf/extensions/hash_spec.rb +90 -0
  75. data/spec/erlang/etf/extensions/integer_spec.rb +259 -0
  76. data/spec/erlang/etf/extensions/nil_class_spec.rb +29 -0
  77. data/spec/erlang/etf/extensions/object_spec.rb +30 -0
  78. data/spec/erlang/etf/extensions/regexp_spec.rb +35 -0
  79. data/spec/erlang/etf/extensions/string_spec.rb +43 -0
  80. data/spec/erlang/etf/extensions/symbol_spec.rb +64 -0
  81. data/spec/erlang/etf/extensions/time_spec.rb +32 -0
  82. data/spec/erlang/etf/extensions/true_class_spec.rb +29 -0
  83. data/spec/erlang/etf/float_spec.rb +92 -0
  84. data/spec/erlang/etf/fun_spec.rb +132 -0
  85. data/spec/erlang/etf/integer_spec.rb +57 -0
  86. data/spec/erlang/etf/large_big_spec.rb +67 -0
  87. data/spec/erlang/etf/large_tuple_spec.rb +119 -0
  88. data/spec/erlang/etf/list_spec.rb +159 -0
  89. data/spec/erlang/etf/new_float_spec.rb +92 -0
  90. data/spec/erlang/etf/new_fun_spec.rb +146 -0
  91. data/spec/erlang/etf/new_reference_spec.rb +60 -0
  92. data/spec/erlang/etf/nil_spec.rb +50 -0
  93. data/spec/erlang/etf/pid_spec.rb +61 -0
  94. data/spec/erlang/etf/port_spec.rb +58 -0
  95. data/spec/erlang/etf/reference_spec.rb +58 -0
  96. data/spec/erlang/etf/small_atom_spec.rb +90 -0
  97. data/spec/erlang/etf/small_atom_utf8_spec.rb +90 -0
  98. data/spec/erlang/etf/small_big_spec.rb +67 -0
  99. data/spec/erlang/etf/small_integer_spec.rb +57 -0
  100. data/spec/erlang/etf/small_tuple_spec.rb +112 -0
  101. data/spec/erlang/etf/string_spec.rb +92 -0
  102. data/spec/erlang/etf/term_spec.rb +27 -0
  103. data/spec/erlang/etf/terms_spec.rb +23 -0
  104. data/spec/erlang/etf_spec.rb +23 -0
  105. data/spec/erlang_spec.rb +77 -0
  106. data/spec/spec_helper.rb +7 -0
  107. metadata +310 -0
@@ -0,0 +1,259 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Erlang::ETF::Extensions::Integer do
6
+ let(:klass) { ::Integer }
7
+
8
+ let(:small_integer_min) { Erlang::ETF::Extensions::Integer::UINT8_MIN }
9
+ let(:small_integer_max) { Erlang::ETF::Extensions::Integer::UINT8_MAX }
10
+ let(:integer_min) { Erlang::ETF::Extensions::Integer::INT32_MIN }
11
+ let(:integer_max) { Erlang::ETF::Extensions::Integer::INT32_MAX }
12
+ let(:small_big_min) { (-1 << (255 * 8)) + 1 }
13
+ let(:small_big_max) { (+1 << (255 * 8)) - 1 }
14
+
15
+ describe '[instance]' do
16
+ describe '__erlang_type__' do
17
+ subject { instance.__erlang_type__ }
18
+
19
+ context "when between #{Erlang::ETF::Extensions::Integer::UINT8_MIN} and #{Erlang::ETF::Extensions::Integer::UINT8_MAX}" do
20
+ let(:min) { small_integer_min }
21
+ let(:max) { small_integer_max }
22
+
23
+ let(:current) { :small_integer }
24
+ let(:positive) { :integer }
25
+ let(:negative) { :integer }
26
+
27
+ context 'minimum' do
28
+ let(:instance) { min }
29
+ it { should eq(current) }
30
+ end
31
+
32
+ context 'minimum + 1' do
33
+ let(:instance) { min + 1 }
34
+ it { should eq(current) }
35
+ end
36
+
37
+ context 'minimum - 1' do
38
+ let(:instance) { min - 1 }
39
+ it { should eq(negative) }
40
+ end
41
+
42
+ context 'maximum' do
43
+ let(:instance) { max }
44
+ it { should eq(current) }
45
+ end
46
+
47
+ context 'maximum + 1' do
48
+ let(:instance) { max + 1 }
49
+ it { should eq(positive) }
50
+ end
51
+
52
+ context 'maximum - 1' do
53
+ let(:instance) { max - 1 }
54
+ it { should eq(current) }
55
+ end
56
+ end
57
+
58
+ context "when between #{Erlang::ETF::Extensions::Integer::INT32_MIN} and #{Erlang::ETF::Extensions::Integer::INT32_MAX}" do
59
+ let(:min) { integer_min }
60
+ let(:max) { integer_max }
61
+
62
+ let(:current) { :integer }
63
+ let(:positive) { :small_big }
64
+ let(:negative) { :small_big }
65
+
66
+ context 'minimum' do
67
+ let(:instance) { min }
68
+ it { should eq(current) }
69
+ end
70
+
71
+ context 'minimum + 1' do
72
+ let(:instance) { min + 1 }
73
+ it { should eq(current) }
74
+ end
75
+
76
+ context 'minimum - 1' do
77
+ let(:instance) { min - 1 }
78
+ it { should eq(negative) }
79
+ end
80
+
81
+ context 'maximum' do
82
+ let(:instance) { max }
83
+ it { should eq(current) }
84
+ end
85
+
86
+ context 'maximum + 1' do
87
+ let(:instance) { max + 1 }
88
+ it { should eq(positive) }
89
+ end
90
+
91
+ context 'maximum - 1' do
92
+ let(:instance) { max - 1 }
93
+ it { should eq(current) }
94
+ end
95
+ end
96
+
97
+ context "when between ((-1 << (255 * 8)) + 1) and ((+1 << (255 * 8)) - 1)" do
98
+ let(:min) { small_big_min }
99
+ let(:max) { small_big_max }
100
+
101
+ let(:current) { :small_big }
102
+ let(:positive) { :large_big }
103
+ let(:negative) { :large_big }
104
+
105
+ context 'minimum' do
106
+ let(:instance) { min }
107
+ it { should eq(current) }
108
+ end
109
+
110
+ context 'minimum + 1' do
111
+ let(:instance) { min + 1 }
112
+ it { should eq(current) }
113
+ end
114
+
115
+ context 'minimum - 1' do
116
+ let(:instance) { min - 1 }
117
+ it { should eq(negative) }
118
+ end
119
+
120
+ context 'maximum' do
121
+ let(:instance) { max }
122
+ it { should eq(current) }
123
+ end
124
+
125
+ context 'maximum + 1' do
126
+ let(:instance) { max + 1 }
127
+ it { should eq(positive) }
128
+ end
129
+
130
+ context 'maximum - 1' do
131
+ let(:instance) { max - 1 }
132
+ it { should eq(current) }
133
+ end
134
+ end
135
+ end
136
+
137
+ describe '__erlang_evolve__' do
138
+ subject { instance.__erlang_evolve__ }
139
+
140
+ context "when >= #{Erlang::ETF::Extensions::Integer::UINT8_MIN} and <= #{Erlang::ETF::Extensions::Integer::UINT8_MAX}" do
141
+ let(:min) { small_integer_min }
142
+ let(:max) { small_integer_max }
143
+
144
+ let(:current) { Erlang::ETF::SmallInteger.new(instance) }
145
+ let(:positive) { Erlang::ETF::Integer.new(instance) }
146
+ let(:negative) { Erlang::ETF::Integer.new(instance) }
147
+
148
+ context 'minimum' do
149
+ let(:instance) { min }
150
+ it { should eq(current) }
151
+ end
152
+
153
+ context 'minimum + 1' do
154
+ let(:instance) { min + 1 }
155
+ it { should eq(current) }
156
+ end
157
+
158
+ context 'minimum - 1' do
159
+ let(:instance) { min - 1 }
160
+ it { should eq(negative) }
161
+ end
162
+
163
+ context 'maximum' do
164
+ let(:instance) { max }
165
+ it { should eq(current) }
166
+ end
167
+
168
+ context 'maximum + 1' do
169
+ let(:instance) { max + 1 }
170
+ it { should eq(positive) }
171
+ end
172
+
173
+ context 'maximum - 1' do
174
+ let(:instance) { max - 1 }
175
+ it { should eq(current) }
176
+ end
177
+ end
178
+
179
+ context "when between #{Erlang::ETF::Extensions::Integer::INT32_MIN} and #{Erlang::ETF::Extensions::Integer::INT32_MAX}" do
180
+ let(:min) { integer_min }
181
+ let(:max) { integer_max }
182
+
183
+ let(:current) { Erlang::ETF::Integer.new(instance) }
184
+ let(:positive) { Erlang::ETF::SmallBig.new(instance) }
185
+ let(:negative) { Erlang::ETF::SmallBig.new(instance) }
186
+
187
+ context 'minimum' do
188
+ let(:instance) { min }
189
+ it { should eq(current) }
190
+ end
191
+
192
+ context 'minimum + 1' do
193
+ let(:instance) { min + 1 }
194
+ it { should eq(current) }
195
+ end
196
+
197
+ context 'minimum - 1' do
198
+ let(:instance) { min - 1 }
199
+ it { should eq(negative) }
200
+ end
201
+
202
+ context 'maximum' do
203
+ let(:instance) { max }
204
+ it { should eq(current) }
205
+ end
206
+
207
+ context 'maximum + 1' do
208
+ let(:instance) { max + 1 }
209
+ it { should eq(positive) }
210
+ end
211
+
212
+ context 'maximum - 1' do
213
+ let(:instance) { max - 1 }
214
+ it { should eq(current) }
215
+ end
216
+ end
217
+
218
+ context "when between ((-1 << (255 * 8)) + 1) and ((+1 << (255 * 8)) - 1)" do
219
+ let(:min) { small_big_min }
220
+ let(:max) { small_big_max }
221
+
222
+ let(:current) { Erlang::ETF::SmallBig.new(instance) }
223
+ let(:positive) { Erlang::ETF::LargeBig.new(instance) }
224
+ let(:negative) { Erlang::ETF::LargeBig.new(instance) }
225
+
226
+ context 'minimum' do
227
+ let(:instance) { min }
228
+ it { should eq(current) }
229
+ end
230
+
231
+ context 'minimum + 1' do
232
+ let(:instance) { min + 1 }
233
+ it { should eq(current) }
234
+ end
235
+
236
+ context 'minimum - 1' do
237
+ let(:instance) { min - 1 }
238
+ it { should eq(negative) }
239
+ end
240
+
241
+ context 'maximum' do
242
+ let(:instance) { max }
243
+ it { should eq(current) }
244
+ end
245
+
246
+ context 'maximum + 1' do
247
+ let(:instance) { max + 1 }
248
+ it { should eq(positive) }
249
+ end
250
+
251
+ context 'maximum - 1' do
252
+ let(:instance) { max - 1 }
253
+ it { should eq(current) }
254
+ end
255
+ end
256
+ end
257
+ end
258
+
259
+ end
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Erlang::ETF::Extensions::NilClass do
6
+ let(:klass) { ::NilClass }
7
+
8
+ describe '[instance]' do
9
+ let(:instance) { nil }
10
+
11
+ describe '__erlang_type__' do
12
+ subject { instance.__erlang_type__ }
13
+
14
+ it { should eq(:bert_nil) }
15
+ end
16
+
17
+ describe '__erlang_evolve__' do
18
+ subject { instance.__erlang_evolve__ }
19
+
20
+ it { should eq(
21
+ Erlang::ETF::SmallTuple.new([
22
+ Erlang::ETF::SmallAtom.new("bert"),
23
+ Erlang::ETF::SmallAtom.new("nil")
24
+ ])
25
+ ) }
26
+ end
27
+ end
28
+
29
+ end
@@ -0,0 +1,30 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Erlang::ETF::Extensions::Object do
6
+ let(:klass) { ::Object }
7
+
8
+ describe '[instance]' do
9
+ let(:instance) { klass.new }
10
+
11
+ describe '__erlang_type__' do
12
+ subject { -> { instance.__erlang_type__ } }
13
+
14
+ it { should raise_error(NotImplementedError) }
15
+ end
16
+
17
+ describe '__erlang_evolve__' do
18
+ subject { -> { instance.__erlang_evolve__ } }
19
+
20
+ it { should raise_error(NotImplementedError) }
21
+ end
22
+
23
+ describe '__erlang_dump__' do
24
+ subject { -> { instance.__erlang_dump__("") } }
25
+
26
+ it { should raise_error(NotImplementedError) }
27
+ end
28
+ end
29
+
30
+ end
@@ -0,0 +1,35 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Erlang::ETF::Extensions::Regexp do
6
+ let(:klass) { ::Regexp }
7
+
8
+ describe '[instance]' do
9
+ let(:instance) { /./imx }
10
+
11
+ describe '__erlang_type__' do
12
+ subject { instance.__erlang_type__ }
13
+
14
+ it { should eq(:bert_regex) }
15
+ end
16
+
17
+ describe '__erlang_evolve__' do
18
+ subject { instance.__erlang_evolve__ }
19
+
20
+ it { should eq(
21
+ Erlang::ETF::SmallTuple.new([
22
+ Erlang::ETF::SmallAtom.new("bert"),
23
+ Erlang::ETF::SmallAtom.new("regex"),
24
+ Erlang::ETF::Binary.new("."),
25
+ Erlang::ETF::List.new([
26
+ Erlang::ETF::SmallAtom.new("caseless"),
27
+ Erlang::ETF::SmallAtom.new("extended"),
28
+ Erlang::ETF::SmallAtom.new("multiline")
29
+ ])
30
+ ])
31
+ ) }
32
+ end
33
+ end
34
+
35
+ end
@@ -0,0 +1,43 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Erlang::ETF::Extensions::String do
6
+ let(:klass) { ::String }
7
+
8
+ describe '[instance]' do
9
+ let(:instance) { "test" }
10
+
11
+ describe '__erlang_type__' do
12
+ subject { instance.__erlang_type__ }
13
+
14
+ it { should eq(:binary) }
15
+ end
16
+
17
+ describe '__erlang_evolve__' do
18
+ subject { instance.__erlang_evolve__ }
19
+
20
+ it { should eq(Erlang::ETF::Binary.new(instance)) }
21
+ end
22
+
23
+ describe 'to_utf8_binary' do
24
+ it "converts \"Ω\" to \"\\xCE\\xA9\"" do
25
+ expect("Ω".to_utf8_binary).to eq("\xCE\xA9".force_encoding('BINARY'))
26
+ end
27
+
28
+ it "handles EncodingError" do
29
+ string = "Ωomega"
30
+ string.should_receive(:encode).with(Erlang::ETF::Extensions::UTF8_ENCODING).and_raise(EncodingError)
31
+
32
+ expect(string.to_utf8_binary).to eq("\xCE\xA9omega".force_encoding('BINARY'))
33
+ end
34
+ end
35
+
36
+ describe 'from_utf8_binary' do
37
+ it "converts \"\\xCE\\xA9\" to \"Ω\"" do
38
+ expect("\xCE\xA9".force_encoding('BINARY').from_utf8_binary).to eq("Ω")
39
+ end
40
+ end
41
+ end
42
+
43
+ end
@@ -0,0 +1,64 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Erlang::ETF::Extensions::Symbol do
6
+ let(:klass) { ::Symbol }
7
+
8
+ describe '[instance]' do
9
+ describe '__erlang_type__' do
10
+ subject { instance.__erlang_type__ }
11
+
12
+ context 'when bytesize is < 256' do
13
+ let(:instance) { :a }
14
+ it { should eq(:small_atom) }
15
+
16
+ context 'when it contains UTF-8 characters' do
17
+ let(:instance) { :'Ω' }
18
+ it { should eq(:small_atom_utf8) }
19
+ end
20
+ end
21
+
22
+ context 'when bytesize is >= 256' do
23
+ let(:instance) { ("a" * 256).intern }
24
+ it { should eq(:atom) }
25
+
26
+ context 'when it contains UTF-8 characters' do
27
+ let(:instance) { ("Ω" * 256).intern }
28
+ it { should eq(:atom_utf8) }
29
+ end
30
+ end
31
+ end
32
+
33
+ describe '__erlang_evolve__' do
34
+ subject { instance.__erlang_evolve__ }
35
+
36
+ context 'when bytesize is < 256' do
37
+ let(:instance) { :a }
38
+ it { should eq(Erlang::ETF::SmallAtom.new(instance.to_s)) }
39
+
40
+ context 'when it contains UTF-8 characters' do
41
+ let(:instance) { :'Ω' }
42
+ it { should eq(Erlang::ETF::SmallAtomUTF8.new(instance.to_s)) }
43
+ end
44
+ end
45
+
46
+ context 'when bytesize is >= 256' do
47
+ let(:instance) { ("a" * 256).intern }
48
+ it { should eq(Erlang::ETF::Atom.new(instance.to_s)) }
49
+
50
+ context 'when it contains UTF-8 characters' do
51
+ let(:instance) { ("Ω" * 256).intern }
52
+ it { should eq(Erlang::ETF::AtomUTF8.new(instance.to_s)) }
53
+ end
54
+ end
55
+ end
56
+
57
+ describe 'to_utf8_binary' do
58
+ it "converts :Ω to \"\\xCE\\xA9\"" do
59
+ expect(:'Ω'.to_utf8_binary).to eq("\xCE\xA9".force_encoding('BINARY'))
60
+ end
61
+ end
62
+ end
63
+
64
+ end