blob: 5554497ed7789479ec3e3bb421f40e43d1437e28 [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 Clarkec9106f2008-06-18 01:14:26 +000052 pending do
53 struct = BoolStruct.new(:yesno => false)
54 struct.yesno.should be_false
55 end
Kevin Clark1cfd6932008-06-18 01:13:58 +000056 end
57
Kevin Clark03d7a472008-06-18 01:09:41 +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
63
64 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)
101
102 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
109
110 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)
130
131 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
138
139 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
Kevin Clark090b69e2008-06-18 01:12:58 +0000172
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 Clark03d7a472008-06-18 01:09:41 +0000178end