/
multi_json_spec.rb
87 lines (76 loc) · 2.91 KB
/
multi_json_spec.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
require 'helper'
require 'adapter_shared_example'
require 'stringio'
describe 'MultiJson' do
context 'adapters' do
before do
MultiJson.use nil
end
context 'when no other json implementations are available' do
before do
@old_map = MultiJson::REQUIREMENT_MAP
@old_json = Object.const_get :JSON if Object.const_defined?(:JSON)
@old_oj = Object.const_get :Oj if Object.const_defined?(:Oj)
@old_yajl = Object.const_get :Yajl if Object.const_defined?(:Yajl)
MultiJson::REQUIREMENT_MAP.each_with_index do |(library, adapter), index|
MultiJson::REQUIREMENT_MAP[index] = ["foo/#{library}", adapter]
end
Object.send :remove_const, :JSON if @old_json
Object.send :remove_const, :Oj if @old_oj
Object.send :remove_const, :Yajl if @old_yajl
end
after do
@old_map.each_with_index do |(library, adapter), index|
MultiJson::REQUIREMENT_MAP[index] = [library, adapter]
end
Object.const_set :JSON, @old_json if @old_json
Object.const_set :Oj, @old_oj if @old_oj
Object.const_set :Yajl, @old_yajl if @old_yajl
end
it 'defaults to ok_json if no other json implementions are available' do
MultiJson.default_adapter.should == :ok_json
end
it 'prints a warning' do
Kernel.should_receive(:warn).with(/warning/i)
MultiJson.default_adapter
end
end
it 'defaults to the best available gem' do
unless jruby?
require 'oj'
MultiJson.adapter.name.should == 'MultiJson::Adapters::Oj'
else
require 'json'
MultiJson.adapter.name.should == 'MultiJson::Adapters::JsonGem'
end
end
it 'is settable via a symbol' do
MultiJson.use :json_gem
MultiJson.adapter.name.should == 'MultiJson::Adapters::JsonGem'
end
it 'is settable via a class' do
MultiJson.use MockDecoder
MultiJson.adapter.name.should == 'MockDecoder'
end
context "using one-shot parser" do
before(:each) do
require 'multi_json/adapters/json_pure'
MultiJson::Adapters::JsonPure.should_receive(:dump).exactly(1).times.and_return('dump_something')
MultiJson::Adapters::JsonPure.should_receive(:load).exactly(1).times.and_return('load_something')
end
it "should use the defined parser just for the call" do
MultiJson.use :yajl
MultiJson.dump('', :adapter => :json_pure).should eql('dump_something')
MultiJson.load('', :adapter => :json_pure).should eql('load_something')
MultiJson.adapter.to_s.should eql("MultiJson::Adapters::Yajl")
end
end
end
%w(json_gem json_pure nsjsonserialization oj ok_json yajl).each do |adapter|
next if !macruby? && adapter == 'nsjsonserialization'
next if jruby? && (adapter == 'oj' || adapter == 'yajl')
context adapter do
it_should_behave_like "an adapter", adapter
end
end
end