Kevin Clark | dc39973 | 2008-06-18 01:06:15 +0000 | [diff] [blame] | 1 | require File.dirname(__FILE__) + '/spec_helper' |
| 2 | |
| 3 | class ThriftSpec < Spec::ExampleGroup |
| 4 | include Thrift |
| 5 | |
| 6 | before(:each) do |
| 7 | @prot = Protocol.new(mock("MockTransport")) |
| 8 | end |
| 9 | |
| 10 | describe Protocol do |
| 11 | # most of the methods are stubs, so we can ignore them |
| 12 | it "should write out a field nicely" do |
| 13 | @prot.should_receive(:write_field_begin).with('field', 'type', 'fid').ordered |
| 14 | @prot.should_receive(:write_type).with('type', 'value').ordered |
| 15 | @prot.should_receive(:write_field_end).ordered |
| 16 | @prot.write_field('field', 'type', 'fid', 'value') |
| 17 | end |
| 18 | |
| 19 | it "should write out the different types" do |
| 20 | @prot.should_receive(:write_bool).with('bool').ordered |
| 21 | @prot.should_receive(:write_byte).with('byte').ordered |
| 22 | @prot.should_receive(:write_double).with('double').ordered |
| 23 | @prot.should_receive(:write_i16).with('i16').ordered |
| 24 | @prot.should_receive(:write_i32).with('i32').ordered |
| 25 | @prot.should_receive(:write_i64).with('i64').ordered |
| 26 | @prot.should_receive(:write_string).with('string').ordered |
| 27 | struct = mock('Struct') |
| 28 | struct.should_receive(:write).with(@prot).ordered |
| 29 | @prot.write_type(Types::BOOL, 'bool') |
| 30 | @prot.write_type(Types::BYTE, 'byte') |
| 31 | @prot.write_type(Types::DOUBLE, 'double') |
| 32 | @prot.write_type(Types::I16, 'i16') |
| 33 | @prot.write_type(Types::I32, 'i32') |
| 34 | @prot.write_type(Types::I64, 'i64') |
| 35 | @prot.write_type(Types::STRING, 'string') |
| 36 | @prot.write_type(Types::STRUCT, struct) |
| 37 | # all other types are not implemented |
| 38 | [Types::STOP, Types::VOID, Types::MAP, Types::SET, Types::LIST].each do |type| |
| 39 | lambda { @prot.write_type(type, type.to_s) }.should raise_error(NotImplementedError) |
| 40 | end |
| 41 | end |
| 42 | |
| 43 | it "should read the different types" do |
| 44 | @prot.should_receive(:read_bool).ordered |
| 45 | @prot.should_receive(:read_byte).ordered |
| 46 | @prot.should_receive(:read_i16).ordered |
| 47 | @prot.should_receive(:read_i32).ordered |
| 48 | @prot.should_receive(:read_i64).ordered |
| 49 | @prot.should_receive(:read_double).ordered |
| 50 | @prot.should_receive(:read_string).ordered |
| 51 | @prot.read_type(Types::BOOL) |
| 52 | @prot.read_type(Types::BYTE) |
| 53 | @prot.read_type(Types::I16) |
| 54 | @prot.read_type(Types::I32) |
| 55 | @prot.read_type(Types::I64) |
| 56 | @prot.read_type(Types::DOUBLE) |
| 57 | @prot.read_type(Types::STRING) |
| 58 | # all other types are not implemented |
| 59 | [Types::STOP, Types::VOID, Types::MAP, Types::SET, Types::LIST].each do |type| |
| 60 | lambda { @prot.read_type(type) }.should raise_error(NotImplementedError) |
| 61 | end |
| 62 | end |
| 63 | |
| 64 | it "should skip the basic types" do |
| 65 | @prot.should_receive(:read_bool).ordered |
| 66 | @prot.should_receive(:read_byte).ordered |
| 67 | @prot.should_receive(:read_i16).ordered |
| 68 | @prot.should_receive(:read_i32).ordered |
| 69 | @prot.should_receive(:read_i64).ordered |
| 70 | @prot.should_receive(:read_double).ordered |
| 71 | @prot.should_receive(:read_string).ordered |
| 72 | @prot.read_type(Types::BOOL) |
| 73 | @prot.read_type(Types::BYTE) |
| 74 | @prot.read_type(Types::I16) |
| 75 | @prot.read_type(Types::I32) |
| 76 | @prot.read_type(Types::I64) |
| 77 | @prot.read_type(Types::DOUBLE) |
| 78 | @prot.read_type(Types::STRING) |
| 79 | end |
| 80 | |
| 81 | it "should skip structs" do |
| 82 | real_skip = @prot.method(:skip) |
| 83 | @prot.should_receive(:read_struct_begin).ordered |
| 84 | @prot.should_receive(:read_field_begin).exactly(4).times.and_return( |
| 85 | ['field 1', Types::STRING, 1], |
| 86 | ['field 2', Types::I32, 2], |
| 87 | ['field 3', Types::MAP, 3], |
| 88 | [nil, Types::STOP, 0] |
| 89 | ) |
| 90 | @prot.should_receive(:skip).with(Types::STRING).ordered |
| 91 | @prot.should_receive(:skip).with(Types::I32).ordered |
| 92 | @prot.should_receive(:skip).with(Types::MAP).ordered |
| 93 | @prot.should_receive(:read_field_end).exactly(3).times |
| 94 | @prot.should_receive(:read_struct_end).ordered |
| 95 | real_skip.call(Types::STRUCT) |
| 96 | end |
| 97 | end |
| 98 | end |