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