blob: 41587e26642bde96f9b46a3931018deb84740a30 (
plain)
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
|
# encoding: UTF-8
require 'spec_helper'
require 'date'
describe "FFI_Yajl::Encoder" do
let(:encoder) { FFI_Yajl::Encoder.new }
it "encodes hashes in keys as strings" do
ruby = { {'a' => 'b'} => 2 }
expect(encoder.encode(ruby)).to eq('{"{\"a\"=>\"b\"}":2}')
end
it "encodes arrays in keys as strings" do
ruby = { [0,1] => 2 }
expect(encoder.encode(ruby)).to eq('{"[0, 1]":2}')
end
it "encodes nil in keys as strings" do
ruby = { nil => 2 }
expect(encoder.encode(ruby)).to eq('{"":2}')
end
it "encodes true in keys as strings" do
ruby = { true => 2 }
expect(encoder.encode(ruby)).to eq('{"true":2}')
end
it "encodes false in keys as strings" do
ruby = { false => 2 }
expect(encoder.encode(ruby)).to eq('{"false":2}')
end
it "encodes fixnums in keys as strings" do
ruby = { 1 => 2 }
expect(encoder.encode(ruby)).to eq('{"1":2}')
end
it "encodes floats in keys as strings" do
ruby = { 1.1 => 2 }
expect(encoder.encode(ruby)).to eq('{"1.1":2}')
end
it "encodes bignums in keys as strings" do
ruby = { 12345678901234567890 => 2 }
expect(encoder.encode(ruby)).to eq('{"12345678901234567890":2}')
end
# XXX: 127 == YAJL_MAX_DEPTH hardcodedness, zero control for us, it isn't even a twiddleable #define
it "raises an exception for deeply nested arrays" do
root = []
a = root
127.times { |_| a << []; a = a[0] }
expect{ encoder.encode(root) }.to raise_error(FFI_Yajl::EncodeError)
end
it "raises an exception for deeply nested hashes" do
root = {}
a = root
127.times {|_| a["a"] = {}; a = a["a"] }
expect{ encoder.encode(root) }.to raise_error(FFI_Yajl::EncodeError)
end
it "encodes symbols in keys as strings" do
ruby = { :thing => 1 }
expect(encoder.encode(ruby)).to eq('{"thing":1}')
end
it "encodes symbols in values as strings" do
ruby = { "thing" => :one }
expect(encoder.encode(ruby)).to eq('{"thing":"one"}')
end
it "can encode 32-bit unsigned ints" do
ruby = { "gid"=>4294967294 }
expect(encoder.encode(ruby)).to eq('{"gid":4294967294}')
end
context "when the encoder has nil passed in for options" do
let(:encoder) { FFI_Yajl::Encoder.new(nil) }
it "does not throw an exception" do
ruby = { "foo" => "bar" }
expect(encoder.encode(ruby)).to eq("{\"foo\":\"bar\"}")
end
end
it "can encode Date objects" do
ruby = Date.parse('2001-02-03')
expect(encoder.encode(ruby)).to eq( %q{"2001-02-03"} )
end
context "when encoding Time objects in UTC timezone" do
before do
@saved_tz = ENV['TZ']
ENV['TZ'] = 'UTC'
end
after do
ENV['TZ'] = @saved_tz
end
it "encodes them correctly" do
ruby = Time.local(2001, 02, 02, 21, 05, 06)
expect(encoder.encode(ruby)).to eq( %q{"2001-02-02 21:05:06 +0000"} )
end
end
it "can encode DateTime objects" do
ruby = DateTime.parse('2001-02-03T04:05:06.1+07:00')
expect(encoder.encode(ruby)).to eq( %q{"2001-02-03T04:05:06+07:00"} )
end
describe "testing .to_json for Objects" do
class NoToJson; end
class HasToJson
def to_json(*args)
"{}"
end
end
it "calls .to_s for objects without .to_json" do
expect(encoder.encode(NoToJson.new)).to match(/^"#<NoToJson:\w+>"$/)
end
it "calls .to_json for objects wit .to_json" do
expect(encoder.encode(HasToJson.new)).to eq("{}")
end
end
end
|