blob: 7c52d16071104dd19d45fbed74271538c06dfa82 [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 = {}
Kevin Clark5ad6d4a2008-08-26 20:02:07 +000020 Foo.new.each_field { |fid,type,name,default,optional| fields[fid] = [type,name,default,optional] }
Kevin Clark140b5552008-06-18 01:17:57 +000021 fields.should == {
Kevin Clark5ad6d4a2008-08-26 20:02:07 +000022 1 => [Types::I32, 'simple', 53, nil],
23 2 => [Types::STRING, 'words', "words", nil],
24 3 => [Types::STRUCT, 'hello', Hello.new(:greeting => 'hello, world!'), nil],
25 4 => [Types::LIST, 'ints', [1, 2, 2, 3], nil],
26 5 => [Types::MAP, 'complex', nil, nil],
27 6 => [Types::SET, 'shorts', Set.new([5, 17, 239]), nil],
28 7 => [Types::STRING, 'opt_string', nil, true]
Kevin Clark140b5552008-06-18 01:17:57 +000029 }
Kevin Clark9479b1a2008-06-18 01:13:37 +000030 end
Kevin Clark9479b1a2008-06-18 01:13:37 +000031
Kevin Clark140b5552008-06-18 01:17:57 +000032 it "should initialize all fields to defaults" do
33 struct = Foo.new
34 struct.simple.should == 53
35 struct.words.should == "words"
36 struct.hello.should == Hello.new(:greeting => 'hello, world!')
37 struct.ints.should == [1, 2, 2, 3]
38 struct.complex.should be_nil
39 struct.shorts.should == Set.new([5, 17, 239])
40 end
Kevin Clark1cfd6932008-06-18 01:13:58 +000041
Kevin Clark140b5552008-06-18 01:17:57 +000042 it "should not share default values between instances" do
43 begin
44 struct = Foo.new
45 struct.ints << 17
46 Foo.new.ints.should == [1,2,2,3]
47 ensure
48 # ensure no leakage to other tests
49 Foo::FIELDS[4][:default] = [1,2,2,3]
50 end
51 end
Kevin Clark03d7a472008-06-18 01:09:41 +000052
Kevin Clark140b5552008-06-18 01:17:57 +000053 it "should properly initialize boolean values" do
54 struct = BoolStruct.new(:yesno => false)
55 struct.yesno.should be_false
56 end
Kevin Clark03d7a472008-06-18 01:09:41 +000057
Kevin Clark140b5552008-06-18 01:17:57 +000058 it "should have proper == semantics" do
59 Foo.new.should_not == Hello.new
60 Foo.new.should == Foo.new
61 Foo.new(:simple => 52).should_not == Foo.new
62 end
Kevin Clark03d7a472008-06-18 01:09:41 +000063
Kevin Clark140b5552008-06-18 01:17:57 +000064 it "should read itself off the wire" do
65 struct = Foo.new
66 prot = mock("Protocol")
67 prot.should_receive(:read_struct_begin).twice
68 prot.should_receive(:read_struct_end).twice
69 prot.should_receive(:read_field_begin).and_return(
70 ['complex', Types::MAP, 5], # Foo
71 ['words', Types::STRING, 2], # Foo
72 ['hello', Types::STRUCT, 3], # Foo
73 ['greeting', Types::STRING, 1], # Hello
74 [nil, Types::STOP, 0], # Hello
75 ['simple', Types::I32, 1], # Foo
76 ['ints', Types::LIST, 4], # Foo
77 ['shorts', Types::SET, 6], # Foo
78 [nil, Types::STOP, 0] # Hello
79 )
80 prot.should_receive(:read_field_end).exactly(7).times
81 prot.should_receive(:read_map_begin).and_return(
82 [Types::I32, Types::MAP, 2], # complex
83 [Types::STRING, Types::DOUBLE, 2], # complex/1/value
84 [Types::STRING, Types::DOUBLE, 1] # complex/2/value
85 )
86 prot.should_receive(:read_map_end).exactly(3).times
87 prot.should_receive(:read_list_begin).and_return([Types::I32, 4])
88 prot.should_receive(:read_list_end)
89 prot.should_receive(:read_set_begin).and_return([Types::I16, 2])
90 prot.should_receive(:read_set_end)
91 prot.should_receive(:read_type).with(Types::I32).and_return(
92 1, 14, # complex keys
93 42, # simple
94 4, 23, 4, 29 # ints
95 )
96 prot.should_receive(:read_type).with(Types::STRING).and_return("pi", "e", "feigenbaum", "apple banana", "what's up?")
97 prot.should_receive(:read_type).with(Types::DOUBLE).and_return(Math::PI, Math::E, 4.669201609)
98 prot.should_receive(:read_type).with(Types::I16).and_return(2, 3)
99 prot.should_not_receive(:skip)
100 struct.read(prot)
Kevin Clark03d7a472008-06-18 01:09:41 +0000101
Kevin Clark140b5552008-06-18 01:17:57 +0000102 struct.simple.should == 42
103 struct.complex.should == {1 => {"pi" => Math::PI, "e" => Math::E}, 14 => {"feigenbaum" => 4.669201609}}
104 struct.hello.should == Hello.new(:greeting => "what's up?")
105 struct.words.should == "apple banana"
106 struct.ints.should == [4, 23, 4, 29]
107 struct.shorts.should == Set.new([3, 2])
108 end
Kevin Clark03d7a472008-06-18 01:09:41 +0000109
Kevin Clark140b5552008-06-18 01:17:57 +0000110 it "should skip unexpected fields in structs and use default values" do
111 struct = Foo.new
112 prot = mock("Protocol")
113 prot.should_receive(:read_struct_begin)
114 prot.should_receive(:read_struct_end)
115 prot.should_receive(:read_field_begin).and_return(
116 ['simple', Types::I32, 1],
117 ['complex', Types::STRUCT, 5],
118 ['thinz', Types::MAP, 7],
119 ['foobar', Types::I32, 3],
120 ['words', Types::STRING, 2],
121 [nil, Types::STOP, 0]
122 )
123 prot.should_receive(:read_field_end).exactly(5).times
124 prot.should_receive(:read_type).with(Types::I32).and_return(42)
125 prot.should_receive(:read_type).with(Types::STRING).and_return("foobar")
126 prot.should_receive(:skip).with(Types::STRUCT)
127 prot.should_receive(:skip).with(Types::MAP)
128 prot.should_receive(:skip).with(Types::I32)
129 struct.read(prot)
Kevin Clark03d7a472008-06-18 01:09:41 +0000130
Kevin Clark140b5552008-06-18 01:17:57 +0000131 struct.simple.should == 42
132 struct.complex.should be_nil
133 struct.words.should == "foobar"
134 struct.hello.should == Hello.new(:greeting => 'hello, world!')
135 struct.ints.should == [1, 2, 2, 3]
136 struct.shorts.should == Set.new([5, 17, 239])
137 end
Kevin Clark090b69e2008-06-18 01:12:58 +0000138
Kevin Clark140b5552008-06-18 01:17:57 +0000139 it "should write itself to the wire" do
140 prot = mock("Protocol")
141 prot.should_receive(:write_struct_begin).with("SpecNamespace::Foo")
142 prot.should_receive(:write_struct_end)
143 prot.should_receive(:write_field_begin).with('ints', Types::LIST, 4)
144 prot.should_receive(:write_field_begin).with('complex', Types::MAP, 5)
145 prot.should_receive(:write_field_begin).with('shorts', Types::SET, 6)
146 prot.should_receive(:write_field_stop)
147 prot.should_receive(:write_field_end).exactly(3).times
148 prot.should_receive(:write_field).with('simple', Types::I32, 1, 53)
149 prot.should_receive(:write_field).with('hello', Types::STRUCT, 3, Hello.new(:greeting => 'hello, world!'))
150 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)
152 prot.should_receive(:write_type).with(Types::I32, 5) # complex/1/key
153 prot.should_receive(:write_type).with(Types::STRING, "foo") # complex/1/value/1/key
154 prot.should_receive(:write_type).with(Types::DOUBLE, 1.23) # complex/1/value/1/value
155 prot.should_receive(:write_map_end).twice
156 prot.should_receive(:write_list_begin).with(Types::I32, 4)
157 prot.should_receive(:write_type).with(Types::I32, 1)
158 prot.should_receive(:write_type).with(Types::I32, 2).twice
159 prot.should_receive(:write_type).with(Types::I32, 3)
160 prot.should_receive(:write_list_end)
161 prot.should_receive(:write_set_begin).with(Types::I16, 3)
162 prot.should_receive(:write_type).with(Types::I16, 5)
163 prot.should_receive(:write_type).with(Types::I16, 17)
164 prot.should_receive(:write_type).with(Types::I16, 239)
165 prot.should_receive(:write_set_end)
166
167 struct = Foo.new
168 struct.words = nil
169 struct.complex = {5 => {"foo" => 1.23}}
170 struct.write(prot)
171 end
172
173 it "should raise an exception if presented with an unknown container" do
174 # yeah this is silly, but I'm going for code coverage here
175 struct = Foo.new
176 lambda { struct.send :write_container, nil, nil, {:type => "foo"} }.should raise_error(StandardError, "Not a container type: foo")
177 end
Kevin Clark23193752008-06-18 01:18:07 +0000178
179 it "should support optional type-checking in Thrift::Struct.new" do
180 Thrift.type_checking = true
181 begin
Kevin Clarkb5863392008-07-18 22:03:48 +0000182 lambda { Hello.new(:greeting => 3) }.should raise_error(TypeError, "Expected Types::STRING, received Fixnum for field greeting")
Kevin Clark23193752008-06-18 01:18:07 +0000183 ensure
184 Thrift.type_checking = false
185 end
186 lambda { Hello.new(:greeting => 3) }.should_not raise_error(TypeError)
187 end
188
189 it "should support optional type-checking in field accessors" do
190 Thrift.type_checking = true
191 begin
192 hello = Hello.new
Kevin Clarkb5863392008-07-18 22:03:48 +0000193 lambda { hello.greeting = 3 }.should raise_error(TypeError, "Expected Types::STRING, received Fixnum for field greeting")
Kevin Clark23193752008-06-18 01:18:07 +0000194 ensure
195 Thrift.type_checking = false
196 end
197 lambda { hello.greeting = 3 }.should_not raise_error(TypeError)
198 end
199
200 it "should raise an exception when unknown types are given to Thrift::Struct.new" do
Kevin Clark38a2ce62008-08-25 21:34:19 +0000201 lambda { Hello.new(:fish => 'salmon') }.should raise_error(Exception, "Unknown key given to SpecNamespace::Hello.new: fish")
Kevin Clark23193752008-06-18 01:18:07 +0000202 end
Kevin Clark3af92872008-07-28 22:20:36 +0000203
204 it "should support `raise Xception, 'message'` for Exception structs" do
205 begin
206 raise Xception, "something happened"
207 rescue Thrift::Exception => e
208 e.message.should == "something happened"
209 e.code.should == 1
210 # ensure it gets serialized properly, this is the really important part
211 prot = mock("Protocol")
212 prot.should_receive(:write_struct_begin).with("ThriftStructSpec::Xception")
213 prot.should_receive(:write_struct_end)
214 prot.should_receive(:write_field).with('message', Types::STRING, 1, "something happened")
215 prot.should_receive(:write_field).with('code', Types::I32, 2, 1)
216 prot.should_receive(:write_field_stop)
217
218 e.write(prot)
219 end
220 end
221
222 it "should support the regular initializer for exception structs" do
223 begin
224 raise Xception, :message => "something happened", :code => 5
225 rescue Thrift::Exception => e
226 e.message.should == "something happened"
227 e.code.should == 5
228 prot = mock("Protocol")
229 prot.should_receive(:write_struct_begin).with("ThriftStructSpec::Xception")
230 prot.should_receive(:write_struct_end)
231 prot.should_receive(:write_field).with('message', Types::STRING, 1, "something happened")
232 prot.should_receive(:write_field).with('code', Types::I32, 2, 5)
233 prot.should_receive(:write_field_stop)
234
235 e.write(prot)
236 end
237 end
Kevin Clark090b69e2008-06-18 01:12:58 +0000238 end
Kevin Clark03d7a472008-06-18 01:09:41 +0000239end