David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | # |
| 2 | # Licensed to the Apache Software Foundation (ASF) under one |
| 3 | # or more contributor license agreements. See the NOTICE file |
| 4 | # distributed with this work for additional information |
| 5 | # regarding copyright ownership. The ASF licenses this file |
| 6 | # to you under the Apache License, Version 2.0 (the |
| 7 | # "License"); you may not use this file except in compliance |
| 8 | # with the License. You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, |
| 13 | # software distributed under the License is distributed on an |
| 14 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | # KIND, either express or implied. See the License for the |
| 16 | # specific language governing permissions and limitations |
| 17 | # under the License. |
| 18 | # |
| 19 | |
Jake Farrell | 8941458 | 2011-11-10 00:53:17 +0000 | [diff] [blame] | 20 | require File.expand_path("#{File.dirname(__FILE__)}/spec_helper") |
Kevin Clark | 03d7a47 | 2008-06-18 01:09:41 +0000 | [diff] [blame] | 21 | |
| 22 | class ThriftStructSpec < Spec::ExampleGroup |
| 23 | include Thrift |
| 24 | include SpecNamespace |
| 25 | |
Kevin Clark | 140b555 | 2008-06-18 01:17:57 +0000 | [diff] [blame] | 26 | describe Struct do |
| 27 | it "should iterate over all fields properly" do |
| 28 | fields = {} |
Bryan Duxbury | d815c21 | 2009-03-19 18:57:43 +0000 | [diff] [blame] | 29 | Foo.new.each_field { |fid,field_info| fields[fid] = field_info } |
| 30 | fields.should == Foo::FIELDS |
Kevin Clark | 9479b1a | 2008-06-18 01:13:37 +0000 | [diff] [blame] | 31 | end |
Kevin Clark | 9479b1a | 2008-06-18 01:13:37 +0000 | [diff] [blame] | 32 | |
Kevin Clark | 140b555 | 2008-06-18 01:17:57 +0000 | [diff] [blame] | 33 | it "should initialize all fields to defaults" do |
Bryan Duxbury | 6a1fb17 | 2010-09-21 16:30:58 +0000 | [diff] [blame] | 34 | validate_default_arguments(Foo.new) |
| 35 | end |
| 36 | |
| 37 | it "should initialize all fields to defaults and accept a block argument" do |
| 38 | Foo.new do |f| |
| 39 | validate_default_arguments(f) |
| 40 | end |
| 41 | end |
| 42 | |
| 43 | def validate_default_arguments(object) |
| 44 | object.simple.should == 53 |
| 45 | object.words.should == "words" |
| 46 | object.hello.should == Hello.new(:greeting => 'hello, world!') |
| 47 | object.ints.should == [1, 2, 2, 3] |
| 48 | object.complex.should be_nil |
| 49 | object.shorts.should == Set.new([5, 17, 239]) |
Kevin Clark | 140b555 | 2008-06-18 01:17:57 +0000 | [diff] [blame] | 50 | end |
Kevin Clark | 1cfd693 | 2008-06-18 01:13:58 +0000 | [diff] [blame] | 51 | |
Kevin Clark | 140b555 | 2008-06-18 01:17:57 +0000 | [diff] [blame] | 52 | it "should not share default values between instances" do |
| 53 | begin |
| 54 | struct = Foo.new |
| 55 | struct.ints << 17 |
| 56 | Foo.new.ints.should == [1,2,2,3] |
| 57 | ensure |
| 58 | # ensure no leakage to other tests |
| 59 | Foo::FIELDS[4][:default] = [1,2,2,3] |
| 60 | end |
| 61 | end |
Kevin Clark | 03d7a47 | 2008-06-18 01:09:41 +0000 | [diff] [blame] | 62 | |
Kevin Clark | 140b555 | 2008-06-18 01:17:57 +0000 | [diff] [blame] | 63 | it "should properly initialize boolean values" do |
| 64 | struct = BoolStruct.new(:yesno => false) |
| 65 | struct.yesno.should be_false |
| 66 | end |
Kevin Clark | 03d7a47 | 2008-06-18 01:09:41 +0000 | [diff] [blame] | 67 | |
Kevin Clark | 140b555 | 2008-06-18 01:17:57 +0000 | [diff] [blame] | 68 | it "should have proper == semantics" do |
| 69 | Foo.new.should_not == Hello.new |
| 70 | Foo.new.should == Foo.new |
| 71 | Foo.new(:simple => 52).should_not == Foo.new |
| 72 | end |
Kevin Clark | 03d7a47 | 2008-06-18 01:09:41 +0000 | [diff] [blame] | 73 | |
Bryan Duxbury | 3d03c52 | 2010-02-18 17:42:06 +0000 | [diff] [blame] | 74 | it "should print enum value names in inspect" do |
| 75 | StructWithSomeEnum.new(:some_enum => SomeEnum::ONE).inspect.should == "<SpecNamespace::StructWithSomeEnum some_enum:ONE (0)>" |
| 76 | |
| 77 | StructWithEnumMap.new(:my_map => {SomeEnum::ONE => [SomeEnum::TWO]}).inspect.should == "<SpecNamespace::StructWithEnumMap my_map:{ONE (0): [TWO (1)]}>" |
| 78 | end |
| 79 | |
Bryan Duxbury | 39dadd6 | 2010-02-18 22:00:45 +0000 | [diff] [blame] | 80 | it "should pretty print binary fields" do |
| 81 | Foo2.new(:my_binary => "\001\002\003").inspect.should == "<SpecNamespace::Foo2 my_binary:010203>" |
| 82 | end |
| 83 | |
Bryan Duxbury | 0e4920c | 2010-02-18 20:28:27 +0000 | [diff] [blame] | 84 | it "should offer field? methods" do |
| 85 | Foo.new.opt_string?.should be_false |
| 86 | Foo.new(:simple => 52).simple?.should be_true |
| 87 | Foo.new(:my_bool => false).my_bool?.should be_true |
| 88 | Foo.new(:my_bool => true).my_bool?.should be_true |
| 89 | end |
| 90 | |
Bryan Duxbury | 205e450 | 2010-02-18 23:19:42 +0000 | [diff] [blame] | 91 | it "should be comparable" do |
| 92 | s1 = StructWithSomeEnum.new(:some_enum => SomeEnum::ONE) |
| 93 | s2 = StructWithSomeEnum.new(:some_enum => SomeEnum::TWO) |
| 94 | |
| 95 | (s1 <=> s2).should == -1 |
| 96 | (s2 <=> s1).should == 1 |
| 97 | (s1 <=> s1).should == 0 |
| 98 | (s1 <=> StructWithSomeEnum.new()).should == -1 |
| 99 | end |
| 100 | |
Kevin Clark | 140b555 | 2008-06-18 01:17:57 +0000 | [diff] [blame] | 101 | it "should read itself off the wire" do |
| 102 | struct = Foo.new |
Bryan Duxbury | d1d1542 | 2009-04-04 00:58:03 +0000 | [diff] [blame] | 103 | prot = BaseProtocol.new(mock("transport")) |
Kevin Clark | 140b555 | 2008-06-18 01:17:57 +0000 | [diff] [blame] | 104 | prot.should_receive(:read_struct_begin).twice |
| 105 | prot.should_receive(:read_struct_end).twice |
| 106 | prot.should_receive(:read_field_begin).and_return( |
| 107 | ['complex', Types::MAP, 5], # Foo |
| 108 | ['words', Types::STRING, 2], # Foo |
| 109 | ['hello', Types::STRUCT, 3], # Foo |
| 110 | ['greeting', Types::STRING, 1], # Hello |
| 111 | [nil, Types::STOP, 0], # Hello |
| 112 | ['simple', Types::I32, 1], # Foo |
| 113 | ['ints', Types::LIST, 4], # Foo |
| 114 | ['shorts', Types::SET, 6], # Foo |
| 115 | [nil, Types::STOP, 0] # Hello |
| 116 | ) |
| 117 | prot.should_receive(:read_field_end).exactly(7).times |
| 118 | prot.should_receive(:read_map_begin).and_return( |
| 119 | [Types::I32, Types::MAP, 2], # complex |
| 120 | [Types::STRING, Types::DOUBLE, 2], # complex/1/value |
| 121 | [Types::STRING, Types::DOUBLE, 1] # complex/2/value |
| 122 | ) |
| 123 | prot.should_receive(:read_map_end).exactly(3).times |
| 124 | prot.should_receive(:read_list_begin).and_return([Types::I32, 4]) |
| 125 | prot.should_receive(:read_list_end) |
| 126 | prot.should_receive(:read_set_begin).and_return([Types::I16, 2]) |
| 127 | prot.should_receive(:read_set_end) |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 128 | prot.should_receive(:read_i32).and_return( |
Kevin Clark | 140b555 | 2008-06-18 01:17:57 +0000 | [diff] [blame] | 129 | 1, 14, # complex keys |
| 130 | 42, # simple |
| 131 | 4, 23, 4, 29 # ints |
| 132 | ) |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 133 | prot.should_receive(:read_string).and_return("pi", "e", "feigenbaum", "apple banana", "what's up?") |
| 134 | prot.should_receive(:read_double).and_return(Math::PI, Math::E, 4.669201609) |
| 135 | prot.should_receive(:read_i16).and_return(2, 3) |
Kevin Clark | 140b555 | 2008-06-18 01:17:57 +0000 | [diff] [blame] | 136 | prot.should_not_receive(:skip) |
| 137 | struct.read(prot) |
Kevin Clark | 03d7a47 | 2008-06-18 01:09:41 +0000 | [diff] [blame] | 138 | |
Kevin Clark | 140b555 | 2008-06-18 01:17:57 +0000 | [diff] [blame] | 139 | struct.simple.should == 42 |
| 140 | struct.complex.should == {1 => {"pi" => Math::PI, "e" => Math::E}, 14 => {"feigenbaum" => 4.669201609}} |
| 141 | struct.hello.should == Hello.new(:greeting => "what's up?") |
| 142 | struct.words.should == "apple banana" |
| 143 | struct.ints.should == [4, 23, 4, 29] |
| 144 | struct.shorts.should == Set.new([3, 2]) |
| 145 | end |
Kevin Clark | 03d7a47 | 2008-06-18 01:09:41 +0000 | [diff] [blame] | 146 | |
Bryan Duxbury | 30dd725 | 2010-02-27 05:47:15 +0000 | [diff] [blame] | 147 | it "should serialize false boolean fields correctly" do |
| 148 | b = BoolStruct.new(:yesno => false) |
| 149 | prot = BinaryProtocol.new(MemoryBufferTransport.new) |
| 150 | prot.should_receive(:write_bool).with(false) |
| 151 | b.write(prot) |
| 152 | end |
| 153 | |
Kevin Clark | 140b555 | 2008-06-18 01:17:57 +0000 | [diff] [blame] | 154 | it "should skip unexpected fields in structs and use default values" do |
| 155 | struct = Foo.new |
Bryan Duxbury | d1d1542 | 2009-04-04 00:58:03 +0000 | [diff] [blame] | 156 | prot = BaseProtocol.new(mock("transport")) |
Kevin Clark | 140b555 | 2008-06-18 01:17:57 +0000 | [diff] [blame] | 157 | prot.should_receive(:read_struct_begin) |
| 158 | prot.should_receive(:read_struct_end) |
| 159 | prot.should_receive(:read_field_begin).and_return( |
| 160 | ['simple', Types::I32, 1], |
| 161 | ['complex', Types::STRUCT, 5], |
| 162 | ['thinz', Types::MAP, 7], |
| 163 | ['foobar', Types::I32, 3], |
| 164 | ['words', Types::STRING, 2], |
| 165 | [nil, Types::STOP, 0] |
| 166 | ) |
| 167 | prot.should_receive(:read_field_end).exactly(5).times |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 168 | prot.should_receive(:read_i32).and_return(42) |
| 169 | prot.should_receive(:read_string).and_return("foobar") |
Kevin Clark | 140b555 | 2008-06-18 01:17:57 +0000 | [diff] [blame] | 170 | prot.should_receive(:skip).with(Types::STRUCT) |
| 171 | prot.should_receive(:skip).with(Types::MAP) |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 172 | # prot.should_receive(:read_map_begin).and_return([Types::I32, Types::I32, 0]) |
| 173 | # prot.should_receive(:read_map_end) |
Kevin Clark | 140b555 | 2008-06-18 01:17:57 +0000 | [diff] [blame] | 174 | prot.should_receive(:skip).with(Types::I32) |
| 175 | struct.read(prot) |
Kevin Clark | 03d7a47 | 2008-06-18 01:09:41 +0000 | [diff] [blame] | 176 | |
Kevin Clark | 140b555 | 2008-06-18 01:17:57 +0000 | [diff] [blame] | 177 | struct.simple.should == 42 |
| 178 | struct.complex.should be_nil |
| 179 | struct.words.should == "foobar" |
| 180 | struct.hello.should == Hello.new(:greeting => 'hello, world!') |
| 181 | struct.ints.should == [1, 2, 2, 3] |
| 182 | struct.shorts.should == Set.new([5, 17, 239]) |
| 183 | end |
Kevin Clark | 090b69e | 2008-06-18 01:12:58 +0000 | [diff] [blame] | 184 | |
Kevin Clark | 140b555 | 2008-06-18 01:17:57 +0000 | [diff] [blame] | 185 | it "should write itself to the wire" do |
Bryan Duxbury | d1d1542 | 2009-04-04 00:58:03 +0000 | [diff] [blame] | 186 | prot = BaseProtocol.new(mock("transport")) #mock("Protocol") |
Kevin Clark | 140b555 | 2008-06-18 01:17:57 +0000 | [diff] [blame] | 187 | prot.should_receive(:write_struct_begin).with("SpecNamespace::Foo") |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 188 | prot.should_receive(:write_struct_begin).with("SpecNamespace::Hello") |
| 189 | prot.should_receive(:write_struct_end).twice |
Kevin Clark | 140b555 | 2008-06-18 01:17:57 +0000 | [diff] [blame] | 190 | prot.should_receive(:write_field_begin).with('ints', Types::LIST, 4) |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 191 | prot.should_receive(:write_i32).with(1) |
| 192 | prot.should_receive(:write_i32).with(2).twice |
| 193 | prot.should_receive(:write_i32).with(3) |
Kevin Clark | 140b555 | 2008-06-18 01:17:57 +0000 | [diff] [blame] | 194 | prot.should_receive(:write_field_begin).with('complex', Types::MAP, 5) |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 195 | prot.should_receive(:write_i32).with(5) |
| 196 | prot.should_receive(:write_string).with('foo') |
| 197 | prot.should_receive(:write_double).with(1.23) |
Kevin Clark | 140b555 | 2008-06-18 01:17:57 +0000 | [diff] [blame] | 198 | prot.should_receive(:write_field_begin).with('shorts', Types::SET, 6) |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 199 | prot.should_receive(:write_i16).with(5) |
| 200 | prot.should_receive(:write_i16).with(17) |
| 201 | prot.should_receive(:write_i16).with(239) |
| 202 | prot.should_receive(:write_field_stop).twice |
| 203 | prot.should_receive(:write_field_end).exactly(6).times |
| 204 | prot.should_receive(:write_field_begin).with('simple', Types::I32, 1) |
| 205 | prot.should_receive(:write_i32).with(53) |
| 206 | prot.should_receive(:write_field_begin).with('hello', Types::STRUCT, 3) |
| 207 | prot.should_receive(:write_field_begin).with('greeting', Types::STRING, 1) |
| 208 | prot.should_receive(:write_string).with('hello, world!') |
Kevin Clark | 140b555 | 2008-06-18 01:17:57 +0000 | [diff] [blame] | 209 | prot.should_receive(:write_map_begin).with(Types::I32, Types::MAP, 1) |
| 210 | prot.should_receive(:write_map_begin).with(Types::STRING, Types::DOUBLE, 1) |
Kevin Clark | 140b555 | 2008-06-18 01:17:57 +0000 | [diff] [blame] | 211 | prot.should_receive(:write_map_end).twice |
| 212 | prot.should_receive(:write_list_begin).with(Types::I32, 4) |
Kevin Clark | 140b555 | 2008-06-18 01:17:57 +0000 | [diff] [blame] | 213 | prot.should_receive(:write_list_end) |
| 214 | prot.should_receive(:write_set_begin).with(Types::I16, 3) |
Kevin Clark | 140b555 | 2008-06-18 01:17:57 +0000 | [diff] [blame] | 215 | prot.should_receive(:write_set_end) |
| 216 | |
| 217 | struct = Foo.new |
| 218 | struct.words = nil |
| 219 | struct.complex = {5 => {"foo" => 1.23}} |
| 220 | struct.write(prot) |
| 221 | end |
| 222 | |
| 223 | it "should raise an exception if presented with an unknown container" do |
| 224 | # yeah this is silly, but I'm going for code coverage here |
| 225 | struct = Foo.new |
| 226 | lambda { struct.send :write_container, nil, nil, {:type => "foo"} }.should raise_error(StandardError, "Not a container type: foo") |
| 227 | end |
Kevin Clark | 2319375 | 2008-06-18 01:18:07 +0000 | [diff] [blame] | 228 | |
| 229 | it "should support optional type-checking in Thrift::Struct.new" do |
| 230 | Thrift.type_checking = true |
| 231 | begin |
Kevin Clark | b586339 | 2008-07-18 22:03:48 +0000 | [diff] [blame] | 232 | lambda { Hello.new(:greeting => 3) }.should raise_error(TypeError, "Expected Types::STRING, received Fixnum for field greeting") |
Kevin Clark | 2319375 | 2008-06-18 01:18:07 +0000 | [diff] [blame] | 233 | ensure |
| 234 | Thrift.type_checking = false |
| 235 | end |
| 236 | lambda { Hello.new(:greeting => 3) }.should_not raise_error(TypeError) |
| 237 | end |
| 238 | |
| 239 | it "should support optional type-checking in field accessors" do |
| 240 | Thrift.type_checking = true |
| 241 | begin |
| 242 | hello = Hello.new |
Kevin Clark | b586339 | 2008-07-18 22:03:48 +0000 | [diff] [blame] | 243 | lambda { hello.greeting = 3 }.should raise_error(TypeError, "Expected Types::STRING, received Fixnum for field greeting") |
Kevin Clark | 2319375 | 2008-06-18 01:18:07 +0000 | [diff] [blame] | 244 | ensure |
| 245 | Thrift.type_checking = false |
| 246 | end |
| 247 | lambda { hello.greeting = 3 }.should_not raise_error(TypeError) |
| 248 | end |
| 249 | |
| 250 | it "should raise an exception when unknown types are given to Thrift::Struct.new" do |
Kevin Clark | 38a2ce6 | 2008-08-25 21:34:19 +0000 | [diff] [blame] | 251 | lambda { Hello.new(:fish => 'salmon') }.should raise_error(Exception, "Unknown key given to SpecNamespace::Hello.new: fish") |
Kevin Clark | 2319375 | 2008-06-18 01:18:07 +0000 | [diff] [blame] | 252 | end |
Kevin Clark | 3af9287 | 2008-07-28 22:20:36 +0000 | [diff] [blame] | 253 | |
| 254 | it "should support `raise Xception, 'message'` for Exception structs" do |
| 255 | begin |
| 256 | raise Xception, "something happened" |
| 257 | rescue Thrift::Exception => e |
| 258 | e.message.should == "something happened" |
| 259 | e.code.should == 1 |
| 260 | # ensure it gets serialized properly, this is the really important part |
Bryan Duxbury | d1d1542 | 2009-04-04 00:58:03 +0000 | [diff] [blame] | 261 | prot = BaseProtocol.new(mock("trans")) |
Kevin Clark | 031baf7 | 2008-11-14 17:11:39 +0000 | [diff] [blame] | 262 | prot.should_receive(:write_struct_begin).with("SpecNamespace::Xception") |
Kevin Clark | 3af9287 | 2008-07-28 22:20:36 +0000 | [diff] [blame] | 263 | prot.should_receive(:write_struct_end) |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 264 | prot.should_receive(:write_field_begin).with('message', Types::STRING, 1)#, "something happened") |
| 265 | prot.should_receive(:write_string).with("something happened") |
| 266 | prot.should_receive(:write_field_begin).with('code', Types::I32, 2)#, 1) |
| 267 | prot.should_receive(:write_i32).with(1) |
Kevin Clark | 3af9287 | 2008-07-28 22:20:36 +0000 | [diff] [blame] | 268 | prot.should_receive(:write_field_stop) |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 269 | prot.should_receive(:write_field_end).twice |
Kevin Clark | 3af9287 | 2008-07-28 22:20:36 +0000 | [diff] [blame] | 270 | |
| 271 | e.write(prot) |
| 272 | end |
| 273 | end |
| 274 | |
| 275 | it "should support the regular initializer for exception structs" do |
| 276 | begin |
| 277 | raise Xception, :message => "something happened", :code => 5 |
| 278 | rescue Thrift::Exception => e |
| 279 | e.message.should == "something happened" |
| 280 | e.code.should == 5 |
Bryan Duxbury | d1d1542 | 2009-04-04 00:58:03 +0000 | [diff] [blame] | 281 | prot = BaseProtocol.new(mock("trans")) |
Kevin Clark | 031baf7 | 2008-11-14 17:11:39 +0000 | [diff] [blame] | 282 | prot.should_receive(:write_struct_begin).with("SpecNamespace::Xception") |
Kevin Clark | 3af9287 | 2008-07-28 22:20:36 +0000 | [diff] [blame] | 283 | prot.should_receive(:write_struct_end) |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 284 | prot.should_receive(:write_field_begin).with('message', Types::STRING, 1) |
| 285 | prot.should_receive(:write_string).with("something happened") |
| 286 | prot.should_receive(:write_field_begin).with('code', Types::I32, 2) |
| 287 | prot.should_receive(:write_i32).with(5) |
Kevin Clark | 3af9287 | 2008-07-28 22:20:36 +0000 | [diff] [blame] | 288 | prot.should_receive(:write_field_stop) |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 289 | prot.should_receive(:write_field_end).twice |
Kevin Clark | 3af9287 | 2008-07-28 22:20:36 +0000 | [diff] [blame] | 290 | |
| 291 | e.write(prot) |
| 292 | end |
| 293 | end |
Kevin Clark | 090b69e | 2008-06-18 01:12:58 +0000 | [diff] [blame] | 294 | end |
Kevin Clark | 03d7a47 | 2008-06-18 01:09:41 +0000 | [diff] [blame] | 295 | end |