blob: 1a22f57f70ab414dd6fe0708d082e8206149cd7d [file] [log] [blame]
Kevin Clark03d7a472008-06-18 01:09:41 +00001require File.dirname(__FILE__) + '/spec_helper'
Kevin Clark2bd3a302008-06-26 17:49:49 +00002require File.dirname(__FILE__) + '/gen-rb/ThriftSpec_types'
Kevin Clark03d7a472008-06-18 01:09:41 +00003
Bryan Duxburyc0166282009-02-02 00:48:17 +00004# require "binaryprotocolaccelerated"
5
Kevin Clark03d7a472008-06-18 01:09:41 +00006class ThriftStructSpec < Spec::ExampleGroup
7 include Thrift
8 include SpecNamespace
9
Kevin Clark140b5552008-06-18 01:17:57 +000010 describe Struct do
11 it "should iterate over all fields properly" do
12 fields = {}
Kevin Clark5ad6d4a2008-08-26 20:02:07 +000013 Foo.new.each_field { |fid,type,name,default,optional| fields[fid] = [type,name,default,optional] }
Kevin Clark140b5552008-06-18 01:17:57 +000014 fields.should == {
Kevin Clark5ad6d4a2008-08-26 20:02:07 +000015 1 => [Types::I32, 'simple', 53, nil],
16 2 => [Types::STRING, 'words', "words", nil],
17 3 => [Types::STRUCT, 'hello', Hello.new(:greeting => 'hello, world!'), nil],
18 4 => [Types::LIST, 'ints', [1, 2, 2, 3], nil],
19 5 => [Types::MAP, 'complex', nil, nil],
20 6 => [Types::SET, 'shorts', Set.new([5, 17, 239]), nil],
21 7 => [Types::STRING, 'opt_string', nil, true]
Kevin Clark140b5552008-06-18 01:17:57 +000022 }
Kevin Clark9479b1a2008-06-18 01:13:37 +000023 end
Kevin Clark9479b1a2008-06-18 01:13:37 +000024
Kevin Clark140b5552008-06-18 01:17:57 +000025 it "should initialize all fields to defaults" do
26 struct = Foo.new
27 struct.simple.should == 53
28 struct.words.should == "words"
29 struct.hello.should == Hello.new(:greeting => 'hello, world!')
30 struct.ints.should == [1, 2, 2, 3]
31 struct.complex.should be_nil
32 struct.shorts.should == Set.new([5, 17, 239])
33 end
Kevin Clark1cfd6932008-06-18 01:13:58 +000034
Kevin Clark140b5552008-06-18 01:17:57 +000035 it "should not share default values between instances" do
36 begin
37 struct = Foo.new
38 struct.ints << 17
39 Foo.new.ints.should == [1,2,2,3]
40 ensure
41 # ensure no leakage to other tests
42 Foo::FIELDS[4][:default] = [1,2,2,3]
43 end
44 end
Kevin Clark03d7a472008-06-18 01:09:41 +000045
Kevin Clark140b5552008-06-18 01:17:57 +000046 it "should properly initialize boolean values" do
47 struct = BoolStruct.new(:yesno => false)
48 struct.yesno.should be_false
49 end
Kevin Clark03d7a472008-06-18 01:09:41 +000050
Kevin Clark140b5552008-06-18 01:17:57 +000051 it "should have proper == semantics" do
52 Foo.new.should_not == Hello.new
53 Foo.new.should == Foo.new
54 Foo.new(:simple => 52).should_not == Foo.new
55 end
Kevin Clark03d7a472008-06-18 01:09:41 +000056
Kevin Clark140b5552008-06-18 01:17:57 +000057 it "should read itself off the wire" do
58 struct = Foo.new
Bryan Duxburyc0166282009-02-02 00:48:17 +000059 prot = Protocol.new(mock("transport"))
Kevin Clark140b5552008-06-18 01:17:57 +000060 prot.should_receive(:read_struct_begin).twice
61 prot.should_receive(:read_struct_end).twice
62 prot.should_receive(:read_field_begin).and_return(
63 ['complex', Types::MAP, 5], # Foo
64 ['words', Types::STRING, 2], # Foo
65 ['hello', Types::STRUCT, 3], # Foo
66 ['greeting', Types::STRING, 1], # Hello
67 [nil, Types::STOP, 0], # Hello
68 ['simple', Types::I32, 1], # Foo
69 ['ints', Types::LIST, 4], # Foo
70 ['shorts', Types::SET, 6], # Foo
71 [nil, Types::STOP, 0] # Hello
72 )
73 prot.should_receive(:read_field_end).exactly(7).times
74 prot.should_receive(:read_map_begin).and_return(
75 [Types::I32, Types::MAP, 2], # complex
76 [Types::STRING, Types::DOUBLE, 2], # complex/1/value
77 [Types::STRING, Types::DOUBLE, 1] # complex/2/value
78 )
79 prot.should_receive(:read_map_end).exactly(3).times
80 prot.should_receive(:read_list_begin).and_return([Types::I32, 4])
81 prot.should_receive(:read_list_end)
82 prot.should_receive(:read_set_begin).and_return([Types::I16, 2])
83 prot.should_receive(:read_set_end)
Bryan Duxburyc0166282009-02-02 00:48:17 +000084 prot.should_receive(:read_i32).and_return(
Kevin Clark140b5552008-06-18 01:17:57 +000085 1, 14, # complex keys
86 42, # simple
87 4, 23, 4, 29 # ints
88 )
Bryan Duxburyc0166282009-02-02 00:48:17 +000089 prot.should_receive(:read_string).and_return("pi", "e", "feigenbaum", "apple banana", "what's up?")
90 prot.should_receive(:read_double).and_return(Math::PI, Math::E, 4.669201609)
91 prot.should_receive(:read_i16).and_return(2, 3)
Kevin Clark140b5552008-06-18 01:17:57 +000092 prot.should_not_receive(:skip)
93 struct.read(prot)
Kevin Clark03d7a472008-06-18 01:09:41 +000094
Kevin Clark140b5552008-06-18 01:17:57 +000095 struct.simple.should == 42
96 struct.complex.should == {1 => {"pi" => Math::PI, "e" => Math::E}, 14 => {"feigenbaum" => 4.669201609}}
97 struct.hello.should == Hello.new(:greeting => "what's up?")
98 struct.words.should == "apple banana"
99 struct.ints.should == [4, 23, 4, 29]
100 struct.shorts.should == Set.new([3, 2])
101 end
Kevin Clark03d7a472008-06-18 01:09:41 +0000102
Kevin Clark140b5552008-06-18 01:17:57 +0000103 it "should skip unexpected fields in structs and use default values" do
104 struct = Foo.new
Bryan Duxburyc0166282009-02-02 00:48:17 +0000105 prot = Protocol.new(mock("transport"))
Kevin Clark140b5552008-06-18 01:17:57 +0000106 prot.should_receive(:read_struct_begin)
107 prot.should_receive(:read_struct_end)
108 prot.should_receive(:read_field_begin).and_return(
109 ['simple', Types::I32, 1],
110 ['complex', Types::STRUCT, 5],
111 ['thinz', Types::MAP, 7],
112 ['foobar', Types::I32, 3],
113 ['words', Types::STRING, 2],
114 [nil, Types::STOP, 0]
115 )
116 prot.should_receive(:read_field_end).exactly(5).times
Bryan Duxburyc0166282009-02-02 00:48:17 +0000117 prot.should_receive(:read_i32).and_return(42)
118 prot.should_receive(:read_string).and_return("foobar")
Kevin Clark140b5552008-06-18 01:17:57 +0000119 prot.should_receive(:skip).with(Types::STRUCT)
120 prot.should_receive(:skip).with(Types::MAP)
Bryan Duxburyc0166282009-02-02 00:48:17 +0000121 # prot.should_receive(:read_map_begin).and_return([Types::I32, Types::I32, 0])
122 # prot.should_receive(:read_map_end)
Kevin Clark140b5552008-06-18 01:17:57 +0000123 prot.should_receive(:skip).with(Types::I32)
124 struct.read(prot)
Kevin Clark03d7a472008-06-18 01:09:41 +0000125
Kevin Clark140b5552008-06-18 01:17:57 +0000126 struct.simple.should == 42
127 struct.complex.should be_nil
128 struct.words.should == "foobar"
129 struct.hello.should == Hello.new(:greeting => 'hello, world!')
130 struct.ints.should == [1, 2, 2, 3]
131 struct.shorts.should == Set.new([5, 17, 239])
132 end
Kevin Clark090b69e2008-06-18 01:12:58 +0000133
Kevin Clark140b5552008-06-18 01:17:57 +0000134 it "should write itself to the wire" do
Bryan Duxburyc0166282009-02-02 00:48:17 +0000135 prot = Protocol.new(mock("transport")) #mock("Protocol")
Kevin Clark140b5552008-06-18 01:17:57 +0000136 prot.should_receive(:write_struct_begin).with("SpecNamespace::Foo")
Bryan Duxburyc0166282009-02-02 00:48:17 +0000137 prot.should_receive(:write_struct_begin).with("SpecNamespace::Hello")
138 prot.should_receive(:write_struct_end).twice
Kevin Clark140b5552008-06-18 01:17:57 +0000139 prot.should_receive(:write_field_begin).with('ints', Types::LIST, 4)
Bryan Duxburyc0166282009-02-02 00:48:17 +0000140 prot.should_receive(:write_i32).with(1)
141 prot.should_receive(:write_i32).with(2).twice
142 prot.should_receive(:write_i32).with(3)
Kevin Clark140b5552008-06-18 01:17:57 +0000143 prot.should_receive(:write_field_begin).with('complex', Types::MAP, 5)
Bryan Duxburyc0166282009-02-02 00:48:17 +0000144 prot.should_receive(:write_i32).with(5)
145 prot.should_receive(:write_string).with('foo')
146 prot.should_receive(:write_double).with(1.23)
Kevin Clark140b5552008-06-18 01:17:57 +0000147 prot.should_receive(:write_field_begin).with('shorts', Types::SET, 6)
Bryan Duxburyc0166282009-02-02 00:48:17 +0000148 prot.should_receive(:write_i16).with(5)
149 prot.should_receive(:write_i16).with(17)
150 prot.should_receive(:write_i16).with(239)
151 prot.should_receive(:write_field_stop).twice
152 prot.should_receive(:write_field_end).exactly(6).times
153 prot.should_receive(:write_field_begin).with('simple', Types::I32, 1)
154 prot.should_receive(:write_i32).with(53)
155 prot.should_receive(:write_field_begin).with('hello', Types::STRUCT, 3)
156 prot.should_receive(:write_field_begin).with('greeting', Types::STRING, 1)
157 prot.should_receive(:write_string).with('hello, world!')
Kevin Clark140b5552008-06-18 01:17:57 +0000158 prot.should_receive(:write_map_begin).with(Types::I32, Types::MAP, 1)
159 prot.should_receive(:write_map_begin).with(Types::STRING, Types::DOUBLE, 1)
Kevin Clark140b5552008-06-18 01:17:57 +0000160 prot.should_receive(:write_map_end).twice
161 prot.should_receive(:write_list_begin).with(Types::I32, 4)
Kevin Clark140b5552008-06-18 01:17:57 +0000162 prot.should_receive(:write_list_end)
163 prot.should_receive(:write_set_begin).with(Types::I16, 3)
Kevin Clark140b5552008-06-18 01:17:57 +0000164 prot.should_receive(:write_set_end)
165
166 struct = Foo.new
167 struct.words = nil
168 struct.complex = {5 => {"foo" => 1.23}}
169 struct.write(prot)
170 end
171
172 it "should raise an exception if presented with an unknown container" do
173 # yeah this is silly, but I'm going for code coverage here
174 struct = Foo.new
175 lambda { struct.send :write_container, nil, nil, {:type => "foo"} }.should raise_error(StandardError, "Not a container type: foo")
176 end
Kevin Clark23193752008-06-18 01:18:07 +0000177
178 it "should support optional type-checking in Thrift::Struct.new" do
179 Thrift.type_checking = true
180 begin
Kevin Clarkb5863392008-07-18 22:03:48 +0000181 lambda { Hello.new(:greeting => 3) }.should raise_error(TypeError, "Expected Types::STRING, received Fixnum for field greeting")
Kevin Clark23193752008-06-18 01:18:07 +0000182 ensure
183 Thrift.type_checking = false
184 end
185 lambda { Hello.new(:greeting => 3) }.should_not raise_error(TypeError)
186 end
187
188 it "should support optional type-checking in field accessors" do
189 Thrift.type_checking = true
190 begin
191 hello = Hello.new
Kevin Clarkb5863392008-07-18 22:03:48 +0000192 lambda { hello.greeting = 3 }.should raise_error(TypeError, "Expected Types::STRING, received Fixnum for field greeting")
Kevin Clark23193752008-06-18 01:18:07 +0000193 ensure
194 Thrift.type_checking = false
195 end
196 lambda { hello.greeting = 3 }.should_not raise_error(TypeError)
197 end
198
199 it "should raise an exception when unknown types are given to Thrift::Struct.new" do
Kevin Clark38a2ce62008-08-25 21:34:19 +0000200 lambda { Hello.new(:fish => 'salmon') }.should raise_error(Exception, "Unknown key given to SpecNamespace::Hello.new: fish")
Kevin Clark23193752008-06-18 01:18:07 +0000201 end
Kevin Clark3af92872008-07-28 22:20:36 +0000202
203 it "should support `raise Xception, 'message'` for Exception structs" do
204 begin
205 raise Xception, "something happened"
206 rescue Thrift::Exception => e
207 e.message.should == "something happened"
208 e.code.should == 1
209 # ensure it gets serialized properly, this is the really important part
Bryan Duxburyc0166282009-02-02 00:48:17 +0000210 prot = Protocol.new(mock("trans"))
Kevin Clark031baf72008-11-14 17:11:39 +0000211 prot.should_receive(:write_struct_begin).with("SpecNamespace::Xception")
Kevin Clark3af92872008-07-28 22:20:36 +0000212 prot.should_receive(:write_struct_end)
Bryan Duxburyc0166282009-02-02 00:48:17 +0000213 prot.should_receive(:write_field_begin).with('message', Types::STRING, 1)#, "something happened")
214 prot.should_receive(:write_string).with("something happened")
215 prot.should_receive(:write_field_begin).with('code', Types::I32, 2)#, 1)
216 prot.should_receive(:write_i32).with(1)
Kevin Clark3af92872008-07-28 22:20:36 +0000217 prot.should_receive(:write_field_stop)
Bryan Duxburyc0166282009-02-02 00:48:17 +0000218 prot.should_receive(:write_field_end).twice
Kevin Clark3af92872008-07-28 22:20:36 +0000219
220 e.write(prot)
221 end
222 end
223
224 it "should support the regular initializer for exception structs" do
225 begin
226 raise Xception, :message => "something happened", :code => 5
227 rescue Thrift::Exception => e
228 e.message.should == "something happened"
229 e.code.should == 5
Bryan Duxburyc0166282009-02-02 00:48:17 +0000230 prot = Protocol.new(mock("trans"))
Kevin Clark031baf72008-11-14 17:11:39 +0000231 prot.should_receive(:write_struct_begin).with("SpecNamespace::Xception")
Kevin Clark3af92872008-07-28 22:20:36 +0000232 prot.should_receive(:write_struct_end)
Bryan Duxburyc0166282009-02-02 00:48:17 +0000233 prot.should_receive(:write_field_begin).with('message', Types::STRING, 1)
234 prot.should_receive(:write_string).with("something happened")
235 prot.should_receive(:write_field_begin).with('code', Types::I32, 2)
236 prot.should_receive(:write_i32).with(5)
Kevin Clark3af92872008-07-28 22:20:36 +0000237 prot.should_receive(:write_field_stop)
Bryan Duxburyc0166282009-02-02 00:48:17 +0000238 prot.should_receive(:write_field_end).twice
Kevin Clark3af92872008-07-28 22:20:36 +0000239
240 e.write(prot)
241 end
242 end
Kevin Clark090b69e2008-06-18 01:12:58 +0000243 end
Kevin Clark03d7a472008-06-18 01:09:41 +0000244end