-
Notifications
You must be signed in to change notification settings - Fork 177
Expand file tree
/
Copy pathchunked_spec.rb
More file actions
105 lines (91 loc) · 3.36 KB
/
chunked_spec.rb
File metadata and controls
105 lines (91 loc) · 3.36 KB
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
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper.rb')
describe "Chunked parser" do
before(:all) do
@final = [{"abc" => 123}, {"def" => 456}]
end
before(:each) do
@callback = lambda { |hash|
# no-op
}
@parser = Yajl::Parser.new
@parser.on_parse_complete = @callback
end
it "should take the callback as block parameter" do
@callback = lambda { |hash|
# no-op
}
@parser = Yajl::Parser.new(&@callback)
@callback.should_receive(:call).with(@final)
@parser << '[{"abc": 123},{"def": 456}]'
end
it "should parse a single chunk" do
@callback.should_receive(:call).with(@final)
@parser << '[{"abc": 123},{"def": 456}]'
end
it "should parse a single chunk, 3 times" do
@callback.should_receive(:call).with(@final).exactly(3).times
@parser << '[{"abc": 123},{"def": 456}]'
@parser << '[{"abc": 123},{"def": 456}]'
@parser << '[{"abc": 123},{"def": 456}]'
end
it "should parse in two chunks" do
@callback.should_receive(:call).with(@final)
@parser << '[{"abc": 123},'
@parser << '{"def": 456}]'
end
it "should parse in 2 chunks, twice" do
@callback.should_receive(:call).with(@final).exactly(2).times
@parser << '[{"abc": 123},'
@parser << '{"def": 456}]'
@parser << '[{"abc": 123},'
@parser << '{"def": 456}]'
end
it "should parse 2 JSON strings, in 3 chunks" do
@callback.should_receive(:call).with(@final).exactly(2).times
@parser << '[{"abc": 123},'
@parser << '{"def": 456}][{"abc": 123},{"def":'
@parser << ' 456}]'
end
it "should parse 2 JSON strings in 1 chunk" do
@callback.should_receive(:call).with(@final).exactly(2).times
@parser << '[{"abc": 123},{"def": 456}][{"abc": 123},{"def": 456}]'
end
it "should parse 2 JSON strings from an IO" do
@callback.should_receive(:call).with(@final).exactly(2).times
@parser.parse(StringIO.new('[{"abc": 123},{"def": 456}][{"abc": 123},{"def": 456}]'))
end
it "should parse a JSON string an IO and fire callback once" do
@callback.should_receive(:call).with(@final)
@parser.parse(StringIO.new('[{"abc": 123},{"def": 456}]'))
end
it "should parse twitter_stream.json and fire callback 430 times" do
path = File.expand_path(File.dirname(__FILE__) + '/../../benchmark/subjects/twitter_stream.json')
json = File.new(path, 'r')
@callback.should_receive(:call).exactly(430).times
lambda {
@parser.parse(json)
}.should_not raise_error
end
it "should parse twitter_stream.json and fire callback 430 times, with a block as the callback" do
path = File.expand_path(File.dirname(__FILE__) + '/../../benchmark/subjects/twitter_stream.json')
json = File.new(path, 'r')
@callback.should_receive(:call).exactly(0).times
@parser.on_parse_complete = nil
lambda {
times = 0
@parser.parse(json) do |hsh|
times += 1
end
times.should eql(430)
}.should_not raise_error
end
it "should raise a Yajl::ParseError error if multiple JSON strings were found when no on_parse_complete callback assigned" do
path = File.expand_path(File.dirname(__FILE__) + '/../../benchmark/subjects/twitter_stream.json')
json = File.new(path, 'r')
@parser.on_parse_complete = nil
@callback.should_receive(:call).exactly(0).times
lambda {
@parser.parse(json)
}.should raise_error(Yajl::ParseError)
end
end