blob: 59a40181580a6356494d9879d2a3626f29581718 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001#
2# Licensed to the Apache Software Foundation (ASF) under one
3# or more contributor license agreements. See the NOTICE file
4# distributed with this work for additional information
5# regarding copyright ownership. The ASF licenses this file
6# to you under the Apache License, Version 2.0 (the
7# "License"); you may not use this file except in compliance
8# with the License. You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing,
13# software distributed under the License is distributed on an
14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15# KIND, either express or implied. See the License for the
16# specific language governing permissions and limitations
17# under the License.
18#
19
Kevin Clark03d7a472008-06-18 01:09:41 +000020require File.dirname(__FILE__) + '/spec_helper'
Kevin Clark03d7a472008-06-18 01:09:41 +000021
22class ThriftStructSpec < Spec::ExampleGroup
23 include Thrift
24 include SpecNamespace
25
Kevin Clark140b5552008-06-18 01:17:57 +000026 describe Struct do
27 it "should iterate over all fields properly" do
28 fields = {}
Bryan Duxburyd815c212009-03-19 18:57:43 +000029 Foo.new.each_field { |fid,field_info| fields[fid] = field_info }
30 fields.should == Foo::FIELDS
Kevin Clark9479b1a2008-06-18 01:13:37 +000031 end
Kevin Clark9479b1a2008-06-18 01:13:37 +000032
Kevin Clark140b5552008-06-18 01:17:57 +000033 it "should initialize all fields to defaults" do
34 struct = Foo.new
35 struct.simple.should == 53
36 struct.words.should == "words"
37 struct.hello.should == Hello.new(:greeting => 'hello, world!')
38 struct.ints.should == [1, 2, 2, 3]
39 struct.complex.should be_nil
40 struct.shorts.should == Set.new([5, 17, 239])
41 end
Kevin Clark1cfd6932008-06-18 01:13:58 +000042
Kevin Clark140b5552008-06-18 01:17:57 +000043 it "should not share default values between instances" do
44 begin
45 struct = Foo.new
46 struct.ints << 17
47 Foo.new.ints.should == [1,2,2,3]
48 ensure
49 # ensure no leakage to other tests
50 Foo::FIELDS[4][:default] = [1,2,2,3]
51 end
52 end
Kevin Clark03d7a472008-06-18 01:09:41 +000053
Kevin Clark140b5552008-06-18 01:17:57 +000054 it "should properly initialize boolean values" do
55 struct = BoolStruct.new(:yesno => false)
56 struct.yesno.should be_false
57 end
Kevin Clark03d7a472008-06-18 01:09:41 +000058
Kevin Clark140b5552008-06-18 01:17:57 +000059 it "should have proper == semantics" do
60 Foo.new.should_not == Hello.new
61 Foo.new.should == Foo.new
62 Foo.new(:simple => 52).should_not == Foo.new
63 end
Kevin Clark03d7a472008-06-18 01:09:41 +000064
Bryan Duxbury3d03c522010-02-18 17:42:06 +000065 it "should print enum value names in inspect" do
66 StructWithSomeEnum.new(:some_enum => SomeEnum::ONE).inspect.should == "<SpecNamespace::StructWithSomeEnum some_enum:ONE (0)>"
67
68 StructWithEnumMap.new(:my_map => {SomeEnum::ONE => [SomeEnum::TWO]}).inspect.should == "<SpecNamespace::StructWithEnumMap my_map:{ONE (0): [TWO (1)]}>"
69 end
70
Bryan Duxbury39dadd62010-02-18 22:00:45 +000071 it "should pretty print binary fields" do
72 Foo2.new(:my_binary => "\001\002\003").inspect.should == "<SpecNamespace::Foo2 my_binary:010203>"
73 end
74
Bryan Duxbury0e4920c2010-02-18 20:28:27 +000075 it "should offer field? methods" do
76 Foo.new.opt_string?.should be_false
77 Foo.new(:simple => 52).simple?.should be_true
78 Foo.new(:my_bool => false).my_bool?.should be_true
79 Foo.new(:my_bool => true).my_bool?.should be_true
80 end
81
Bryan Duxbury205e4502010-02-18 23:19:42 +000082 it "should be comparable" do
83 s1 = StructWithSomeEnum.new(:some_enum => SomeEnum::ONE)
84 s2 = StructWithSomeEnum.new(:some_enum => SomeEnum::TWO)
85
86 (s1 <=> s2).should == -1
87 (s2 <=> s1).should == 1
88 (s1 <=> s1).should == 0
89 (s1 <=> StructWithSomeEnum.new()).should == -1
90 end
91
Kevin Clark140b5552008-06-18 01:17:57 +000092 it "should read itself off the wire" do
93 struct = Foo.new
Bryan Duxburyd1d15422009-04-04 00:58:03 +000094 prot = BaseProtocol.new(mock("transport"))
Kevin Clark140b5552008-06-18 01:17:57 +000095 prot.should_receive(:read_struct_begin).twice
96 prot.should_receive(:read_struct_end).twice
97 prot.should_receive(:read_field_begin).and_return(
98 ['complex', Types::MAP, 5], # Foo
99 ['words', Types::STRING, 2], # Foo
100 ['hello', Types::STRUCT, 3], # Foo
101 ['greeting', Types::STRING, 1], # Hello
102 [nil, Types::STOP, 0], # Hello
103 ['simple', Types::I32, 1], # Foo
104 ['ints', Types::LIST, 4], # Foo
105 ['shorts', Types::SET, 6], # Foo
106 [nil, Types::STOP, 0] # Hello
107 )
108 prot.should_receive(:read_field_end).exactly(7).times
109 prot.should_receive(:read_map_begin).and_return(
110 [Types::I32, Types::MAP, 2], # complex
111 [Types::STRING, Types::DOUBLE, 2], # complex/1/value
112 [Types::STRING, Types::DOUBLE, 1] # complex/2/value
113 )
114 prot.should_receive(:read_map_end).exactly(3).times
115 prot.should_receive(:read_list_begin).and_return([Types::I32, 4])
116 prot.should_receive(:read_list_end)
117 prot.should_receive(:read_set_begin).and_return([Types::I16, 2])
118 prot.should_receive(:read_set_end)
Bryan Duxburyc0166282009-02-02 00:48:17 +0000119 prot.should_receive(:read_i32).and_return(
Kevin Clark140b5552008-06-18 01:17:57 +0000120 1, 14, # complex keys
121 42, # simple
122 4, 23, 4, 29 # ints
123 )
Bryan Duxburyc0166282009-02-02 00:48:17 +0000124 prot.should_receive(:read_string).and_return("pi", "e", "feigenbaum", "apple banana", "what's up?")
125 prot.should_receive(:read_double).and_return(Math::PI, Math::E, 4.669201609)
126 prot.should_receive(:read_i16).and_return(2, 3)
Kevin Clark140b5552008-06-18 01:17:57 +0000127 prot.should_not_receive(:skip)
128 struct.read(prot)
Kevin Clark03d7a472008-06-18 01:09:41 +0000129
Kevin Clark140b5552008-06-18 01:17:57 +0000130 struct.simple.should == 42
131 struct.complex.should == {1 => {"pi" => Math::PI, "e" => Math::E}, 14 => {"feigenbaum" => 4.669201609}}
132 struct.hello.should == Hello.new(:greeting => "what's up?")
133 struct.words.should == "apple banana"
134 struct.ints.should == [4, 23, 4, 29]
135 struct.shorts.should == Set.new([3, 2])
136 end
Kevin Clark03d7a472008-06-18 01:09:41 +0000137
Bryan Duxbury30dd7252010-02-27 05:47:15 +0000138 it "should serialize false boolean fields correctly" do
139 b = BoolStruct.new(:yesno => false)
140 prot = BinaryProtocol.new(MemoryBufferTransport.new)
141 prot.should_receive(:write_bool).with(false)
142 b.write(prot)
143 end
144
Kevin Clark140b5552008-06-18 01:17:57 +0000145 it "should skip unexpected fields in structs and use default values" do
146 struct = Foo.new
Bryan Duxburyd1d15422009-04-04 00:58:03 +0000147 prot = BaseProtocol.new(mock("transport"))
Kevin Clark140b5552008-06-18 01:17:57 +0000148 prot.should_receive(:read_struct_begin)
149 prot.should_receive(:read_struct_end)
150 prot.should_receive(:read_field_begin).and_return(
151 ['simple', Types::I32, 1],
152 ['complex', Types::STRUCT, 5],
153 ['thinz', Types::MAP, 7],
154 ['foobar', Types::I32, 3],
155 ['words', Types::STRING, 2],
156 [nil, Types::STOP, 0]
157 )
158 prot.should_receive(:read_field_end).exactly(5).times
Bryan Duxburyc0166282009-02-02 00:48:17 +0000159 prot.should_receive(:read_i32).and_return(42)
160 prot.should_receive(:read_string).and_return("foobar")
Kevin Clark140b5552008-06-18 01:17:57 +0000161 prot.should_receive(:skip).with(Types::STRUCT)
162 prot.should_receive(:skip).with(Types::MAP)
Bryan Duxburyc0166282009-02-02 00:48:17 +0000163 # prot.should_receive(:read_map_begin).and_return([Types::I32, Types::I32, 0])
164 # prot.should_receive(:read_map_end)
Kevin Clark140b5552008-06-18 01:17:57 +0000165 prot.should_receive(:skip).with(Types::I32)
166 struct.read(prot)
Kevin Clark03d7a472008-06-18 01:09:41 +0000167
Kevin Clark140b5552008-06-18 01:17:57 +0000168 struct.simple.should == 42
169 struct.complex.should be_nil
170 struct.words.should == "foobar"
171 struct.hello.should == Hello.new(:greeting => 'hello, world!')
172 struct.ints.should == [1, 2, 2, 3]
173 struct.shorts.should == Set.new([5, 17, 239])
174 end
Kevin Clark090b69e2008-06-18 01:12:58 +0000175
Kevin Clark140b5552008-06-18 01:17:57 +0000176 it "should write itself to the wire" do
Bryan Duxburyd1d15422009-04-04 00:58:03 +0000177 prot = BaseProtocol.new(mock("transport")) #mock("Protocol")
Kevin Clark140b5552008-06-18 01:17:57 +0000178 prot.should_receive(:write_struct_begin).with("SpecNamespace::Foo")
Bryan Duxburyc0166282009-02-02 00:48:17 +0000179 prot.should_receive(:write_struct_begin).with("SpecNamespace::Hello")
180 prot.should_receive(:write_struct_end).twice
Kevin Clark140b5552008-06-18 01:17:57 +0000181 prot.should_receive(:write_field_begin).with('ints', Types::LIST, 4)
Bryan Duxburyc0166282009-02-02 00:48:17 +0000182 prot.should_receive(:write_i32).with(1)
183 prot.should_receive(:write_i32).with(2).twice
184 prot.should_receive(:write_i32).with(3)
Kevin Clark140b5552008-06-18 01:17:57 +0000185 prot.should_receive(:write_field_begin).with('complex', Types::MAP, 5)
Bryan Duxburyc0166282009-02-02 00:48:17 +0000186 prot.should_receive(:write_i32).with(5)
187 prot.should_receive(:write_string).with('foo')
188 prot.should_receive(:write_double).with(1.23)
Kevin Clark140b5552008-06-18 01:17:57 +0000189 prot.should_receive(:write_field_begin).with('shorts', Types::SET, 6)
Bryan Duxburyc0166282009-02-02 00:48:17 +0000190 prot.should_receive(:write_i16).with(5)
191 prot.should_receive(:write_i16).with(17)
192 prot.should_receive(:write_i16).with(239)
193 prot.should_receive(:write_field_stop).twice
194 prot.should_receive(:write_field_end).exactly(6).times
195 prot.should_receive(:write_field_begin).with('simple', Types::I32, 1)
196 prot.should_receive(:write_i32).with(53)
197 prot.should_receive(:write_field_begin).with('hello', Types::STRUCT, 3)
198 prot.should_receive(:write_field_begin).with('greeting', Types::STRING, 1)
199 prot.should_receive(:write_string).with('hello, world!')
Kevin Clark140b5552008-06-18 01:17:57 +0000200 prot.should_receive(:write_map_begin).with(Types::I32, Types::MAP, 1)
201 prot.should_receive(:write_map_begin).with(Types::STRING, Types::DOUBLE, 1)
Kevin Clark140b5552008-06-18 01:17:57 +0000202 prot.should_receive(:write_map_end).twice
203 prot.should_receive(:write_list_begin).with(Types::I32, 4)
Kevin Clark140b5552008-06-18 01:17:57 +0000204 prot.should_receive(:write_list_end)
205 prot.should_receive(:write_set_begin).with(Types::I16, 3)
Kevin Clark140b5552008-06-18 01:17:57 +0000206 prot.should_receive(:write_set_end)
207
208 struct = Foo.new
209 struct.words = nil
210 struct.complex = {5 => {"foo" => 1.23}}
211 struct.write(prot)
212 end
213
214 it "should raise an exception if presented with an unknown container" do
215 # yeah this is silly, but I'm going for code coverage here
216 struct = Foo.new
217 lambda { struct.send :write_container, nil, nil, {:type => "foo"} }.should raise_error(StandardError, "Not a container type: foo")
218 end
Kevin Clark23193752008-06-18 01:18:07 +0000219
220 it "should support optional type-checking in Thrift::Struct.new" do
221 Thrift.type_checking = true
222 begin
Kevin Clarkb5863392008-07-18 22:03:48 +0000223 lambda { Hello.new(:greeting => 3) }.should raise_error(TypeError, "Expected Types::STRING, received Fixnum for field greeting")
Kevin Clark23193752008-06-18 01:18:07 +0000224 ensure
225 Thrift.type_checking = false
226 end
227 lambda { Hello.new(:greeting => 3) }.should_not raise_error(TypeError)
228 end
229
230 it "should support optional type-checking in field accessors" do
231 Thrift.type_checking = true
232 begin
233 hello = Hello.new
Kevin Clarkb5863392008-07-18 22:03:48 +0000234 lambda { hello.greeting = 3 }.should raise_error(TypeError, "Expected Types::STRING, received Fixnum for field greeting")
Kevin Clark23193752008-06-18 01:18:07 +0000235 ensure
236 Thrift.type_checking = false
237 end
238 lambda { hello.greeting = 3 }.should_not raise_error(TypeError)
239 end
240
241 it "should raise an exception when unknown types are given to Thrift::Struct.new" do
Kevin Clark38a2ce62008-08-25 21:34:19 +0000242 lambda { Hello.new(:fish => 'salmon') }.should raise_error(Exception, "Unknown key given to SpecNamespace::Hello.new: fish")
Kevin Clark23193752008-06-18 01:18:07 +0000243 end
Kevin Clark3af92872008-07-28 22:20:36 +0000244
245 it "should support `raise Xception, 'message'` for Exception structs" do
246 begin
247 raise Xception, "something happened"
248 rescue Thrift::Exception => e
249 e.message.should == "something happened"
250 e.code.should == 1
251 # ensure it gets serialized properly, this is the really important part
Bryan Duxburyd1d15422009-04-04 00:58:03 +0000252 prot = BaseProtocol.new(mock("trans"))
Kevin Clark031baf72008-11-14 17:11:39 +0000253 prot.should_receive(:write_struct_begin).with("SpecNamespace::Xception")
Kevin Clark3af92872008-07-28 22:20:36 +0000254 prot.should_receive(:write_struct_end)
Bryan Duxburyc0166282009-02-02 00:48:17 +0000255 prot.should_receive(:write_field_begin).with('message', Types::STRING, 1)#, "something happened")
256 prot.should_receive(:write_string).with("something happened")
257 prot.should_receive(:write_field_begin).with('code', Types::I32, 2)#, 1)
258 prot.should_receive(:write_i32).with(1)
Kevin Clark3af92872008-07-28 22:20:36 +0000259 prot.should_receive(:write_field_stop)
Bryan Duxburyc0166282009-02-02 00:48:17 +0000260 prot.should_receive(:write_field_end).twice
Kevin Clark3af92872008-07-28 22:20:36 +0000261
262 e.write(prot)
263 end
264 end
265
266 it "should support the regular initializer for exception structs" do
267 begin
268 raise Xception, :message => "something happened", :code => 5
269 rescue Thrift::Exception => e
270 e.message.should == "something happened"
271 e.code.should == 5
Bryan Duxburyd1d15422009-04-04 00:58:03 +0000272 prot = BaseProtocol.new(mock("trans"))
Kevin Clark031baf72008-11-14 17:11:39 +0000273 prot.should_receive(:write_struct_begin).with("SpecNamespace::Xception")
Kevin Clark3af92872008-07-28 22:20:36 +0000274 prot.should_receive(:write_struct_end)
Bryan Duxburyc0166282009-02-02 00:48:17 +0000275 prot.should_receive(:write_field_begin).with('message', Types::STRING, 1)
276 prot.should_receive(:write_string).with("something happened")
277 prot.should_receive(:write_field_begin).with('code', Types::I32, 2)
278 prot.should_receive(:write_i32).with(5)
Kevin Clark3af92872008-07-28 22:20:36 +0000279 prot.should_receive(:write_field_stop)
Bryan Duxburyc0166282009-02-02 00:48:17 +0000280 prot.should_receive(:write_field_end).twice
Kevin Clark3af92872008-07-28 22:20:36 +0000281
282 e.write(prot)
283 end
284 end
Kevin Clark090b69e2008-06-18 01:12:58 +0000285 end
Kevin Clark03d7a472008-06-18 01:09:41 +0000286end