blob: 917ca454961605047f042f3edf8315ba2c129f47 [file] [log] [blame]
Kevin Clarkdc399732008-06-18 01:06:15 +00001require File.dirname(__FILE__) + '/spec_helper'
2
Kevin Clark356f8612008-06-18 01:08:05 +00003class ThriftProtocolSpec < Spec::ExampleGroup
Kevin Clarkdc399732008-06-18 01:06:15 +00004 include Thrift
5
6 before(:each) do
Kevin Clark9db1c2e2008-06-18 01:06:42 +00007 @trans = mock("MockTransport")
8 @prot = Protocol.new(@trans)
Kevin Clarkdc399732008-06-18 01:06:15 +00009 end
10
11 describe Protocol do
12 # most of the methods are stubs, so we can ignore them
Kevin Clark9db1c2e2008-06-18 01:06:42 +000013
14 it "should make trans accessible" do
15 @prot.trans.should eql(@trans)
16 end
17
Kevin Clarkdc399732008-06-18 01:06:15 +000018 it "should write out a field nicely" do
19 @prot.should_receive(:write_field_begin).with('field', 'type', 'fid').ordered
20 @prot.should_receive(:write_type).with('type', 'value').ordered
21 @prot.should_receive(:write_field_end).ordered
22 @prot.write_field('field', 'type', 'fid', 'value')
23 end
24
25 it "should write out the different types" do
26 @prot.should_receive(:write_bool).with('bool').ordered
27 @prot.should_receive(:write_byte).with('byte').ordered
28 @prot.should_receive(:write_double).with('double').ordered
29 @prot.should_receive(:write_i16).with('i16').ordered
30 @prot.should_receive(:write_i32).with('i32').ordered
31 @prot.should_receive(:write_i64).with('i64').ordered
32 @prot.should_receive(:write_string).with('string').ordered
33 struct = mock('Struct')
34 struct.should_receive(:write).with(@prot).ordered
35 @prot.write_type(Types::BOOL, 'bool')
36 @prot.write_type(Types::BYTE, 'byte')
37 @prot.write_type(Types::DOUBLE, 'double')
38 @prot.write_type(Types::I16, 'i16')
39 @prot.write_type(Types::I32, 'i32')
40 @prot.write_type(Types::I64, 'i64')
41 @prot.write_type(Types::STRING, 'string')
42 @prot.write_type(Types::STRUCT, struct)
43 # all other types are not implemented
44 [Types::STOP, Types::VOID, Types::MAP, Types::SET, Types::LIST].each do |type|
45 lambda { @prot.write_type(type, type.to_s) }.should raise_error(NotImplementedError)
46 end
47 end
48
49 it "should read the different types" do
50 @prot.should_receive(:read_bool).ordered
51 @prot.should_receive(:read_byte).ordered
52 @prot.should_receive(:read_i16).ordered
53 @prot.should_receive(:read_i32).ordered
54 @prot.should_receive(:read_i64).ordered
55 @prot.should_receive(:read_double).ordered
56 @prot.should_receive(:read_string).ordered
57 @prot.read_type(Types::BOOL)
58 @prot.read_type(Types::BYTE)
59 @prot.read_type(Types::I16)
60 @prot.read_type(Types::I32)
61 @prot.read_type(Types::I64)
62 @prot.read_type(Types::DOUBLE)
63 @prot.read_type(Types::STRING)
64 # all other types are not implemented
65 [Types::STOP, Types::VOID, Types::MAP, Types::SET, Types::LIST].each do |type|
66 lambda { @prot.read_type(type) }.should raise_error(NotImplementedError)
67 end
68 end
69
70 it "should skip the basic types" do
71 @prot.should_receive(:read_bool).ordered
72 @prot.should_receive(:read_byte).ordered
73 @prot.should_receive(:read_i16).ordered
74 @prot.should_receive(:read_i32).ordered
75 @prot.should_receive(:read_i64).ordered
76 @prot.should_receive(:read_double).ordered
77 @prot.should_receive(:read_string).ordered
Kevin Clark3f483332008-06-18 01:12:26 +000078 @prot.skip(Types::BOOL)
79 @prot.skip(Types::BYTE)
80 @prot.skip(Types::I16)
81 @prot.skip(Types::I32)
82 @prot.skip(Types::I64)
83 @prot.skip(Types::DOUBLE)
84 @prot.skip(Types::STRING)
85 @prot.skip(Types::STOP) # should do absolutely nothing
Kevin Clarkdc399732008-06-18 01:06:15 +000086 end
87
88 it "should skip structs" do
89 real_skip = @prot.method(:skip)
90 @prot.should_receive(:read_struct_begin).ordered
91 @prot.should_receive(:read_field_begin).exactly(4).times.and_return(
92 ['field 1', Types::STRING, 1],
93 ['field 2', Types::I32, 2],
94 ['field 3', Types::MAP, 3],
95 [nil, Types::STOP, 0]
96 )
97 @prot.should_receive(:skip).with(Types::STRING).ordered
98 @prot.should_receive(:skip).with(Types::I32).ordered
99 @prot.should_receive(:skip).with(Types::MAP).ordered
100 @prot.should_receive(:read_field_end).exactly(3).times
101 @prot.should_receive(:read_struct_end).ordered
102 real_skip.call(Types::STRUCT)
103 end
Kevin Clark9db1c2e2008-06-18 01:06:42 +0000104
105 it "should skip maps" do
106 real_skip = @prot.method(:skip)
107 @prot.should_receive(:read_map_begin).ordered.and_return([Types::STRING, Types::STRUCT, 7])
108 @prot.should_receive(:skip).ordered.exactly(14).times # once per key and once per value
109 @prot.should_receive(:read_map_end).ordered
110 real_skip.call(Types::MAP)
111 end
112
113 it "should skip sets" do
114 real_skip = @prot.method(:skip)
115 @prot.should_receive(:read_set_begin).ordered.and_return([Types::I64, 9])
116 @prot.should_receive(:skip).with(Types::I64).ordered.exactly(9).times
117 @prot.should_receive(:read_set_end)
118 real_skip.call(Types::SET)
119 end
120
121 it "should skip lists" do
122 real_skip = @prot.method(:skip)
123 @prot.should_receive(:read_list_begin).ordered.and_return([Types::DOUBLE, 11])
124 @prot.should_receive(:skip).with(Types::DOUBLE).ordered.exactly(11).times
125 @prot.should_receive(:read_list_end)
126 real_skip.call(Types::LIST)
127 end
128 end
129
130 describe ProtocolFactory do
131 it "should return nil" do
132 # returning nil since Protocol is just an abstract class
133 ProtocolFactory.new.get_protocol(mock("MockTransport")).should be_nil
134 end
Kevin Clarkdc399732008-06-18 01:06:15 +0000135 end
136end