edn_turbo 0.5.7 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/ext/edn_turbo/util.h CHANGED
@@ -1,39 +1,68 @@
1
+ // The MIT License (MIT)
2
+
3
+ // Copyright (c) 2015-2021 Ed Porras
4
+
5
+ // Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ // of this software and associated documentation files (the "Software"), to deal
7
+ // in the Software without restriction, including without limitation the rights
8
+ // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ // copies of the Software, and to permit persons to whom the Software is
10
+ // furnished to do so, subject to the following conditions:
11
+
12
+ // The above copyright notice and this permission notice shall be included in
13
+ // all copies or substantial portions of the Software.
14
+
15
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ // THE SOFTWARE.
22
+
1
23
  #pragma once
2
24
 
25
+ #include <cstddef>
26
+
3
27
  namespace edn
4
28
  {
5
- extern VALUE rb_mEDN;
6
- extern VALUE rb_mEDNT;
29
+ extern VALUE rb_mEDN;
30
+ extern VALUE rb_mEDNT;
31
+
32
+ extern VALUE EDN_MAKE_SYMBOL_METHOD;
33
+ extern VALUE EDN_MAKE_LIST_METHOD;
34
+ extern VALUE EDN_MAKE_SET_METHOD;
35
+ extern VALUE EDN_MAKE_BIG_DECIMAL_METHOD;
36
+ extern VALUE EDN_MAKE_RATIONAL_METHOD;
37
+ extern VALUE EDN_TAGGED_ELEM_METHOD;
38
+ extern VALUE EDN_EOF_CONST;
7
39
 
8
- extern VALUE EDN_MAKE_SYMBOL_METHOD;
9
- extern VALUE EDN_MAKE_LIST_METHOD;
10
- extern VALUE EDN_MAKE_SET_METHOD;
11
- extern VALUE EDN_MAKE_BIG_DECIMAL_METHOD;
12
- extern VALUE EDN_TAGGED_ELEM_METHOD;
13
- extern VALUE EDN_EOF_CONST;
40
+ extern VALUE EDNT_EXTENDED_VALUE_METHOD;
14
41
 
15
- extern VALUE EDNT_EXTENDED_VALUE_METHOD;
42
+ extern VALUE RUBY_STRING_TO_I_METHOD;
43
+ extern VALUE RUBY_STRING_TO_F_METHOD;
44
+ extern VALUE RUBY_READ_METHOD;
16
45
 
17
- extern VALUE RUBY_STRING_TO_I_METHOD;
18
- extern VALUE RUBY_STRING_TO_F_METHOD;
19
- extern VALUE RUBY_READ_METHOD;
46
+ extern VALUE RUBY_NAN_CONST;
47
+ extern VALUE RUBY_INF_CONST;
20
48
 
21
- namespace util
22
- {
23
- // defined in edn_parser_util.cc
24
- VALUE integer_to_ruby(const char* str, std::size_t len);
25
- VALUE float_to_ruby (const char* str, std::size_t len);
49
+ namespace util
50
+ {
51
+ // defined in edn_parser_util.cc
52
+ VALUE integer_to_ruby(const char* str, std::size_t len);
53
+ VALUE float_to_ruby (const char* str, std::size_t len);
54
+ VALUE ratio_to_ruby (const char* str, std::size_t len);
26
55
 
27
- VALUE ruby_io_read(VALUE io);
56
+ VALUE ruby_io_read(VALUE io);
28
57
 
29
- bool parse_byte_stream (const char *p, const char *pe, VALUE& rslt, bool encode);
30
- bool parse_escaped_char(const char *p, const char *pe, VALUE& rslt);
58
+ bool parse_byte_stream (const char *p, const char *pe, VALUE& rslt, bool encode);
59
+ bool parse_escaped_char(const char *p, const char *pe, VALUE& rslt);
31
60
 
32
- VALUE call_module_fn(VALUE module, ID method);
33
- VALUE call_module_fn(VALUE module, ID method, VALUE value);
34
- VALUE call_module_fn(VALUE module, ID method, VALUE value1, VALUE value2);
61
+ VALUE call_module_fn(VALUE module, ID method);
62
+ VALUE call_module_fn(VALUE module, ID method, VALUE value);
63
+ VALUE call_module_fn(VALUE module, ID method, VALUE value1, VALUE value2);
35
64
 
36
- // edn_parser_util_unicode.cc
37
- bool to_utf8(const char *s, uint32_t len, std::string& rslt);
38
- }
65
+ // edn_parser_util_unicode.cc
66
+ bool to_utf8(const char *s, uint32_t len, std::string& rslt);
67
+ }
39
68
  }
