blob: 2ed1874badb59f860efa310a4e09d08cda3efec9 [file] [log] [blame]
Kevin Clark03d7a472008-06-18 01:09:41 +00001require File.dirname(__FILE__) + '/spec_helper'
Kevin Clark90a2cbe2008-06-18 01:15:53 +00002$:.unshift File.dirname(__FILE__) + "/gen-rb"
3require "ThriftSpec_types"
Kevin Clark03d7a472008-06-18 01:09:41 +00004
5class ThriftStructSpec < Spec::ExampleGroup
6 include Thrift
7 include SpecNamespace
8
9 class OldStruct
10 include Thrift::Struct
11 attr_accessor :set
12 FIELDS = {
13 1 => {:type => Thrift::Types::SET, :name => 'val', :default => {:foo => true, :bar => true}}
14 }
15 end
16
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 }
28 end
29
30 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
39
Kevin Clark9479b1a2008-06-18 01:13:37 +000040 it "should not share default values between instances" do
Kevin Clark01a51442008-06-18 01:14:36 +000041 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]
Kevin Clark9479b1a2008-06-18 01:13:37 +000048 end
49 end
50
Kevin Clark1cfd6932008-06-18 01:13:58 +000051 it "should properly initialize boolean values" do
Kevin Clark6c4598b2008-06-18 01:16:20 +000052 struct = BoolStruct.new(:yesno => false)
53 struct.yesno.should be_false
Kevin Clark1cfd6932008-06-18 01:13:58 +000054 end
55
Kevin Clark03d7a472008-06-18 01:09:41 +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
61
62 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)
99
100 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
107
108 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)
128
129 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
136
137 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
Kevin Clark090b69e2008-06-18 01:12:58 +0000170
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 Clark03d7a472008-06-18 01:09:41 +0000176end