json 2.5.1 → 2.6.0
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.
- checksums.yaml +4 -4
- data/CHANGES.md +5 -5
- data/README.md +3 -3
- data/VERSION +1 -1
- data/ext/json/ext/generator/generator.c +1 -11
- data/ext/json/ext/parser/extconf.rb +1 -0
- data/ext/json/ext/parser/parser.c +2972 -1813
- data/ext/json/ext/parser/parser.h +5 -1
- data/ext/json/ext/parser/parser.rl +66 -28
- data/json.gemspec +2 -8
- data/lib/json/pure/parser.rb +1 -1
- data/lib/json/version.rb +1 -1
- data/lib/json.rb +1 -1
- metadata +6 -87
- data/Gemfile +0 -14
- data/lib/json/ext/.keep +0 -0
- data/tests/fixtures/fail10.json +0 -1
- data/tests/fixtures/fail11.json +0 -1
- data/tests/fixtures/fail12.json +0 -1
- data/tests/fixtures/fail13.json +0 -1
- data/tests/fixtures/fail14.json +0 -1
- data/tests/fixtures/fail18.json +0 -1
- data/tests/fixtures/fail19.json +0 -1
- data/tests/fixtures/fail2.json +0 -1
- data/tests/fixtures/fail20.json +0 -1
- data/tests/fixtures/fail21.json +0 -1
- data/tests/fixtures/fail22.json +0 -1
- data/tests/fixtures/fail23.json +0 -1
- data/tests/fixtures/fail24.json +0 -1
- data/tests/fixtures/fail25.json +0 -1
- data/tests/fixtures/fail27.json +0 -2
- data/tests/fixtures/fail28.json +0 -2
- data/tests/fixtures/fail29.json +0 -1
- data/tests/fixtures/fail3.json +0 -1
- data/tests/fixtures/fail30.json +0 -1
- data/tests/fixtures/fail31.json +0 -1
- data/tests/fixtures/fail32.json +0 -1
- data/tests/fixtures/fail4.json +0 -1
- data/tests/fixtures/fail5.json +0 -1
- data/tests/fixtures/fail6.json +0 -1
- data/tests/fixtures/fail7.json +0 -1
- data/tests/fixtures/fail8.json +0 -1
- data/tests/fixtures/fail9.json +0 -1
- data/tests/fixtures/obsolete_fail1.json +0 -1
- data/tests/fixtures/pass1.json +0 -56
- data/tests/fixtures/pass15.json +0 -1
- data/tests/fixtures/pass16.json +0 -1
- data/tests/fixtures/pass17.json +0 -1
- data/tests/fixtures/pass2.json +0 -1
- data/tests/fixtures/pass26.json +0 -1
- data/tests/fixtures/pass3.json +0 -6
- data/tests/json_addition_test.rb +0 -199
- data/tests/json_common_interface_test.rb +0 -169
- data/tests/json_encoding_test.rb +0 -107
- data/tests/json_ext_parser_test.rb +0 -15
- data/tests/json_fixtures_test.rb +0 -40
- data/tests/json_generator_test.rb +0 -399
- data/tests/json_generic_object_test.rb +0 -82
- data/tests/json_parser_test.rb +0 -497
- data/tests/json_string_matching_test.rb +0 -38
- data/tests/lib/core_assertions.rb +0 -763
- data/tests/lib/envutil.rb +0 -365
- data/tests/lib/find_executable.rb +0 -22
- data/tests/lib/helper.rb +0 -4
- data/tests/ractor_test.rb +0 -30
- data/tests/test_helper.rb +0 -17
data/tests/json_addition_test.rb
DELETED
@@ -1,199 +0,0 @@
|
|
1
|
-
#frozen_string_literal: false
|
2
|
-
require 'test_helper'
|
3
|
-
require 'json/add/core'
|
4
|
-
require 'json/add/complex'
|
5
|
-
require 'json/add/rational'
|
6
|
-
require 'json/add/bigdecimal'
|
7
|
-
require 'json/add/ostruct'
|
8
|
-
require 'json/add/set'
|
9
|
-
require 'date'
|
10
|
-
|
11
|
-
class JSONAdditionTest < Test::Unit::TestCase
|
12
|
-
include JSON
|
13
|
-
|
14
|
-
class A
|
15
|
-
def initialize(a)
|
16
|
-
@a = a
|
17
|
-
end
|
18
|
-
|
19
|
-
attr_reader :a
|
20
|
-
|
21
|
-
def ==(other)
|
22
|
-
a == other.a
|
23
|
-
end
|
24
|
-
|
25
|
-
def self.json_create(object)
|
26
|
-
new(*object['args'])
|
27
|
-
end
|
28
|
-
|
29
|
-
def to_json(*args)
|
30
|
-
{
|
31
|
-
'json_class' => self.class.name,
|
32
|
-
'args' => [ @a ],
|
33
|
-
}.to_json(*args)
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
class A2 < A
|
38
|
-
def to_json(*args)
|
39
|
-
{
|
40
|
-
'json_class' => self.class.name,
|
41
|
-
'args' => [ @a ],
|
42
|
-
}.to_json(*args)
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
class B
|
47
|
-
def self.json_creatable?
|
48
|
-
false
|
49
|
-
end
|
50
|
-
|
51
|
-
def to_json(*args)
|
52
|
-
{
|
53
|
-
'json_class' => self.class.name,
|
54
|
-
}.to_json(*args)
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
class C
|
59
|
-
def self.json_creatable?
|
60
|
-
false
|
61
|
-
end
|
62
|
-
|
63
|
-
def to_json(*args)
|
64
|
-
{
|
65
|
-
'json_class' => 'JSONAdditionTest::Nix',
|
66
|
-
}.to_json(*args)
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
def test_extended_json
|
71
|
-
a = A.new(666)
|
72
|
-
assert A.json_creatable?
|
73
|
-
json = generate(a)
|
74
|
-
a_again = parse(json, :create_additions => true)
|
75
|
-
assert_kind_of a.class, a_again
|
76
|
-
assert_equal a, a_again
|
77
|
-
end
|
78
|
-
|
79
|
-
def test_extended_json_default
|
80
|
-
a = A.new(666)
|
81
|
-
assert A.json_creatable?
|
82
|
-
json = generate(a)
|
83
|
-
a_hash = parse(json)
|
84
|
-
assert_kind_of Hash, a_hash
|
85
|
-
end
|
86
|
-
|
87
|
-
def test_extended_json_disabled
|
88
|
-
a = A.new(666)
|
89
|
-
assert A.json_creatable?
|
90
|
-
json = generate(a)
|
91
|
-
a_again = parse(json, :create_additions => true)
|
92
|
-
assert_kind_of a.class, a_again
|
93
|
-
assert_equal a, a_again
|
94
|
-
a_hash = parse(json, :create_additions => false)
|
95
|
-
assert_kind_of Hash, a_hash
|
96
|
-
assert_equal(
|
97
|
-
{"args"=>[666], "json_class"=>"JSONAdditionTest::A"}.sort_by { |k,| k },
|
98
|
-
a_hash.sort_by { |k,| k }
|
99
|
-
)
|
100
|
-
end
|
101
|
-
|
102
|
-
def test_extended_json_fail1
|
103
|
-
b = B.new
|
104
|
-
assert !B.json_creatable?
|
105
|
-
json = generate(b)
|
106
|
-
assert_equal({ "json_class"=>"JSONAdditionTest::B" }, parse(json))
|
107
|
-
end
|
108
|
-
|
109
|
-
def test_extended_json_fail2
|
110
|
-
c = C.new
|
111
|
-
assert !C.json_creatable?
|
112
|
-
json = generate(c)
|
113
|
-
assert_raise(ArgumentError, NameError) { parse(json, :create_additions => true) }
|
114
|
-
end
|
115
|
-
|
116
|
-
def test_raw_strings
|
117
|
-
raw = ''
|
118
|
-
raw.respond_to?(:encode!) and raw.encode!(Encoding::ASCII_8BIT)
|
119
|
-
raw_array = []
|
120
|
-
for i in 0..255
|
121
|
-
raw << i
|
122
|
-
raw_array << i
|
123
|
-
end
|
124
|
-
json = raw.to_json_raw
|
125
|
-
json_raw_object = raw.to_json_raw_object
|
126
|
-
hash = { 'json_class' => 'String', 'raw'=> raw_array }
|
127
|
-
assert_equal hash, json_raw_object
|
128
|
-
assert_match(/\A\{.*\}\z/, json)
|
129
|
-
assert_match(/"json_class":"String"/, json)
|
130
|
-
assert_match(/"raw":\[0,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,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255\]/, json)
|
131
|
-
raw_again = parse(json, :create_additions => true)
|
132
|
-
assert_equal raw, raw_again
|
133
|
-
end
|
134
|
-
|
135
|
-
MyJsonStruct = Struct.new 'MyJsonStruct', :foo, :bar
|
136
|
-
|
137
|
-
def test_core
|
138
|
-
t = Time.now
|
139
|
-
assert_equal t, JSON(JSON(t), :create_additions => true)
|
140
|
-
d = Date.today
|
141
|
-
assert_equal d, JSON(JSON(d), :create_additions => true)
|
142
|
-
d = DateTime.civil(2007, 6, 14, 14, 57, 10, Rational(1, 12), 2299161)
|
143
|
-
assert_equal d, JSON(JSON(d), :create_additions => true)
|
144
|
-
assert_equal 1..10, JSON(JSON(1..10), :create_additions => true)
|
145
|
-
assert_equal 1...10, JSON(JSON(1...10), :create_additions => true)
|
146
|
-
assert_equal "a".."c", JSON(JSON("a".."c"), :create_additions => true)
|
147
|
-
assert_equal "a"..."c", JSON(JSON("a"..."c"), :create_additions => true)
|
148
|
-
s = MyJsonStruct.new 4711, 'foot'
|
149
|
-
assert_equal s, JSON(JSON(s), :create_additions => true)
|
150
|
-
struct = Struct.new :foo, :bar
|
151
|
-
s = struct.new 4711, 'foot'
|
152
|
-
assert_raise(JSONError) { JSON(s) }
|
153
|
-
begin
|
154
|
-
raise TypeError, "test me"
|
155
|
-
rescue TypeError => e
|
156
|
-
e_json = JSON.generate e
|
157
|
-
e_again = JSON e_json, :create_additions => true
|
158
|
-
assert_kind_of TypeError, e_again
|
159
|
-
assert_equal e.message, e_again.message
|
160
|
-
assert_equal e.backtrace, e_again.backtrace
|
161
|
-
end
|
162
|
-
assert_equal(/foo/, JSON(JSON(/foo/), :create_additions => true))
|
163
|
-
assert_equal(/foo/i, JSON(JSON(/foo/i), :create_additions => true))
|
164
|
-
end
|
165
|
-
|
166
|
-
def test_utc_datetime
|
167
|
-
now = Time.now
|
168
|
-
d = DateTime.parse(now.to_s, :create_additions => true) # usual case
|
169
|
-
assert_equal d, parse(d.to_json, :create_additions => true)
|
170
|
-
d = DateTime.parse(now.utc.to_s) # of = 0
|
171
|
-
assert_equal d, parse(d.to_json, :create_additions => true)
|
172
|
-
d = DateTime.civil(2008, 6, 17, 11, 48, 32, Rational(1,24))
|
173
|
-
assert_equal d, parse(d.to_json, :create_additions => true)
|
174
|
-
d = DateTime.civil(2008, 6, 17, 11, 48, 32, Rational(12,24))
|
175
|
-
assert_equal d, parse(d.to_json, :create_additions => true)
|
176
|
-
end
|
177
|
-
|
178
|
-
def test_rational_complex
|
179
|
-
assert_equal Rational(2, 9), parse(JSON(Rational(2, 9)), :create_additions => true)
|
180
|
-
assert_equal Complex(2, 9), parse(JSON(Complex(2, 9)), :create_additions => true)
|
181
|
-
end
|
182
|
-
|
183
|
-
def test_bigdecimal
|
184
|
-
assert_equal BigDecimal('3.141', 23), JSON(JSON(BigDecimal('3.141', 23)), :create_additions => true)
|
185
|
-
assert_equal BigDecimal('3.141', 666), JSON(JSON(BigDecimal('3.141', 666)), :create_additions => true)
|
186
|
-
end
|
187
|
-
|
188
|
-
def test_ostruct
|
189
|
-
o = OpenStruct.new
|
190
|
-
# XXX this won't work; o.foo = { :bar => true }
|
191
|
-
o.foo = { 'bar' => true }
|
192
|
-
assert_equal o, parse(JSON(o), :create_additions => true)
|
193
|
-
end
|
194
|
-
|
195
|
-
def test_set
|
196
|
-
s = Set.new([:a, :b, :c, :a])
|
197
|
-
assert_equal s, JSON.parse(JSON(s), :create_additions => true)
|
198
|
-
end
|
199
|
-
end
|
@@ -1,169 +0,0 @@
|
|
1
|
-
#frozen_string_literal: false
|
2
|
-
require 'test_helper'
|
3
|
-
require 'stringio'
|
4
|
-
require 'tempfile'
|
5
|
-
|
6
|
-
class JSONCommonInterfaceTest < Test::Unit::TestCase
|
7
|
-
include JSON
|
8
|
-
|
9
|
-
def setup
|
10
|
-
@hash = {
|
11
|
-
'a' => 2,
|
12
|
-
'b' => 3.141,
|
13
|
-
'c' => 'c',
|
14
|
-
'd' => [ 1, "b", 3.14 ],
|
15
|
-
'e' => { 'foo' => 'bar' },
|
16
|
-
'g' => "\"\0\037",
|
17
|
-
'h' => 1000.0,
|
18
|
-
'i' => 0.001
|
19
|
-
}
|
20
|
-
@json = '{"a":2,"b":3.141,"c":"c","d":[1,"b",3.14],"e":{"foo":"bar"},'\
|
21
|
-
'"g":"\\"\\u0000\\u001f","h":1000.0,"i":0.001}'
|
22
|
-
end
|
23
|
-
|
24
|
-
def test_index
|
25
|
-
assert_equal @json, JSON[@hash]
|
26
|
-
assert_equal @hash, JSON[@json]
|
27
|
-
end
|
28
|
-
|
29
|
-
def test_parser
|
30
|
-
assert_match(/::Parser\z/, JSON.parser.name)
|
31
|
-
end
|
32
|
-
|
33
|
-
def test_generator
|
34
|
-
assert_match(/::Generator\z/, JSON.generator.name)
|
35
|
-
end
|
36
|
-
|
37
|
-
def test_state
|
38
|
-
assert_match(/::Generator::State\z/, JSON.state.name)
|
39
|
-
end
|
40
|
-
|
41
|
-
def test_create_id
|
42
|
-
assert_equal 'json_class', JSON.create_id
|
43
|
-
JSON.create_id = 'foo_bar'
|
44
|
-
assert_equal 'foo_bar', JSON.create_id
|
45
|
-
ensure
|
46
|
-
JSON.create_id = 'json_class'
|
47
|
-
end
|
48
|
-
|
49
|
-
def test_deep_const_get
|
50
|
-
assert_raise(ArgumentError) { JSON.deep_const_get('Nix::Da') }
|
51
|
-
assert_equal File::SEPARATOR, JSON.deep_const_get('File::SEPARATOR')
|
52
|
-
end
|
53
|
-
|
54
|
-
def test_parse
|
55
|
-
assert_equal [ 1, 2, 3, ], JSON.parse('[ 1, 2, 3 ]')
|
56
|
-
end
|
57
|
-
|
58
|
-
def test_parse_bang
|
59
|
-
assert_equal [ 1, Infinity, 3, ], JSON.parse!('[ 1, Infinity, 3 ]')
|
60
|
-
end
|
61
|
-
|
62
|
-
def test_generate
|
63
|
-
assert_equal '[1,2,3]', JSON.generate([ 1, 2, 3 ])
|
64
|
-
end
|
65
|
-
|
66
|
-
def test_fast_generate
|
67
|
-
assert_equal '[1,2,3]', JSON.generate([ 1, 2, 3 ])
|
68
|
-
end
|
69
|
-
|
70
|
-
def test_pretty_generate
|
71
|
-
assert_equal "[\n 1,\n 2,\n 3\n]", JSON.pretty_generate([ 1, 2, 3 ])
|
72
|
-
end
|
73
|
-
|
74
|
-
def test_load
|
75
|
-
assert_equal @hash, JSON.load(@json)
|
76
|
-
tempfile = Tempfile.open('@json')
|
77
|
-
tempfile.write @json
|
78
|
-
tempfile.rewind
|
79
|
-
assert_equal @hash, JSON.load(tempfile)
|
80
|
-
stringio = StringIO.new(@json)
|
81
|
-
stringio.rewind
|
82
|
-
assert_equal @hash, JSON.load(stringio)
|
83
|
-
assert_equal nil, JSON.load(nil)
|
84
|
-
assert_equal nil, JSON.load('')
|
85
|
-
ensure
|
86
|
-
tempfile.close!
|
87
|
-
end
|
88
|
-
|
89
|
-
def test_load_with_options
|
90
|
-
json = '{ "foo": NaN }'
|
91
|
-
assert JSON.load(json, nil, :allow_nan => true)['foo'].nan?
|
92
|
-
end
|
93
|
-
|
94
|
-
def test_load_null
|
95
|
-
assert_equal nil, JSON.load(nil, nil, :allow_blank => true)
|
96
|
-
assert_raise(TypeError) { JSON.load(nil, nil, :allow_blank => false) }
|
97
|
-
assert_raise(JSON::ParserError) { JSON.load('', nil, :allow_blank => false) }
|
98
|
-
end
|
99
|
-
|
100
|
-
def test_dump
|
101
|
-
too_deep = '[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]'
|
102
|
-
assert_equal too_deep, dump(eval(too_deep))
|
103
|
-
assert_kind_of String, Marshal.dump(eval(too_deep))
|
104
|
-
assert_raise(ArgumentError) { dump(eval(too_deep), 100) }
|
105
|
-
assert_raise(ArgumentError) { Marshal.dump(eval(too_deep), 100) }
|
106
|
-
assert_equal too_deep, dump(eval(too_deep), 101)
|
107
|
-
assert_kind_of String, Marshal.dump(eval(too_deep), 101)
|
108
|
-
output = StringIO.new
|
109
|
-
dump(eval(too_deep), output)
|
110
|
-
assert_equal too_deep, output.string
|
111
|
-
output = StringIO.new
|
112
|
-
dump(eval(too_deep), output, 101)
|
113
|
-
assert_equal too_deep, output.string
|
114
|
-
end
|
115
|
-
|
116
|
-
def test_dump_should_modify_defaults
|
117
|
-
max_nesting = JSON.dump_default_options[:max_nesting]
|
118
|
-
dump([], StringIO.new, 10)
|
119
|
-
assert_equal max_nesting, JSON.dump_default_options[:max_nesting]
|
120
|
-
end
|
121
|
-
|
122
|
-
def test_JSON
|
123
|
-
assert_equal @json, JSON(@hash)
|
124
|
-
assert_equal @hash, JSON(@json)
|
125
|
-
end
|
126
|
-
|
127
|
-
def test_load_file
|
128
|
-
test_load_shared(:load_file)
|
129
|
-
end
|
130
|
-
|
131
|
-
def test_load_file!
|
132
|
-
test_load_shared(:load_file!)
|
133
|
-
end
|
134
|
-
|
135
|
-
def test_load_file_with_option
|
136
|
-
test_load_file_with_option_shared(:load_file)
|
137
|
-
end
|
138
|
-
|
139
|
-
def test_load_file_with_option!
|
140
|
-
test_load_file_with_option_shared(:load_file!)
|
141
|
-
end
|
142
|
-
|
143
|
-
private
|
144
|
-
|
145
|
-
def test_load_shared(method_name)
|
146
|
-
temp_file_containing(@json) do |filespec|
|
147
|
-
assert_equal JSON.public_send(method_name, filespec), @hash
|
148
|
-
end
|
149
|
-
end
|
150
|
-
|
151
|
-
def test_load_file_with_option_shared(method_name)
|
152
|
-
temp_file_containing(@json) do |filespec|
|
153
|
-
parsed_object = JSON.public_send(method_name, filespec, symbolize_names: true)
|
154
|
-
key_classes = parsed_object.keys.map(&:class)
|
155
|
-
assert_include(key_classes, Symbol)
|
156
|
-
assert_not_include(key_classes, String)
|
157
|
-
end
|
158
|
-
end
|
159
|
-
|
160
|
-
def temp_file_containing(text, file_prefix = '')
|
161
|
-
raise "This method must be called with a code block." unless block_given?
|
162
|
-
|
163
|
-
Tempfile.create(file_prefix) do |file|
|
164
|
-
file << text
|
165
|
-
file.close
|
166
|
-
yield file.path
|
167
|
-
end
|
168
|
-
end
|
169
|
-
end
|
data/tests/json_encoding_test.rb
DELETED
@@ -1,107 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
#frozen_string_literal: false
|
3
|
-
require 'test_helper'
|
4
|
-
|
5
|
-
class JSONEncodingTest < Test::Unit::TestCase
|
6
|
-
include JSON
|
7
|
-
|
8
|
-
def setup
|
9
|
-
@utf_8 = '"© ≠ €!"'
|
10
|
-
@ascii_8bit = @utf_8.dup.force_encoding('ascii-8bit')
|
11
|
-
@parsed = "© ≠ €!"
|
12
|
-
@generated = '"\u00a9 \u2260 \u20ac!"'
|
13
|
-
if String.method_defined?(:encode)
|
14
|
-
@utf_16_data = @parsed.encode('utf-16be', 'utf-8')
|
15
|
-
@utf_16be = @utf_8.encode('utf-16be', 'utf-8')
|
16
|
-
@utf_16le = @utf_8.encode('utf-16le', 'utf-8')
|
17
|
-
@utf_32be = @utf_8.encode('utf-32be', 'utf-8')
|
18
|
-
@utf_32le = @utf_8.encode('utf-32le', 'utf-8')
|
19
|
-
else
|
20
|
-
require 'iconv'
|
21
|
-
@utf_16_data, = Iconv.iconv('utf-16be', 'utf-8', @parsed)
|
22
|
-
@utf_16be, = Iconv.iconv('utf-16be', 'utf-8', @utf_8)
|
23
|
-
@utf_16le, = Iconv.iconv('utf-16le', 'utf-8', @utf_8)
|
24
|
-
@utf_32be, = Iconv.iconv('utf-32be', 'utf-8', @utf_8)
|
25
|
-
@utf_32le, = Iconv.iconv('utf-32le', 'utf-8', @utf_8)
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
def test_parse
|
30
|
-
assert_equal @parsed, JSON.parse(@ascii_8bit)
|
31
|
-
assert_equal @parsed, JSON.parse(@utf_8)
|
32
|
-
assert_equal @parsed, JSON.parse(@utf_16be)
|
33
|
-
assert_equal @parsed, JSON.parse(@utf_16le)
|
34
|
-
assert_equal @parsed, JSON.parse(@utf_32be)
|
35
|
-
assert_equal @parsed, JSON.parse(@utf_32le)
|
36
|
-
end
|
37
|
-
|
38
|
-
def test_generate
|
39
|
-
assert_equal @generated, JSON.generate(@parsed, :ascii_only => true)
|
40
|
-
assert_equal @generated, JSON.generate(@utf_16_data, :ascii_only => true)
|
41
|
-
end
|
42
|
-
|
43
|
-
def test_unicode
|
44
|
-
assert_equal '""', ''.to_json
|
45
|
-
assert_equal '"\\b"', "\b".to_json
|
46
|
-
assert_equal '"\u0001"', 0x1.chr.to_json
|
47
|
-
assert_equal '"\u001f"', 0x1f.chr.to_json
|
48
|
-
assert_equal '" "', ' '.to_json
|
49
|
-
assert_equal "\"#{0x7f.chr}\"", 0x7f.chr.to_json
|
50
|
-
utf8 = [ "© ≠ €! \01" ]
|
51
|
-
json = '["© ≠ €! \u0001"]'
|
52
|
-
assert_equal json, utf8.to_json(:ascii_only => false)
|
53
|
-
assert_equal utf8, parse(json)
|
54
|
-
json = '["\u00a9 \u2260 \u20ac! \u0001"]'
|
55
|
-
assert_equal json, utf8.to_json(:ascii_only => true)
|
56
|
-
assert_equal utf8, parse(json)
|
57
|
-
utf8 = ["\343\201\202\343\201\204\343\201\206\343\201\210\343\201\212"]
|
58
|
-
json = "[\"\343\201\202\343\201\204\343\201\206\343\201\210\343\201\212\"]"
|
59
|
-
assert_equal utf8, parse(json)
|
60
|
-
assert_equal json, utf8.to_json(:ascii_only => false)
|
61
|
-
utf8 = ["\343\201\202\343\201\204\343\201\206\343\201\210\343\201\212"]
|
62
|
-
assert_equal utf8, parse(json)
|
63
|
-
json = "[\"\\u3042\\u3044\\u3046\\u3048\\u304a\"]"
|
64
|
-
assert_equal json, utf8.to_json(:ascii_only => true)
|
65
|
-
assert_equal utf8, parse(json)
|
66
|
-
utf8 = ['საქართველო']
|
67
|
-
json = '["საქართველო"]'
|
68
|
-
assert_equal json, utf8.to_json(:ascii_only => false)
|
69
|
-
json = "[\"\\u10e1\\u10d0\\u10e5\\u10d0\\u10e0\\u10d7\\u10d5\\u10d4\\u10da\\u10dd\"]"
|
70
|
-
assert_equal json, utf8.to_json(:ascii_only => true)
|
71
|
-
assert_equal utf8, parse(json)
|
72
|
-
assert_equal '["Ã"]', generate(["Ã"], :ascii_only => false)
|
73
|
-
assert_equal '["\\u00c3"]', generate(["Ã"], :ascii_only => true)
|
74
|
-
assert_equal ["€"], parse('["\u20ac"]')
|
75
|
-
utf8 = ["\xf0\xa0\x80\x81"]
|
76
|
-
json = "[\"\xf0\xa0\x80\x81\"]"
|
77
|
-
assert_equal json, generate(utf8, :ascii_only => false)
|
78
|
-
assert_equal utf8, parse(json)
|
79
|
-
json = '["\ud840\udc01"]'
|
80
|
-
assert_equal json, generate(utf8, :ascii_only => true)
|
81
|
-
assert_equal utf8, parse(json)
|
82
|
-
assert_raise(JSON::ParserError) { parse('"\u"') }
|
83
|
-
assert_raise(JSON::ParserError) { parse('"\ud800"') }
|
84
|
-
end
|
85
|
-
|
86
|
-
def test_chars
|
87
|
-
(0..0x7f).each do |i|
|
88
|
-
json = '["\u%04x"]' % i
|
89
|
-
if RUBY_VERSION >= "1.9."
|
90
|
-
i = i.chr
|
91
|
-
end
|
92
|
-
assert_equal i, parse(json).first[0]
|
93
|
-
if i == ?\b
|
94
|
-
generated = generate(["" << i])
|
95
|
-
assert '["\b"]' == generated || '["\10"]' == generated
|
96
|
-
elsif [?\n, ?\r, ?\t, ?\f].include?(i)
|
97
|
-
assert_equal '[' << ('' << i).dump << ']', generate(["" << i])
|
98
|
-
elsif i.chr < 0x20.chr
|
99
|
-
assert_equal json, generate(["" << i])
|
100
|
-
end
|
101
|
-
end
|
102
|
-
assert_raise(JSON::GeneratorError) do
|
103
|
-
generate(["\x80"], :ascii_only => true)
|
104
|
-
end
|
105
|
-
assert_equal "\302\200", parse('["\u0080"]').first
|
106
|
-
end
|
107
|
-
end
|
@@ -1,15 +0,0 @@
|
|
1
|
-
#frozen_string_literal: false
|
2
|
-
require 'test_helper'
|
3
|
-
|
4
|
-
class JSONExtParserTest < Test::Unit::TestCase
|
5
|
-
if defined?(JSON::Ext::Parser)
|
6
|
-
def test_allocate
|
7
|
-
parser = JSON::Ext::Parser.new("{}")
|
8
|
-
assert_raise(TypeError, '[ruby-core:35079]') do
|
9
|
-
parser.__send__(:initialize, "{}")
|
10
|
-
end
|
11
|
-
parser = JSON::Ext::Parser.allocate
|
12
|
-
assert_raise(TypeError, '[ruby-core:35079]') { parser.source }
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
data/tests/json_fixtures_test.rb
DELETED
@@ -1,40 +0,0 @@
|
|
1
|
-
#frozen_string_literal: false
|
2
|
-
require 'test_helper'
|
3
|
-
|
4
|
-
class JSONFixturesTest < Test::Unit::TestCase
|
5
|
-
def setup
|
6
|
-
fixtures = File.join(File.dirname(__FILE__), 'fixtures/{fail,pass}*.json')
|
7
|
-
passed, failed = Dir[fixtures].partition { |f| f['pass'] }
|
8
|
-
@passed = passed.inject([]) { |a, f| a << [ f, File.read(f) ] }.sort
|
9
|
-
@failed = failed.inject([]) { |a, f| a << [ f, File.read(f) ] }.sort
|
10
|
-
end
|
11
|
-
|
12
|
-
def test_passing
|
13
|
-
verbose_bak, $VERBOSE = $VERBOSE, nil
|
14
|
-
for name, source in @passed
|
15
|
-
begin
|
16
|
-
assert JSON.parse(source),
|
17
|
-
"Did not pass for fixture '#{name}': #{source.inspect}"
|
18
|
-
rescue => e
|
19
|
-
warn "\nCaught #{e.class}(#{e}) for fixture '#{name}': #{source.inspect}\n#{e.backtrace * "\n"}"
|
20
|
-
raise e
|
21
|
-
end
|
22
|
-
end
|
23
|
-
ensure
|
24
|
-
$VERBOSE = verbose_bak
|
25
|
-
end
|
26
|
-
|
27
|
-
def test_failing
|
28
|
-
for name, source in @failed
|
29
|
-
assert_raise(JSON::ParserError, JSON::NestingError,
|
30
|
-
"Did not fail for fixture '#{name}': #{source.inspect}") do
|
31
|
-
JSON.parse(source)
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
def test_sanity
|
37
|
-
assert(@passed.size > 5)
|
38
|
-
assert(@failed.size > 20)
|
39
|
-
end
|
40
|
-
end
|