blob: 443c8f09d50bed6eed16705dcdd52526926be482 [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 )
Kevin Clarkdc399732008-06-18 01:06:15 +000097 @prot.should_receive(:read_field_end).exactly(3).times
Bryan Duxburyc0166282009-02-02 00:48:17 +000098 @prot.should_receive(:read_string).exactly(3).times
99 @prot.should_receive(:read_i32).ordered
100 @prot.should_receive(:read_map_begin).ordered.and_return([Types::STRING, Types::STRING, 1])
101 # @prot.should_receive(:read_string).exactly(2).times
102 @prot.should_receive(:read_map_end).ordered
Kevin Clarkdc399732008-06-18 01:06:15 +0000103 @prot.should_receive(:read_struct_end).ordered
104 real_skip.call(Types::STRUCT)
105 end
Kevin Clark9db1c2e2008-06-18 01:06:42 +0000106
107 it "should skip maps" do
108 real_skip = @prot.method(:skip)
Bryan Duxburyc0166282009-02-02 00:48:17 +0000109 @prot.should_receive(:read_map_begin).ordered.and_return([Types::STRING, Types::STRUCT, 1])
110 @prot.should_receive(:read_string).ordered
111 @prot.should_receive(:read_struct_begin).ordered.and_return(["some_struct"])
112 @prot.should_receive(:read_field_begin).ordered.and_return([nil, Types::STOP, nil]);
113 @prot.should_receive(:read_struct_end).ordered
Kevin Clark9db1c2e2008-06-18 01:06:42 +0000114 @prot.should_receive(:read_map_end).ordered
115 real_skip.call(Types::MAP)
116 end
117
118 it "should skip sets" do
119 real_skip = @prot.method(:skip)
120 @prot.should_receive(:read_set_begin).ordered.and_return([Types::I64, 9])
Bryan Duxburyc0166282009-02-02 00:48:17 +0000121 @prot.should_receive(:read_i64).ordered.exactly(9).times
Kevin Clark9db1c2e2008-06-18 01:06:42 +0000122 @prot.should_receive(:read_set_end)
123 real_skip.call(Types::SET)
124 end
125
126 it "should skip lists" do
127 real_skip = @prot.method(:skip)
128 @prot.should_receive(:read_list_begin).ordered.and_return([Types::DOUBLE, 11])
Bryan Duxburyc0166282009-02-02 00:48:17 +0000129 @prot.should_receive(:read_double).ordered.exactly(11).times
Kevin Clark9db1c2e2008-06-18 01:06:42 +0000130 @prot.should_receive(:read_list_end)
131 real_skip.call(Types::LIST)
132 end
133 end
134
135 describe ProtocolFactory do
Bryan Duxburye8ae5d32009-03-24 05:23:52 +0000136 it "should raise NotImplementedError" do
Kevin Clark9db1c2e2008-06-18 01:06:42 +0000137 # returning nil since Protocol is just an abstract class
Bryan Duxburye8ae5d32009-03-24 05:23:52 +0000138 lambda {ProtocolFactory.new.get_protocol(mock("MockTransport"))}.should raise_error(NotImplementedError)
Kevin Clark9db1c2e2008-06-18 01:06:42 +0000139 end
Kevin Clarkdc399732008-06-18 01:06:15 +0000140 end
141end