blob: 5e21593fe2a0a4f66d7e34b25abbc1600e135199 [file] [log] [blame]
Kevin Clark03d7a472008-06-18 01:09:41 +00001require File.dirname(__FILE__) + '/spec_helper'
2require File.dirname(__FILE__) + "/gen-rb/ThriftSpec_types"
3
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
16 it "should iterate over all fields properly" do
17 fields = {}
18 Foo.new.each_field { |fid,type,name,default| fields[fid] = [type,name,default] }
19 fields.should == {
20 1 => [Types::I32, 'simple', 53],
21 2 => [Types::STRING, 'words', "words"],
22 3 => [Types::STRUCT, 'hello', Hello.new(:greeting => 'hello, world!')],
23 4 => [Types::LIST, 'ints', [1, 2, 2, 3]],
24 5 => [Types::MAP, 'complex', nil],
25 6 => [Types::SET, 'shorts', Set.new([5, 17, 239])]
26 }
27 end
28
29 it "should initialize all fields to defaults" do
30 struct = Foo.new
31 struct.simple.should == 53
32 struct.words.should == "words"
33 struct.hello.should == Hello.new(:greeting => 'hello, world!')
34 struct.ints.should == [1, 2, 2, 3]
35 struct.complex.should be_nil
36 struct.shorts.should == Set.new([5, 17, 239])
37 end
38
Kevin Clark9479b1a2008-06-18 01:13:37 +000039 it "should not share default values between instances" do
Kevin Clarkec9106f2008-06-18 01:14:26 +000040 pending 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
Kevin Clark9479b1a2008-06-18 01:13:37 +000049 end
50 end
51
Kevin Clark1cfd6932008-06-18 01:13:58 +000052 it "should properly initialize boolean values" do
Kevin Clarkec9106f2008-06-18 01:14:26 +000053 pending do
54 struct = BoolStruct.new(:yesno => false)
55 struct.yesno.should be_false
56 end
Kevin Clark1cfd6932008-06-18 01:13:58 +000057 end
58
Kevin Clark03d7a472008-06-18 01:09:41 +000059 it "should have proper == semantics" do
60 Foo.new.should_not == Hello.new
61 Foo.new.should == Foo.new
62 Foo.new(:simple => 52).should_not == Foo.new
63 end
64
65 it "should read itself off the wire" do
66 struct = Foo.new
67 prot = mock("Protocol")
68 prot.should_receive(:read_struct_begin).twice
69 prot.should_receive(:read_struct_end).twice
70 prot.should_receive(:read_field_begin).and_return(
71 ['complex', Types::MAP, 5], # Foo
72 ['words', Types::STRING, 2], # Foo
73 ['hello', Types::STRUCT, 3], # Foo
74 ['greeting', Types::STRING, 1], # Hello
75 [nil, Types::STOP, 0], # Hello
76 ['simple', Types::I32, 1], # Foo
77 ['ints', Types::LIST, 4], # Foo
78 ['shorts', Types::SET, 6], # Foo
79 [nil, Types::STOP, 0] # Hello
80 )
81 prot.should_receive(:read_field_end).exactly(7).times
82 prot.should_receive(:read_map_begin).and_return(
83 [Types::I32, Types::MAP, 2], # complex
84 [Types::STRING, Types::DOUBLE, 2], # complex/1/value
85 [Types::STRING, Types::DOUBLE, 1] # complex/2/value
86 )
87 prot.should_receive(:read_map_end).exactly(3).times
88 prot.should_receive(:read_list_begin).and_return([Types::I32, 4])
89 prot.should_receive(:read_list_end)
90 prot.should_receive(:read_set_begin).and_return([Types::I16, 2])
91 prot.should_receive(:read_set_end)
92 prot.should_receive(:read_type).with(Types::I32).and_return(
93 1, 14, # complex keys
94 42, # simple
95 4, 23, 4, 29 # ints
96 )
97 prot.should_receive(:read_type).with(Types::STRING).and_return("pi", "e", "feigenbaum", "apple banana", "what's up?")
98 prot.should_receive(:read_type).with(Types::DOUBLE).and_return(Math::PI, Math::E, 4.669201609)
99 prot.should_receive(:read_type).with(Types::I16).and_return(2, 3)
100 prot.should_not_receive(:skip)
101 struct.read(prot)
102
103 struct.simple.should == 42
104 struct.complex.should == {1 => {"pi" => Math::PI, "e" => Math::E}, 14 => {"feigenbaum" => 4.669201609}}
105 struct.hello.should == Hello.new(:greeting => "what's up?")
106 struct.words.should == "apple banana"
107 struct.ints.should == [4, 23, 4, 29]
108 struct.shorts.should == Set.new([3, 2])
109 end
110
111 it "should skip unexpected fields in structs and use default values" do
112 struct = Foo.new
113 prot = mock("Protocol")
114 prot.should_receive(:read_struct_begin)
115 prot.should_receive(:read_struct_end)
116 prot.should_receive(:read_field_begin).and_return(
117 ['simple', Types::I32, 1],
118 ['complex', Types::STRUCT, 5],
119 ['thinz', Types::MAP, 7],
120 ['foobar', Types::I32, 3],
121 ['words', Types::STRING, 2],
122 [nil, Types::STOP, 0]
123 )
124 prot.should_receive(:read_field_end).exactly(5).times
125 prot.should_receive(:read_type).with(Types::I32).and_return(42)
126 prot.should_receive(:read_type).with(Types::STRING).and_return("foobar")
127 prot.should_receive(:skip).with(Types::STRUCT)
128 prot.should_receive(:skip).with(Types::MAP)
129 prot.should_receive(:skip).with(Types::I32)
130 struct.read(prot)
131
132 struct.simple.should == 42
133 struct.complex.should be_nil
134 struct.words.should == "foobar"
135 struct.hello.should == Hello.new(:greeting => 'hello, world!')
136 struct.ints.should == [1, 2, 2, 3]
137 struct.shorts.should == Set.new([5, 17, 239])
138 end
139
140 it "should write itself to the wire" do
141 prot = mock("Protocol")
142 prot.should_receive(:write_struct_begin).with("SpecNamespace::Foo")
143 prot.should_receive(:write_struct_end)
144 prot.should_receive(:write_field_begin).with('ints', Types::LIST, 4)
145 prot.should_receive(:write_field_begin).with('complex', Types::MAP, 5)
146 prot.should_receive(:write_field_begin).with('shorts', Types::SET, 6)
147 prot.should_receive(:write_field_stop)
148 prot.should_receive(:write_field_end).exactly(3).times
149 prot.should_receive(:write_field).with('simple', Types::I32, 1, 53)
150 prot.should_receive(:write_field).with('hello', Types::STRUCT, 3, Hello.new(:greeting => 'hello, world!'))
151 prot.should_receive(:write_map_begin).with(Types::I32, Types::MAP, 1)
152 prot.should_receive(:write_map_begin).with(Types::STRING, Types::DOUBLE, 1)
153 prot.should_receive(:write_type).with(Types::I32, 5) # complex/1/key
154 prot.should_receive(:write_type).with(Types::STRING, "foo") # complex/1/value/1/key
155 prot.should_receive(:write_type).with(Types::DOUBLE, 1.23) # complex/1/value/1/value
156 prot.should_receive(:write_map_end).twice
157 prot.should_receive(:write_list_begin).with(Types::I32, 4)
158 prot.should_receive(:write_type).with(Types::I32, 1)
159 prot.should_receive(:write_type).with(Types::I32, 2).twice
160 prot.should_receive(:write_type).with(Types::I32, 3)
161 prot.should_receive(:write_list_end)
162 prot.should_receive(:write_set_begin).with(Types::I16, 3)
163 prot.should_receive(:write_type).with(Types::I16, 5)
164 prot.should_receive(:write_type).with(Types::I16, 17)
165 prot.should_receive(:write_type).with(Types::I16, 239)
166 prot.should_receive(:write_set_end)
167
168 struct = Foo.new
169 struct.words = nil
170 struct.complex = {5 => {"foo" => 1.23}}
171 struct.write(prot)
172 end
Kevin Clark090b69e2008-06-18 01:12:58 +0000173
174 it "should raise an exception if presented with an unknown container" do
175 # yeah this is silly, but I'm going for code coverage here
176 struct = Foo.new
177 lambda { struct.send :write_container, nil, nil, {:type => "foo"} }.should raise_error(StandardError, "Not a container type: foo")
178 end
Kevin Clark03d7a472008-06-18 01:09:41 +0000179end