blob: 4dc8714a7d51cc287974a8cbcfbda6c7fde0d39c [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 Clark140b5552008-06-18 01:17:57 +00008 describe Struct do
9 it "should iterate over all fields properly" do
10 fields = {}
Kevin Clark5ad6d4a2008-08-26 20:02:07 +000011 Foo.new.each_field { |fid,type,name,default,optional| fields[fid] = [type,name,default,optional] }
Kevin Clark140b5552008-06-18 01:17:57 +000012 fields.should == {
Kevin Clark5ad6d4a2008-08-26 20:02:07 +000013 1 => [Types::I32, 'simple', 53, nil],
14 2 => [Types::STRING, 'words', "words", nil],
15 3 => [Types::STRUCT, 'hello', Hello.new(:greeting => 'hello, world!'), nil],
16 4 => [Types::LIST, 'ints', [1, 2, 2, 3], nil],
17 5 => [Types::MAP, 'complex', nil, nil],
18 6 => [Types::SET, 'shorts', Set.new([5, 17, 239]), nil],
19 7 => [Types::STRING, 'opt_string', nil, true]
Kevin Clark140b5552008-06-18 01:17:57 +000020 }
Kevin Clark9479b1a2008-06-18 01:13:37 +000021 end
Kevin Clark9479b1a2008-06-18 01:13:37 +000022
Kevin Clark140b5552008-06-18 01:17:57 +000023 it "should initialize all fields to defaults" do
24 struct = Foo.new
25 struct.simple.should == 53
26 struct.words.should == "words"
27 struct.hello.should == Hello.new(:greeting => 'hello, world!')
28 struct.ints.should == [1, 2, 2, 3]
29 struct.complex.should be_nil
30 struct.shorts.should == Set.new([5, 17, 239])
31 end
Kevin Clark1cfd6932008-06-18 01:13:58 +000032
Kevin Clark140b5552008-06-18 01:17:57 +000033 it "should not share default values between instances" do
34 begin
35 struct = Foo.new
36 struct.ints << 17
37 Foo.new.ints.should == [1,2,2,3]
38 ensure
39 # ensure no leakage to other tests
40 Foo::FIELDS[4][:default] = [1,2,2,3]
41 end
42 end
Kevin Clark03d7a472008-06-18 01:09:41 +000043
Kevin Clark140b5552008-06-18 01:17:57 +000044 it "should properly initialize boolean values" do
45 struct = BoolStruct.new(:yesno => false)
46 struct.yesno.should be_false
47 end
Kevin Clark03d7a472008-06-18 01:09:41 +000048
Kevin Clark140b5552008-06-18 01:17:57 +000049 it "should have proper == semantics" do
50 Foo.new.should_not == Hello.new
51 Foo.new.should == Foo.new
52 Foo.new(:simple => 52).should_not == Foo.new
53 end
Kevin Clark03d7a472008-06-18 01:09:41 +000054
Kevin Clark140b5552008-06-18 01:17:57 +000055 it "should read itself off the wire" do
56 struct = Foo.new
57 prot = mock("Protocol")
58 prot.should_receive(:read_struct_begin).twice
59 prot.should_receive(:read_struct_end).twice
60 prot.should_receive(:read_field_begin).and_return(
61 ['complex', Types::MAP, 5], # Foo
62 ['words', Types::STRING, 2], # Foo
63 ['hello', Types::STRUCT, 3], # Foo
64 ['greeting', Types::STRING, 1], # Hello
65 [nil, Types::STOP, 0], # Hello
66 ['simple', Types::I32, 1], # Foo
67 ['ints', Types::LIST, 4], # Foo
68 ['shorts', Types::SET, 6], # Foo
69 [nil, Types::STOP, 0] # Hello
70 )
71 prot.should_receive(:read_field_end).exactly(7).times
72 prot.should_receive(:read_map_begin).and_return(
73 [Types::I32, Types::MAP, 2], # complex
74 [Types::STRING, Types::DOUBLE, 2], # complex/1/value
75 [Types::STRING, Types::DOUBLE, 1] # complex/2/value
76 )
77 prot.should_receive(:read_map_end).exactly(3).times
78 prot.should_receive(:read_list_begin).and_return([Types::I32, 4])
79 prot.should_receive(:read_list_end)
80 prot.should_receive(:read_set_begin).and_return([Types::I16, 2])
81 prot.should_receive(:read_set_end)
82 prot.should_receive(:read_type).with(Types::I32).and_return(
83 1, 14, # complex keys
84 42, # simple
85 4, 23, 4, 29 # ints
86 )
87 prot.should_receive(:read_type).with(Types::STRING).and_return("pi", "e", "feigenbaum", "apple banana", "what's up?")
88 prot.should_receive(:read_type).with(Types::DOUBLE).and_return(Math::PI, Math::E, 4.669201609)
89 prot.should_receive(:read_type).with(Types::I16).and_return(2, 3)
90 prot.should_not_receive(:skip)
91 struct.read(prot)
Kevin Clark03d7a472008-06-18 01:09:41 +000092
Kevin Clark140b5552008-06-18 01:17:57 +000093 struct.simple.should == 42
94 struct.complex.should == {1 => {"pi" => Math::PI, "e" => Math::E}, 14 => {"feigenbaum" => 4.669201609}}
95 struct.hello.should == Hello.new(:greeting => "what's up?")
96 struct.words.should == "apple banana"
97 struct.ints.should == [4, 23, 4, 29]
98 struct.shorts.should == Set.new([3, 2])
99 end
Kevin Clark03d7a472008-06-18 01:09:41 +0000100
Kevin Clark140b5552008-06-18 01:17:57 +0000101 it "should skip unexpected fields in structs and use default values" do
102 struct = Foo.new
103 prot = mock("Protocol")
104 prot.should_receive(:read_struct_begin)
105 prot.should_receive(:read_struct_end)
106 prot.should_receive(:read_field_begin).and_return(
107 ['simple', Types::I32, 1],
108 ['complex', Types::STRUCT, 5],
109 ['thinz', Types::MAP, 7],
110 ['foobar', Types::I32, 3],
111 ['words', Types::STRING, 2],
112 [nil, Types::STOP, 0]
113 )
114 prot.should_receive(:read_field_end).exactly(5).times
115 prot.should_receive(:read_type).with(Types::I32).and_return(42)
116 prot.should_receive(:read_type).with(Types::STRING).and_return("foobar")
117 prot.should_receive(:skip).with(Types::STRUCT)
118 prot.should_receive(:skip).with(Types::MAP)
119 prot.should_receive(:skip).with(Types::I32)
120 struct.read(prot)
Kevin Clark03d7a472008-06-18 01:09:41 +0000121
Kevin Clark140b5552008-06-18 01:17:57 +0000122 struct.simple.should == 42
123 struct.complex.should be_nil
124 struct.words.should == "foobar"
125 struct.hello.should == Hello.new(:greeting => 'hello, world!')
126 struct.ints.should == [1, 2, 2, 3]
127 struct.shorts.should == Set.new([5, 17, 239])
128 end
Kevin Clark090b69e2008-06-18 01:12:58 +0000129
Kevin Clark140b5552008-06-18 01:17:57 +0000130 it "should write itself to the wire" do
131 prot = mock("Protocol")
132 prot.should_receive(:write_struct_begin).with("SpecNamespace::Foo")
133 prot.should_receive(:write_struct_end)
134 prot.should_receive(:write_field_begin).with('ints', Types::LIST, 4)
135 prot.should_receive(:write_field_begin).with('complex', Types::MAP, 5)
136 prot.should_receive(:write_field_begin).with('shorts', Types::SET, 6)
137 prot.should_receive(:write_field_stop)
138 prot.should_receive(:write_field_end).exactly(3).times
139 prot.should_receive(:write_field).with('simple', Types::I32, 1, 53)
140 prot.should_receive(:write_field).with('hello', Types::STRUCT, 3, Hello.new(:greeting => 'hello, world!'))
141 prot.should_receive(:write_map_begin).with(Types::I32, Types::MAP, 1)
142 prot.should_receive(:write_map_begin).with(Types::STRING, Types::DOUBLE, 1)
143 prot.should_receive(:write_type).with(Types::I32, 5) # complex/1/key
144 prot.should_receive(:write_type).with(Types::STRING, "foo") # complex/1/value/1/key
145 prot.should_receive(:write_type).with(Types::DOUBLE, 1.23) # complex/1/value/1/value
146 prot.should_receive(:write_map_end).twice
147 prot.should_receive(:write_list_begin).with(Types::I32, 4)
148 prot.should_receive(:write_type).with(Types::I32, 1)
149 prot.should_receive(:write_type).with(Types::I32, 2).twice
150 prot.should_receive(:write_type).with(Types::I32, 3)
151 prot.should_receive(:write_list_end)
152 prot.should_receive(:write_set_begin).with(Types::I16, 3)
153 prot.should_receive(:write_type).with(Types::I16, 5)
154 prot.should_receive(:write_type).with(Types::I16, 17)
155 prot.should_receive(:write_type).with(Types::I16, 239)
156 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
202 prot = mock("Protocol")
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)
205 prot.should_receive(:write_field).with('message', Types::STRING, 1, "something happened")
206 prot.should_receive(:write_field).with('code', Types::I32, 2, 1)
207 prot.should_receive(:write_field_stop)
208
209 e.write(prot)
210 end
211 end
212
213 it "should support the regular initializer for exception structs" do
214 begin
215 raise Xception, :message => "something happened", :code => 5
216 rescue Thrift::Exception => e
217 e.message.should == "something happened"
218 e.code.should == 5
219 prot = mock("Protocol")
Kevin Clark031baf72008-11-14 17:11:39 +0000220 prot.should_receive(:write_struct_begin).with("SpecNamespace::Xception")
Kevin Clark3af92872008-07-28 22:20:36 +0000221 prot.should_receive(:write_struct_end)
222 prot.should_receive(:write_field).with('message', Types::STRING, 1, "something happened")
223 prot.should_receive(:write_field).with('code', Types::I32, 2, 5)
224 prot.should_receive(:write_field_stop)
225
226 e.write(prot)
227 end
228 end
Kevin Clark090b69e2008-06-18 01:12:58 +0000229 end
Kevin Clark03d7a472008-06-18 01:09:41 +0000230end