blob: 6ff9bdbf725f4acb9f2c049e514c9aa0f5001aec [file] [log] [blame]
Kevin Clark03d7a472008-06-18 01:09:41 +00001require File.dirname(__FILE__) + '/spec_helper'
Kevin Clark90a2cbe2008-06-18 01:15:53 +00002require "ThriftSpec_types"
Kevin Clark03d7a472008-06-18 01:09:41 +00003
4class ThriftStructSpec < Spec::ExampleGroup
5 include Thrift
6 include SpecNamespace
7
8 class OldStruct
9 include Thrift::Struct
10 attr_accessor :set
11 FIELDS = {
12 1 => {:type => Thrift::Types::SET, :name => 'val', :default => {:foo => true, :bar => true}}
13 }
14 end
15
Kevin Clark140b5552008-06-18 01:17:57 +000016 describe Struct do
17 it "should iterate over all fields properly" do
18 fields = {}
19 Foo.new.each_field { |fid,type,name,default| fields[fid] = [type,name,default] }
20 fields.should == {
21 1 => [Types::I32, 'simple', 53],
22 2 => [Types::STRING, 'words', "words"],
23 3 => [Types::STRUCT, 'hello', Hello.new(:greeting => 'hello, world!')],
24 4 => [Types::LIST, 'ints', [1, 2, 2, 3]],
25 5 => [Types::MAP, 'complex', nil],
26 6 => [Types::SET, 'shorts', Set.new([5, 17, 239])]
27 }
Kevin Clark9479b1a2008-06-18 01:13:37 +000028 end
Kevin Clark9479b1a2008-06-18 01:13:37 +000029
Kevin Clark140b5552008-06-18 01:17:57 +000030 it "should initialize all fields to defaults" do
31 struct = Foo.new
32 struct.simple.should == 53
33 struct.words.should == "words"
34 struct.hello.should == Hello.new(:greeting => 'hello, world!')
35 struct.ints.should == [1, 2, 2, 3]
36 struct.complex.should be_nil
37 struct.shorts.should == Set.new([5, 17, 239])
38 end
Kevin Clark1cfd6932008-06-18 01:13:58 +000039
Kevin Clark140b5552008-06-18 01:17:57 +000040 it "should not share default values between instances" do
41 begin
42 struct = Foo.new
43 struct.ints << 17
44 Foo.new.ints.should == [1,2,2,3]
45 ensure
46 # ensure no leakage to other tests
47 Foo::FIELDS[4][:default] = [1,2,2,3]
48 end
49 end
Kevin Clark03d7a472008-06-18 01:09:41 +000050
Kevin Clark140b5552008-06-18 01:17:57 +000051 it "should properly initialize boolean values" do
52 struct = BoolStruct.new(:yesno => false)
53 struct.yesno.should be_false
54 end
Kevin Clark03d7a472008-06-18 01:09:41 +000055
Kevin Clark140b5552008-06-18 01:17:57 +000056 it "should have proper == semantics" do
57 Foo.new.should_not == Hello.new
58 Foo.new.should == Foo.new
59 Foo.new(:simple => 52).should_not == Foo.new
60 end
Kevin Clark03d7a472008-06-18 01:09:41 +000061
Kevin Clark140b5552008-06-18 01:17:57 +000062 it "should read itself off the wire" do
63 struct = Foo.new
64 prot = mock("Protocol")
65 prot.should_receive(:read_struct_begin).twice
66 prot.should_receive(:read_struct_end).twice
67 prot.should_receive(:read_field_begin).and_return(
68 ['complex', Types::MAP, 5], # Foo
69 ['words', Types::STRING, 2], # Foo
70 ['hello', Types::STRUCT, 3], # Foo
71 ['greeting', Types::STRING, 1], # Hello
72 [nil, Types::STOP, 0], # Hello
73 ['simple', Types::I32, 1], # Foo
74 ['ints', Types::LIST, 4], # Foo
75 ['shorts', Types::SET, 6], # Foo
76 [nil, Types::STOP, 0] # Hello
77 )
78 prot.should_receive(:read_field_end).exactly(7).times
79 prot.should_receive(:read_map_begin).and_return(
80 [Types::I32, Types::MAP, 2], # complex
81 [Types::STRING, Types::DOUBLE, 2], # complex/1/value
82 [Types::STRING, Types::DOUBLE, 1] # complex/2/value
83 )
84 prot.should_receive(:read_map_end).exactly(3).times
85 prot.should_receive(:read_list_begin).and_return([Types::I32, 4])
86 prot.should_receive(:read_list_end)
87 prot.should_receive(:read_set_begin).and_return([Types::I16, 2])
88 prot.should_receive(:read_set_end)
89 prot.should_receive(:read_type).with(Types::I32).and_return(
90 1, 14, # complex keys
91 42, # simple
92 4, 23, 4, 29 # ints
93 )
94 prot.should_receive(:read_type).with(Types::STRING).and_return("pi", "e", "feigenbaum", "apple banana", "what's up?")
95 prot.should_receive(:read_type).with(Types::DOUBLE).and_return(Math::PI, Math::E, 4.669201609)
96 prot.should_receive(:read_type).with(Types::I16).and_return(2, 3)
97 prot.should_not_receive(:skip)
98 struct.read(prot)
Kevin Clark03d7a472008-06-18 01:09:41 +000099
Kevin Clark140b5552008-06-18 01:17:57 +0000100 struct.simple.should == 42
101 struct.complex.should == {1 => {"pi" => Math::PI, "e" => Math::E}, 14 => {"feigenbaum" => 4.669201609}}
102 struct.hello.should == Hello.new(:greeting => "what's up?")
103 struct.words.should == "apple banana"
104 struct.ints.should == [4, 23, 4, 29]
105 struct.shorts.should == Set.new([3, 2])
106 end
Kevin Clark03d7a472008-06-18 01:09:41 +0000107
Kevin Clark140b5552008-06-18 01:17:57 +0000108 it "should skip unexpected fields in structs and use default values" do
109 struct = Foo.new
110 prot = mock("Protocol")
111 prot.should_receive(:read_struct_begin)
112 prot.should_receive(:read_struct_end)
113 prot.should_receive(:read_field_begin).and_return(
114 ['simple', Types::I32, 1],
115 ['complex', Types::STRUCT, 5],
116 ['thinz', Types::MAP, 7],
117 ['foobar', Types::I32, 3],
118 ['words', Types::STRING, 2],
119 [nil, Types::STOP, 0]
120 )
121 prot.should_receive(:read_field_end).exactly(5).times
122 prot.should_receive(:read_type).with(Types::I32).and_return(42)
123 prot.should_receive(:read_type).with(Types::STRING).and_return("foobar")
124 prot.should_receive(:skip).with(Types::STRUCT)
125 prot.should_receive(:skip).with(Types::MAP)
126 prot.should_receive(:skip).with(Types::I32)
127 struct.read(prot)
Kevin Clark03d7a472008-06-18 01:09:41 +0000128
Kevin Clark140b5552008-06-18 01:17:57 +0000129 struct.simple.should == 42
130 struct.complex.should be_nil
131 struct.words.should == "foobar"
132 struct.hello.should == Hello.new(:greeting => 'hello, world!')
133 struct.ints.should == [1, 2, 2, 3]
134 struct.shorts.should == Set.new([5, 17, 239])
135 end
Kevin Clark090b69e2008-06-18 01:12:58 +0000136
Kevin Clark140b5552008-06-18 01:17:57 +0000137 it "should write itself to the wire" do
138 prot = mock("Protocol")
139 prot.should_receive(:write_struct_begin).with("SpecNamespace::Foo")
140 prot.should_receive(:write_struct_end)
141 prot.should_receive(:write_field_begin).with('ints', Types::LIST, 4)
142 prot.should_receive(:write_field_begin).with('complex', Types::MAP, 5)
143 prot.should_receive(:write_field_begin).with('shorts', Types::SET, 6)
144 prot.should_receive(:write_field_stop)
145 prot.should_receive(:write_field_end).exactly(3).times
146 prot.should_receive(:write_field).with('simple', Types::I32, 1, 53)
147 prot.should_receive(:write_field).with('hello', Types::STRUCT, 3, Hello.new(:greeting => 'hello, world!'))
148 prot.should_receive(:write_map_begin).with(Types::I32, Types::MAP, 1)
149 prot.should_receive(:write_map_begin).with(Types::STRING, Types::DOUBLE, 1)
150 prot.should_receive(:write_type).with(Types::I32, 5) # complex/1/key
151 prot.should_receive(:write_type).with(Types::STRING, "foo") # complex/1/value/1/key
152 prot.should_receive(:write_type).with(Types::DOUBLE, 1.23) # complex/1/value/1/value
153 prot.should_receive(:write_map_end).twice
154 prot.should_receive(:write_list_begin).with(Types::I32, 4)
155 prot.should_receive(:write_type).with(Types::I32, 1)
156 prot.should_receive(:write_type).with(Types::I32, 2).twice
157 prot.should_receive(:write_type).with(Types::I32, 3)
158 prot.should_receive(:write_list_end)
159 prot.should_receive(:write_set_begin).with(Types::I16, 3)
160 prot.should_receive(:write_type).with(Types::I16, 5)
161 prot.should_receive(:write_type).with(Types::I16, 17)
162 prot.should_receive(:write_type).with(Types::I16, 239)
163 prot.should_receive(:write_set_end)
164
165 struct = Foo.new
166 struct.words = nil
167 struct.complex = {5 => {"foo" => 1.23}}
168 struct.write(prot)
169 end
170
171 it "should raise an exception if presented with an unknown container" do
172 # yeah this is silly, but I'm going for code coverage here
173 struct = Foo.new
174 lambda { struct.send :write_container, nil, nil, {:type => "foo"} }.should raise_error(StandardError, "Not a container type: foo")
175 end
Kevin Clark23193752008-06-18 01:18:07 +0000176
177 it "should support optional type-checking in Thrift::Struct.new" do
178 Thrift.type_checking = true
179 begin
180 lambda { Hello.new(:greeting => 3) }.should raise_error(TypeError, "Expected Types::STRING, received Fixnum")
181 ensure
182 Thrift.type_checking = false
183 end
184 lambda { Hello.new(:greeting => 3) }.should_not raise_error(TypeError)
185 end
186
187 it "should support optional type-checking in field accessors" do
188 Thrift.type_checking = true
189 begin
190 hello = Hello.new
191 lambda { hello.greeting = 3 }.should raise_error(TypeError, "Expected Types::STRING, received Fixnum")
192 ensure
193 Thrift.type_checking = false
194 end
195 lambda { hello.greeting = 3 }.should_not raise_error(TypeError)
196 end
197
198 it "should raise an exception when unknown types are given to Thrift::Struct.new" do
199 lambda { Hello.new(:fish => 'salmon') }.should raise_error(Exception, "Unknown arguments given to SpecNamespace::Hello.new")
200 end
Kevin Clark090b69e2008-06-18 01:12:58 +0000201 end
Kevin Clark03d7a472008-06-18 01:09:41 +0000202end