blob: 8e83d4781c6e30e5b30e21642c2270e72ba07a45 [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
4class ThriftStructSpec < Spec::ExampleGroup
5 include Thrift
6 include SpecNamespace
7
Kevin Clark3af92872008-07-28 22:20:36 +00008 class Xception < Thrift::Exception
Kevin Clark03d7a472008-06-18 01:09:41 +00009 include Thrift::Struct
Kevin Clark3af92872008-07-28 22:20:36 +000010 attr_accessor :message, :code
Kevin Clark03d7a472008-06-18 01:09:41 +000011 FIELDS = {
Kevin Clark3af92872008-07-28 22:20:36 +000012 1 => {:type => Thrift::Types::STRING, :name => 'message'},
13 2 => {:type => Thrift::Types::I32, :name => 'code', :default => 1}
Kevin Clark03d7a472008-06-18 01:09:41 +000014 }
15 end
16
Kevin Clark140b5552008-06-18 01:17:57 +000017 describe Struct do
18 it "should iterate over all fields properly" do
19 fields = {}
20 Foo.new.each_field { |fid,type,name,default| fields[fid] = [type,name,default] }
21 fields.should == {
22 1 => [Types::I32, 'simple', 53],
23 2 => [Types::STRING, 'words', "words"],
24 3 => [Types::STRUCT, 'hello', Hello.new(:greeting => 'hello, world!')],
25 4 => [Types::LIST, 'ints', [1, 2, 2, 3]],
26 5 => [Types::MAP, 'complex', nil],
27 6 => [Types::SET, 'shorts', Set.new([5, 17, 239])]
28 }
Kevin Clark9479b1a2008-06-18 01:13:37 +000029 end
Kevin Clark9479b1a2008-06-18 01:13:37 +000030
Kevin Clark140b5552008-06-18 01:17:57 +000031 it "should initialize all fields to defaults" do
32 struct = Foo.new
33 struct.simple.should == 53
34 struct.words.should == "words"
35 struct.hello.should == Hello.new(:greeting => 'hello, world!')
36 struct.ints.should == [1, 2, 2, 3]
37 struct.complex.should be_nil
38 struct.shorts.should == Set.new([5, 17, 239])
39 end
Kevin Clark1cfd6932008-06-18 01:13:58 +000040
Kevin Clark140b5552008-06-18 01:17:57 +000041 it "should not share default values between instances" do
42 begin
43 struct = Foo.new
44 struct.ints << 17
45 Foo.new.ints.should == [1,2,2,3]
46 ensure
47 # ensure no leakage to other tests
48 Foo::FIELDS[4][:default] = [1,2,2,3]
49 end
50 end
Kevin Clark03d7a472008-06-18 01:09:41 +000051
Kevin Clark140b5552008-06-18 01:17:57 +000052 it "should properly initialize boolean values" do
53 struct = BoolStruct.new(:yesno => false)
54 struct.yesno.should be_false
55 end
Kevin Clark03d7a472008-06-18 01:09:41 +000056
Kevin Clark140b5552008-06-18 01:17:57 +000057 it "should have proper == semantics" do
58 Foo.new.should_not == Hello.new
59 Foo.new.should == Foo.new
60 Foo.new(:simple => 52).should_not == Foo.new
61 end
Kevin Clark03d7a472008-06-18 01:09:41 +000062
Kevin Clark140b5552008-06-18 01:17:57 +000063 it "should read itself off the wire" do
64 struct = Foo.new
65 prot = mock("Protocol")
66 prot.should_receive(:read_struct_begin).twice
67 prot.should_receive(:read_struct_end).twice
68 prot.should_receive(:read_field_begin).and_return(
69 ['complex', Types::MAP, 5], # Foo
70 ['words', Types::STRING, 2], # Foo
71 ['hello', Types::STRUCT, 3], # Foo
72 ['greeting', Types::STRING, 1], # Hello
73 [nil, Types::STOP, 0], # Hello
74 ['simple', Types::I32, 1], # Foo
75 ['ints', Types::LIST, 4], # Foo
76 ['shorts', Types::SET, 6], # Foo
77 [nil, Types::STOP, 0] # Hello
78 )
79 prot.should_receive(:read_field_end).exactly(7).times
80 prot.should_receive(:read_map_begin).and_return(
81 [Types::I32, Types::MAP, 2], # complex
82 [Types::STRING, Types::DOUBLE, 2], # complex/1/value
83 [Types::STRING, Types::DOUBLE, 1] # complex/2/value
84 )
85 prot.should_receive(:read_map_end).exactly(3).times
86 prot.should_receive(:read_list_begin).and_return([Types::I32, 4])
87 prot.should_receive(:read_list_end)
88 prot.should_receive(:read_set_begin).and_return([Types::I16, 2])
89 prot.should_receive(:read_set_end)
90 prot.should_receive(:read_type).with(Types::I32).and_return(
91 1, 14, # complex keys
92 42, # simple
93 4, 23, 4, 29 # ints
94 )
95 prot.should_receive(:read_type).with(Types::STRING).and_return("pi", "e", "feigenbaum", "apple banana", "what's up?")
96 prot.should_receive(:read_type).with(Types::DOUBLE).and_return(Math::PI, Math::E, 4.669201609)
97 prot.should_receive(:read_type).with(Types::I16).and_return(2, 3)
98 prot.should_not_receive(:skip)
99 struct.read(prot)
Kevin Clark03d7a472008-06-18 01:09:41 +0000100
Kevin Clark140b5552008-06-18 01:17:57 +0000101 struct.simple.should == 42
102 struct.complex.should == {1 => {"pi" => Math::PI, "e" => Math::E}, 14 => {"feigenbaum" => 4.669201609}}
103 struct.hello.should == Hello.new(:greeting => "what's up?")
104 struct.words.should == "apple banana"
105 struct.ints.should == [4, 23, 4, 29]
106 struct.shorts.should == Set.new([3, 2])
107 end
Kevin Clark03d7a472008-06-18 01:09:41 +0000108
Kevin Clark140b5552008-06-18 01:17:57 +0000109 it "should skip unexpected fields in structs and use default values" do
110 struct = Foo.new
111 prot = mock("Protocol")
112 prot.should_receive(:read_struct_begin)
113 prot.should_receive(:read_struct_end)
114 prot.should_receive(:read_field_begin).and_return(
115 ['simple', Types::I32, 1],
116 ['complex', Types::STRUCT, 5],
117 ['thinz', Types::MAP, 7],
118 ['foobar', Types::I32, 3],
119 ['words', Types::STRING, 2],
120 [nil, Types::STOP, 0]
121 )
122 prot.should_receive(:read_field_end).exactly(5).times
123 prot.should_receive(:read_type).with(Types::I32).and_return(42)
124 prot.should_receive(:read_type).with(Types::STRING).and_return("foobar")
125 prot.should_receive(:skip).with(Types::STRUCT)
126 prot.should_receive(:skip).with(Types::MAP)
127 prot.should_receive(:skip).with(Types::I32)
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 be_nil
132 struct.words.should == "foobar"
133 struct.hello.should == Hello.new(:greeting => 'hello, world!')
134 struct.ints.should == [1, 2, 2, 3]
135 struct.shorts.should == Set.new([5, 17, 239])
136 end
Kevin Clark090b69e2008-06-18 01:12:58 +0000137
Kevin Clark140b5552008-06-18 01:17:57 +0000138 it "should write itself to the wire" do
139 prot = mock("Protocol")
140 prot.should_receive(:write_struct_begin).with("SpecNamespace::Foo")
141 prot.should_receive(:write_struct_end)
142 prot.should_receive(:write_field_begin).with('ints', Types::LIST, 4)
143 prot.should_receive(:write_field_begin).with('complex', Types::MAP, 5)
144 prot.should_receive(:write_field_begin).with('shorts', Types::SET, 6)
145 prot.should_receive(:write_field_stop)
146 prot.should_receive(:write_field_end).exactly(3).times
147 prot.should_receive(:write_field).with('simple', Types::I32, 1, 53)
148 prot.should_receive(:write_field).with('hello', Types::STRUCT, 3, Hello.new(:greeting => 'hello, world!'))
149 prot.should_receive(:write_map_begin).with(Types::I32, Types::MAP, 1)
150 prot.should_receive(:write_map_begin).with(Types::STRING, Types::DOUBLE, 1)
151 prot.should_receive(:write_type).with(Types::I32, 5) # complex/1/key
152 prot.should_receive(:write_type).with(Types::STRING, "foo") # complex/1/value/1/key
153 prot.should_receive(:write_type).with(Types::DOUBLE, 1.23) # complex/1/value/1/value
154 prot.should_receive(:write_map_end).twice
155 prot.should_receive(:write_list_begin).with(Types::I32, 4)
156 prot.should_receive(:write_type).with(Types::I32, 1)
157 prot.should_receive(:write_type).with(Types::I32, 2).twice
158 prot.should_receive(:write_type).with(Types::I32, 3)
159 prot.should_receive(:write_list_end)
160 prot.should_receive(:write_set_begin).with(Types::I16, 3)
161 prot.should_receive(:write_type).with(Types::I16, 5)
162 prot.should_receive(:write_type).with(Types::I16, 17)
163 prot.should_receive(:write_type).with(Types::I16, 239)
164 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
210 prot = mock("Protocol")
211 prot.should_receive(:write_struct_begin).with("ThriftStructSpec::Xception")
212 prot.should_receive(:write_struct_end)
213 prot.should_receive(:write_field).with('message', Types::STRING, 1, "something happened")
214 prot.should_receive(:write_field).with('code', Types::I32, 2, 1)
215 prot.should_receive(:write_field_stop)
216
217 e.write(prot)
218 end
219 end
220
221 it "should support the regular initializer for exception structs" do
222 begin
223 raise Xception, :message => "something happened", :code => 5
224 rescue Thrift::Exception => e
225 e.message.should == "something happened"
226 e.code.should == 5
227 prot = mock("Protocol")
228 prot.should_receive(:write_struct_begin).with("ThriftStructSpec::Xception")
229 prot.should_receive(:write_struct_end)
230 prot.should_receive(:write_field).with('message', Types::STRING, 1, "something happened")
231 prot.should_receive(:write_field).with('code', Types::I32, 2, 5)
232 prot.should_receive(:write_field_stop)
233
234 e.write(prot)
235 end
236 end
Kevin Clark090b69e2008-06-18 01:12:58 +0000237 end
Kevin Clark03d7a472008-06-18 01:09:41 +0000238end