/
multi_json_spec.rb
63 lines (54 loc) · 1.61 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
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
class MockDecoder
def self.decode(string, options = {})
{'abc' => 'def'}
end
def self.encode(string)
'{"abc":"def"}'
end
end
describe "MultiJson" do
context 'engines' do
it 'should default to the best available gem' do
require 'yajl'
MultiJson.engine.name.should == 'MultiJson::Engines::Yajl'
end
it 'should be settable via a symbol' do
MultiJson.engine = :yajl
MultiJson.engine.name.should == 'MultiJson::Engines::Yajl'
end
it 'should be settable via a class' do
MultiJson.engine = MockDecoder
MultiJson.engine.name.should == 'MockDecoder'
end
end
%w(active_support json_gem json_pure yajl).each do |engine|
context engine do
before do
begin
MultiJson.engine = engine
rescue LoadError
pending "Engine #{engine} couldn't be loaded (not installed?)"
end
end
describe '.encode' do
it 'should write decodable JSON' do
[
{'abc' => 'def'},
[1, 2, 3, "4"]
].each do |example|
MultiJson.decode(MultiJson.encode(example)).should == example
end
end
end
describe '.decode' do
it 'should properly decode some json' do
MultiJson.decode('{"abc":"def"}').should == {'abc' => 'def'}
end
it 'should allow for symbolization of keys' do
MultiJson.decode('{"abc":{"def":"hgi"}}', :symbolize_keys => true).should == {:abc => {:def => 'hgi'}}
end
end
end
end
end