scout 5.7.3.pre.2 → 5.7.4
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.markdown +4 -0
- data/lib/scout/version.rb +1 -1
- data/vendor/json_pure/.gitignore +12 -0
- data/vendor/json_pure/.travis.yml +20 -0
- data/vendor/json_pure/CHANGES +282 -0
- data/vendor/json_pure/COPYING +58 -0
- data/vendor/json_pure/COPYING-json-jruby +57 -0
- data/vendor/json_pure/GPL +340 -0
- data/vendor/json_pure/Gemfile +11 -0
- data/vendor/json_pure/README-json-jruby.markdown +33 -0
- data/vendor/json_pure/README.rdoc +358 -0
- data/vendor/json_pure/Rakefile +412 -0
- data/vendor/json_pure/TODO +1 -0
- data/vendor/json_pure/VERSION +1 -0
- data/vendor/json_pure/data/example.json +1 -0
- data/vendor/json_pure/data/index.html +38 -0
- data/vendor/json_pure/data/prototype.js +4184 -0
- data/vendor/json_pure/diagrams/.keep +0 -0
- data/vendor/json_pure/ext/json/ext/fbuffer/fbuffer.h +181 -0
- data/vendor/json_pure/ext/json/ext/generator/depend +1 -0
- data/vendor/json_pure/ext/json/ext/generator/extconf.rb +14 -0
- data/vendor/json_pure/ext/json/ext/generator/generator.c +1435 -0
- data/vendor/json_pure/ext/json/ext/generator/generator.h +148 -0
- data/vendor/json_pure/ext/json/ext/parser/depend +1 -0
- data/vendor/json_pure/ext/json/ext/parser/extconf.rb +13 -0
- data/vendor/json_pure/ext/json/ext/parser/parser.c +2204 -0
- data/vendor/json_pure/ext/json/ext/parser/parser.h +77 -0
- data/vendor/json_pure/ext/json/ext/parser/parser.rl +927 -0
- data/vendor/json_pure/install.rb +23 -0
- data/vendor/json_pure/java/src/json/ext/ByteListTranscoder.java +167 -0
- data/vendor/json_pure/java/src/json/ext/Generator.java +444 -0
- data/vendor/json_pure/java/src/json/ext/GeneratorMethods.java +232 -0
- data/vendor/json_pure/java/src/json/ext/GeneratorService.java +43 -0
- data/vendor/json_pure/java/src/json/ext/GeneratorState.java +543 -0
- data/vendor/json_pure/java/src/json/ext/OptionsReader.java +114 -0
- data/vendor/json_pure/java/src/json/ext/Parser.java +2644 -0
- data/vendor/json_pure/java/src/json/ext/Parser.rl +968 -0
- data/vendor/json_pure/java/src/json/ext/ParserService.java +35 -0
- data/vendor/json_pure/java/src/json/ext/RuntimeInfo.java +121 -0
- data/vendor/json_pure/java/src/json/ext/StringDecoder.java +167 -0
- data/vendor/json_pure/java/src/json/ext/StringEncoder.java +106 -0
- data/vendor/json_pure/java/src/json/ext/Utils.java +89 -0
- data/vendor/json_pure/json-java.gemspec +23 -0
- data/vendor/json_pure/json.gemspec +37 -0
- data/vendor/json_pure/json_pure.gemspec +39 -0
- data/vendor/json_pure/lib/json.rb +62 -0
- data/vendor/json_pure/lib/json/add/bigdecimal.rb +28 -0
- data/vendor/json_pure/lib/json/add/complex.rb +22 -0
- data/vendor/json_pure/lib/json/add/core.rb +11 -0
- data/vendor/json_pure/lib/json/add/date.rb +34 -0
- data/vendor/json_pure/lib/json/add/date_time.rb +50 -0
- data/vendor/json_pure/lib/json/add/exception.rb +31 -0
- data/vendor/json_pure/lib/json/add/ostruct.rb +31 -0
- data/vendor/json_pure/lib/json/add/range.rb +29 -0
- data/vendor/json_pure/lib/json/add/rational.rb +22 -0
- data/vendor/json_pure/lib/json/add/regexp.rb +30 -0
- data/vendor/json_pure/lib/json/add/struct.rb +30 -0
- data/vendor/json_pure/lib/json/add/symbol.rb +25 -0
- data/vendor/json_pure/lib/json/add/time.rb +38 -0
- data/vendor/json_pure/lib/json/common.rb +487 -0
- data/vendor/json_pure/lib/json/ext.rb +21 -0
- data/vendor/json_pure/lib/json/ext/.keep +0 -0
- data/vendor/json_pure/lib/json/generic_object.rb +70 -0
- data/vendor/json_pure/lib/json/pure.rb +21 -0
- data/vendor/json_pure/lib/json/pure/generator.rb +522 -0
- data/vendor/json_pure/lib/json/pure/parser.rb +359 -0
- data/vendor/json_pure/lib/json/version.rb +8 -0
- data/vendor/json_pure/tests/fixtures/fail1.json +1 -0
- data/vendor/json_pure/tests/fixtures/fail10.json +1 -0
- data/vendor/json_pure/tests/fixtures/fail11.json +1 -0
- data/vendor/json_pure/tests/fixtures/fail12.json +1 -0
- data/vendor/json_pure/tests/fixtures/fail13.json +1 -0
- data/vendor/json_pure/tests/fixtures/fail14.json +1 -0
- data/vendor/json_pure/tests/fixtures/fail18.json +1 -0
- data/vendor/json_pure/tests/fixtures/fail19.json +1 -0
- data/vendor/json_pure/tests/fixtures/fail2.json +1 -0
- data/vendor/json_pure/tests/fixtures/fail20.json +1 -0
- data/vendor/json_pure/tests/fixtures/fail21.json +1 -0
- data/vendor/json_pure/tests/fixtures/fail22.json +1 -0
- data/vendor/json_pure/tests/fixtures/fail23.json +1 -0
- data/vendor/json_pure/tests/fixtures/fail24.json +1 -0
- data/vendor/json_pure/tests/fixtures/fail25.json +1 -0
- data/vendor/json_pure/tests/fixtures/fail27.json +2 -0
- data/vendor/json_pure/tests/fixtures/fail28.json +2 -0
- data/vendor/json_pure/tests/fixtures/fail3.json +1 -0
- data/vendor/json_pure/tests/fixtures/fail4.json +1 -0
- data/vendor/json_pure/tests/fixtures/fail5.json +1 -0
- data/vendor/json_pure/tests/fixtures/fail6.json +1 -0
- data/vendor/json_pure/tests/fixtures/fail7.json +1 -0
- data/vendor/json_pure/tests/fixtures/fail8.json +1 -0
- data/vendor/json_pure/tests/fixtures/fail9.json +1 -0
- data/vendor/json_pure/tests/fixtures/pass1.json +56 -0
- data/vendor/json_pure/tests/fixtures/pass15.json +1 -0
- data/vendor/json_pure/tests/fixtures/pass16.json +1 -0
- data/vendor/json_pure/tests/fixtures/pass17.json +1 -0
- data/vendor/json_pure/tests/fixtures/pass2.json +1 -0
- data/vendor/json_pure/tests/fixtures/pass26.json +1 -0
- data/vendor/json_pure/tests/fixtures/pass3.json +6 -0
- data/vendor/json_pure/tests/setup_variant.rb +11 -0
- data/vendor/json_pure/tests/test_json.rb +545 -0
- data/vendor/json_pure/tests/test_json_addition.rb +196 -0
- data/vendor/json_pure/tests/test_json_encoding.rb +65 -0
- data/vendor/json_pure/tests/test_json_fixtures.rb +35 -0
- data/vendor/json_pure/tests/test_json_generate.rb +322 -0
- data/vendor/json_pure/tests/test_json_generic_object.rb +75 -0
- data/vendor/json_pure/tests/test_json_string_matching.rb +39 -0
- data/vendor/json_pure/tests/test_json_unicode.rb +72 -0
- data/vendor/json_pure/tools/fuzz.rb +139 -0
- data/vendor/json_pure/tools/server.rb +62 -0
- metadata +111 -4
@@ -0,0 +1,196 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# -*- coding:utf-8 -*-
|
3
|
+
|
4
|
+
require 'test/unit'
|
5
|
+
require File.join(File.dirname(__FILE__), 'setup_variant')
|
6
|
+
require 'json/add/core'
|
7
|
+
require 'json/add/complex'
|
8
|
+
require 'json/add/rational'
|
9
|
+
require 'json/add/bigdecimal'
|
10
|
+
require 'json/add/ostruct'
|
11
|
+
require 'date'
|
12
|
+
|
13
|
+
class TestJSONAddition < Test::Unit::TestCase
|
14
|
+
include JSON
|
15
|
+
|
16
|
+
class A
|
17
|
+
def initialize(a)
|
18
|
+
@a = a
|
19
|
+
end
|
20
|
+
|
21
|
+
attr_reader :a
|
22
|
+
|
23
|
+
def ==(other)
|
24
|
+
a == other.a
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.json_create(object)
|
28
|
+
new(*object['args'])
|
29
|
+
end
|
30
|
+
|
31
|
+
def to_json(*args)
|
32
|
+
{
|
33
|
+
'json_class' => self.class.name,
|
34
|
+
'args' => [ @a ],
|
35
|
+
}.to_json(*args)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
class A2 < A
|
40
|
+
def to_json(*args)
|
41
|
+
{
|
42
|
+
'json_class' => self.class.name,
|
43
|
+
'args' => [ @a ],
|
44
|
+
}.to_json(*args)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
class B
|
49
|
+
def self.json_creatable?
|
50
|
+
false
|
51
|
+
end
|
52
|
+
|
53
|
+
def to_json(*args)
|
54
|
+
{
|
55
|
+
'json_class' => self.class.name,
|
56
|
+
}.to_json(*args)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
class C
|
61
|
+
def self.json_creatable?
|
62
|
+
false
|
63
|
+
end
|
64
|
+
|
65
|
+
def to_json(*args)
|
66
|
+
{
|
67
|
+
'json_class' => 'TestJSONAddition::Nix',
|
68
|
+
}.to_json(*args)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_extended_json
|
73
|
+
a = A.new(666)
|
74
|
+
assert A.json_creatable?
|
75
|
+
json = generate(a)
|
76
|
+
a_again = JSON.parse(json, :create_additions => true)
|
77
|
+
assert_kind_of a.class, a_again
|
78
|
+
assert_equal a, a_again
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_extended_json_default
|
82
|
+
a = A.new(666)
|
83
|
+
assert A.json_creatable?
|
84
|
+
json = generate(a)
|
85
|
+
a_hash = JSON.parse(json)
|
86
|
+
assert_kind_of Hash, a_hash
|
87
|
+
end
|
88
|
+
|
89
|
+
def test_extended_json_disabled
|
90
|
+
a = A.new(666)
|
91
|
+
assert A.json_creatable?
|
92
|
+
json = generate(a)
|
93
|
+
a_again = JSON.parse(json, :create_additions => true)
|
94
|
+
assert_kind_of a.class, a_again
|
95
|
+
assert_equal a, a_again
|
96
|
+
a_hash = JSON.parse(json, :create_additions => false)
|
97
|
+
assert_kind_of Hash, a_hash
|
98
|
+
assert_equal(
|
99
|
+
{"args"=>[666], "json_class"=>"TestJSONAddition::A"}.sort_by { |k,| k },
|
100
|
+
a_hash.sort_by { |k,| k }
|
101
|
+
)
|
102
|
+
end
|
103
|
+
|
104
|
+
def test_extended_json_fail1
|
105
|
+
b = B.new
|
106
|
+
assert !B.json_creatable?
|
107
|
+
json = generate(b)
|
108
|
+
assert_equal({ "json_class"=>"TestJSONAddition::B" }, JSON.parse(json))
|
109
|
+
end
|
110
|
+
|
111
|
+
def test_extended_json_fail2
|
112
|
+
c = C.new
|
113
|
+
assert !C.json_creatable?
|
114
|
+
json = generate(c)
|
115
|
+
assert_raises(ArgumentError, NameError) { JSON.parse(json, :create_additions => true) }
|
116
|
+
end
|
117
|
+
|
118
|
+
def test_raw_strings
|
119
|
+
raw = ''
|
120
|
+
raw.respond_to?(:encode!) and raw.encode!(Encoding::ASCII_8BIT)
|
121
|
+
raw_array = []
|
122
|
+
for i in 0..255
|
123
|
+
raw << i
|
124
|
+
raw_array << i
|
125
|
+
end
|
126
|
+
json = raw.to_json_raw
|
127
|
+
json_raw_object = raw.to_json_raw_object
|
128
|
+
hash = { 'json_class' => 'String', 'raw'=> raw_array }
|
129
|
+
assert_equal hash, json_raw_object
|
130
|
+
assert_match(/\A\{.*\}\z/, json)
|
131
|
+
assert_match(/"json_class":"String"/, json)
|
132
|
+
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)
|
133
|
+
raw_again = JSON.parse(json, :create_additions => true)
|
134
|
+
assert_equal raw, raw_again
|
135
|
+
end
|
136
|
+
|
137
|
+
MyJsonStruct = Struct.new 'MyJsonStruct', :foo, :bar
|
138
|
+
|
139
|
+
def test_core
|
140
|
+
t = Time.now
|
141
|
+
assert_equal t, JSON(JSON(t), :create_additions => true)
|
142
|
+
d = Date.today
|
143
|
+
assert_equal d, JSON(JSON(d), :create_additions => true)
|
144
|
+
d = DateTime.civil(2007, 6, 14, 14, 57, 10, Rational(1, 12), 2299161)
|
145
|
+
assert_equal d, JSON(JSON(d), :create_additions => true)
|
146
|
+
assert_equal 1..10, JSON(JSON(1..10), :create_additions => true)
|
147
|
+
assert_equal 1...10, JSON(JSON(1...10), :create_additions => true)
|
148
|
+
assert_equal "a".."c", JSON(JSON("a".."c"), :create_additions => true)
|
149
|
+
assert_equal "a"..."c", JSON(JSON("a"..."c"), :create_additions => true)
|
150
|
+
s = MyJsonStruct.new 4711, 'foot'
|
151
|
+
assert_equal s, JSON(JSON(s), :create_additions => true)
|
152
|
+
struct = Struct.new :foo, :bar
|
153
|
+
s = struct.new 4711, 'foot'
|
154
|
+
assert_raises(JSONError) { JSON(s) }
|
155
|
+
begin
|
156
|
+
raise TypeError, "test me"
|
157
|
+
rescue TypeError => e
|
158
|
+
e_json = JSON.generate e
|
159
|
+
e_again = JSON e_json, :create_additions => true
|
160
|
+
assert_kind_of TypeError, e_again
|
161
|
+
assert_equal e.message, e_again.message
|
162
|
+
assert_equal e.backtrace, e_again.backtrace
|
163
|
+
end
|
164
|
+
assert_equal(/foo/, JSON(JSON(/foo/), :create_additions => true))
|
165
|
+
assert_equal(/foo/i, JSON(JSON(/foo/i), :create_additions => true))
|
166
|
+
end
|
167
|
+
|
168
|
+
def test_utc_datetime
|
169
|
+
now = Time.now
|
170
|
+
d = DateTime.parse(now.to_s, :create_additions => true) # usual case
|
171
|
+
assert_equal d, JSON.parse(d.to_json, :create_additions => true)
|
172
|
+
d = DateTime.parse(now.utc.to_s) # of = 0
|
173
|
+
assert_equal d, JSON.parse(d.to_json, :create_additions => true)
|
174
|
+
d = DateTime.civil(2008, 6, 17, 11, 48, 32, Rational(1,24))
|
175
|
+
assert_equal d, JSON.parse(d.to_json, :create_additions => true)
|
176
|
+
d = DateTime.civil(2008, 6, 17, 11, 48, 32, Rational(12,24))
|
177
|
+
assert_equal d, JSON.parse(d.to_json, :create_additions => true)
|
178
|
+
end
|
179
|
+
|
180
|
+
def test_rational_complex
|
181
|
+
assert_equal Rational(2, 9), JSON.parse(JSON(Rational(2, 9)), :create_additions => true)
|
182
|
+
assert_equal Complex(2, 9), JSON.parse(JSON(Complex(2, 9)), :create_additions => true)
|
183
|
+
end
|
184
|
+
|
185
|
+
def test_bigdecimal
|
186
|
+
assert_equal BigDecimal('3.141', 23), JSON(JSON(BigDecimal('3.141', 23)), :create_additions => true)
|
187
|
+
assert_equal BigDecimal('3.141', 666), JSON(JSON(BigDecimal('3.141', 666)), :create_additions => true)
|
188
|
+
end
|
189
|
+
|
190
|
+
def test_ostruct
|
191
|
+
o = OpenStruct.new
|
192
|
+
# XXX this won't work; o.foo = { :bar => true }
|
193
|
+
o.foo = { 'bar' => true }
|
194
|
+
assert_equal o, JSON.parse(JSON(o), :create_additions => true)
|
195
|
+
end
|
196
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# encoding: utf-8
|
3
|
+
|
4
|
+
require 'test/unit'
|
5
|
+
require File.join(File.dirname(__FILE__), 'setup_variant')
|
6
|
+
|
7
|
+
class TestJSONEncoding < Test::Unit::TestCase
|
8
|
+
include JSON
|
9
|
+
|
10
|
+
def setup
|
11
|
+
@utf_8 = '["© ≠ €!"]'
|
12
|
+
@parsed = [ "© ≠ €!" ]
|
13
|
+
@generated = '["\u00a9 \u2260 \u20ac!"]'
|
14
|
+
if String.method_defined?(:encode)
|
15
|
+
@utf_16_data = [@parsed.first.encode('utf-16be', 'utf-8')]
|
16
|
+
@utf_8_ascii_8bit = @utf_8.dup.force_encoding(Encoding::ASCII_8BIT)
|
17
|
+
@utf_16be = @utf_8.encode('utf-16be', 'utf-8')
|
18
|
+
@utf_16be_ascii_8bit = @utf_16be.dup.force_encoding(Encoding::ASCII_8BIT)
|
19
|
+
@utf_16le = @utf_8.encode('utf-16le', 'utf-8')
|
20
|
+
@utf_16le_ascii_8bit = @utf_16le.dup.force_encoding(Encoding::ASCII_8BIT)
|
21
|
+
@utf_32be = @utf_8.encode('utf-32be', 'utf-8')
|
22
|
+
@utf_32be_ascii_8bit = @utf_32be.dup.force_encoding(Encoding::ASCII_8BIT)
|
23
|
+
@utf_32le = @utf_8.encode('utf-32le', 'utf-8')
|
24
|
+
@utf_32le_ascii_8bit = @utf_32le.dup.force_encoding(Encoding::ASCII_8BIT)
|
25
|
+
else
|
26
|
+
require 'iconv'
|
27
|
+
@utf_16_data = Iconv.iconv('utf-16be', 'utf-8', @parsed.first)
|
28
|
+
@utf_8_ascii_8bit = @utf_8.dup
|
29
|
+
@utf_16be, = Iconv.iconv('utf-16be', 'utf-8', @utf_8)
|
30
|
+
@utf_16be_ascii_8bit = @utf_16be.dup
|
31
|
+
@utf_16le, = Iconv.iconv('utf-16le', 'utf-8', @utf_8)
|
32
|
+
@utf_16le_ascii_8bit = @utf_16le.dup
|
33
|
+
@utf_32be, = Iconv.iconv('utf-32be', 'utf-8', @utf_8)
|
34
|
+
@utf_32be_ascii_8bit = @utf_32be.dup
|
35
|
+
@utf_32le, = Iconv.iconv('utf-32le', 'utf-8', @utf_8)
|
36
|
+
@utf_32le_ascii_8bit = @utf_32le.dup
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_parse
|
41
|
+
assert_equal @parsed, JSON.parse(@utf_8)
|
42
|
+
assert_equal @parsed, JSON.parse(@utf_16be)
|
43
|
+
assert_equal @parsed, JSON.parse(@utf_16le)
|
44
|
+
assert_equal @parsed, JSON.parse(@utf_32be)
|
45
|
+
assert_equal @parsed, JSON.parse(@utf_32le)
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_parse_ascii_8bit
|
49
|
+
assert_equal @parsed, JSON.parse(@utf_8_ascii_8bit)
|
50
|
+
assert_equal @parsed, JSON.parse(@utf_16be_ascii_8bit)
|
51
|
+
assert_equal @parsed, JSON.parse(@utf_16le_ascii_8bit)
|
52
|
+
assert_equal @parsed, JSON.parse(@utf_32be_ascii_8bit)
|
53
|
+
assert_equal @parsed, JSON.parse(@utf_32le_ascii_8bit)
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_generate
|
57
|
+
assert_equal @generated, JSON.generate(@parsed, :ascii_only => true)
|
58
|
+
if defined?(::Encoding)
|
59
|
+
assert_equal @generated, JSON.generate(@utf_16_data, :ascii_only => true)
|
60
|
+
else
|
61
|
+
# XXX checking of correct utf8 data is not as strict (yet?) without :ascii_only
|
62
|
+
assert_raises(JSON::GeneratorError) { JSON.generate(@utf_16_data, :ascii_only => true) }
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# encoding: utf-8
|
3
|
+
|
4
|
+
require 'test/unit'
|
5
|
+
require File.join(File.dirname(__FILE__), 'setup_variant')
|
6
|
+
|
7
|
+
class TestJSONFixtures < Test::Unit::TestCase
|
8
|
+
def setup
|
9
|
+
fixtures = File.join(File.dirname(__FILE__), 'fixtures/*.json')
|
10
|
+
passed, failed = Dir[fixtures].partition { |f| f['pass'] }
|
11
|
+
@passed = passed.inject([]) { |a, f| a << [ f, File.read(f) ] }.sort
|
12
|
+
@failed = failed.inject([]) { |a, f| a << [ f, File.read(f) ] }.sort
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_passing
|
16
|
+
for name, source in @passed
|
17
|
+
begin
|
18
|
+
assert JSON.parse(source),
|
19
|
+
"Did not pass for fixture '#{name}': #{source.inspect}"
|
20
|
+
rescue => e
|
21
|
+
warn "\nCaught #{e.class}(#{e}) for fixture '#{name}': #{source.inspect}\n#{e.backtrace * "\n"}"
|
22
|
+
raise e
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_failing
|
28
|
+
for name, source in @failed
|
29
|
+
assert_raises(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
|
+
end
|
@@ -0,0 +1,322 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# encoding: utf-8
|
3
|
+
|
4
|
+
require 'test/unit'
|
5
|
+
require File.join(File.dirname(__FILE__), 'setup_variant')
|
6
|
+
|
7
|
+
class TestJSONGenerate < Test::Unit::TestCase
|
8
|
+
include JSON
|
9
|
+
|
10
|
+
def setup
|
11
|
+
@hash = {
|
12
|
+
'a' => 2,
|
13
|
+
'b' => 3.141,
|
14
|
+
'c' => 'c',
|
15
|
+
'd' => [ 1, "b", 3.14 ],
|
16
|
+
'e' => { 'foo' => 'bar' },
|
17
|
+
'g' => "\"\0\037",
|
18
|
+
'h' => 1000.0,
|
19
|
+
'i' => 0.001
|
20
|
+
}
|
21
|
+
@json2 = '{"a":2,"b":3.141,"c":"c","d":[1,"b",3.14],"e":{"foo":"bar"},' +
|
22
|
+
'"g":"\\"\\u0000\\u001f","h":1000.0,"i":0.001}'
|
23
|
+
@json3 = <<'EOT'.chomp
|
24
|
+
{
|
25
|
+
"a": 2,
|
26
|
+
"b": 3.141,
|
27
|
+
"c": "c",
|
28
|
+
"d": [
|
29
|
+
1,
|
30
|
+
"b",
|
31
|
+
3.14
|
32
|
+
],
|
33
|
+
"e": {
|
34
|
+
"foo": "bar"
|
35
|
+
},
|
36
|
+
"g": "\"\u0000\u001f",
|
37
|
+
"h": 1000.0,
|
38
|
+
"i": 0.001
|
39
|
+
}
|
40
|
+
EOT
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_generate
|
44
|
+
json = generate(@hash)
|
45
|
+
assert_equal(JSON.parse(@json2), JSON.parse(json))
|
46
|
+
json = JSON[@hash]
|
47
|
+
assert_equal(JSON.parse(@json2), JSON.parse(json))
|
48
|
+
parsed_json = parse(json)
|
49
|
+
assert_equal(@hash, parsed_json)
|
50
|
+
json = generate({1=>2})
|
51
|
+
assert_equal('{"1":2}', json)
|
52
|
+
parsed_json = parse(json)
|
53
|
+
assert_equal({"1"=>2}, parsed_json)
|
54
|
+
assert_raise(GeneratorError) { generate(666) }
|
55
|
+
assert_equal '666', generate(666, :quirks_mode => true)
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_generate_pretty
|
59
|
+
json = pretty_generate(@hash)
|
60
|
+
# hashes aren't (insertion) ordered on every ruby implementation assert_equal(@json3, json)
|
61
|
+
assert_equal(JSON.parse(@json3), JSON.parse(json))
|
62
|
+
parsed_json = parse(json)
|
63
|
+
assert_equal(@hash, parsed_json)
|
64
|
+
json = pretty_generate({1=>2})
|
65
|
+
assert_equal(<<'EOT'.chomp, json)
|
66
|
+
{
|
67
|
+
"1": 2
|
68
|
+
}
|
69
|
+
EOT
|
70
|
+
parsed_json = parse(json)
|
71
|
+
assert_equal({"1"=>2}, parsed_json)
|
72
|
+
assert_raise(GeneratorError) { pretty_generate(666) }
|
73
|
+
assert_equal '666', pretty_generate(666, :quirks_mode => true)
|
74
|
+
end
|
75
|
+
|
76
|
+
def test_fast_generate
|
77
|
+
json = fast_generate(@hash)
|
78
|
+
assert_equal(JSON.parse(@json2), JSON.parse(json))
|
79
|
+
parsed_json = parse(json)
|
80
|
+
assert_equal(@hash, parsed_json)
|
81
|
+
json = fast_generate({1=>2})
|
82
|
+
assert_equal('{"1":2}', json)
|
83
|
+
parsed_json = parse(json)
|
84
|
+
assert_equal({"1"=>2}, parsed_json)
|
85
|
+
assert_raise(GeneratorError) { fast_generate(666) }
|
86
|
+
assert_equal '666', fast_generate(666, :quirks_mode => true)
|
87
|
+
end
|
88
|
+
|
89
|
+
def test_own_state
|
90
|
+
state = State.new
|
91
|
+
json = generate(@hash, state)
|
92
|
+
assert_equal(JSON.parse(@json2), JSON.parse(json))
|
93
|
+
parsed_json = parse(json)
|
94
|
+
assert_equal(@hash, parsed_json)
|
95
|
+
json = generate({1=>2}, state)
|
96
|
+
assert_equal('{"1":2}', json)
|
97
|
+
parsed_json = parse(json)
|
98
|
+
assert_equal({"1"=>2}, parsed_json)
|
99
|
+
assert_raise(GeneratorError) { generate(666, state) }
|
100
|
+
state.quirks_mode = true
|
101
|
+
assert state.quirks_mode?
|
102
|
+
assert_equal '666', generate(666, state)
|
103
|
+
end
|
104
|
+
|
105
|
+
def test_states
|
106
|
+
json = generate({1=>2}, nil)
|
107
|
+
assert_equal('{"1":2}', json)
|
108
|
+
s = JSON.state.new
|
109
|
+
assert s.check_circular?
|
110
|
+
assert s[:check_circular?]
|
111
|
+
h = { 1=>2 }
|
112
|
+
h[3] = h
|
113
|
+
assert_raises(JSON::NestingError) { generate(h) }
|
114
|
+
assert_raises(JSON::NestingError) { generate(h, s) }
|
115
|
+
s = JSON.state.new
|
116
|
+
a = [ 1, 2 ]
|
117
|
+
a << a
|
118
|
+
assert_raises(JSON::NestingError) { generate(a, s) }
|
119
|
+
assert s.check_circular?
|
120
|
+
assert s[:check_circular?]
|
121
|
+
end
|
122
|
+
|
123
|
+
def test_pretty_state
|
124
|
+
state = PRETTY_STATE_PROTOTYPE.dup
|
125
|
+
assert_equal({
|
126
|
+
:allow_nan => false,
|
127
|
+
:array_nl => "\n",
|
128
|
+
:ascii_only => false,
|
129
|
+
:buffer_initial_length => 1024,
|
130
|
+
:quirks_mode => false,
|
131
|
+
:depth => 0,
|
132
|
+
:indent => " ",
|
133
|
+
:max_nesting => 100,
|
134
|
+
:object_nl => "\n",
|
135
|
+
:space => " ",
|
136
|
+
:space_before => "",
|
137
|
+
}.sort_by { |n,| n.to_s }, state.to_h.sort_by { |n,| n.to_s })
|
138
|
+
end
|
139
|
+
|
140
|
+
def test_safe_state
|
141
|
+
state = SAFE_STATE_PROTOTYPE.dup
|
142
|
+
assert_equal({
|
143
|
+
:allow_nan => false,
|
144
|
+
:array_nl => "",
|
145
|
+
:ascii_only => false,
|
146
|
+
:buffer_initial_length => 1024,
|
147
|
+
:quirks_mode => false,
|
148
|
+
:depth => 0,
|
149
|
+
:indent => "",
|
150
|
+
:max_nesting => 100,
|
151
|
+
:object_nl => "",
|
152
|
+
:space => "",
|
153
|
+
:space_before => "",
|
154
|
+
}.sort_by { |n,| n.to_s }, state.to_h.sort_by { |n,| n.to_s })
|
155
|
+
end
|
156
|
+
|
157
|
+
def test_fast_state
|
158
|
+
state = FAST_STATE_PROTOTYPE.dup
|
159
|
+
assert_equal({
|
160
|
+
:allow_nan => false,
|
161
|
+
:array_nl => "",
|
162
|
+
:ascii_only => false,
|
163
|
+
:buffer_initial_length => 1024,
|
164
|
+
:quirks_mode => false,
|
165
|
+
:depth => 0,
|
166
|
+
:indent => "",
|
167
|
+
:max_nesting => 0,
|
168
|
+
:object_nl => "",
|
169
|
+
:space => "",
|
170
|
+
:space_before => "",
|
171
|
+
}.sort_by { |n,| n.to_s }, state.to_h.sort_by { |n,| n.to_s })
|
172
|
+
end
|
173
|
+
|
174
|
+
def test_allow_nan
|
175
|
+
assert_raises(GeneratorError) { generate([JSON::NaN]) }
|
176
|
+
assert_equal '[NaN]', generate([JSON::NaN], :allow_nan => true)
|
177
|
+
assert_raises(GeneratorError) { fast_generate([JSON::NaN]) }
|
178
|
+
assert_raises(GeneratorError) { pretty_generate([JSON::NaN]) }
|
179
|
+
assert_equal "[\n NaN\n]", pretty_generate([JSON::NaN], :allow_nan => true)
|
180
|
+
assert_raises(GeneratorError) { generate([JSON::Infinity]) }
|
181
|
+
assert_equal '[Infinity]', generate([JSON::Infinity], :allow_nan => true)
|
182
|
+
assert_raises(GeneratorError) { fast_generate([JSON::Infinity]) }
|
183
|
+
assert_raises(GeneratorError) { pretty_generate([JSON::Infinity]) }
|
184
|
+
assert_equal "[\n Infinity\n]", pretty_generate([JSON::Infinity], :allow_nan => true)
|
185
|
+
assert_raises(GeneratorError) { generate([JSON::MinusInfinity]) }
|
186
|
+
assert_equal '[-Infinity]', generate([JSON::MinusInfinity], :allow_nan => true)
|
187
|
+
assert_raises(GeneratorError) { fast_generate([JSON::MinusInfinity]) }
|
188
|
+
assert_raises(GeneratorError) { pretty_generate([JSON::MinusInfinity]) }
|
189
|
+
assert_equal "[\n -Infinity\n]", pretty_generate([JSON::MinusInfinity], :allow_nan => true)
|
190
|
+
end
|
191
|
+
|
192
|
+
def test_depth
|
193
|
+
ary = []; ary << ary
|
194
|
+
assert_equal 0, JSON::SAFE_STATE_PROTOTYPE.depth
|
195
|
+
assert_raises(JSON::NestingError) { JSON.generate(ary) }
|
196
|
+
assert_equal 0, JSON::SAFE_STATE_PROTOTYPE.depth
|
197
|
+
assert_equal 0, JSON::PRETTY_STATE_PROTOTYPE.depth
|
198
|
+
assert_raises(JSON::NestingError) { JSON.pretty_generate(ary) }
|
199
|
+
assert_equal 0, JSON::PRETTY_STATE_PROTOTYPE.depth
|
200
|
+
s = JSON.state.new
|
201
|
+
assert_equal 0, s.depth
|
202
|
+
assert_raises(JSON::NestingError) { ary.to_json(s) }
|
203
|
+
assert_equal 100, s.depth
|
204
|
+
end
|
205
|
+
|
206
|
+
def test_buffer_initial_length
|
207
|
+
s = JSON.state.new
|
208
|
+
assert_equal 1024, s.buffer_initial_length
|
209
|
+
s.buffer_initial_length = 0
|
210
|
+
assert_equal 1024, s.buffer_initial_length
|
211
|
+
s.buffer_initial_length = -1
|
212
|
+
assert_equal 1024, s.buffer_initial_length
|
213
|
+
s.buffer_initial_length = 128
|
214
|
+
assert_equal 128, s.buffer_initial_length
|
215
|
+
end
|
216
|
+
|
217
|
+
def test_gc
|
218
|
+
bignum_too_long_to_embed_as_string = 1234567890123456789012345
|
219
|
+
expect = bignum_too_long_to_embed_as_string.to_s
|
220
|
+
stress, GC.stress = GC.stress, true
|
221
|
+
|
222
|
+
10.times do |i|
|
223
|
+
tmp = bignum_too_long_to_embed_as_string.to_json
|
224
|
+
assert_equal expect, tmp
|
225
|
+
end
|
226
|
+
ensure
|
227
|
+
GC.stress = stress
|
228
|
+
end if GC.respond_to?(:stress=)
|
229
|
+
|
230
|
+
def test_configure_using_configure_and_merge
|
231
|
+
numbered_state = {
|
232
|
+
:indent => "1",
|
233
|
+
:space => '2',
|
234
|
+
:space_before => '3',
|
235
|
+
:object_nl => '4',
|
236
|
+
:array_nl => '5'
|
237
|
+
}
|
238
|
+
state1 = JSON.state.new
|
239
|
+
state1.merge(numbered_state)
|
240
|
+
assert_equal '1', state1.indent
|
241
|
+
assert_equal '2', state1.space
|
242
|
+
assert_equal '3', state1.space_before
|
243
|
+
assert_equal '4', state1.object_nl
|
244
|
+
assert_equal '5', state1.array_nl
|
245
|
+
state2 = JSON.state.new
|
246
|
+
state2.configure(numbered_state)
|
247
|
+
assert_equal '1', state2.indent
|
248
|
+
assert_equal '2', state2.space
|
249
|
+
assert_equal '3', state2.space_before
|
250
|
+
assert_equal '4', state2.object_nl
|
251
|
+
assert_equal '5', state2.array_nl
|
252
|
+
end
|
253
|
+
|
254
|
+
def test_configure_hash_conversion
|
255
|
+
state = JSON.state.new
|
256
|
+
state.configure(:indent => '1')
|
257
|
+
assert_equal '1', state.indent
|
258
|
+
state = JSON.state.new
|
259
|
+
foo = 'foo'
|
260
|
+
assert_raise(TypeError) do
|
261
|
+
state.configure(foo)
|
262
|
+
end
|
263
|
+
def foo.to_h
|
264
|
+
{ :indent => '2' }
|
265
|
+
end
|
266
|
+
state.configure(foo)
|
267
|
+
assert_equal '2', state.indent
|
268
|
+
end
|
269
|
+
|
270
|
+
if defined?(JSON::Ext::Generator)
|
271
|
+
def test_broken_bignum # [ruby-core:38867]
|
272
|
+
pid = fork do
|
273
|
+
Bignum.class_eval do
|
274
|
+
def to_s
|
275
|
+
end
|
276
|
+
end
|
277
|
+
begin
|
278
|
+
JSON::Ext::Generator::State.new.generate(1<<64)
|
279
|
+
exit 1
|
280
|
+
rescue TypeError
|
281
|
+
exit 0
|
282
|
+
end
|
283
|
+
end
|
284
|
+
_, status = Process.waitpid2(pid)
|
285
|
+
assert status.success?
|
286
|
+
rescue NotImplementedError
|
287
|
+
# forking to avoid modifying core class of a parent process and
|
288
|
+
# introducing race conditions of tests are run in parallel
|
289
|
+
end
|
290
|
+
end
|
291
|
+
|
292
|
+
def test_hash_likeness_set_symbol
|
293
|
+
state = JSON.state.new
|
294
|
+
assert_equal nil, state[:foo]
|
295
|
+
assert_equal nil.class, state[:foo].class
|
296
|
+
assert_equal nil, state['foo']
|
297
|
+
state[:foo] = :bar
|
298
|
+
assert_equal :bar, state[:foo]
|
299
|
+
assert_equal :bar, state['foo']
|
300
|
+
state_hash = state.to_hash
|
301
|
+
assert_kind_of Hash, state_hash
|
302
|
+
assert_equal :bar, state_hash[:foo]
|
303
|
+
end
|
304
|
+
|
305
|
+
def test_hash_likeness_set_string
|
306
|
+
state = JSON.state.new
|
307
|
+
assert_equal nil, state[:foo]
|
308
|
+
assert_equal nil, state['foo']
|
309
|
+
state['foo'] = :bar
|
310
|
+
assert_equal :bar, state[:foo]
|
311
|
+
assert_equal :bar, state['foo']
|
312
|
+
state_hash = state.to_hash
|
313
|
+
assert_kind_of Hash, state_hash
|
314
|
+
assert_equal :bar, state_hash[:foo]
|
315
|
+
end
|
316
|
+
|
317
|
+
def test_json_generate
|
318
|
+
assert_raise JSON::GeneratorError do
|
319
|
+
assert_equal true, JSON.generate(["\xea"])
|
320
|
+
end
|
321
|
+
end
|
322
|
+
end
|