blob: 5a4dc6a7612fa8a2b39a71a47229d01297f003dd [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
39 it "should have proper == semantics" do
40 Foo.new.should_not == Hello.new
41 Foo.new.should == Foo.new
42 Foo.new(:simple => 52).should_not == Foo.new
43 end
44
45 it "should read itself off the wire" do
46 struct = Foo.new
47 prot = mock("Protocol")
48 prot.should_receive(:read_struct_begin).twice
49 prot.should_receive(:read_struct_end).twice
50 prot.should_receive(:read_field_begin).and_return(
51 ['complex', Types::MAP, 5], # Foo
52 ['words', Types::STRING, 2], # Foo
53 ['hello', Types::STRUCT, 3], # Foo
54 ['greeting', Types::STRING, 1], # Hello
55 [nil, Types::STOP, 0], # Hello
56 ['simple', Types::I32, 1], # Foo
57 ['ints', Types::LIST, 4], # Foo
58 ['shorts', Types::SET, 6], # Foo
59 [nil, Types::STOP, 0] # Hello
60 )
61 prot.should_receive(:read_field_end).exactly(7).times
62 prot.should_receive(:read_map_begin).and_return(
63 [Types::I32, Types::MAP, 2], # complex
64 [Types::STRING, Types::DOUBLE, 2], # complex/1/value
65 [Types::STRING, Types::DOUBLE, 1] # complex/2/value
66 )
67 prot.should_receive(:read_map_end).exactly(3).times
68 prot.should_receive(:read_list_begin).and_return([Types::I32, 4])
69 prot.should_receive(:read_list_end)
70 prot.should_receive(:read_set_begin).and_return([Types::I16, 2])
71 prot.should_receive(:read_set_end)
72 prot.should_receive(:read_type).with(Types::I32).and_return(
73 1, 14, # complex keys
74 42, # simple
75 4, 23, 4, 29 # ints
76 )
77 prot.should_receive(:read_type).with(Types::STRING).and_return("pi", "e", "feigenbaum", "apple banana", "what's up?")
78 prot.should_receive(:read_type).with(Types::DOUBLE).and_return(Math::PI, Math::E, 4.669201609)
79 prot.should_receive(:read_type).with(Types::I16).and_return(2, 3)
80 prot.should_not_receive(:skip)
81 struct.read(prot)
82
83 struct.simple.should == 42
84 struct.complex.should == {1 => {"pi" => Math::PI, "e" => Math::E}, 14 => {"feigenbaum" => 4.669201609}}
85 struct.hello.should == Hello.new(:greeting => "what's up?")
86 struct.words.should == "apple banana"
87 struct.ints.should == [4, 23, 4, 29]
88 struct.shorts.should == Set.new([3, 2])
89 end
90
91 it "should skip unexpected fields in structs and use default values" do
92 struct = Foo.new
93 prot = mock("Protocol")
94 prot.should_receive(:read_struct_begin)
95 prot.should_receive(:read_struct_end)
96 prot.should_receive(:read_field_begin).and_return(
97 ['simple', Types::I32, 1],
98 ['complex', Types::STRUCT, 5],
99 ['thinz', Types::MAP, 7],
100 ['foobar', Types::I32, 3],
101 ['words', Types::STRING, 2],
102 [nil, Types::STOP, 0]
103 )
104 prot.should_receive(:read_field_end).exactly(5).times
105 prot.should_receive(:read_type).with(Types::I32).and_return(42)
106 prot.should_receive(:read_type).with(Types::STRING).and_return("foobar")
107 prot.should_receive(:skip).with(Types::STRUCT)
108 prot.should_receive(:skip).with(Types::MAP)
109 prot.should_receive(:skip).with(Types::I32)
110 struct.read(prot)
111
112 struct.simple.should == 42
113 struct.complex.should be_nil
114 struct.words.should == "foobar"
115 struct.hello.should == Hello.new(:greeting => 'hello, world!')
116 struct.ints.should == [1, 2, 2, 3]
117 struct.shorts.should == Set.new([5, 17, 239])
118 end
119
120 it "should write itself to the wire" do
121 prot = mock("Protocol")
122 prot.should_receive(:write_struct_begin).with("SpecNamespace::Foo")
123 prot.should_receive(:write_struct_end)
124 prot.should_receive(:write_field_begin).with('ints', Types::LIST, 4)
125 prot.should_receive(:write_field_begin).with('complex', Types::MAP, 5)
126 prot.should_receive(:write_field_begin).with('shorts', Types::SET, 6)
127 prot.should_receive(:write_field_stop)
128 prot.should_receive(:write_field_end).exactly(3).times
129 prot.should_receive(:write_field).with('simple', Types::I32, 1, 53)
130 prot.should_receive(:write_field).with('hello', Types::STRUCT, 3, Hello.new(:greeting => 'hello, world!'))
131 prot.should_receive(:write_map_begin).with(Types::I32, Types::MAP, 1)
132 prot.should_receive(:write_map_begin).with(Types::STRING, Types::DOUBLE, 1)
133 prot.should_receive(:write_type).with(Types::I32, 5) # complex/1/key
134 prot.should_receive(:write_type).with(Types::STRING, "foo") # complex/1/value/1/key
135 prot.should_receive(:write_type).with(Types::DOUBLE, 1.23) # complex/1/value/1/value
136 prot.should_receive(:write_map_end).twice
137 prot.should_receive(:write_list_begin).with(Types::I32, 4)
138 prot.should_receive(:write_type).with(Types::I32, 1)
139 prot.should_receive(:write_type).with(Types::I32, 2).twice
140 prot.should_receive(:write_type).with(Types::I32, 3)
141 prot.should_receive(:write_list_end)
142 prot.should_receive(:write_set_begin).with(Types::I16, 3)
143 prot.should_receive(:write_type).with(Types::I16, 5)
144 prot.should_receive(:write_type).with(Types::I16, 17)
145 prot.should_receive(:write_type).with(Types::I16, 239)
146 prot.should_receive(:write_set_end)
147
148 struct = Foo.new
149 struct.words = nil
150 struct.complex = {5 => {"foo" => 1.23}}
151 struct.write(prot)
152 end
153end