@@ -1,3 +1,25 @@
1
+ // The MIT License (MIT)
2
+
3
+ // Copyright (c) 2015-2021 Ed Porras
4
+
5
+ // Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ // of this software and associated documentation files (the "Software"), to deal
7
+ // in the Software without restriction, including without limitation the rights
8
+ // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ // copies of the Software, and to permit persons to whom the Software is
10
+ // furnished to do so, subject to the following conditions:
11
+
12
+ // The above copyright notice and this permission notice shall be included in
13
+ // all copies or substantial portions of the Software.
14
+
15
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ // THE SOFTWARE.
22
+
1
23
  #include <string>
2
24
 
3
25
  //
@@ -13,24 +35,24 @@
13
35
 
14
36
  namespace edn
15
37
  {
16
- namespace util
17
- {
18
- namespace unicode
19
- {
20
- //
21
- // unescapes any values that need to be replaced, saves it to utf8
22
- //
23
- bool to_utf8(const char *s, uint32_t len, std::string& rslt)
24
- {
25
- icu::UnicodeString ustr(s, len);
26
-
27
- if (ustr.isBogus()) {
28
- return false;
29
- }
30
-
31
- ustr.unescape().toUTF8String(rslt);
32
- return true;
38
+ namespace util
39
+ {
40
+ namespace unicode
41
+ {
42
+ //
43
+ // unescapes any values that need to be replaced, saves it to utf8
44
+ //
45
+ bool to_utf8(const char *s, uint32_t len, std::string& rslt)
46
+ {
47
+ icu::UnicodeString ustr(s, len);
48
+
49
+ if (ustr.isBogus()) {
50
+ return false;
33
51
  }
34
- }
35
- }
52
+
53
+ ustr.unescape().toUTF8String(rslt);
54
+ return true;
55
+ }
56
+ }
57
+ }
36
58
  }
@@ -1,14 +1,36 @@
1
+ // The MIT License (MIT)
2
+
3
+ // Copyright (c) 2015-2021 Ed Porras
4
+
5
+ // Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ // of this software and associated documentation files (the "Software"), to deal
7
+ // in the Software without restriction, including without limitation the rights
8
+ // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ // copies of the Software, and to permit persons to whom the Software is
10
+ // furnished to do so, subject to the following conditions:
11
+
12
+ // The above copyright notice and this permission notice shall be included in
13
+ // all copies or substantial portions of the Software.
14
+
15
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ // THE SOFTWARE.
22
+
1
23
  #pragma once
2
24
 
3
25
  #include <string>
4
26
 
5
27
  namespace edn
6
28
  {
7
- namespace util
8
- {
9
- namespace unicode
10
- {
11
- bool to_utf8(const char *s, uint32_t len, std::string& rslt);
12
- }
13
- }
29
+ namespace util
30
+ {
31
+ namespace unicode
32
+ {
33
+ bool to_utf8(const char *s, uint32_t len, std::string& rslt);
34
+ }
35
+ }
14
36
  }
data/lib/edn_turbo.rb CHANGED
@@ -1,5 +1,27 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # The MIT License (MIT)
4
+ #
5
+ # Copyright (c) 2015-2021 Ed Porras
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ # of this software and associated documentation files (the "Software"), to deal
9
+ # in the Software without restriction, including without limitation the rights
10
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ # copies of the Software, and to permit persons to whom the Software is
12
+ # furnished to do so, subject to the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included in
15
+ # all copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
+ # THE SOFTWARE.
24
+
3
25
  require 'edn'
4
26
  require 'edn_turbo/version'
5
27
  require 'edn_turbo/edn_parser'
@@ -8,4 +30,16 @@ require 'edn_turbo/edn_turbo'
8
30
  # Replace the parser in the EDN module with the C based one.
9
31
  module EDN
10
32
  self.parser = EDNT::Parser
33
+
34
+ # makes a rational type for converting a clojure ratio
35
+ # - this should be in edn-ruby
36
+ def self.rational(value)
37
+ Rational(value)
38
+ end
39
+
40
+ # edn-ruby uses BigDecimal.new() which breaks in ruby >= 2.7.0 so we
41
+ # use this instead
42
+ def self.big_decimal_edn_turbo(value)
43
+ BigDecimal(value)
44
+ end
11
45
  end
@@ -1,5 +1,27 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # The MIT License (MIT)
4
+ #
5
+ # Copyright (c) 2015-2021 Ed Porras
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ # of this software and associated documentation files (the "Software"), to deal
9
+ # in the Software without restriction, including without limitation the rights
10
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ # copies of the Software, and to permit persons to whom the Software is
12
+ # furnished to do so, subject to the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included in
15
+ # all copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
+ # THE SOFTWARE.
24
+
3
25
  #
4
26
  # methods specific to edn_turbo
5
27
  module EDNT
@@ -1,6 +1,28 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # The MIT License (MIT)
4
+ #
5
+ # Copyright (c) 2015-2021 Ed Porras
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ # of this software and associated documentation files (the "Software"), to deal
9
+ # in the Software without restriction, including without limitation the rights
10
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ # copies of the Software, and to permit persons to whom the Software is
12
+ # furnished to do so, subject to the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included in
15
+ # all copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
+ # THE SOFTWARE.
24
+
3
25
  module EDNT
4
- VERSION = '0.5.7'
5
- RELEASE_DATE = '2019-01-30'
26
+ VERSION = '0.7.1'
27
+ RELEASE_DATE = '2021-02-07'
6
28
  end
@@ -0,0 +1,419 @@
1
+ require 'edn_turbo'
2
+ require 'pry-byebug'
3
+
4
+ module EDNT
5
+ RSpec.describe Parser do
6
+ let(:subject) { described_class.new }
7
+
8
+ def fixture_path(*fixture_filename)
9
+ File.join(__dir__, '../fixtures', fixture_filename)
10
+ end
11
+
12
+ def fixture(*fixture_filename)
13
+ File.read(fixture_path(fixture_filename))
14
+ end
15
+
16
+ context 'setting input' do
17
+ it 'File' do
18
+ io = File.open(fixture_path('true.edn'))
19
+ expect(io.class).to eq(File)
20
+ subject.set_input(io)
21
+ expect(subject.read).to eq(true)
22
+ end
23
+ it 'String' do
24
+ io = File.read(fixture_path('true.edn'))
25
+ expect(io.class).to eq(String)
26
+ subject.set_input(io)
27
+ expect(subject.read).to eq(true)
28
+ end
29
+ it 'IO that responds to read' do
30
+ io = StringIO.new(fixture('true.edn'))
31
+ expect(io.respond_to?('read')).to be_truthy
32
+ subject.set_input(io)
33
+ io.rewind
34
+ expect(subject.read).to eq(true)
35
+ end
36
+ it 'raises ArgumentError if IO input does not respond to read()' do
37
+ expect { subject.set_input(4) }.to raise_error(ArgumentError)
38
+ end
39
+ end
40
+
41
+ context 'comments' do
42
+ it 'semicolon' do
43
+ data = <<-EDN
44
+ ; ignore me
45
+ 23 ; ignore me too
46
+ EDN
47
+ expect(subject.parse(data)).to eq(23)
48
+ end
49
+ end
50
+
51
+ context 'built-in keywords' do
52
+ it 'true' do
53
+ expect(subject.parse('true')).to eq(true)
54
+ expect(subject.parse(' true ')).to eq(true)
55
+ end
56
+ it 'false' do
57
+ expect(subject.parse('false')).to eq(false)
58
+ expect(subject.parse(' false ')).to eq(false)
59
+ end
60
+ it 'nil' do
61
+ expect(subject.parse('nil')).to eq(nil)
62
+ expect(subject.parse(' nil')).to eq(nil)
63
+ end
64
+ end
65
+
66
+ context 'escaped character keywords' do
67
+ it 'formfeed' do
68
+ expect(subject.parse('\formfeed')).to eq("\f")
69
+ end
70
+ it 'space' do
71
+ expect(subject.parse('\space')).to eq(' ')
72
+ end
73
+ it 'newline' do
74
+ expect(subject.parse('\newline')).to eq("\n")
75
+ end
76
+ it 'tab' do
77
+ expect(subject.parse('\tab')).to eq("\t")
78
+ end
79
+ it 'return' do
80
+ expect(subject.parse('\return')).to eq("\r")
81
+ end
82
+ it 'backspace' do
83
+ expect(subject.parse('\backspace')).to eq("\b")
84
+ end
85
+ # it 'verticaltab' do
86
+ # expect(subject.parse('\verticaltab')).to eq("\v")
87
+ # end
88
+ end
89
+
90
+ context 'strings' do
91
+ it 'simple' do
92
+ expect(subject.parse(' "abc" ')).to eq('abc')
93
+ end
94
+ it 'with unicode' do
95
+ test_file = fixture('string.edn')
96
+ expect(subject.parse(test_file)).to eq("abc\"➪舦")
97
+ end
98
+ it 'with unicode in ascii range' do
99
+ test_file = fixture('unicode.edn')
100
+ expect(subject.parse(test_file)).to eq("Page \u0018,")
101
+ end
102
+ it 'with escaped characters' do
103
+ test_file = fixture('escaped_string.edn')
104
+ expect(subject.parse(test_file)).to eq("this\tis\\only\ta\ttest\rof\"various\nescaped\\values")
105
+ end
106
+ end
107
+
108
+ context 'keywords' do
109
+ it 'basic' do
110
+ expect(subject.parse(' :key1 ')).to eq(:key1)
111
+ end
112
+ it 'with underscore' do
113
+ expect(subject.parse(' :key_3 ')).to eq(:key_3)
114
+ end
115
+ it 'with dash' do
116
+ expect(subject.parse(' :key-4 ')).to eq(:'key-4')
117
+ end
118
+ it 'with namespace' do
119
+ expect(subject.parse(':key_2/adsd2')).to eq(:"key_2/adsd2")
120
+ end
121
+ it 'with long namespace' do
122
+ expect(subject.parse(' :namespace.of.some_length/keyword-name ')).to eq(:"namespace.of.some_length/keyword-name")
123
+ end
124
+ it 'with #' do
125
+ expect(subject.parse(' :#/:a ')).to eq(:'#/:a')
126
+ end
127
+ end
128
+
129
+ context 'numeric' do
130
+ it 'zero' do
131
+ expect(subject.parse(' 0 ')).to eq(0)
132
+ end
133
+ it 'neg zero' do
134
+ expect(subject.parse(' -0 ')).to eq(-0)
135
+ end
136
+ it 'integer' do
137
+ expect(subject.parse(' 5 ')).to eq(5)
138
+ end
139
+ it 'positive integer' do
140
+ expect(subject.parse(' +5 ')).to eq(5)
141
+ end
142
+ it 'negative integer' do
143
+ expect(subject.parse(' -98798 ')).to eq(-98_798)
144
+ end
145
+ it 'decimal' do
146
+ expect(subject.parse(' 231.312 ')).to eq(231.312)
147
+ end
148
+ it 'positive decimal' do
149
+ expect(subject.parse(' +231.312 ')).to eq(231.312)
150
+ end
151
+ it 'negative decimal' do
152
+ expect(subject.parse(' -2321.0 ')).to eq(-2321.0)
153
+ end
154
+ it 'scientific notation with E' do
155
+ val = subject.parse('2.74877906944E11')
156
+ expect(val).to eq(274_877_906_944.0)
157
+ expect(val.class).to eq(Float)
158
+ end
159
+ it 'scientific notation with e' do
160
+ val = subject.parse('2.74877906944e11')
161
+ expect(val).to eq(274_877_906_944.0)
162
+ expect(val.class).to eq(Float)
163
+ end
164
+ it 'scientific notation with E-' do
165
+ val = subject.parse('1.35E-12')
166
+ expect(val).to eq(1.35e-12)
167
+ expect(val.class).to eq(Float)
168
+ end
169
+ it 'scientific notation with e-' do
170
+ val = subject.parse('1.35e-12')
171
+ expect(val).to eq(1.35e-12)
172
+ expect(val.class).to eq(Float)
173
+ end
174
+ it 'arbitrary precision integer' do
175
+ val = subject.parse(' 432N ')
176
+ expect(val).to eq(432)
177
+ expect(val.class).to eq(Integer)
178
+ end
179
+ it 'exact precision integer' do
180
+ val = subject.parse(' 12M ')
181
+ expect(val).to eq(12)
182
+ expect(val.class).to eq(Integer)
183
+ end
184
+ it 'exact precision decimal' do
185
+ val = subject.parse('3.0M')
186
+ expect(val).to eq(3.0)
187
+ expect(val.class).to eq(BigDecimal)
188
+ end
189
+ it 'exact precision positive decimal with exp' do
190
+ val = subject.parse('+3.0e44M')
191
+ expect(val).to eq(3e44)
192
+ expect(val.class).to eq(BigDecimal)
193
+ end
194
+ it 'exact precision negative decimal with exp' do
195
+ val = subject.parse('-3.0e44M')
196
+ expect(val).to eq(-3e44)
197
+ expect(val.class).to eq(BigDecimal)
198
+ end
199
+ it 'exact precision decimal with negative exp' do
200
+ val = subject.parse('3.0e-44M')
201
+ expect(val).to eq(3e-44)
202
+ expect(val.class).to eq(BigDecimal)
203
+ end
204
+ it 'ratio' do
205
+ val = subject.parse(' 2/3 ')
206
+ expect(val).to eq(Rational(2, 3))
207
+ end
208
+ it 'positive ratio' do
209
+ val = subject.parse(' +2/3 ')
210
+ expect(val).to eq(Rational(2, 3))
211
+ end
212
+ it 'negative ratio' do
213
+ val = subject.parse(' -2/3 ')
214
+ expect(val).to eq(Rational(-2, 3))
215
+ end
216
+ end
217
+
218
+ context 'operators' do
219
+ it '/' do
220
+ expect(subject.parse('/')).to eq(EDN::Type::Symbol.new('/'))
221
+ end
222
+ it '.' do
223
+ expect(subject.parse('.')).to eq(EDN::Type::Symbol.new('.'))
224
+ end
225
+ it '*' do
226
+ expect(subject.parse('*')).to eq(EDN::Type::Symbol.new('*'))
227
+ end
228
+ it '!' do
229
+ expect(subject.parse('!')).to eq(EDN::Type::Symbol.new('!'))
230
+ end
231
+ it '_' do
232
+ expect(subject.parse('_')).to eq(EDN::Type::Symbol.new('_'))
233
+ end
234
+ it '?' do
235
+ expect(subject.parse('?')).to eq(EDN::Type::Symbol.new('?'))
236
+ end
237
+ it '$' do
238
+ expect(subject.parse('$')).to eq(EDN::Type::Symbol.new('$'))
239
+ end
240
+ it '%' do
241
+ expect(subject.parse('%')).to eq(EDN::Type::Symbol.new('%'))
242
+ end
243
+ it '>' do
244
+ expect(subject.parse('>')).to eq(EDN::Type::Symbol.new('>'))
245
+ end
246
+ it '<' do
247
+ expect(subject.parse('<')).to eq(EDN::Type::Symbol.new('<'))
248
+ end
249
+ it '&' do
250
+ expect(subject.parse('&')).to eq(EDN::Type::Symbol.new('&'))
251
+ end
252
+ it '=' do
253
+ expect(subject.parse('=')).to eq(EDN::Type::Symbol.new('='))
254
+ end
255
+ it '-' do
256
+ expect(subject.parse('-')).to eq(EDN::Type::Symbol.new('-'))
257
+ end
258
+ it '+' do
259
+ expect(subject.parse('+')).to eq(EDN::Type::Symbol.new('+'))
260
+ end
261
+ end
262
+
263
+ context 'built-in tagged' do
264
+ it 'rfc3339 dates' do
265
+ test = [
266
+ ' #inst "1985-04-12T23:20:50.52Z" ',
267
+ ' #inst "1996-12-19T16:39:57-08:00" ',
268
+ ' #inst "1990-12-31T23:59:60Z" ',
269
+ ' #inst "1990-12-31T15:59:60-08:00" ',
270
+ ' #inst "1937-01-01T12:00:27.87+00:20" '
271
+ ]
272
+
273
+ expected = [
274
+ DateTime.rfc3339('1985-04-12T23:20:50.52Z'),
275
+ DateTime.rfc3339('1996-12-19T16:39:57-08:00'),
276
+ DateTime.rfc3339('1990-12-31T23:59:60Z'),
277
+ DateTime.rfc3339('1990-12-31T15:59:60-08:00'),
278
+ DateTime.rfc3339('1937-01-01T12:00:27.87+00:20')
279
+ ]
280
+
281
+ test.each_with_index do |str, i|
282
+ expect(subject.parse(str)).to eq(expected[i])
283
+ end
284
+ end
285
+
286
+ it 'uuid' do
287
+ expect(subject.parse(' #uuid "f81d4fae-7dec-11d0-a765-00a0c91e6bf6" ')).to eq('f81d4fae-7dec-11d0-a765-00a0c91e6bf6')
288
+ end
289
+ end
290
+
291
+ context 'collections' do
292
+ it 'empty set' do
293
+ expect(subject.parse(' #{} ')).to eq(Set.new)
294
+ end
295
+ it 'single-element set' do
296
+ expect(subject.parse(' #{ 1 } ')).to eq(Set.new([1]))
297
+ end
298
+ it 'multi-element set' do
299
+ expect(subject.parse(' #{ 1 "abc" } ')).to eq(Set.new([1, 'abc']))
300
+ end
301
+ it 'nested set' do
302
+ expect(subject.parse(' #{1 #{:abc}}')).to eq(Set.new([1, Set.new([:abc])]))
303
+ end
304
+ it 'empty vector' do
305
+ expect(subject.parse('[]')).to eq([])
306
+ end
307
+ it 'nested vectors' do
308
+ expect(subject.parse('[1 2 [3 4]]')).to eq([1, 2, [3, 4]])
309
+ end
310
+ it 'vector with other collection' do
311
+ expect(subject.parse('[1 2 #{3}]')).to eq([1, 2, Set.new([3])])
312
+ end
313
+ it 'empty list' do
314
+ expect(subject.parse('()')).to eq([])
315
+ end
316
+ it 'nested lists' do
317
+ expect(subject.parse('(1 2 (3 4))')).to eq([1, 2, [3, 4]])
318
+ end
319
+ it 'list with other collection' do
320
+ expect(subject.parse('(1 2 [3 4 #{5}])')).to eq([1, 2, [3, 4, Set.new([5])]])
321
+ end
322
+ it 'empty map' do
323
+ expect(subject.parse('{}')).to eq({})
324
+ end
325
+ it 'nested map' do
326
+ expect(subject.parse('{:a 1 :b {:c 2}}')).to eq(a: 1, b: { c: 2 })
327
+ end
328
+ it 'map with other collection' do
329
+ expect(subject.parse('{:a #{1}}')).to eq(a: Set.new([1]))
330
+ end
331
+ end
332
+
333
+ context 'symbols' do
334
+ it 'simple' do
335
+ expect(subject.parse('asymbol')).to eq(EDN::Type::Symbol.new('asymbol'))
336
+ end
337
+ it 'with a leading period' do
338
+ expect(subject.parse('.asymbol')).to eq(EDN::Type::Symbol.new('.asymbol'))
339
+ end
340
+ it 'with single quotes' do
341
+ expect(subject.parse("with'_a_'")).to eq(EDN::Type::Symbol.new("with'_a_'"))
342
+ end
343
+ it 'with a period in the middle' do
344
+ expect(subject.parse('with.123')).to eq(EDN::Type::Symbol.new('with.123'))
345
+ end
346
+ it 'with a leading dash' do
347
+ expect(subject.parse('-with.123')).to eq(EDN::Type::Symbol.new('-with.123'))
348
+ end
349
+ it 'slash' do
350
+ expect(subject.parse('/')).to eq(EDN::Type::Symbol.new('/'))
351
+ end
352
+ it 'with mathematical operators' do
353
+ expect(subject.parse('>:FOuy/+')).to eq(EDN::Type::Symbol.new('>:FOuy/+'))
354
+ end
355
+ it 'NaN' do
356
+ expect(subject.parse('##NaN').to_f.nan?).to be_truthy
357
+ end
358
+ it 'infinity' do
359
+ expect(subject.parse('##Inf')).to eq(Float::INFINITY)
360
+ end
361
+ it 'negative infinity' do
362
+ expect(subject.parse('##-Inf')).to eq(EDN::Type::Symbol.new('-Inf'))
363
+ end
364
+ end
365
+
366
+ context 'metadata' do
367
+ it 'array with meta' do
368
+ rslt = subject.parse('^{:doc "This is my vector" :rel :temps} [98.6 99.7]')
369
+ expect(rslt.metadata).to eq(doc: 'This is my vector', rel: :temps)
370
+ end
371
+ it 'object tags' do
372
+ rslt = subject.parse('^String ^:foo ^{:foo false :tag Boolean :bar 2} [1 2]')
373
+ expect(rslt.metadata).to eq(foo: true, tag: EDN::Type::Symbol.new('String'), bar: 2)
374
+ end
375
+ it 'meta in vector' do
376
+ rslt = subject.parse('[ [ ^{:a :b} c, :d, true ], #inst "1390-09-07T21:27:03+00:00" ]')
377
+ expect(rslt).to eq([[EDN::Type::Symbol.new('c'), :d, true],
378
+ DateTime.rfc3339('1390-09-07T21:27:03+00:00')])
379
+ expect(rslt.first.first.metadata).to eq(a: :b)
380
+ end
381
+ end
382
+
383
+ context 'tagged element' do
384
+ class Tagged
385
+ def initialize(data)
386
+ @item = data[:item]
387
+ @other = data[:other]
388
+ end
389
+
390
+ def to_s
391
+ [@item, @other]
392
+ end
393
+ end
394
+
395
+ it 'with user-defined type' do
396
+ # register the tag
397
+ EDN.register('edn_turbo/test_tagged') { |data| Tagged.new(data).to_s }
398
+
399
+ # test
400
+ expect(subject.parse('#edn_turbo/test_tagged { :item 345 :other :a }')).to eq([345, :a])
401
+ end
402
+ end
403
+
404
+ context 'discard' do
405
+ it 'single discard' do
406
+ expect(subject.parse('#_ :foo')).to eq(EDN::EOF)
407
+ end
408
+ it 'in vector' do
409
+ expect(subject.parse('[:a :b #_ :foo 42 [ :c #_dd]]')).to eq([:a, :b, 42, [:c]])
410
+ end
411
+ it 'at beginning of line' do
412
+ expect(subject.parse('#_ > 456')).to eq(456)
413
+ end
414
+ it 'collection' do
415
+ expect(subject.parse('[ 1 2 3 4 #_ { :a 1 } ]')).to eq([1, 2, 3, 4])
416
+ end
417
+ end
418
+ end
419
+ end