blob: 69826e7b03667e57e30f0e06839e7314a4a68a18 [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 Clark2bd3a302008-06-26 17:49:49 +000021require File.dirname(__FILE__) + '/gen-rb/ThriftSpec_types'
Kevin Clark03d7a472008-06-18 01:09:41 +000022
Bryan Duxburyc0166282009-02-02 00:48:17 +000023# require "binaryprotocolaccelerated"
24
Kevin Clark03d7a472008-06-18 01:09:41 +000025class ThriftStructSpec < Spec::ExampleGroup
26 include Thrift
27 include SpecNamespace
28
Kevin Clark140b5552008-06-18 01:17:57 +000029 describe Struct do
30 it "should iterate over all fields properly" do
31 fields = {}
Bryan Duxburyd815c212009-03-19 18:57:43 +000032 Foo.new.each_field { |fid,field_info| fields[fid] = field_info }
33 fields.should == Foo::FIELDS
Kevin Clark9479b1a2008-06-18 01:13:37 +000034 end
Kevin Clark9479b1a2008-06-18 01:13:37 +000035
Kevin Clark140b5552008-06-18 01:17:57 +000036 it "should initialize all fields to defaults" do
37 struct = Foo.new
38 struct.simple.should == 53
39 struct.words.should == "words"
40 struct.hello.should == Hello.new(:greeting => 'hello, world!')
41 struct.ints.should == [1, 2, 2, 3]
42 struct.complex.should be_nil
43 struct.shorts.should == Set.new([5, 17, 239])
44 end
Kevin Clark1cfd6932008-06-18 01:13:58 +000045
Kevin Clark140b5552008-06-18 01:17:57 +000046 it "should not share default values between instances" do
47 begin
48 struct = Foo.new
49 struct.ints << 17
50 Foo.new.ints.should == [1,2,2,3]
51 ensure
52 # ensure no leakage to other tests
53 Foo::FIELDS[4][:default] = [1,2,2,3]
54 end
55 end
Kevin Clark03d7a472008-06-18 01:09:41 +000056
Kevin Clark140b5552008-06-18 01:17:57 +000057 it "should properly initialize boolean values" do
58 struct = BoolStruct.new(:yesno => false)
59 struct.yesno.should be_false
60 end
Kevin Clark03d7a472008-06-18 01:09:41 +000061
Kevin Clark140b5552008-06-18 01:17:57 +000062 it "should have proper == semantics" do
63 Foo.new.should_not == Hello.new
64 Foo.new.should == Foo.new
65 Foo.new(:simple => 52).should_not == Foo.new
66 end
Kevin Clark03d7a472008-06-18 01:09:41 +000067
Kevin Clark140b5552008-06-18 01:17:57 +000068 it "should read itself off the wire" do
69 struct = Foo.new
Bryan Duxburyc0166282009-02-02 00:48:17 +000070 prot = Protocol.new(mock("transport"))
Kevin Clark140b5552008-06-18 01:17:57 +000071 prot.should_receive(:read_struct_begin).twice
72 prot.should_receive(:read_struct_end).twice
73 prot.should_receive(:read_field_begin).and_return(
74 ['complex', Types::MAP, 5], # Foo
75 ['words', Types::STRING, 2], # Foo
76 ['hello', Types::STRUCT, 3], # Foo
77 ['greeting', Types::STRING, 1], # Hello
78 [nil, Types::STOP, 0], # Hello
79 ['simple', Types::I32, 1], # Foo
80 ['ints', Types::LIST, 4], # Foo
81 ['shorts', Types::SET, 6], # Foo
82 [nil, Types::STOP, 0] # Hello
83 )
84 prot.should_receive(:read_field_end).exactly(7).times
85 prot.should_receive(:read_map_begin).and_return(
86 [Types::I32, Types::MAP, 2], # complex
87 [Types::STRING, Types::DOUBLE, 2], # complex/1/value
88 [Types::STRING, Types::DOUBLE, 1] # complex/2/value
89 )
90 prot.should_receive(:read_map_end).exactly(3).times
91 prot.should_receive(:read_list_begin).and_return([Types::I32, 4])
92 prot.should_receive(:read_list_end)
93 prot.should_receive(:read_set_begin).and_return([Types::I16, 2])
94 prot.should_receive(:read_set_end)
Bryan Duxburyc0166282009-02-02 00:48:17 +000095 prot.should_receive(:read_i32).and_return(
Kevin Clark140b5552008-06-18 01:17:57 +000096 1, 14, # complex keys
97 42, # simple
98 4, 23, 4, 29 # ints
99 )
Bryan Duxburyc0166282009-02-02 00:48:17 +0000100 prot.should_receive(:read_string).and_return("pi", "e", "feigenbaum", "apple banana", "what's up?")
101 prot.should_receive(:read_double).and_return(Math::PI, Math::E, 4.669201609)
102 prot.should_receive(:read_i16).and_return(2, 3)
Kevin Clark140b5552008-06-18 01:17:57 +0000103 prot.should_not_receive(:skip)
104 struct.read(prot)
Kevin Clark03d7a472008-06-18 01:09:41 +0000105
Kevin Clark140b5552008-06-18 01:17:57 +0000106 struct.simple.should == 42
107 struct.complex.should == {1 => {"pi" => Math::PI, "e" => Math::E}, 14 => {"feigenbaum" => 4.669201609}}
108 struct.hello.should == Hello.new(:greeting => "what's up?")
109 struct.words.should == "apple banana"
110 struct.ints.should == [4, 23, 4, 29]
111 struct.shorts.should == Set.new([3, 2])
112 end
Kevin Clark03d7a472008-06-18 01:09:41 +0000113
Kevin Clark140b5552008-06-18 01:17:57 +0000114 it "should skip unexpected fields in structs and use default values" do
115 struct = Foo.new
Bryan Duxburyc0166282009-02-02 00:48:17 +0000116 prot = Protocol.new(mock("transport"))
Kevin Clark140b5552008-06-18 01:17:57 +0000117 prot.should_receive(:read_struct_begin)
118 prot.should_receive(:read_struct_end)
119 prot.should_receive(:read_field_begin).and_return(
120 ['simple', Types::I32, 1],
121 ['complex', Types::STRUCT, 5],
122 ['thinz', Types::MAP, 7],
123 ['foobar', Types::I32, 3],
124 ['words', Types::STRING, 2],
125 [nil, Types::STOP, 0]
126 )
127 prot.should_receive(:read_field_end).exactly(5).times
Bryan Duxburyc0166282009-02-02 00:48:17 +0000128 prot.should_receive(:read_i32).and_return(42)
129 prot.should_receive(:read_string).and_return("foobar")
Kevin Clark140b5552008-06-18 01:17:57 +0000130 prot.should_receive(:skip).with(Types::STRUCT)
131 prot.should_receive(:skip).with(Types::MAP)
Bryan Duxburyc0166282009-02-02 00:48:17 +0000132 # prot.should_receive(:read_map_begin).and_return([Types::I32, Types::I32, 0])
133 # prot.should_receive(:read_map_end)
Kevin Clark140b5552008-06-18 01:17:57 +0000134 prot.should_receive(:skip).with(Types::I32)
135 struct.read(prot)
Kevin Clark03d7a472008-06-18 01:09:41 +0000136
Kevin Clark140b5552008-06-18 01:17:57 +0000137 struct.simple.should == 42
138 struct.complex.should be_nil
139 struct.words.should == "foobar"
140 struct.hello.should == Hello.new(:greeting => 'hello, world!')
141 struct.ints.should == [1, 2, 2, 3]
142 struct.shorts.should == Set.new([5, 17, 239])
143 end
Kevin Clark090b69e2008-06-18 01:12:58 +0000144
Kevin Clark140b5552008-06-18 01:17:57 +0000145 it "should write itself to the wire" do
Bryan Duxburyc0166282009-02-02 00:48:17 +0000146 prot = Protocol.new(mock("transport")) #mock("Protocol")
Kevin Clark140b5552008-06-18 01:17:57 +0000147 prot.should_receive(:write_struct_begin).with("SpecNamespace::Foo")
Bryan Duxburyc0166282009-02-02 00:48:17 +0000148 prot.should_receive(:write_struct_begin).with("SpecNamespace::Hello")
149 prot.should_receive(:write_struct_end).twice
Kevin Clark140b5552008-06-18 01:17:57 +0000150 prot.should_receive(:write_field_begin).with('ints', Types::LIST, 4)
Bryan Duxburyc0166282009-02-02 00:48:17 +0000151 prot.should_receive(:write_i32).with(1)
152 prot.should_receive(:write_i32).with(2).twice
153 prot.should_receive(:write_i32).with(3)
Kevin Clark140b5552008-06-18 01:17:57 +0000154 prot.should_receive(:write_field_begin).with('complex', Types::MAP, 5)
Bryan Duxburyc0166282009-02-02 00:48:17 +0000155 prot.should_receive(:write_i32).with(5)
156 prot.should_receive(:write_string).with('foo')
157 prot.should_receive(:write_double).with(1.23)
Kevin Clark140b5552008-06-18 01:17:57 +0000158 prot.should_receive(:write_field_begin).with('shorts', Types::SET, 6)
Bryan Duxburyc0166282009-02-02 00:48:17 +0000159 prot.should_receive(:write_i16).with(5)
160 prot.should_receive(:write_i16).with(17)
161 prot.should_receive(:write_i16).with(239)
162 prot.should_receive(:write_field_stop).twice
163 prot.should_receive(:write_field_end).exactly(6).times
164 prot.should_receive(:write_field_begin).with('simple', Types::I32, 1)
165 prot.should_receive(:write_i32).with(53)
166 prot.should_receive(:write_field_begin).with('hello', Types::STRUCT, 3)
167 prot.should_receive(:write_field_begin).with('greeting', Types::STRING, 1)
168 prot.should_receive(:write_string).with('hello, world!')
Kevin Clark140b5552008-06-18 01:17:57 +0000169 prot.should_receive(:write_map_begin).with(Types::I32, Types::MAP, 1)
170 prot.should_receive(:write_map_begin).with(Types::STRING, Types::DOUBLE, 1)
Kevin Clark140b5552008-06-18 01:17:57 +0000171 prot.should_receive(:write_map_end).twice
172 prot.should_receive(:write_list_begin).with(Types::I32, 4)
Kevin Clark140b5552008-06-18 01:17:57 +0000173 prot.should_receive(:write_list_end)
174 prot.should_receive(:write_set_begin).with(Types::I16, 3)
Kevin Clark140b5552008-06-18 01:17:57 +0000175 prot.should_receive(:write_set_end)
176
177 struct = Foo.new
178 struct.words = nil
179 struct.complex = {5 => {"foo" => 1.23}}
180 struct.write(prot)
181 end
182
183 it "should raise an exception if presented with an unknown container" do
184 # yeah this is silly, but I'm going for code coverage here
185 struct = Foo.new
186 lambda { struct.send :write_container, nil, nil, {:type => "foo"} }.should raise_error(StandardError, "Not a container type: foo")
187 end
Kevin Clark23193752008-06-18 01:18:07 +0000188
189 it "should support optional type-checking in Thrift::Struct.new" do
190 Thrift.type_checking = true
191 begin
Kevin Clarkb5863392008-07-18 22:03:48 +0000192 lambda { Hello.new(: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.new(:greeting => 3) }.should_not raise_error(TypeError)
197 end
198
199 it "should support optional type-checking in field accessors" do
200 Thrift.type_checking = true
201 begin
202 hello = Hello.new
Kevin Clarkb5863392008-07-18 22:03:48 +0000203 lambda { hello.greeting = 3 }.should raise_error(TypeError, "Expected Types::STRING, received Fixnum for field greeting")
Kevin Clark23193752008-06-18 01:18:07 +0000204 ensure
205 Thrift.type_checking = false
206 end
207 lambda { hello.greeting = 3 }.should_not raise_error(TypeError)
208 end
209
210 it "should raise an exception when unknown types are given to Thrift::Struct.new" do
Kevin Clark38a2ce62008-08-25 21:34:19 +0000211 lambda { Hello.new(:fish => 'salmon') }.should raise_error(Exception, "Unknown key given to SpecNamespace::Hello.new: fish")
Kevin Clark23193752008-06-18 01:18:07 +0000212 end
Kevin Clark3af92872008-07-28 22:20:36 +0000213
214 it "should support `raise Xception, 'message'` for Exception structs" do
215 begin
216 raise Xception, "something happened"
217 rescue Thrift::Exception => e
218 e.message.should == "something happened"
219 e.code.should == 1
220 # ensure it gets serialized properly, this is the really important part
Bryan Duxburyc0166282009-02-02 00:48:17 +0000221 prot = Protocol.new(mock("trans"))
Kevin Clark031baf72008-11-14 17:11:39 +0000222 prot.should_receive(:write_struct_begin).with("SpecNamespace::Xception")
Kevin Clark3af92872008-07-28 22:20:36 +0000223 prot.should_receive(:write_struct_end)
Bryan Duxburyc0166282009-02-02 00:48:17 +0000224 prot.should_receive(:write_field_begin).with('message', Types::STRING, 1)#, "something happened")
225 prot.should_receive(:write_string).with("something happened")
226 prot.should_receive(:write_field_begin).with('code', Types::I32, 2)#, 1)
227 prot.should_receive(:write_i32).with(1)
Kevin Clark3af92872008-07-28 22:20:36 +0000228 prot.should_receive(:write_field_stop)
Bryan Duxburyc0166282009-02-02 00:48:17 +0000229 prot.should_receive(:write_field_end).twice
Kevin Clark3af92872008-07-28 22:20:36 +0000230
231 e.write(prot)
232 end
233 end
234
235 it "should support the regular initializer for exception structs" do
236 begin
237 raise Xception, :message => "something happened", :code => 5
238 rescue Thrift::Exception => e
239 e.message.should == "something happened"
240 e.code.should == 5
Bryan Duxburyc0166282009-02-02 00:48:17 +0000241 prot = Protocol.new(mock("trans"))
Kevin Clark031baf72008-11-14 17:11:39 +0000242 prot.should_receive(:write_struct_begin).with("SpecNamespace::Xception")
Kevin Clark3af92872008-07-28 22:20:36 +0000243 prot.should_receive(:write_struct_end)
Bryan Duxburyc0166282009-02-02 00:48:17 +0000244 prot.should_receive(:write_field_begin).with('message', Types::STRING, 1)
245 prot.should_receive(:write_string).with("something happened")
246 prot.should_receive(:write_field_begin).with('code', Types::I32, 2)
247 prot.should_receive(:write_i32).with(5)
Kevin Clark3af92872008-07-28 22:20:36 +0000248 prot.should_receive(:write_field_stop)
Bryan Duxburyc0166282009-02-02 00:48:17 +0000249 prot.should_receive(:write_field_end).twice
Kevin Clark3af92872008-07-28 22:20:36 +0000250
251 e.write(prot)
252 end
253 end
Kevin Clark090b69e2008-06-18 01:12:58 +0000254 end
Kevin Clark03d7a472008-06-18 01:09:41 +0000255end