blob: 1eda3c3b4f44cd4919b910690fdf4a8552859e56 [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 = {}
Bryan Duxburyd815c212009-03-19 18:57:43 +000013 Foo.new.each_field { |fid,field_info| fields[fid] = field_info }
14 fields.should == Foo::FIELDS
Kevin Clark9479b1a2008-06-18 01:13:37 +000015 end
Kevin Clark9479b1a2008-06-18 01:13:37 +000016
Kevin Clark140b5552008-06-18 01:17:57 +000017 it "should initialize all fields to defaults" do
18 struct = Foo.new
19 struct.simple.should == 53
20 struct.words.should == "words"
21 struct.hello.should == Hello.new(:greeting => 'hello, world!')
22 struct.ints.should == [1, 2, 2, 3]
23 struct.complex.should be_nil
24 struct.shorts.should == Set.new([5, 17, 239])
25 end
Kevin Clark1cfd6932008-06-18 01:13:58 +000026
Kevin Clark140b5552008-06-18 01:17:57 +000027 it "should not share default values between instances" do
28 begin
29 struct = Foo.new
30 struct.ints << 17
31 Foo.new.ints.should == [1,2,2,3]
32 ensure
33 # ensure no leakage to other tests
34 Foo::FIELDS[4][:default] = [1,2,2,3]
35 end
36 end
Kevin Clark03d7a472008-06-18 01:09:41 +000037
Kevin Clark140b5552008-06-18 01:17:57 +000038 it "should properly initialize boolean values" do
39 struct = BoolStruct.new(:yesno => false)
40 struct.yesno.should be_false
41 end
Kevin Clark03d7a472008-06-18 01:09:41 +000042
Kevin Clark140b5552008-06-18 01:17:57 +000043 it "should have proper == semantics" do
44 Foo.new.should_not == Hello.new
45 Foo.new.should == Foo.new
46 Foo.new(:simple => 52).should_not == Foo.new
47 end
Kevin Clark03d7a472008-06-18 01:09:41 +000048
Kevin Clark140b5552008-06-18 01:17:57 +000049 it "should read itself off the wire" do
50 struct = Foo.new
Bryan Duxburyc0166282009-02-02 00:48:17 +000051 prot = Protocol.new(mock("transport"))
Kevin Clark140b5552008-06-18 01:17:57 +000052 prot.should_receive(:read_struct_begin).twice
53 prot.should_receive(:read_struct_end).twice
54 prot.should_receive(:read_field_begin).and_return(
55 ['complex', Types::MAP, 5], # Foo
56 ['words', Types::STRING, 2], # Foo
57 ['hello', Types::STRUCT, 3], # Foo
58 ['greeting', Types::STRING, 1], # Hello
59 [nil, Types::STOP, 0], # Hello
60 ['simple', Types::I32, 1], # Foo
61 ['ints', Types::LIST, 4], # Foo
62 ['shorts', Types::SET, 6], # Foo
63 [nil, Types::STOP, 0] # Hello
64 )
65 prot.should_receive(:read_field_end).exactly(7).times
66 prot.should_receive(:read_map_begin).and_return(
67 [Types::I32, Types::MAP, 2], # complex
68 [Types::STRING, Types::DOUBLE, 2], # complex/1/value
69 [Types::STRING, Types::DOUBLE, 1] # complex/2/value
70 )
71 prot.should_receive(:read_map_end).exactly(3).times
72 prot.should_receive(:read_list_begin).and_return([Types::I32, 4])
73 prot.should_receive(:read_list_end)
74 prot.should_receive(:read_set_begin).and_return([Types::I16, 2])
75 prot.should_receive(:read_set_end)
Bryan Duxburyc0166282009-02-02 00:48:17 +000076 prot.should_receive(:read_i32).and_return(
Kevin Clark140b5552008-06-18 01:17:57 +000077 1, 14, # complex keys
78 42, # simple
79 4, 23, 4, 29 # ints
80 )
Bryan Duxburyc0166282009-02-02 00:48:17 +000081 prot.should_receive(:read_string).and_return("pi", "e", "feigenbaum", "apple banana", "what's up?")
82 prot.should_receive(:read_double).and_return(Math::PI, Math::E, 4.669201609)
83 prot.should_receive(:read_i16).and_return(2, 3)
Kevin Clark140b5552008-06-18 01:17:57 +000084 prot.should_not_receive(:skip)
85 struct.read(prot)
Kevin Clark03d7a472008-06-18 01:09:41 +000086
Kevin Clark140b5552008-06-18 01:17:57 +000087 struct.simple.should == 42
88 struct.complex.should == {1 => {"pi" => Math::PI, "e" => Math::E}, 14 => {"feigenbaum" => 4.669201609}}
89 struct.hello.should == Hello.new(:greeting => "what's up?")
90 struct.words.should == "apple banana"
91 struct.ints.should == [4, 23, 4, 29]
92 struct.shorts.should == Set.new([3, 2])
93 end
Kevin Clark03d7a472008-06-18 01:09:41 +000094
Kevin Clark140b5552008-06-18 01:17:57 +000095 it "should skip unexpected fields in structs and use default values" do
96 struct = Foo.new
Bryan Duxburyc0166282009-02-02 00:48:17 +000097 prot = Protocol.new(mock("transport"))
Kevin Clark140b5552008-06-18 01:17:57 +000098 prot.should_receive(:read_struct_begin)
99 prot.should_receive(:read_struct_end)
100 prot.should_receive(:read_field_begin).and_return(
101 ['simple', Types::I32, 1],
102 ['complex', Types::STRUCT, 5],
103 ['thinz', Types::MAP, 7],
104 ['foobar', Types::I32, 3],
105 ['words', Types::STRING, 2],
106 [nil, Types::STOP, 0]
107 )
108 prot.should_receive(:read_field_end).exactly(5).times
Bryan Duxburyc0166282009-02-02 00:48:17 +0000109 prot.should_receive(:read_i32).and_return(42)
110 prot.should_receive(:read_string).and_return("foobar")
Kevin Clark140b5552008-06-18 01:17:57 +0000111 prot.should_receive(:skip).with(Types::STRUCT)
112 prot.should_receive(:skip).with(Types::MAP)
Bryan Duxburyc0166282009-02-02 00:48:17 +0000113 # prot.should_receive(:read_map_begin).and_return([Types::I32, Types::I32, 0])
114 # prot.should_receive(:read_map_end)
Kevin Clark140b5552008-06-18 01:17:57 +0000115 prot.should_receive(:skip).with(Types::I32)
116 struct.read(prot)
Kevin Clark03d7a472008-06-18 01:09:41 +0000117
Kevin Clark140b5552008-06-18 01:17:57 +0000118 struct.simple.should == 42
119 struct.complex.should be_nil
120 struct.words.should == "foobar"
121 struct.hello.should == Hello.new(:greeting => 'hello, world!')
122 struct.ints.should == [1, 2, 2, 3]
123 struct.shorts.should == Set.new([5, 17, 239])
124 end
Kevin Clark090b69e2008-06-18 01:12:58 +0000125
Kevin Clark140b5552008-06-18 01:17:57 +0000126 it "should write itself to the wire" do
Bryan Duxburyc0166282009-02-02 00:48:17 +0000127 prot = Protocol.new(mock("transport")) #mock("Protocol")
Kevin Clark140b5552008-06-18 01:17:57 +0000128 prot.should_receive(:write_struct_begin).with("SpecNamespace::Foo")
Bryan Duxburyc0166282009-02-02 00:48:17 +0000129 prot.should_receive(:write_struct_begin).with("SpecNamespace::Hello")
130 prot.should_receive(:write_struct_end).twice
Kevin Clark140b5552008-06-18 01:17:57 +0000131 prot.should_receive(:write_field_begin).with('ints', Types::LIST, 4)
Bryan Duxburyc0166282009-02-02 00:48:17 +0000132 prot.should_receive(:write_i32).with(1)
133 prot.should_receive(:write_i32).with(2).twice
134 prot.should_receive(:write_i32).with(3)
Kevin Clark140b5552008-06-18 01:17:57 +0000135 prot.should_receive(:write_field_begin).with('complex', Types::MAP, 5)
Bryan Duxburyc0166282009-02-02 00:48:17 +0000136 prot.should_receive(:write_i32).with(5)
137 prot.should_receive(:write_string).with('foo')
138 prot.should_receive(:write_double).with(1.23)
Kevin Clark140b5552008-06-18 01:17:57 +0000139 prot.should_receive(:write_field_begin).with('shorts', Types::SET, 6)
Bryan Duxburyc0166282009-02-02 00:48:17 +0000140 prot.should_receive(:write_i16).with(5)
141 prot.should_receive(:write_i16).with(17)
142 prot.should_receive(:write_i16).with(239)
143 prot.should_receive(:write_field_stop).twice
144 prot.should_receive(:write_field_end).exactly(6).times
145 prot.should_receive(:write_field_begin).with('simple', Types::I32, 1)
146 prot.should_receive(:write_i32).with(53)
147 prot.should_receive(:write_field_begin).with('hello', Types::STRUCT, 3)
148 prot.should_receive(:write_field_begin).with('greeting', Types::STRING, 1)
149 prot.should_receive(:write_string).with('hello, world!')
Kevin Clark140b5552008-06-18 01:17:57 +0000150 prot.should_receive(:write_map_begin).with(Types::I32, Types::MAP, 1)
151 prot.should_receive(:write_map_begin).with(Types::STRING, Types::DOUBLE, 1)
Kevin Clark140b5552008-06-18 01:17:57 +0000152 prot.should_receive(:write_map_end).twice
153 prot.should_receive(:write_list_begin).with(Types::I32, 4)
Kevin Clark140b5552008-06-18 01:17:57 +0000154 prot.should_receive(:write_list_end)
155 prot.should_receive(:write_set_begin).with(Types::I16, 3)
Kevin Clark140b5552008-06-18 01:17:57 +0000156 prot.should_receive(:write_set_end)
157
158 struct = Foo.new
159 struct.words = nil
160 struct.complex = {5 => {"foo" => 1.23}}
161 struct.write(prot)
162 end
163
164 it "should raise an exception if presented with an unknown container" do
165 # yeah this is silly, but I'm going for code coverage here
166 struct = Foo.new
167 lambda { struct.send :write_container, nil, nil, {:type => "foo"} }.should raise_error(StandardError, "Not a container type: foo")
168 end
Kevin Clark23193752008-06-18 01:18:07 +0000169
170 it "should support optional type-checking in Thrift::Struct.new" do
171 Thrift.type_checking = true
172 begin
Kevin Clarkb5863392008-07-18 22:03:48 +0000173 lambda { Hello.new(:greeting => 3) }.should raise_error(TypeError, "Expected Types::STRING, received Fixnum for field greeting")
Kevin Clark23193752008-06-18 01:18:07 +0000174 ensure
175 Thrift.type_checking = false
176 end
177 lambda { Hello.new(:greeting => 3) }.should_not raise_error(TypeError)
178 end
179
180 it "should support optional type-checking in field accessors" do
181 Thrift.type_checking = true
182 begin
183 hello = Hello.new
Kevin Clarkb5863392008-07-18 22:03:48 +0000184 lambda { hello.greeting = 3 }.should raise_error(TypeError, "Expected Types::STRING, received Fixnum for field greeting")
Kevin Clark23193752008-06-18 01:18:07 +0000185 ensure
186 Thrift.type_checking = false
187 end
188 lambda { hello.greeting = 3 }.should_not raise_error(TypeError)
189 end
190
191 it "should raise an exception when unknown types are given to Thrift::Struct.new" do
Kevin Clark38a2ce62008-08-25 21:34:19 +0000192 lambda { Hello.new(:fish => 'salmon') }.should raise_error(Exception, "Unknown key given to SpecNamespace::Hello.new: fish")
Kevin Clark23193752008-06-18 01:18:07 +0000193 end
Kevin Clark3af92872008-07-28 22:20:36 +0000194
195 it "should support `raise Xception, 'message'` for Exception structs" do
196 begin
197 raise Xception, "something happened"
198 rescue Thrift::Exception => e
199 e.message.should == "something happened"
200 e.code.should == 1
201 # ensure it gets serialized properly, this is the really important part
Bryan Duxburyc0166282009-02-02 00:48:17 +0000202 prot = Protocol.new(mock("trans"))
Kevin Clark031baf72008-11-14 17:11:39 +0000203 prot.should_receive(:write_struct_begin).with("SpecNamespace::Xception")
Kevin Clark3af92872008-07-28 22:20:36 +0000204 prot.should_receive(:write_struct_end)
Bryan Duxburyc0166282009-02-02 00:48:17 +0000205 prot.should_receive(:write_field_begin).with('message', Types::STRING, 1)#, "something happened")
206 prot.should_receive(:write_string).with("something happened")
207 prot.should_receive(:write_field_begin).with('code', Types::I32, 2)#, 1)
208 prot.should_receive(:write_i32).with(1)
Kevin Clark3af92872008-07-28 22:20:36 +0000209 prot.should_receive(:write_field_stop)
Bryan Duxburyc0166282009-02-02 00:48:17 +0000210 prot.should_receive(:write_field_end).twice
Kevin Clark3af92872008-07-28 22:20:36 +0000211
212 e.write(prot)
213 end
214 end
215
216 it "should support the regular initializer for exception structs" do
217 begin
218 raise Xception, :message => "something happened", :code => 5
219 rescue Thrift::Exception => e
220 e.message.should == "something happened"
221 e.code.should == 5
Bryan Duxburyc0166282009-02-02 00:48:17 +0000222 prot = Protocol.new(mock("trans"))
Kevin Clark031baf72008-11-14 17:11:39 +0000223 prot.should_receive(:write_struct_begin).with("SpecNamespace::Xception")
Kevin Clark3af92872008-07-28 22:20:36 +0000224 prot.should_receive(:write_struct_end)
Bryan Duxburyc0166282009-02-02 00:48:17 +0000225 prot.should_receive(:write_field_begin).with('message', Types::STRING, 1)
226 prot.should_receive(:write_string).with("something happened")
227 prot.should_receive(:write_field_begin).with('code', Types::I32, 2)
228 prot.should_receive(:write_i32).with(5)
Kevin Clark3af92872008-07-28 22:20:36 +0000229 prot.should_receive(:write_field_stop)
Bryan Duxburyc0166282009-02-02 00:48:17 +0000230 prot.should_receive(:write_field_end).twice
Kevin Clark3af92872008-07-28 22:20:36 +0000231
232 e.write(prot)
233 end
234 end
Kevin Clark090b69e2008-06-18 01:12:58 +0000235 end
Kevin Clark03d7a472008-06-18 01:09:41 +0000236end