yajl-ruby 1.2.3 → 1.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of yajl-ruby might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/.codeclimate.yml +40 -0
- data/.travis.yml +4 -13
- data/LICENSE +21 -0
- data/examples/encoding/chunked_encoding.rb +1 -1
- data/examples/encoding/one_shot.rb +1 -1
- data/examples/encoding/to_an_io.rb +1 -1
- data/examples/http/twitter_search_api.rb +1 -1
- data/examples/http/twitter_stream_api.rb +1 -1
- data/examples/parsing/from_file.rb +1 -1
- data/examples/parsing/from_stdin.rb +1 -1
- data/examples/parsing/from_string.rb +1 -1
- data/lib/yajl/bzip2.rb +2 -2
- data/lib/yajl/http_stream.rb +3 -2
- data/lib/yajl/version.rb +1 -1
- data/lib/yajl.rb +2 -2
- data/spec/encoding/encoding_spec.rb +45 -45
- data/spec/global/global_spec.rb +10 -10
- data/spec/http/http_delete_spec.rb +15 -15
- data/spec/http/http_error_spec.rb +7 -8
- data/spec/http/http_get_spec.rb +17 -17
- data/spec/http/http_post_spec.rb +19 -19
- data/spec/http/http_put_spec.rb +16 -16
- data/spec/http/http_stream_options_spec.rb +4 -4
- data/spec/json_gem_compatibility/compatibility_spec.rb +70 -70
- data/spec/parsing/active_support_spec.rb +10 -10
- data/spec/parsing/chunked_spec.rb +18 -18
- data/spec/parsing/fixtures_spec.rb +8 -8
- data/spec/parsing/large_number_spec.rb +1 -1
- data/spec/parsing/one_off_spec.rb +22 -22
- data/tasks/rspec.rake +1 -1
- data/yajl-ruby.gemspec +2 -2
- metadata +6 -5
- data/MIT-LICENSE +0 -20
@@ -9,12 +9,11 @@ require 'yajl/deflate'
|
|
9
9
|
require 'yajl/http_stream'
|
10
10
|
|
11
11
|
describe "Yajl HTTP error" do
|
12
|
-
before
|
12
|
+
before do
|
13
|
+
@uri = 'file://' + File.expand_path(File.dirname(__FILE__) + "/fixtures/http/http.error.dump")
|
13
14
|
@request = File.new(File.expand_path(File.dirname(__FILE__) + "/fixtures/http.error.dump"), 'r')
|
14
|
-
|
15
|
-
|
16
|
-
@request.should_receive(:write)
|
17
|
-
|
15
|
+
allow(TCPSocket).to receive(:new).and_return(@request)
|
16
|
+
allow(@request).to receive(:write)
|
18
17
|
begin
|
19
18
|
Yajl::HttpStream.get(@uri)
|
20
19
|
rescue Yajl::HttpStream::HttpError => e
|
@@ -23,10 +22,10 @@ describe "Yajl HTTP error" do
|
|
23
22
|
end
|
24
23
|
|
25
24
|
it "should contain the error code in the message" do
|
26
|
-
@error.message.
|
25
|
+
expect(@error.message).to match(/404/)
|
27
26
|
end
|
28
27
|
|
29
28
|
it "should provide the HTTP response headers" do
|
30
|
-
@error.headers.keys.
|
29
|
+
expect(@error.headers.keys).to include('ETag', 'Content-Length', 'Server')
|
31
30
|
end
|
32
|
-
end
|
31
|
+
end
|
data/spec/http/http_get_spec.rb
CHANGED
@@ -38,72 +38,72 @@ describe "Yajl HTTP GET request" do
|
|
38
38
|
def prepare_mock_request_dump(format=:raw)
|
39
39
|
@request = File.new(File.expand_path(File.dirname(__FILE__) + "/fixtures/http.#{format}.dump"), 'r')
|
40
40
|
@uri = 'file://'+File.expand_path(File.dirname(__FILE__) + "/fixtures/http/http.#{format}.dump")
|
41
|
-
TCPSocket.
|
42
|
-
@request.
|
41
|
+
expect(TCPSocket).to receive(:new).and_return(@request)
|
42
|
+
expect(@request).to receive(:write)
|
43
43
|
end
|
44
44
|
|
45
45
|
it "should parse a raw response" do
|
46
46
|
prepare_mock_request_dump :raw
|
47
|
-
@template_hash.
|
47
|
+
expect(@template_hash).to eq(Yajl::HttpStream.get(@uri))
|
48
48
|
end
|
49
49
|
|
50
50
|
it "should parse a raw response and symbolize keys" do
|
51
51
|
prepare_mock_request_dump :raw
|
52
|
-
@template_hash_symbolized.
|
52
|
+
expect(@template_hash_symbolized).to eq(Yajl::HttpStream.get(@uri, :symbolize_keys => true))
|
53
53
|
end
|
54
54
|
|
55
55
|
it "should parse a raw response using instance method" do
|
56
56
|
prepare_mock_request_dump :raw
|
57
|
-
@uri.
|
58
|
-
@uri.
|
57
|
+
expect(@uri).to receive(:host)
|
58
|
+
expect(@uri).to receive(:port)
|
59
59
|
stream = Yajl::HttpStream.new
|
60
|
-
@template_hash.
|
60
|
+
expect(@template_hash).to eq(stream.get(@uri))
|
61
61
|
end
|
62
62
|
|
63
63
|
it "should parse a chunked response using instance method" do
|
64
64
|
prepare_mock_request_dump :chunked
|
65
|
-
@uri.
|
66
|
-
@uri.
|
65
|
+
expect(@uri).to receive(:host)
|
66
|
+
expect(@uri).to receive(:port)
|
67
67
|
stream = Yajl::HttpStream.new
|
68
68
|
stream.get(@uri) do |obj|
|
69
|
-
obj.
|
69
|
+
expect(obj).to eql(@chunked_body)
|
70
70
|
end
|
71
71
|
end
|
72
72
|
|
73
73
|
if defined?(Yajl::Bzip2::StreamReader)
|
74
74
|
it "should parse a bzip2 compressed response" do
|
75
75
|
prepare_mock_request_dump :bzip2
|
76
|
-
@template_hash.
|
76
|
+
expect(@template_hash).to eq(Yajl::HttpStream.get(@uri))
|
77
77
|
end
|
78
78
|
|
79
79
|
it "should parse a bzip2 compressed response and symbolize keys" do
|
80
80
|
prepare_mock_request_dump :bzip2
|
81
|
-
@template_hash_symbolized.
|
81
|
+
expect(@template_hash_symbolized).to eq(Yajl::HttpStream.get(@uri, :symbolize_keys => true))
|
82
82
|
end
|
83
83
|
end
|
84
84
|
|
85
85
|
it "should parse a deflate compressed response" do
|
86
86
|
prepare_mock_request_dump :deflate
|
87
|
-
@template_hash.
|
87
|
+
expect(@template_hash).to eq(Yajl::HttpStream.get(@uri))
|
88
88
|
end
|
89
89
|
|
90
90
|
it "should parse a deflate compressed response and symbolize keys" do
|
91
91
|
prepare_mock_request_dump :deflate
|
92
|
-
@template_hash_symbolized.
|
92
|
+
expect(@template_hash_symbolized).to eq(Yajl::HttpStream.get(@uri, :symbolize_keys => true))
|
93
93
|
end
|
94
94
|
|
95
95
|
it "should parse a gzip compressed response" do
|
96
96
|
prepare_mock_request_dump :gzip
|
97
|
-
@template_hash.
|
97
|
+
expect(@template_hash).to eq(Yajl::HttpStream.get(@uri))
|
98
98
|
end
|
99
99
|
|
100
100
|
it "should parse a gzip compressed response and symbolize keys" do
|
101
101
|
prepare_mock_request_dump :gzip
|
102
|
-
@template_hash_symbolized.
|
102
|
+
expect(@template_hash_symbolized).to eq(Yajl::HttpStream.get(@uri, :symbolize_keys => true))
|
103
103
|
end
|
104
104
|
|
105
105
|
it "should raise when an HTTP code that isn't 200 is returned" do
|
106
106
|
prepare_mock_request_dump :error
|
107
|
-
|
107
|
+
expect { Yajl::HttpStream.get(@uri) }.to raise_exception(Yajl::HttpStream::HttpError)
|
108
108
|
end
|
109
109
|
end
|
data/spec/http/http_post_spec.rb
CHANGED
@@ -40,84 +40,84 @@ describe "Yajl HTTP POST request" do
|
|
40
40
|
def prepare_mock_request_dump(format=:raw)
|
41
41
|
@request = File.new(File.expand_path(File.dirname(__FILE__) + "/fixtures/http.#{format}.dump"), 'r')
|
42
42
|
@uri = 'file://'+File.expand_path(File.dirname(__FILE__) + "/fixtures/http/http.#{format}.dump")
|
43
|
-
TCPSocket.
|
44
|
-
@request.
|
43
|
+
expect(TCPSocket).to receive(:new).and_return(@request)
|
44
|
+
expect(@request).to receive(:write)
|
45
45
|
end
|
46
46
|
|
47
47
|
it "should parse a raw response" do
|
48
48
|
prepare_mock_request_dump :raw
|
49
|
-
@template_hash.
|
49
|
+
expect(@template_hash).to eq(Yajl::HttpStream.post(@uri, @body))
|
50
50
|
end
|
51
51
|
|
52
52
|
it "should parse a raw response using instance method" do
|
53
53
|
prepare_mock_request_dump :raw
|
54
|
-
@uri.
|
55
|
-
@uri.
|
54
|
+
expect(@uri).to receive(:host)
|
55
|
+
expect(@uri).to receive(:port)
|
56
56
|
stream = Yajl::HttpStream.new
|
57
|
-
@template_hash.
|
57
|
+
expect(@template_hash).to eq(stream.post(@uri, @body))
|
58
58
|
end
|
59
59
|
|
60
60
|
it "should parse a raw response with hashed body" do
|
61
61
|
prepare_mock_request_dump :raw
|
62
|
-
@template_hash.
|
62
|
+
expect(@template_hash).to eq(Yajl::HttpStream.post(@uri, @hashed_body))
|
63
63
|
end
|
64
64
|
|
65
65
|
it "should parse a raw response and symbolize keys" do
|
66
66
|
prepare_mock_request_dump :raw
|
67
|
-
@template_hash_symbolized.
|
67
|
+
expect(@template_hash_symbolized).to eq(Yajl::HttpStream.post(@uri, @body, :symbolize_keys => true))
|
68
68
|
end
|
69
69
|
|
70
70
|
if defined?(Yajl::Bzip2::StreamReader)
|
71
71
|
it "should parse a bzip2 compressed response" do
|
72
72
|
prepare_mock_request_dump :bzip2
|
73
|
-
@template_hash.
|
73
|
+
expect(@template_hash).to eq(Yajl::HttpStream.post(@uri, @body))
|
74
74
|
end
|
75
75
|
|
76
76
|
it "should parse a bzip2 compressed response and symbolize keys" do
|
77
77
|
prepare_mock_request_dump :bzip2
|
78
|
-
@template_hash_symbolized.
|
78
|
+
expect(@template_hash_symbolized).to eq(Yajl::HttpStream.post(@uri, @body, :symbolize_keys => true))
|
79
79
|
end
|
80
80
|
end
|
81
81
|
|
82
82
|
it "should parse a deflate compressed response" do
|
83
83
|
prepare_mock_request_dump :deflate
|
84
|
-
@template_hash.
|
84
|
+
expect(@template_hash).to eq(Yajl::HttpStream.post(@uri, @body))
|
85
85
|
end
|
86
86
|
|
87
87
|
it "should parse a deflate compressed response and symbolize keys" do
|
88
88
|
prepare_mock_request_dump :deflate
|
89
|
-
@template_hash_symbolized.
|
89
|
+
expect(@template_hash_symbolized).to eq(Yajl::HttpStream.post(@uri, @body, :symbolize_keys => true))
|
90
90
|
end
|
91
91
|
|
92
92
|
it "should parse a gzip compressed response" do
|
93
93
|
prepare_mock_request_dump :gzip
|
94
|
-
@template_hash.
|
94
|
+
expect(@template_hash).to eq(Yajl::HttpStream.post(@uri, @body))
|
95
95
|
end
|
96
96
|
|
97
97
|
it "should parse a gzip compressed response and symbolize keys" do
|
98
98
|
prepare_mock_request_dump :gzip
|
99
|
-
@template_hash_symbolized.
|
99
|
+
expect(@template_hash_symbolized).to eq(Yajl::HttpStream.post(@uri, @body, :symbolize_keys => true))
|
100
100
|
end
|
101
101
|
|
102
102
|
it "should parse a chunked raw response" do
|
103
103
|
prepare_mock_request_dump :chunked
|
104
104
|
Yajl::HttpStream.post(@uri, @body) do |obj|
|
105
|
-
obj.
|
105
|
+
expect(obj).to eql(@chunked_body)
|
106
106
|
end
|
107
107
|
end
|
108
108
|
|
109
109
|
it "should throw Exception if chunked response and no block given" do
|
110
110
|
prepare_mock_request_dump :chunked
|
111
|
-
|
111
|
+
expect {Yajl::HttpStream.post(@uri, @body)}.to raise_error(Exception)
|
112
112
|
end
|
113
113
|
|
114
114
|
it "should throw InvalidContentType if unable to handle the MIME type" do
|
115
115
|
prepare_mock_request_dump :html
|
116
|
-
|
116
|
+
expect {Yajl::HttpStream.post(@uri, @body)}.to raise_error(Yajl::HttpStream::InvalidContentType)
|
117
117
|
end
|
118
118
|
|
119
119
|
it "should raise when an HTTP code that isn't 200 is returned" do
|
120
120
|
prepare_mock_request_dump :error
|
121
|
-
|
121
|
+
expect { Yajl::HttpStream.post(@uri, @body) }.to raise_exception(Yajl::HttpStream::HttpError)
|
122
122
|
end
|
123
|
-
end
|
123
|
+
end
|
data/spec/http/http_put_spec.rb
CHANGED
@@ -39,67 +39,67 @@ describe "Yajl HTTP PUT request" do
|
|
39
39
|
def prepare_mock_request_dump(format=:raw)
|
40
40
|
@request = File.new(File.expand_path(File.dirname(__FILE__) + "/fixtures/http.#{format}.dump"), 'r')
|
41
41
|
@uri = 'file://'+File.expand_path(File.dirname(__FILE__) + "/fixtures/http/http.#{format}.dump")
|
42
|
-
TCPSocket.
|
43
|
-
@request.
|
42
|
+
expect(TCPSocket).to receive(:new).and_return(@request)
|
43
|
+
expect(@request).to receive(:write)
|
44
44
|
end
|
45
45
|
|
46
46
|
it "should parse a raw response" do
|
47
47
|
prepare_mock_request_dump :raw
|
48
|
-
@template_hash.
|
48
|
+
expect(@template_hash).to eq(Yajl::HttpStream.put(@uri, @body))
|
49
49
|
end
|
50
50
|
|
51
51
|
it "should parse a raw response using instance method" do
|
52
52
|
prepare_mock_request_dump :raw
|
53
|
-
@uri.
|
54
|
-
@uri.
|
53
|
+
expect(@uri).to receive(:host)
|
54
|
+
expect(@uri).to receive(:port)
|
55
55
|
stream = Yajl::HttpStream.new
|
56
|
-
@template_hash.
|
56
|
+
expect(@template_hash).to eq(stream.put(@uri, @body))
|
57
57
|
end
|
58
58
|
|
59
59
|
it "should parse a raw response with hashed body" do
|
60
60
|
prepare_mock_request_dump :raw
|
61
|
-
@template_hash.
|
61
|
+
expect(@template_hash).to eq(Yajl::HttpStream.post(@uri, @hashed_body))
|
62
62
|
end
|
63
63
|
|
64
64
|
it "should parse a raw response and symbolize keys" do
|
65
65
|
prepare_mock_request_dump :raw
|
66
|
-
@template_hash_symbolized.
|
66
|
+
expect(@template_hash_symbolized).to eq(Yajl::HttpStream.put(@uri, @body, :symbolize_keys => true))
|
67
67
|
end
|
68
68
|
|
69
69
|
if defined?(Yajl::Bzip2::StreamReader)
|
70
70
|
it "should parse a bzip2 compressed response" do
|
71
71
|
prepare_mock_request_dump :bzip2
|
72
|
-
@template_hash.
|
72
|
+
expect(@template_hash).to eq(Yajl::HttpStream.put(@uri, @body))
|
73
73
|
end
|
74
74
|
|
75
75
|
it "should parse a bzip2 compressed response and symbolize keys" do
|
76
76
|
prepare_mock_request_dump :bzip2
|
77
|
-
@template_hash_symbolized.
|
77
|
+
expect(@template_hash_symbolized).to eq(Yajl::HttpStream.put(@uri, @body, :symbolize_keys => true))
|
78
78
|
end
|
79
79
|
end
|
80
80
|
|
81
81
|
it "should parse a deflate compressed response" do
|
82
82
|
prepare_mock_request_dump :deflate
|
83
|
-
@template_hash.
|
83
|
+
expect(@template_hash).to eq(Yajl::HttpStream.put(@uri, @body))
|
84
84
|
end
|
85
85
|
|
86
86
|
it "should parse a deflate compressed response and symbolize keys" do
|
87
87
|
prepare_mock_request_dump :deflate
|
88
|
-
@template_hash_symbolized.
|
88
|
+
expect(@template_hash_symbolized).to eq(Yajl::HttpStream.put(@uri, @body, :symbolize_keys => true))
|
89
89
|
end
|
90
90
|
|
91
91
|
it "should parse a gzip compressed response" do
|
92
92
|
prepare_mock_request_dump :gzip
|
93
|
-
@template_hash.
|
93
|
+
expect(@template_hash).to eq(Yajl::HttpStream.put(@uri, @body))
|
94
94
|
end
|
95
95
|
|
96
96
|
it "should parse a gzip compressed response and symbolize keys" do
|
97
97
|
prepare_mock_request_dump :gzip
|
98
|
-
@template_hash_symbolized.
|
98
|
+
expect(@template_hash_symbolized).to eq(Yajl::HttpStream.put(@uri, @body, :symbolize_keys => true))
|
99
99
|
end
|
100
100
|
|
101
101
|
it "should raise when an HTTP code that isn't 200 is returned" do
|
102
102
|
prepare_mock_request_dump :error
|
103
|
-
|
103
|
+
expect { Yajl::HttpStream.put(@uri, @body) }.to raise_exception(Yajl::HttpStream::HttpError)
|
104
104
|
end
|
105
|
-
end
|
105
|
+
end
|
@@ -8,20 +8,20 @@ describe "Passing options to HttpStream instance methods" do
|
|
8
8
|
end
|
9
9
|
|
10
10
|
it "should not create a new socket it one is provided" do
|
11
|
-
TCPSocket.
|
11
|
+
expect(TCPSocket).not_to receive(:new)
|
12
12
|
options = {:socket => :my_provided_socket}
|
13
13
|
|
14
14
|
@stream.send(:initialize_socket, URI.parse("http://google.com"), options)
|
15
15
|
|
16
|
-
options[:socket].
|
16
|
+
expect(options[:socket]).to eq(:my_provided_socket)
|
17
17
|
end
|
18
18
|
|
19
19
|
it "should create a new socket if one is not provided" do
|
20
|
-
TCPSocket.
|
20
|
+
expect(TCPSocket).to receive(:new).with("google.com", 80).and_return( :tcp_socket )
|
21
21
|
options = {}
|
22
22
|
|
23
23
|
@stream.send(:initialize_socket, URI.parse("http://google.com"), options)
|
24
24
|
|
25
|
-
options[:socket].
|
25
|
+
expect(options[:socket]).to eq(:tcp_socket)
|
26
26
|
end
|
27
27
|
end
|
@@ -7,109 +7,109 @@ describe "JSON Gem compatability API" do
|
|
7
7
|
it "shoud not mixin #to_json on base objects until compatability has been enabled" do
|
8
8
|
d = Dummy.new
|
9
9
|
|
10
|
-
d.respond_to?(:to_json).
|
11
|
-
"".respond_to?(:to_json).
|
12
|
-
1.respond_to?(:to_json).
|
13
|
-
"1.5".to_f.respond_to?(:to_json).
|
14
|
-
[].respond_to?(:to_json).
|
15
|
-
{:foo => "bar"}.respond_to?(:to_json).
|
16
|
-
true.respond_to?(:to_json).
|
17
|
-
false.respond_to?(:to_json).
|
18
|
-
nil.respond_to?(:to_json).
|
10
|
+
expect(d.respond_to?(:to_json)).not_to be_truthy
|
11
|
+
expect("".respond_to?(:to_json)).not_to be_truthy
|
12
|
+
expect(1.respond_to?(:to_json)).not_to be_truthy
|
13
|
+
expect("1.5".to_f.respond_to?(:to_json)).not_to be_truthy
|
14
|
+
expect([].respond_to?(:to_json)).not_to be_truthy
|
15
|
+
expect({:foo => "bar"}.respond_to?(:to_json)).not_to be_truthy
|
16
|
+
expect(true.respond_to?(:to_json)).not_to be_truthy
|
17
|
+
expect(false.respond_to?(:to_json)).not_to be_truthy
|
18
|
+
expect(nil.respond_to?(:to_json)).not_to be_truthy
|
19
19
|
end
|
20
20
|
|
21
21
|
it "should mixin #to_json on base objects after compatability has been enabled" do
|
22
22
|
require 'yajl/json_gem'
|
23
23
|
d = Dummy.new
|
24
24
|
|
25
|
-
d.respond_to?(:to_json).
|
26
|
-
"".respond_to?(:to_json).
|
27
|
-
1.respond_to?(:to_json).
|
28
|
-
"1.5".to_f.respond_to?(:to_json).
|
29
|
-
[].respond_to?(:to_json).
|
30
|
-
{:foo => "bar"}.respond_to?(:to_json).
|
31
|
-
true.respond_to?(:to_json).
|
32
|
-
false.respond_to?(:to_json).
|
33
|
-
nil.respond_to?(:to_json).
|
25
|
+
expect(d.respond_to?(:to_json)).to be_truthy
|
26
|
+
expect("".respond_to?(:to_json)).to be_truthy
|
27
|
+
expect(1.respond_to?(:to_json)).to be_truthy
|
28
|
+
expect("1.5".to_f.respond_to?(:to_json)).to be_truthy
|
29
|
+
expect([].respond_to?(:to_json)).to be_truthy
|
30
|
+
expect({:foo => "bar"}.respond_to?(:to_json)).to be_truthy
|
31
|
+
expect(true.respond_to?(:to_json)).to be_truthy
|
32
|
+
expect(false.respond_to?(:to_json)).to be_truthy
|
33
|
+
expect(nil.respond_to?(:to_json)).to be_truthy
|
34
34
|
end
|
35
35
|
|
36
36
|
it "should require yajl/json_gem to enable the compatability API" do
|
37
|
-
defined?(JSON).
|
37
|
+
expect(defined?(JSON)).to be_truthy
|
38
38
|
|
39
|
-
JSON.respond_to?(:parse).
|
40
|
-
JSON.respond_to?(:generate).
|
41
|
-
JSON.respond_to?(:pretty_generate).
|
42
|
-
JSON.respond_to?(:load).
|
43
|
-
JSON.respond_to?(:dump).
|
39
|
+
expect(JSON.respond_to?(:parse)).to be_truthy
|
40
|
+
expect(JSON.respond_to?(:generate)).to be_truthy
|
41
|
+
expect(JSON.respond_to?(:pretty_generate)).to be_truthy
|
42
|
+
expect(JSON.respond_to?(:load)).to be_truthy
|
43
|
+
expect(JSON.respond_to?(:dump)).to be_truthy
|
44
44
|
end
|
45
45
|
|
46
46
|
it "should allow default parsing options be set with JSON.default_options" do
|
47
47
|
default = JSON.default_options[:symbolize_keys]
|
48
|
-
JSON.parse('{"foo": 1234}').
|
48
|
+
expect(JSON.parse('{"foo": 1234}')).to be === {"foo" => 1234}
|
49
49
|
JSON.default_options[:symbolize_keys] = true
|
50
|
-
JSON.parse('{"foo": 1234}').
|
50
|
+
expect(JSON.parse('{"foo": 1234}')).to be === {:foo => 1234}
|
51
51
|
JSON.default_options[:symbolize_keys] = default # ensure the rest of the test cases expect the default
|
52
52
|
end
|
53
53
|
|
54
54
|
it "should also allow the json gem's symbolize_names key" do
|
55
|
-
JSON.parse('{"foo": 1234}', :symbolize_names => true).
|
55
|
+
expect(JSON.parse('{"foo": 1234}', :symbolize_names => true)).to be === {:foo => 1234}
|
56
56
|
end
|
57
57
|
|
58
58
|
it "should encode arbitrary classes via their default to_json method" do
|
59
59
|
d = Dummy.new
|
60
|
-
d.to_json.
|
60
|
+
expect(d.to_json).to eq("\"#{d.to_s}\"")
|
61
61
|
|
62
62
|
t = Time.now
|
63
|
-
t.to_json.
|
63
|
+
expect(t.to_json).to eq("\"#{t.to_s}\"")
|
64
64
|
|
65
65
|
da = Date.today
|
66
|
-
da.to_json.
|
66
|
+
expect(da.to_json).to eq("\"#{da.to_s}\"")
|
67
67
|
|
68
68
|
dt = DateTime.new
|
69
|
-
dt.to_json.
|
69
|
+
expect(dt.to_json).to eq("\"#{dt.to_s}\"")
|
70
70
|
end
|
71
71
|
|
72
72
|
it "should have the standard parsing and encoding exceptions mapped" do
|
73
|
-
JSON::JSONError.new.is_a?(StandardError).
|
74
|
-
JSON::ParserError.new.is_a?(JSON::JSONError).
|
75
|
-
JSON::GeneratorError.new.is_a?(JSON::JSONError).
|
73
|
+
expect(JSON::JSONError.new.is_a?(StandardError)).to be_truthy
|
74
|
+
expect(JSON::ParserError.new.is_a?(JSON::JSONError)).to be_truthy
|
75
|
+
expect(JSON::GeneratorError.new.is_a?(JSON::JSONError)).to be_truthy
|
76
76
|
|
77
|
-
|
77
|
+
expect {
|
78
78
|
JSON.parse("blah")
|
79
|
-
}.
|
79
|
+
}.to raise_error(JSON::ParserError)
|
80
80
|
|
81
|
-
|
81
|
+
expect {
|
82
82
|
JSON.generate(0.0/0.0)
|
83
|
-
}.
|
83
|
+
}.to raise_error(JSON::GeneratorError)
|
84
84
|
end
|
85
85
|
|
86
86
|
context "ported tests for Unicode" do
|
87
87
|
it "should be able to encode and parse unicode" do
|
88
|
-
'""'.
|
89
|
-
'"\\b"'.
|
90
|
-
'"\u0001"'.
|
91
|
-
'"\u001F"'.
|
92
|
-
'" "'.
|
93
|
-
"\"#{0x7f.chr}\"".
|
88
|
+
expect('""').to eql(''.to_json)
|
89
|
+
expect('"\\b"').to eql("\b".to_json)
|
90
|
+
expect('"\u0001"').to eql(0x1.chr.to_json)
|
91
|
+
expect('"\u001F"').to eql(0x1f.chr.to_json)
|
92
|
+
expect('" "').to eql(' '.to_json)
|
93
|
+
expect("\"#{0x7f.chr}\"").to eql(0x7f.chr.to_json)
|
94
94
|
utf8 = [ "© ≠ €! \01" ]
|
95
95
|
json = "[\"© ≠ €! \\u0001\"]"
|
96
|
-
json.
|
97
|
-
utf8.
|
96
|
+
expect(json).to eql(utf8.to_json)
|
97
|
+
expect(utf8).to eql(JSON.parse(json))
|
98
98
|
utf8 = ["\343\201\202\343\201\204\343\201\206\343\201\210\343\201\212"]
|
99
99
|
json = "[\"あいうえお\"]"
|
100
|
-
json.
|
101
|
-
utf8.
|
100
|
+
expect(json).to eql(utf8.to_json)
|
101
|
+
expect(utf8).to eql(JSON.parse(json))
|
102
102
|
utf8 = ['საქართველო']
|
103
103
|
json = "[\"საქართველო\"]"
|
104
|
-
json.
|
105
|
-
utf8.
|
106
|
-
'["Ã"]'.
|
107
|
-
["€"].
|
104
|
+
expect(json).to eql(utf8.to_json)
|
105
|
+
expect(utf8).to eql(JSON.parse(json))
|
106
|
+
expect('["Ã"]').to eql(JSON.generate(["Ã"]))
|
107
|
+
expect(["€"]).to eql(JSON.parse('["\u20ac"]'))
|
108
108
|
utf8_str = "\xf0\xa0\x80\x81"
|
109
109
|
utf8 = [utf8_str]
|
110
110
|
json = "[\"#{utf8_str}\"]"
|
111
|
-
json.
|
112
|
-
utf8.
|
111
|
+
expect(json).to eql(JSON.generate(utf8))
|
112
|
+
expect(utf8).to eql(JSON.parse(json))
|
113
113
|
end
|
114
114
|
end
|
115
115
|
|
@@ -144,25 +144,25 @@ describe "JSON Gem compatability API" do
|
|
144
144
|
|
145
145
|
it "should be able to unparse" do
|
146
146
|
json = JSON.generate(@hash)
|
147
|
-
JSON.parse(@json2).
|
147
|
+
expect(JSON.parse(@json2)).to eq(JSON.parse(json))
|
148
148
|
parsed_json = JSON.parse(json)
|
149
|
-
@hash.
|
149
|
+
expect(@hash).to eq(parsed_json)
|
150
150
|
json = JSON.generate({1=>2})
|
151
|
-
'{"1":2}'.
|
151
|
+
expect('{"1":2}').to eql(json)
|
152
152
|
parsed_json = JSON.parse(json)
|
153
|
-
{"1"=>2}.
|
153
|
+
expect({"1"=>2}).to eq(parsed_json)
|
154
154
|
end
|
155
155
|
|
156
156
|
it "should be able to unparse pretty" do
|
157
157
|
json = JSON.pretty_generate(@hash)
|
158
|
-
JSON.parse(@json3).
|
158
|
+
expect(JSON.parse(@json3)).to eq(JSON.parse(json))
|
159
159
|
parsed_json = JSON.parse(json)
|
160
|
-
@hash.
|
160
|
+
expect(@hash).to eq(parsed_json)
|
161
161
|
json = JSON.pretty_generate({1=>2})
|
162
162
|
test = "{\n \"1\": 2\n}".chomp
|
163
|
-
test.
|
163
|
+
expect(test).to eq(json)
|
164
164
|
parsed_json = JSON.parse(json)
|
165
|
-
{"1"=>2}.
|
165
|
+
expect({"1"=>2}).to eq(parsed_json)
|
166
166
|
end
|
167
167
|
end
|
168
168
|
|
@@ -174,33 +174,33 @@ describe "JSON Gem compatability API" do
|
|
174
174
|
|
175
175
|
JSON_FAILED.each do |name, source|
|
176
176
|
it "should not be able to parse #{File.basename(name)} as an IO" do
|
177
|
-
|
177
|
+
expect {
|
178
178
|
JSON.parse(StringIO.new(source))
|
179
|
-
}.
|
179
|
+
}.to raise_error(JSON::ParserError)
|
180
180
|
end
|
181
181
|
end
|
182
182
|
|
183
183
|
JSON_FAILED.each do |name, source|
|
184
184
|
it "should not be able to parse #{File.basename(name)} as a string" do
|
185
|
-
|
185
|
+
expect {
|
186
186
|
JSON.parse(source)
|
187
|
-
}.
|
187
|
+
}.to raise_error(JSON::ParserError)
|
188
188
|
end
|
189
189
|
end
|
190
190
|
|
191
191
|
JSON_PASSED.each do |name, source|
|
192
192
|
it "should be able to parse #{File.basename(name)} as an IO" do
|
193
|
-
|
193
|
+
expect {
|
194
194
|
JSON.parse(StringIO.new(source))
|
195
|
-
}.
|
195
|
+
}.not_to raise_error
|
196
196
|
end
|
197
197
|
end
|
198
198
|
|
199
199
|
JSON_PASSED.each do |name, source|
|
200
200
|
it "should be able to parse #{File.basename(name)} as a string" do
|
201
|
-
|
201
|
+
expect {
|
202
202
|
JSON.parse(source)
|
203
|
-
}.
|
203
|
+
}.not_to raise_error
|
204
204
|
end
|
205
205
|
end
|
206
206
|
end
|
@@ -36,29 +36,29 @@ describe "ActiveSupport test cases" do
|
|
36
36
|
|
37
37
|
TESTS.each do |json, expected|
|
38
38
|
it "should be able to parse #{json} as an IO" do
|
39
|
-
|
40
|
-
Yajl::Parser.parse(StringIO.new(json)).
|
41
|
-
}.
|
39
|
+
expect {
|
40
|
+
expect(Yajl::Parser.parse(StringIO.new(json))).to eq(expected)
|
41
|
+
}.not_to raise_error
|
42
42
|
end
|
43
43
|
end
|
44
44
|
|
45
45
|
TESTS.each do |json, expected|
|
46
46
|
it "should be able to parse #{json} as a string" do
|
47
|
-
|
48
|
-
Yajl::Parser.parse(json).
|
49
|
-
}.
|
47
|
+
expect {
|
48
|
+
expect(Yajl::Parser.parse(json)).to eq(expected)
|
49
|
+
}.not_to raise_error
|
50
50
|
end
|
51
51
|
end
|
52
52
|
|
53
53
|
it "should fail parsing {: 1} as an IO" do
|
54
|
-
|
54
|
+
expect {
|
55
55
|
Yajl::Parser.parse(StringIO.new("{: 1}"))
|
56
|
-
}.
|
56
|
+
}.to raise_error(Yajl::ParseError)
|
57
57
|
end
|
58
58
|
|
59
59
|
it "should fail parsing {: 1} as a string" do
|
60
|
-
|
60
|
+
expect {
|
61
61
|
Yajl::Parser.parse("{: 1}")
|
62
|
-
}.
|
62
|
+
}.to raise_error(Yajl::ParseError)
|
63
63
|
end
|
64
64
|
end
|