Spec out the BinaryProtocol read methods
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@668952 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/rb/spec/binaryprotocol_spec.rb b/lib/rb/spec/binaryprotocol_spec.rb
index e949753..4a9fd3c 100644
--- a/lib/rb/spec/binaryprotocol_spec.rb
+++ b/lib/rb/spec/binaryprotocol_spec.rb
@@ -68,39 +68,37 @@
end
it "should write a byte" do
- # byte is small enough, let's check -128..255
- (-128..255).each do |i|
- # do the verify/clear after each round because negative values
- # will double-up the same args as positive values
- @trans.should_receive(:write).with([i].pack('c'))
- @prot.write_byte(i)
- @trans.rspec_verify
- @trans.rspec_clear
+ # byte is small enough, let's check -128..127
+ (-128..127).each do |i|
+ @trans.should_receive(:write).with([i].pack('c')).ordered
end
- # now try out of range
- lambda { @prot.write_byte(512) }.should raise_error(RangeError)
+ (-128..127).each do |i|
+ @prot.write_byte(i)
+ end
+ # now try out of signed range
+ lambda { @prot.write_byte(224) }.should raise_error(RangeError)
end
it "should write an i16" do
# try a random scattering of values
- # include the signed i16 minimum and the unsigned i16 maximum
+ # include the signed i16 minimum/maximum
@trans.should_receive(:write).with("\200\000").ordered
@trans.should_receive(:write).with("\374\000").ordered
@trans.should_receive(:write).with("\000\021").ordered
@trans.should_receive(:write).with("\000\000").ordered
@trans.should_receive(:write).with("\330\360").ordered
@trans.should_receive(:write).with("\006\273").ordered
- @trans.should_receive(:write).with("\377\377").ordered
- [-2**15, -1024, 17, 0, -10000, 1723, 2**16-1].each do |i|
+ @trans.should_receive(:write).with("\177\377").ordered
+ [-2**15, -1024, 17, 0, -10000, 1723, 2**15-1].each do |i|
@prot.write_i16(i)
end
- # and try something out of range
- lambda { @prot.write_i16(2**18) }.should raise_error(RangeError)
+ # and try something out of signed range
+ lambda { @prot.write_i16(2**15 + 5) }.should raise_error(RangeError)
end
it "should write an i32" do
# try a random scattering of values
- # include the signed i32 minimum and the unsigned i32 maximum
+ # include the signed i32 minimum/maximum
@trans.should_receive(:write).with("\200\000\000\000").ordered
@trans.should_receive(:write).with("\377\376\037\r").ordered
@trans.should_receive(:write).with("\377\377\366\034").ordered
@@ -108,17 +106,17 @@
@trans.should_receive(:write).with("\000\000\000\000").ordered
@trans.should_receive(:write).with("\000#\340\203").ordered
@trans.should_receive(:write).with("\000\0000+").ordered
- @trans.should_receive(:write).with("\377\377\377\377").ordered
- [-2**31, -123123, -2532, -3, 0, 2351235, 12331, 2**32-1].each do |i|
+ @trans.should_receive(:write).with("\177\377\377\377").ordered
+ [-2**31, -123123, -2532, -3, 0, 2351235, 12331, 2**31-1].each do |i|
@prot.write_i32(i)
end
- # try something out of range
- lambda { @prot.write_i32(2 ** 34) }.should raise_error(RangeError)
+ # try something out of signed range
+ lambda { @prot.write_i32(2 ** 31 + 5) }.should raise_error(RangeError)
end
it "should write an i64" do
# try a random scattering of values
- # include the signed i64 minimum and the unsigned i64 maximum
+ # try the signed i64 minimum/maximum
@trans.should_receive(:write).with("\200\000\000\000\000\000\000\000").ordered
@trans.should_receive(:write).with("\377\377\364\303\035\244+]").ordered
@trans.should_receive(:write).with("\377\377\377\377\376\231:\341").ordered
@@ -127,16 +125,16 @@
@trans.should_receive(:write).with("\000\000\000\000\000\000\004\317").ordered
@trans.should_receive(:write).with("\000\000\000\000\000#\340\204").ordered
@trans.should_receive(:write).with("\000\000\000\002\340\311~\365").ordered
- @trans.should_receive(:write).with("\377\377\377\377\377\377\377\377").ordered
- [-2**63, -12356123612323, -23512351, -234, 0, 1231, 2351236, 12361236213, 2**64-1].each do |i|
+ @trans.should_receive(:write).with("\177\377\377\377\377\377\377\377").ordered
+ [-2**63, -12356123612323, -23512351, -234, 0, 1231, 2351236, 12361236213, 2**63-1].each do |i|
@prot.write_i64(i)
end
- # try something out of range
- lambda { @prot.write_i64(2 ** 72) }.should raise_error(RangeError)
+ # try something out of signed range
+ lambda { @prot.write_i64(2 ** 63 + 5) }.should raise_error(RangeError)
end
it "should write a double" do
- # try a random scattering of values
+ # try a random scattering of values, including min/max
@trans.should_receive(:write).with("\000\020\000\000\000\000\000\000").ordered
@trans.should_receive(:write).with("\300\223<\234\355\221hs").ordered
@trans.should_receive(:write).with("\300\376\0173\256\024z\341").ordered
@@ -156,5 +154,122 @@
@trans.should_receive(:write).with(str).ordered
@prot.write_string(str)
end
+
+ it "should read a message header" do
+ @prot.should_receive(:read_i32).and_return(BinaryProtocol::VERSION_1 | MessageTypes::REPLY, 42)
+ @prot.should_receive(:read_string).and_return('testMessage')
+ @prot.read_message_begin.should == ['testMessage', MessageTypes::REPLY, 42]
+ end
+
+ # message header is a noop
+
+ it "should read a field header" do
+ @prot.should_receive(:read_byte).ordered.and_return(Types::STRING)
+ @prot.should_receive(:read_i16).ordered.and_return(3)
+ @prot.read_field_begin.should == [nil, Types::STRING, 3]
+ end
+
+ # field footer is a noop
+
+ it "should read a stop field" do
+ @prot.should_receive(:read_byte).and_return(Types::STOP)
+ @prot.should_not_receive(:read_i16)
+ @prot.read_field_begin.should == [nil, Types::STOP, 0]
+ end
+
+ it "should read a map header" do
+ @prot.should_receive(:read_byte).and_return(Types::DOUBLE, Types::I64)
+ @prot.should_receive(:read_i32).and_return(42)
+ @prot.read_map_begin.should == [Types::DOUBLE, Types::I64, 42]
+ end
+
+ # map footer is a noop
+
+ it "should read a list header" do
+ @prot.should_receive(:read_byte).ordered.and_return(Types::STRING)
+ @prot.should_receive(:read_i32).and_return(17)
+ @prot.read_list_begin.should == [Types::STRING, 17]
+ end
+
+ # list footer is a noop
+
+ it "should read a set header" do
+ @prot.should_receive(:read_byte).ordered.and_return(Types::MAP)
+ @prot.should_receive(:read_i32).ordered.and_return(42)
+ @prot.read_set_begin.should == [Types::MAP, 42]
+ end
+
+ # set footer is a noop
+
+ it "should read a bool" do
+ @prot.should_receive(:read_byte).and_return(1, 0)
+ @prot.read_bool.should == true
+ @prot.read_bool.should == false
+ end
+
+ it "should read a byte" do
+ # try a scattering of values, including min/max
+ @trans.should_receive(:read_all).with(1).and_return(
+ "\200", "\307", "\375",
+ "\000", "\021", "\030", "\177"
+ )
+ [-128, -57, -3, 0, 17, 24, 127].each do |i|
+ @prot.read_byte.should == i
+ end
+ end
+
+ it "should read an i16" do
+ # try a scattering of values, including min/max
+ @trans.should_receive(:read_all).with(2).and_return(
+ "\200\000", "\353\213", "\376\237",
+ "\000\000", "\005\367", "\b\272", "\177\377"
+ )
+ [-2**15, -5237, -353, 0, 1527, 2234, 2**15-1].each do |i|
+ @prot.read_i16.should == i
+ end
+ end
+
+ it "should read an i32" do
+ # try a scattering of values, including min/max
+ @trans.should_receive(:read_all).with(4).and_return(
+ "\200\000\000\000", "\377\374i\213", "\377\377\347\244",
+ "\000\000\000\000", "\000\000\t/", "\000\001\340\363", "\177\377\377\377"
+ )
+ [-2**31, -235125, -6236, 0, 2351, 123123, 2**31-1].each do |i|
+ @prot.read_i32.should == i
+ end
+ end
+
+ it "should read an i64" do
+ # try a scattering of values, including min/max
+ @trans.should_receive(:read_all).with(8).and_return(
+ "\200\000\000\000\000\000\000\000", "\377\377\377\377\370\243Z\b",
+ "\377\377\377\377\377\377\3476", "\000\000\000\000\000\000\000\000",
+ "\000\000\000\000\000\000\000 ", "\000\000\000\000\213\332\t\223",
+ "\177\377\377\377\377\377\377\377"
+ )
+ [-2**63, -123512312, -6346, 0, 32, 2346322323, 2**63-1].each do |i|
+ @prot.read_i64.should == i
+ end
+ end
+
+ it "should read a double" do
+ # try a random scattering of values, including min/max
+ @trans.should_receive(:read_all).with(8).and_return(
+ "\000\020\000\000\000\000\000\000", "\301\f9\370\374\362\317\226",
+ "\300t3\274x \243\016", "\000\000\000\000\000\000\000\000", "@^\317\fCo\301Y",
+ "AA\360A\217\317@\260", "\177\357\377\377\377\377\377\377"
+ )
+ [Float::MIN, -231231.12351, -323.233513, 0, 123.2351235, 2351235.12351235, Float::MAX].each do |f|
+ @prot.read_double.should == f
+ end
+ end
+
+ it "should read a string" do
+ str = "hello world"
+ @prot.should_receive(:read_i32).and_return(str.length)
+ @trans.should_receive(:read_all).with(str.length).and_return(str)
+ @prot.read_string.should == str
+ end
end
end