blob: e949753c579f6926179e483213964fd4fc64ea4f [file] [log] [blame]
Kevin Clarkf18b6432008-06-18 01:07:10 +00001require File.dirname(__FILE__) + '/spec_helper'
2require 'thrift/protocol/binaryprotocol'
3
4class ThriftSpec < Spec::ExampleGroup
5 include Thrift
6
7 describe BinaryProtocol do
8 before(:each) do
9 @trans = mock("MockTransport", :null_object => true)
10 @prot = BinaryProtocol.new(@trans)
11 end
12
13 it "should define the proper VERSION_1 and VERSION_MASK" do
14 BinaryProtocol::VERSION_MASK.should == 0xffff0000
15 BinaryProtocol::VERSION_1.should == 0x80010000
16 end
17
18 it "should write the message header" do
19 @prot.should_receive(:write_i32).with(BinaryProtocol::VERSION_1 | MessageTypes::CALL).ordered
20 @prot.should_receive(:write_string).with('testMessage').ordered
21 @prot.should_receive(:write_i32).with(17).ordered
22 @prot.write_message_begin('testMessage', MessageTypes::CALL, 17)
23 end
24
25 # message footer is a noop
26
27 it "should write the field header" do
28 @prot.should_receive(:write_byte).with(Types::DOUBLE).ordered
29 @prot.should_receive(:write_i16).with(3).ordered
30 @prot.write_field_begin('foo', Types::DOUBLE, 3)
31 end
32
33 # field footer is a noop
34
35 it "should write the STOP field" do
36 @prot.should_receive(:write_byte).with(Types::STOP)
37 @prot.write_field_stop
38 end
39
40 it "should write the map header" do
41 @prot.should_receive(:write_byte).with(Types::STRING).ordered
42 @prot.should_receive(:write_byte).with(Types::LIST).ordered
43 @prot.should_receive(:write_i32).with(17).ordered
44 @prot.write_map_begin(Types::STRING, Types::LIST, 17)
45 end
46
47 # map footer is a noop
48
49 it "should write the list header" do
50 @prot.should_receive(:write_byte).with(Types::I16).ordered
51 @prot.should_receive(:write_i32).with(42).ordered
52 @prot.write_list_begin(Types::I16, 42)
53 end
54
55 # list footer is a noop
56
57 it "should write the set header" do
58 @prot.should_receive(:write_byte).with(Types::BOOL).ordered
59 @prot.should_receive(:write_i32).with(2).ordered
60 @prot.write_set_begin(Types::BOOL, 2)
61 end
62
63 it "should write a bool" do
64 @prot.should_receive(:write_byte).with(1).ordered
65 @prot.write_bool(true)
66 @prot.should_receive(:write_byte).with(0).ordered
67 @prot.write_bool(false)
68 end
69
70 it "should write a byte" do
71 # byte is small enough, let's check -128..255
72 (-128..255).each do |i|
73 # do the verify/clear after each round because negative values
74 # will double-up the same args as positive values
75 @trans.should_receive(:write).with([i].pack('c'))
76 @prot.write_byte(i)
77 @trans.rspec_verify
78 @trans.rspec_clear
79 end
80 # now try out of range
81 lambda { @prot.write_byte(512) }.should raise_error(RangeError)
82 end
83
84 it "should write an i16" do
85 # try a random scattering of values
86 # include the signed i16 minimum and the unsigned i16 maximum
87 @trans.should_receive(:write).with("\200\000").ordered
88 @trans.should_receive(:write).with("\374\000").ordered
89 @trans.should_receive(:write).with("\000\021").ordered
90 @trans.should_receive(:write).with("\000\000").ordered
91 @trans.should_receive(:write).with("\330\360").ordered
92 @trans.should_receive(:write).with("\006\273").ordered
93 @trans.should_receive(:write).with("\377\377").ordered
94 [-2**15, -1024, 17, 0, -10000, 1723, 2**16-1].each do |i|
95 @prot.write_i16(i)
96 end
97 # and try something out of range
98 lambda { @prot.write_i16(2**18) }.should raise_error(RangeError)
99 end
100
101 it "should write an i32" do
102 # try a random scattering of values
103 # include the signed i32 minimum and the unsigned i32 maximum
104 @trans.should_receive(:write).with("\200\000\000\000").ordered
105 @trans.should_receive(:write).with("\377\376\037\r").ordered
106 @trans.should_receive(:write).with("\377\377\366\034").ordered
107 @trans.should_receive(:write).with("\377\377\377\375").ordered
108 @trans.should_receive(:write).with("\000\000\000\000").ordered
109 @trans.should_receive(:write).with("\000#\340\203").ordered
110 @trans.should_receive(:write).with("\000\0000+").ordered
111 @trans.should_receive(:write).with("\377\377\377\377").ordered
112 [-2**31, -123123, -2532, -3, 0, 2351235, 12331, 2**32-1].each do |i|
113 @prot.write_i32(i)
114 end
115 # try something out of range
116 lambda { @prot.write_i32(2 ** 34) }.should raise_error(RangeError)
117 end
118
119 it "should write an i64" do
120 # try a random scattering of values
121 # include the signed i64 minimum and the unsigned i64 maximum
122 @trans.should_receive(:write).with("\200\000\000\000\000\000\000\000").ordered
123 @trans.should_receive(:write).with("\377\377\364\303\035\244+]").ordered
124 @trans.should_receive(:write).with("\377\377\377\377\376\231:\341").ordered
125 @trans.should_receive(:write).with("\377\377\377\377\377\377\377\026").ordered
126 @trans.should_receive(:write).with("\000\000\000\000\000\000\000\000").ordered
127 @trans.should_receive(:write).with("\000\000\000\000\000\000\004\317").ordered
128 @trans.should_receive(:write).with("\000\000\000\000\000#\340\204").ordered
129 @trans.should_receive(:write).with("\000\000\000\002\340\311~\365").ordered
130 @trans.should_receive(:write).with("\377\377\377\377\377\377\377\377").ordered
131 [-2**63, -12356123612323, -23512351, -234, 0, 1231, 2351236, 12361236213, 2**64-1].each do |i|
132 @prot.write_i64(i)
133 end
134 # try something out of range
135 lambda { @prot.write_i64(2 ** 72) }.should raise_error(RangeError)
136 end
137
138 it "should write a double" do
139 # try a random scattering of values
140 @trans.should_receive(:write).with("\000\020\000\000\000\000\000\000").ordered
141 @trans.should_receive(:write).with("\300\223<\234\355\221hs").ordered
142 @trans.should_receive(:write).with("\300\376\0173\256\024z\341").ordered
143 @trans.should_receive(:write).with("\3007<2\336\372v\324").ordered
144 @trans.should_receive(:write).with("\000\000\000\000\000\000\000\000").ordered
145 @trans.should_receive(:write).with("@\310\037\220\365\302\217\\").ordered
146 @trans.should_receive(:write).with("@\200Y\327\n=p\244").ordered
147 @trans.should_receive(:write).with("\177\357\377\377\377\377\377\377").ordered
148 [Float::MIN, -1231.15325, -123123.23, -23.23515123, 0, 12351.1325, 523.23, Float::MAX].each do |f|
149 @prot.write_double(f)
150 end
151 end
152
153 it "should write a string" do
154 str = "hello world"
155 @prot.should_receive(:write_i32).with(str.length).ordered
156 @trans.should_receive(:write).with(str).ordered
157 @prot.write_string(str)
158 end
159 end
160end