-
Notifications
You must be signed in to change notification settings - Fork 129
/
adapter_shared_example.rb
234 lines (197 loc) · 6.53 KB
/
adapter_shared_example.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
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
# encoding: UTF-8
shared_examples_for 'an adapter' do |adapter|
before do
begin
MultiJson.use adapter
rescue LoadError
pending "Adapter #{adapter} couldn't be loaded (not installed?)"
end
end
it_behaves_like 'has options', lambda{ MultiJson.adapter }
it 'does not modify argument hashes' do
options = { :symbolize_keys => true, :pretty => false, :adapter => :json_gem }
expect{MultiJson.load('{}', options)}.to_not change{options}
expect{MultiJson.dump([42], options)}.to_not change{options}
end
describe '.dump' do
describe '#dump_options' do
before{ MultiJson.dump_options = MultiJson.adapter.dump_options = {} }
after do
MultiJson.adapter.instance.should_receive(:dump).with(1, :foo=>'bar', :fizz=>'buzz')
MultiJson.dump(1, :fizz => 'buzz')
MultiJson.dump_options = MultiJson.adapter.dump_options = nil
end
it 'respects global dump options' do
MultiJson.dump_options = {:foo => 'bar'}
end
it 'respects per-adapter dump options' do
MultiJson.adapter.dump_options = {:foo => 'bar'}
end
it 'adapter-specific are overridden by global options' do
MultiJson.adapter.dump_options = {:foo => 'foo'}
MultiJson.dump_options = {:foo => 'bar'}
end
end
it 'writes decodable JSON' do
[
{'abc' => 'def'},
[1, 2, 3, '4', true, false, nil]
].each do |example|
expect(MultiJson.load(MultiJson.dump(example))).to eq example
end
end
unless 'json_pure' == adapter || 'json_gem' == adapter
it 'dumps time in correct format' do
time = Time.at(1355218745).utc
# time does not respond to to_json method
class << time
undef_method :to_json
end
dumped_json = MultiJson.dump(time)
expected = if RUBY_VERSION > '1.9'
'2012-12-11 09:39:05 UTC'
else
'Tue Dec 11 09:39:05 UTC 2012'
end
expect(MultiJson.load(dumped_json)).to eq expected
end
end
it 'dumps symbol and fixnum keys as strings' do
[
[
{:foo => {:bar => 'baz'}},
{'foo' => {'bar' => 'baz'}},
],
[
[{:foo => {:bar => 'baz'}}],
[{'foo' => {'bar' => 'baz'}}],
],
[
{:foo => [{:bar => 'baz'}]},
{'foo' => [{'bar' => 'baz'}]},
],
[
{1 => {2 => {3 => 'bar'}}},
{'1' => {'2' => {'3' => 'bar'}}}
]
].each do |example, expected|
dumped_json = MultiJson.dump(example)
expect(MultiJson.load(dumped_json)).to eq expected
end
end
it 'dumps rootless JSON' do
expect(MultiJson.dump('random rootless string')).to eq '"random rootless string"'
expect(MultiJson.dump(123)).to eq '123'
end
it 'passes options to the adapter' do
MultiJson.adapter.should_receive(:dump).with('foo', {:bar => :baz})
MultiJson.dump('foo', :bar => :baz)
end
# This behavior is currently not supported by gson.rb
# See discussion at https://github.com/intridea/multi_json/pull/71
unless %w(gson jrjackson).include?(adapter)
it 'dumps custom objects that implement to_json' do
klass = Class.new do
def to_json(*)
'"foobar"'
end
end
expect(MultiJson.dump(klass.new)).to eq '"foobar"'
end
end
it 'allows to dump JSON values' do
expect(MultiJson.dump(42)).to eq '42'
end
it 'allows to dump JSON with UTF-8 characters' do
expect(MultiJson.dump({'color' => 'żółć'})).to eq('{"color":"żółć"}')
end
end
describe '.load' do
describe '#load_options' do
before{ MultiJson.load_options = MultiJson.adapter.load_options = {} }
after do
MultiJson.adapter.instance.should_receive(:load).with('1', :foo => 'bar', :fizz => 'buzz')
MultiJson.load('1', :fizz => 'buzz')
MultiJson.load_options = MultiJson.adapter.load_options = nil
end
it 'respects global load options' do
MultiJson.load_options = {:foo => 'bar'}
end
it 'respects per-adapter load options' do
MultiJson.adapter.load_options = {:foo => 'bar'}
end
it 'adapter-specific are overridden by global options' do
MultiJson.adapter.load_options = {:foo => 'foo'}
MultiJson.load_options = {:foo => 'bar'}
end
end
it 'does not modify input' do
input = %Q{\n\n {"foo":"bar"} \n\n\t}
expect{
MultiJson.load(input)
}.to_not change{ input }
end
it 'does not modify input encoding' do
pending 'only in 1.9' unless RUBY_VERSION > '1.9'
input = '[123]'
input.force_encoding('iso-8859-1')
expect{
MultiJson.load(input)
}.to_not change{ input.encoding }
end
it 'properly loads valid JSON' do
expect(MultiJson.load('{"abc":"def"}')).to eq({'abc' => 'def'})
end
it 'raises MultiJson::LoadError on invalid JSON' do
expect{MultiJson.load('{"abc"}')}.to raise_error(MultiJson::LoadError)
end
it 'raises MultiJson::LoadError with data on invalid JSON' do
data = '{invalid}'
begin
MultiJson.load(data)
rescue MultiJson::LoadError => le
expect(le.data).to eq data
end
end
it 'catches MultiJson::DecodeError for legacy support' do
data = '{invalid}'
begin
MultiJson.load(data)
rescue MultiJson::DecodeError => de
expect(de.data).to eq data
end
end
it 'stringifys symbol keys when encoding' do
dumped_json = MultiJson.dump(:a => 1, :b => {:c => 2})
expect(MultiJson.load(dumped_json)).to eq({'a' => 1, 'b' => {'c' => 2}})
end
it 'properly loads valid JSON in StringIOs' do
json = StringIO.new('{"abc":"def"}')
expect(MultiJson.load(json)).to eq({'abc' => 'def'})
end
it 'allows for symbolization of keys' do
[
[
'{"abc":{"def":"hgi"}}',
{:abc => {:def => 'hgi'}},
],
[
'[{"abc":{"def":"hgi"}}]',
[{:abc => {:def => 'hgi'}}],
],
[
'{"abc":[{"def":"hgi"}]}',
{:abc => [{:def => 'hgi'}]},
],
].each do |example, expected|
expect(MultiJson.load(example, :symbolize_keys => true)).to eq expected
end
end
it 'allows to load JSON values' do
expect(MultiJson.load('42')).to eq 42
end
it 'allows to load JSON with UTF-8 characters' do
expect(MultiJson.load('{"color":"żółć"}')).to eq({'color' => 'żółć'})
end
end
end