blob: 6c08c43bbe799fec2da1460753a4ba311056d94a [file] [log] [blame]
Kevin Clarkdc399732008-06-18 01:06:15 +00001require File.dirname(__FILE__) + '/spec_helper'
2
3class ThriftSpec < Spec::ExampleGroup
4 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
78 @prot.read_type(Types::BOOL)
79 @prot.read_type(Types::BYTE)
80 @prot.read_type(Types::I16)
81 @prot.read_type(Types::I32)
82 @prot.read_type(Types::I64)
83 @prot.read_type(Types::DOUBLE)
84 @prot.read_type(Types::STRING)
85 end
86
87 it "should skip structs" do
88 real_skip = @prot.method(:skip)
89 @prot.should_receive(:read_struct_begin).ordered
90 @prot.should_receive(:read_field_begin).exactly(4).times.and_return(
91 ['field 1', Types::STRING, 1],
92 ['field 2', Types::I32, 2],
93 ['field 3', Types::MAP, 3],
94 [nil, Types::STOP, 0]
95 )
96 @prot.should_receive(:skip).with(Types::STRING).ordered
97 @prot.should_receive(:skip).with(Types::I32).ordered
98 @prot.should_receive(:skip).with(Types::MAP).ordered
99 @prot.should_receive(:read_field_end).exactly(3).times
100 @prot.should_receive(:read_struct_end).ordered
101 real_skip.call(Types::STRUCT)
102 end
Kevin Clark9db1c2e2008-06-18 01:06:42 +0000103
104 it "should skip maps" do
105 real_skip = @prot.method(:skip)
106 @prot.should_receive(:read_map_begin).ordered.and_return([Types::STRING, Types::STRUCT, 7])
107 @prot.should_receive(:skip).ordered.exactly(14).times # once per key and once per value
108 @prot.should_receive(:read_map_end).ordered
109 real_skip.call(Types::MAP)
110 end
111
112 it "should skip sets" do
113 real_skip = @prot.method(:skip)
114 @prot.should_receive(:read_set_begin).ordered.and_return([Types::I64, 9])
115 @prot.should_receive(:skip).with(Types::I64).ordered.exactly(9).times
116 @prot.should_receive(:read_set_end)
117 real_skip.call(Types::SET)
118 end
119
120 it "should skip lists" do
121 real_skip = @prot.method(:skip)
122 @prot.should_receive(:read_list_begin).ordered.and_return([Types::DOUBLE, 11])
123 @prot.should_receive(:skip).with(Types::DOUBLE).ordered.exactly(11).times
124 @prot.should_receive(:read_list_end)
125 real_skip.call(Types::LIST)
126 end
127 end
128
129 describe ProtocolFactory do
130 it "should return nil" do
131 # returning nil since Protocol is just an abstract class
132 ProtocolFactory.new.get_protocol(mock("MockTransport")).should be_nil
133 end
Kevin Clarkdc399732008-06-18 01:06:15 +0000134 end
135end