-
-
Notifications
You must be signed in to change notification settings - Fork 251
/
test_mimic.rb
executable file
·194 lines (169 loc) · 4.6 KB
/
test_mimic.rb
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
#!/usr/bin/env ruby -wW1
# encoding: UTF-8
$: << File.join(File.dirname(__FILE__), "../lib")
$: << File.join(File.dirname(__FILE__), "../ext")
require 'test/unit'
require 'stringio'
require 'oj'
$ruby = RUBY_DESCRIPTION.split(' ')[0]
$ruby = 'ree' if 'ruby' == $ruby && RUBY_DESCRIPTION.include?('Ruby Enterprise Edition')
class Jam
attr_accessor :x, :y
def initialize(x, y)
@x = x
@y = y
end
def eql?(o)
self.class == o.class && @x == o.x && @y == o.y
end
alias == eql?
def to_json()
%{{"json_class":"#{self.class}","x":#{@x},"y":#{@y}}}
end
def self.json_create(h)
self.new(h['x'], h['y'])
end
end # Jam
class Mimic < ::Test::Unit::TestCase
def test0_mimic_json
assert(defined?(JSON).nil?)
Oj.mimic_JSON
assert(!defined?(JSON).nil?)
end
# dump
def test_dump_string
json = JSON.dump([1, true, nil])
assert_equal(%{[1,true,null]}, json)
end
def test_dump_io
s = StringIO.new()
json = JSON.dump([1, true, nil], s)
assert_equal(s, json)
assert_equal(%{[1,true,null]}, s.string)
end
# TBD options
# load
def test_load_string
json = %{{"a":1,"b":[true,false]}}
obj = JSON.load(json)
assert_equal({ 'a' => 1, 'b' => [true, false]}, obj)
end
def test_load_io
json = %{{"a":1,"b":[true,false]}}
obj = JSON.load(StringIO.new(json))
assert_equal({ 'a' => 1, 'b' => [true, false]}, obj)
end
def test_load_proc
children = []
json = %{{"a":1,"b":[true,false]}}
if 'rubinius' == $ruby || 'jruby' == $ruby || '1.8.7' == RUBY_VERSION
obj = JSON.load(json) {|x| children << x }
else
p = Proc.new {|x| children << x }
obj = JSON.load(json, p)
end
assert_equal({ 'a' => 1, 'b' => [true, false]}, obj)
assert([1, true, false, [true, false], { 'a' => 1, 'b' => [true, false]}] == children ||
[true, false, [true, false], 1, { 'a' => 1, 'b' => [true, false]}] == children)
end
# []
def test_bracket_load
json = %{{"a":1,"b":[true,false]}}
obj = JSON[json]
assert_equal({ 'a' => 1, 'b' => [true, false]}, obj)
end
def test_bracket_dump
json = JSON[[1, true, nil]]
assert_equal(%{[1,true,null]}, json)
end
# generate
def test_generate
json = JSON.generate({ 'a' => 1, 'b' => [true, false]})
assert(%{{"a":1,"b":[true,false]}} == json ||
%{{"b":[true,false],"a":1}} == json)
end
def test_generate_options
json = JSON.generate({ 'a' => 1, 'b' => [true, false]},
:indent => "--",
:array_nl => "\n",
:object_nl => "#\n",
:space => "*",
:space_before => "~")
assert(%{{#
--"a"~:*1,#
--"b"~:*[
----true,
----false
--]#
}} == json ||
%{{#
--"b"~:*[
----true,
----false
--],#
--"a"~:*1#
}} == json)
end
# fast_generate
def test_fast_generate
json = JSON.generate({ 'a' => 1, 'b' => [true, false]})
assert(%{{"a":1,"b":[true,false]}} == json ||
%{{"b":[true,false],"a":1}} == json)
end
# pretty_generate
def test_pretty_generate
json = JSON.pretty_generate({ 'a' => 1, 'b' => [true, false]})
assert(%{{
"a" : 1,
"b" : [
true,
false
]
}} == json ||
%{{
"b" : [
true,
false
],
"a" : 1
}} == json)
end
# parse
def test_parse
json = %{{"a":1,"b":[true,false]}}
obj = JSON.parse(json)
assert_equal({ 'a' => 1, 'b' => [true, false]}, obj)
end
def test_parse_sym_names
json = %{{"a":1,"b":[true,false]}}
obj = JSON.parse(json, :symbolize_names => true)
assert_equal({ :a => 1, :b => [true, false]}, obj)
end
def test_parse_additions
jam = Jam.new(true, 58)
json = Oj.dump(jam, :mode => :compat)
obj = JSON.parse(json)
assert_equal(jam, obj)
obj = JSON.parse(json, :create_additions => true)
assert_equal(jam, obj)
obj = JSON.parse(json, :create_additions => false)
assert_equal({'json_class' => 'Jam', 'x' => true, 'y' => 58}, obj)
json.gsub!('json_class', 'kson_class')
JSON.create_id = 'kson_class'
obj = JSON.parse(json, :create_additions => true)
JSON.create_id = 'json_class'
assert_equal(jam, obj)
end
def test_parse_bang
json = %{{"a":1,"b":[true,false]}}
obj = JSON.parse!(json)
assert_equal({ 'a' => 1, 'b' => [true, false]}, obj)
end
# recurse_proc
def test_recurse_proc
children = []
obj = JSON.recurse_proc({ 'a' => 1, 'b' => [true, false]}) { |x| children << x }
assert([1, true, false, [true, false], { 'a' => 1, 'b' => [true, false]}] == children ||
[true, false, [true, false], 1, { 'b' => [true, false], 'a' => 1}] == children)
end
end # Mimic