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