| 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 |
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 23 | describe Thrift::Struct do |
| Kevin Clark | 140b555 | 2008-06-18 01:17:57 +0000 | [diff] [blame] | 24 | it "should iterate over all fields properly" do |
| 25 | fields = {} |
| Dmytro Shteflyuk | 3b0ab4d | 2026-03-11 17:46:48 -0400 | [diff] [blame^] | 26 | SpecNamespace::Foo.new.each_field { |fid, field_info| fields[fid] = field_info } |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 27 | expect(fields).to eq(SpecNamespace::Foo::FIELDS) |
| Kevin Clark | 9479b1a | 2008-06-18 01:13:37 +0000 | [diff] [blame] | 28 | end |
| Kevin Clark | 9479b1a | 2008-06-18 01:13:37 +0000 | [diff] [blame] | 29 | |
| Kevin Clark | 140b555 | 2008-06-18 01:17:57 +0000 | [diff] [blame] | 30 | it "should initialize all fields to defaults" do |
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 31 | validate_default_arguments(SpecNamespace::Foo.new) |
| Bryan Duxbury | 6a1fb17 | 2010-09-21 16:30:58 +0000 | [diff] [blame] | 32 | end |
| 33 | |
| 34 | 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] | 35 | SpecNamespace::Foo.new do |f| |
| Bryan Duxbury | 6a1fb17 | 2010-09-21 16:30:58 +0000 | [diff] [blame] | 36 | validate_default_arguments(f) |
| 37 | end |
| 38 | end |
| 39 | |
| 40 | def validate_default_arguments(object) |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 41 | expect(object.simple).to eq(53) |
| 42 | expect(object.words).to eq("words") |
| 43 | expect(object.hello).to eq(SpecNamespace::Hello.new(:greeting => 'hello, world!')) |
| 44 | expect(object.ints).to eq([1, 2, 2, 3]) |
| 45 | expect(object.complex).to be_nil |
| 46 | expect(object.shorts).to eq(Set.new([5, 17, 239])) |
| Kevin Clark | 140b555 | 2008-06-18 01:17:57 +0000 | [diff] [blame] | 47 | end |
| Kevin Clark | 1cfd693 | 2008-06-18 01:13:58 +0000 | [diff] [blame] | 48 | |
| Kevin Clark | 140b555 | 2008-06-18 01:17:57 +0000 | [diff] [blame] | 49 | it "should not share default values between instances" do |
| 50 | begin |
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 51 | struct = SpecNamespace::Foo.new |
| Kevin Clark | 140b555 | 2008-06-18 01:17:57 +0000 | [diff] [blame] | 52 | struct.ints << 17 |
| Dmytro Shteflyuk | 3b0ab4d | 2026-03-11 17:46:48 -0400 | [diff] [blame^] | 53 | expect(SpecNamespace::Foo.new.ints).to eq([1, 2, 2, 3]) |
| Kevin Clark | 140b555 | 2008-06-18 01:17:57 +0000 | [diff] [blame] | 54 | ensure |
| 55 | # ensure no leakage to other tests |
| Dmytro Shteflyuk | 3b0ab4d | 2026-03-11 17:46:48 -0400 | [diff] [blame^] | 56 | SpecNamespace::Foo::FIELDS[4][:default] = [1, 2, 2, 3] |
| Kevin Clark | 140b555 | 2008-06-18 01:17:57 +0000 | [diff] [blame] | 57 | end |
| 58 | end |
| Kevin Clark | 03d7a47 | 2008-06-18 01:09:41 +0000 | [diff] [blame] | 59 | |
| Kevin Clark | 140b555 | 2008-06-18 01:17:57 +0000 | [diff] [blame] | 60 | it "should properly initialize boolean values" do |
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 61 | struct = SpecNamespace::BoolStruct.new(:yesno => false) |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 62 | expect(struct.yesno).to be_falsey |
| Kevin Clark | 140b555 | 2008-06-18 01:17:57 +0000 | [diff] [blame] | 63 | end |
| Kevin Clark | 03d7a47 | 2008-06-18 01:09:41 +0000 | [diff] [blame] | 64 | |
| Kevin Clark | 140b555 | 2008-06-18 01:17:57 +0000 | [diff] [blame] | 65 | it "should have proper == semantics" do |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 66 | expect(SpecNamespace::Foo.new).not_to eq(SpecNamespace::Hello.new) |
| 67 | expect(SpecNamespace::Foo.new).to eq(SpecNamespace::Foo.new) |
| 68 | expect(SpecNamespace::Foo.new(:simple => 52)).not_to eq(SpecNamespace::Foo.new) |
| Kevin Clark | 140b555 | 2008-06-18 01:17:57 +0000 | [diff] [blame] | 69 | end |
| Kevin Clark | 03d7a47 | 2008-06-18 01:09:41 +0000 | [diff] [blame] | 70 | |
| Bryan Duxbury | 3d03c52 | 2010-02-18 17:42:06 +0000 | [diff] [blame] | 71 | it "should print enum value names in inspect" do |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 72 | expect(SpecNamespace::StructWithSomeEnum.new(:some_enum => SpecNamespace::SomeEnum::ONE).inspect).to eq("<SpecNamespace::StructWithSomeEnum some_enum:ONE (0)>") |
| Bryan Duxbury | 3d03c52 | 2010-02-18 17:42:06 +0000 | [diff] [blame] | 73 | |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 74 | expect(SpecNamespace::StructWithEnumMap.new(:my_map => {SpecNamespace::SomeEnum::ONE => [SpecNamespace::SomeEnum::TWO]}).inspect).to eq("<SpecNamespace::StructWithEnumMap my_map:{ONE (0): [TWO (1)]}>") |
| Bryan Duxbury | 3d03c52 | 2010-02-18 17:42:06 +0000 | [diff] [blame] | 75 | end |
| 76 | |
| Bryan Duxbury | 39dadd6 | 2010-02-18 22:00:45 +0000 | [diff] [blame] | 77 | it "should pretty print binary fields" do |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 78 | expect(SpecNamespace::Foo2.new(:my_binary => "\001\002\003").inspect).to eq("<SpecNamespace::Foo2 my_binary:010203>") |
| Bryan Duxbury | 39dadd6 | 2010-02-18 22:00:45 +0000 | [diff] [blame] | 79 | end |
| 80 | |
| Bryan Duxbury | 0e4920c | 2010-02-18 20:28:27 +0000 | [diff] [blame] | 81 | it "should offer field? methods" do |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 82 | expect(SpecNamespace::Foo.new.opt_string?).to be_falsey |
| 83 | expect(SpecNamespace::Foo.new(:simple => 52).simple?).to be_truthy |
| 84 | expect(SpecNamespace::Foo.new(:my_bool => false).my_bool?).to be_truthy |
| 85 | expect(SpecNamespace::Foo.new(:my_bool => true).my_bool?).to be_truthy |
| Bryan Duxbury | 0e4920c | 2010-02-18 20:28:27 +0000 | [diff] [blame] | 86 | end |
| 87 | |
| Bryan Duxbury | 205e450 | 2010-02-18 23:19:42 +0000 | [diff] [blame] | 88 | it "should be comparable" do |
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 89 | s1 = SpecNamespace::StructWithSomeEnum.new(:some_enum => SpecNamespace::SomeEnum::ONE) |
| 90 | s2 = SpecNamespace::StructWithSomeEnum.new(:some_enum => SpecNamespace::SomeEnum::TWO) |
| Bryan Duxbury | 205e450 | 2010-02-18 23:19:42 +0000 | [diff] [blame] | 91 | |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 92 | expect(s1 <=> s2).to eq(-1) |
| 93 | expect(s2 <=> s1).to eq(1) |
| 94 | expect(s1 <=> s1).to eq(0) |
| 95 | expect(s1 <=> SpecNamespace::StructWithSomeEnum.new()).to eq(-1) |
| Bryan Duxbury | 205e450 | 2010-02-18 23:19:42 +0000 | [diff] [blame] | 96 | end |
| 97 | |
| Kevin Clark | 140b555 | 2008-06-18 01:17:57 +0000 | [diff] [blame] | 98 | it "should read itself off the wire" do |
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 99 | struct = SpecNamespace::Foo.new |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 100 | prot = Thrift::BaseProtocol.new(double("transport")) |
| 101 | expect(prot).to receive(:read_struct_begin).twice |
| 102 | expect(prot).to receive(:read_struct_end).twice |
| 103 | expect(prot).to receive(:read_field_begin).and_return( |
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 104 | ['complex', Thrift::Types::MAP, 5], # Foo |
| 105 | ['words', Thrift::Types::STRING, 2], # Foo |
| 106 | ['hello', Thrift::Types::STRUCT, 3], # Foo |
| 107 | ['greeting', Thrift::Types::STRING, 1], # Hello |
| 108 | [nil, Thrift::Types::STOP, 0], # Hello |
| 109 | ['simple', Thrift::Types::I32, 1], # Foo |
| 110 | ['ints', Thrift::Types::LIST, 4], # Foo |
| 111 | ['shorts', Thrift::Types::SET, 6], # Foo |
| 112 | [nil, Thrift::Types::STOP, 0] # Hello |
| Kevin Clark | 140b555 | 2008-06-18 01:17:57 +0000 | [diff] [blame] | 113 | ) |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 114 | expect(prot).to receive(:read_field_end).exactly(7).times |
| 115 | expect(prot).to receive(:read_map_begin).and_return( |
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 116 | [Thrift::Types::I32, Thrift::Types::MAP, 2], # complex |
| 117 | [Thrift::Types::STRING, Thrift::Types::DOUBLE, 2], # complex/1/value |
| 118 | [Thrift::Types::STRING, Thrift::Types::DOUBLE, 1] # complex/2/value |
| Kevin Clark | 140b555 | 2008-06-18 01:17:57 +0000 | [diff] [blame] | 119 | ) |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 120 | expect(prot).to receive(:read_map_end).exactly(3).times |
| 121 | expect(prot).to receive(:read_list_begin).and_return([Thrift::Types::I32, 4]) |
| 122 | expect(prot).to receive(:read_list_end) |
| 123 | expect(prot).to receive(:read_set_begin).and_return([Thrift::Types::I16, 2]) |
| 124 | expect(prot).to receive(:read_set_end) |
| 125 | expect(prot).to receive(:read_i32).and_return( |
| Kevin Clark | 140b555 | 2008-06-18 01:17:57 +0000 | [diff] [blame] | 126 | 1, 14, # complex keys |
| 127 | 42, # simple |
| 128 | 4, 23, 4, 29 # ints |
| 129 | ) |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 130 | expect(prot).to receive(:read_string).and_return("pi", "e", "feigenbaum", "apple banana", "what's up?") |
| 131 | expect(prot).to receive(:read_double).and_return(Math::PI, Math::E, 4.669201609) |
| 132 | expect(prot).to receive(:read_i16).and_return(2, 3) |
| 133 | expect(prot).not_to receive(:skip) |
| Kevin Clark | 140b555 | 2008-06-18 01:17:57 +0000 | [diff] [blame] | 134 | struct.read(prot) |
| Kevin Clark | 03d7a47 | 2008-06-18 01:09:41 +0000 | [diff] [blame] | 135 | |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 136 | expect(struct.simple).to eq(42) |
| 137 | expect(struct.complex).to eq({1 => {"pi" => Math::PI, "e" => Math::E}, 14 => {"feigenbaum" => 4.669201609}}) |
| 138 | expect(struct.hello).to eq(SpecNamespace::Hello.new(:greeting => "what's up?")) |
| 139 | expect(struct.words).to eq("apple banana") |
| 140 | expect(struct.ints).to eq([4, 23, 4, 29]) |
| 141 | expect(struct.shorts).to eq(Set.new([3, 2])) |
| Kevin Clark | 140b555 | 2008-06-18 01:17:57 +0000 | [diff] [blame] | 142 | end |
| Kevin Clark | 03d7a47 | 2008-06-18 01:09:41 +0000 | [diff] [blame] | 143 | |
| Dmytro Shteflyuk | 5b5fd3c | 2026-03-11 19:50:50 -0400 | [diff] [blame] | 144 | it "rejects negative container sizes while reading" do |
| 145 | struct = SpecNamespace::Foo.new |
| 146 | prot = Thrift::BaseProtocol.new(double("transport")) |
| 147 | |
| 148 | expect(prot).to receive(:read_list_begin).and_return([Thrift::Types::I32, -1]) |
| 149 | |
| 150 | expect { |
| 151 | struct.send(:read_field, prot, SpecNamespace::Foo::FIELDS[4]) |
| 152 | }.to raise_error(Thrift::ProtocolException, "Negative size") { |error| |
| 153 | expect(error.type).to eq(Thrift::ProtocolException::NEGATIVE_SIZE) |
| 154 | } |
| 155 | end |
| 156 | |
| 157 | it "does not preallocate arrays from declared list sizes" do |
| 158 | struct = SpecNamespace::Foo.new |
| 159 | prot = Thrift::BaseProtocol.new(double("transport")) |
| 160 | declared_size = 1 << 30 |
| 161 | sentinel = RuntimeError.new("stop after first element") |
| 162 | |
| 163 | expect(prot).to receive(:read_list_begin).and_return([Thrift::Types::I32, declared_size]) |
| 164 | expect(prot).to receive(:read_i32).and_raise(sentinel) |
| 165 | expect(Array).not_to receive(:new).with(declared_size) |
| 166 | |
| 167 | expect { |
| 168 | struct.send(:read_field, prot, SpecNamespace::Foo::FIELDS[4]) |
| 169 | }.to raise_error(sentinel) |
| 170 | end |
| 171 | |
| Bryan Duxbury | 30dd725 | 2010-02-27 05:47:15 +0000 | [diff] [blame] | 172 | it "should serialize false boolean fields correctly" do |
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 173 | b = SpecNamespace::BoolStruct.new(:yesno => false) |
| 174 | prot = Thrift::BinaryProtocol.new(Thrift::MemoryBufferTransport.new) |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 175 | expect(prot).to receive(:write_bool).with(false) |
| Bryan Duxbury | 30dd725 | 2010-02-27 05:47:15 +0000 | [diff] [blame] | 176 | b.write(prot) |
| 177 | end |
| 178 | |
| Kevin Clark | 140b555 | 2008-06-18 01:17:57 +0000 | [diff] [blame] | 179 | it "should skip unexpected fields in structs and use default values" do |
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 180 | struct = SpecNamespace::Foo.new |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 181 | prot = Thrift::BaseProtocol.new(double("transport")) |
| 182 | expect(prot).to receive(:read_struct_begin) |
| 183 | expect(prot).to receive(:read_struct_end) |
| 184 | expect(prot).to receive(:read_field_begin).and_return( |
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 185 | ['simple', Thrift::Types::I32, 1], |
| 186 | ['complex', Thrift::Types::STRUCT, 5], |
| 187 | ['thinz', Thrift::Types::MAP, 7], |
| 188 | ['foobar', Thrift::Types::I32, 3], |
| 189 | ['words', Thrift::Types::STRING, 2], |
| 190 | [nil, Thrift::Types::STOP, 0] |
| Kevin Clark | 140b555 | 2008-06-18 01:17:57 +0000 | [diff] [blame] | 191 | ) |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 192 | expect(prot).to receive(:read_field_end).exactly(5).times |
| 193 | expect(prot).to receive(:read_i32).and_return(42) |
| 194 | expect(prot).to receive(:read_string).and_return("foobar") |
| 195 | expect(prot).to receive(:skip).with(Thrift::Types::STRUCT) |
| 196 | expect(prot).to receive(:skip).with(Thrift::Types::MAP) |
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 197 | # 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] | 198 | # prot.should_receive(:read_map_end) |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 199 | expect(prot).to receive(:skip).with(Thrift::Types::I32) |
| Kevin Clark | 140b555 | 2008-06-18 01:17:57 +0000 | [diff] [blame] | 200 | struct.read(prot) |
| Kevin Clark | 03d7a47 | 2008-06-18 01:09:41 +0000 | [diff] [blame] | 201 | |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 202 | expect(struct.simple).to eq(42) |
| 203 | expect(struct.complex).to be_nil |
| 204 | expect(struct.words).to eq("foobar") |
| 205 | expect(struct.hello).to eq(SpecNamespace::Hello.new(:greeting => 'hello, world!')) |
| 206 | expect(struct.ints).to eq([1, 2, 2, 3]) |
| 207 | expect(struct.shorts).to eq(Set.new([5, 17, 239])) |
| Kevin Clark | 140b555 | 2008-06-18 01:17:57 +0000 | [diff] [blame] | 208 | end |
| Kevin Clark | 090b69e | 2008-06-18 01:12:58 +0000 | [diff] [blame] | 209 | |
| Kevin Clark | 140b555 | 2008-06-18 01:17:57 +0000 | [diff] [blame] | 210 | it "should write itself to the wire" do |
| Dmytro Shteflyuk | 3b0ab4d | 2026-03-11 17:46:48 -0400 | [diff] [blame^] | 211 | prot = Thrift::BaseProtocol.new(double("transport")) # mock("Protocol") |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 212 | expect(prot).to receive(:write_struct_begin).with("SpecNamespace::Foo") |
| 213 | expect(prot).to receive(:write_struct_begin).with("SpecNamespace::Hello") |
| 214 | expect(prot).to receive(:write_struct_end).twice |
| 215 | expect(prot).to receive(:write_field_begin).with('ints', Thrift::Types::LIST, 4) |
| 216 | expect(prot).to receive(:write_i32).with(1) |
| 217 | expect(prot).to receive(:write_i32).with(2).twice |
| 218 | expect(prot).to receive(:write_i32).with(3) |
| 219 | expect(prot).to receive(:write_field_begin).with('complex', Thrift::Types::MAP, 5) |
| 220 | expect(prot).to receive(:write_i32).with(5) |
| 221 | expect(prot).to receive(:write_string).with('foo') |
| 222 | expect(prot).to receive(:write_double).with(1.23) |
| 223 | expect(prot).to receive(:write_field_begin).with('shorts', Thrift::Types::SET, 6) |
| 224 | expect(prot).to receive(:write_i16).with(5) |
| 225 | expect(prot).to receive(:write_i16).with(17) |
| 226 | expect(prot).to receive(:write_i16).with(239) |
| 227 | expect(prot).to receive(:write_field_stop).twice |
| 228 | expect(prot).to receive(:write_field_end).exactly(6).times |
| 229 | expect(prot).to receive(:write_field_begin).with('simple', Thrift::Types::I32, 1) |
| 230 | expect(prot).to receive(:write_i32).with(53) |
| 231 | expect(prot).to receive(:write_field_begin).with('hello', Thrift::Types::STRUCT, 3) |
| 232 | expect(prot).to receive(:write_field_begin).with('greeting', Thrift::Types::STRING, 1) |
| 233 | expect(prot).to receive(:write_string).with('hello, world!') |
| 234 | expect(prot).to receive(:write_map_begin).with(Thrift::Types::I32, Thrift::Types::MAP, 1) |
| 235 | expect(prot).to receive(:write_map_begin).with(Thrift::Types::STRING, Thrift::Types::DOUBLE, 1) |
| 236 | expect(prot).to receive(:write_map_end).twice |
| 237 | expect(prot).to receive(:write_list_begin).with(Thrift::Types::I32, 4) |
| 238 | expect(prot).to receive(:write_list_end) |
| 239 | expect(prot).to receive(:write_set_begin).with(Thrift::Types::I16, 3) |
| 240 | expect(prot).to receive(:write_set_end) |
| Kevin Clark | 140b555 | 2008-06-18 01:17:57 +0000 | [diff] [blame] | 241 | |
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 242 | struct = SpecNamespace::Foo.new |
| Kevin Clark | 140b555 | 2008-06-18 01:17:57 +0000 | [diff] [blame] | 243 | struct.words = nil |
| 244 | struct.complex = {5 => {"foo" => 1.23}} |
| 245 | struct.write(prot) |
| 246 | end |
| 247 | |
| 248 | it "should raise an exception if presented with an unknown container" do |
| 249 | # yeah this is silly, but I'm going for code coverage here |
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 250 | struct = SpecNamespace::Foo.new |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 251 | expect { struct.send :write_container, nil, nil, {:type => "foo"} }.to raise_error(StandardError, "Not a container type: foo") |
| Kevin Clark | 140b555 | 2008-06-18 01:17:57 +0000 | [diff] [blame] | 252 | end |
| Kevin Clark | 2319375 | 2008-06-18 01:18:07 +0000 | [diff] [blame] | 253 | |
| 254 | it "should support optional type-checking in Thrift::Struct.new" do |
| 255 | Thrift.type_checking = true |
| 256 | begin |
| Dmytro Shteflyuk | f06db1b | 2025-11-20 18:09:21 -0500 | [diff] [blame] | 257 | expect { SpecNamespace::Hello.new(:greeting => 3) }.to raise_error(Thrift::TypeError, "Expected Types::STRING, received Integer for field greeting") |
| Kevin Clark | 2319375 | 2008-06-18 01:18:07 +0000 | [diff] [blame] | 258 | ensure |
| 259 | Thrift.type_checking = false |
| 260 | end |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 261 | expect { SpecNamespace::Hello.new(:greeting => 3) }.not_to raise_error |
| Kevin Clark | 2319375 | 2008-06-18 01:18:07 +0000 | [diff] [blame] | 262 | end |
| 263 | |
| 264 | it "should support optional type-checking in field accessors" do |
| 265 | Thrift.type_checking = true |
| 266 | begin |
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 267 | hello = SpecNamespace::Hello.new |
| Dmytro Shteflyuk | f06db1b | 2025-11-20 18:09:21 -0500 | [diff] [blame] | 268 | expect { hello.greeting = 3 }.to raise_error(Thrift::TypeError, "Expected Types::STRING, received Integer for field greeting") |
| Kevin Clark | 2319375 | 2008-06-18 01:18:07 +0000 | [diff] [blame] | 269 | ensure |
| 270 | Thrift.type_checking = false |
| 271 | end |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 272 | expect { hello.greeting = 3 }.not_to raise_error |
| Kevin Clark | 2319375 | 2008-06-18 01:18:07 +0000 | [diff] [blame] | 273 | end |
| 274 | |
| 275 | it "should raise an exception when unknown types are given to Thrift::Struct.new" do |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 276 | expect { SpecNamespace::Hello.new(:fish => 'salmon') }.to raise_error(Exception, "Unknown key given to SpecNamespace::Hello.new: fish") |
| Kevin Clark | 2319375 | 2008-06-18 01:18:07 +0000 | [diff] [blame] | 277 | end |
| Kevin Clark | 3af9287 | 2008-07-28 22:20:36 +0000 | [diff] [blame] | 278 | |
| 279 | it "should support `raise Xception, 'message'` for Exception structs" do |
| 280 | begin |
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 281 | raise SpecNamespace::Xception, "something happened" |
| Kevin Clark | 3af9287 | 2008-07-28 22:20:36 +0000 | [diff] [blame] | 282 | rescue Thrift::Exception => e |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 283 | expect(e.message).to eq("something happened") |
| 284 | expect(e.code).to eq(1) |
| Kevin Clark | 3af9287 | 2008-07-28 22:20:36 +0000 | [diff] [blame] | 285 | # ensure it gets serialized properly, this is the really important part |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 286 | prot = Thrift::BaseProtocol.new(double("trans")) |
| 287 | expect(prot).to receive(:write_struct_begin).with("SpecNamespace::Xception") |
| 288 | expect(prot).to receive(:write_struct_end) |
| Dmytro Shteflyuk | 3b0ab4d | 2026-03-11 17:46:48 -0400 | [diff] [blame^] | 289 | expect(prot).to receive(:write_field_begin).with('message', Thrift::Types::STRING, 1) |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 290 | expect(prot).to receive(:write_string).with("something happened") |
| Dmytro Shteflyuk | 3b0ab4d | 2026-03-11 17:46:48 -0400 | [diff] [blame^] | 291 | expect(prot).to receive(:write_field_begin).with('code', Thrift::Types::I32, 2) |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 292 | expect(prot).to receive(:write_i32).with(1) |
| 293 | expect(prot).to receive(:write_field_stop) |
| 294 | expect(prot).to receive(:write_field_end).twice |
| Kevin Clark | 3af9287 | 2008-07-28 22:20:36 +0000 | [diff] [blame] | 295 | |
| 296 | e.write(prot) |
| 297 | end |
| 298 | end |
| 299 | |
| 300 | it "should support the regular initializer for exception structs" do |
| 301 | begin |
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 302 | raise SpecNamespace::Xception, :message => "something happened", :code => 5 |
| Kevin Clark | 3af9287 | 2008-07-28 22:20:36 +0000 | [diff] [blame] | 303 | rescue Thrift::Exception => e |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 304 | expect(e.message).to eq("something happened") |
| 305 | expect(e.code).to eq(5) |
| 306 | prot = Thrift::BaseProtocol.new(double("trans")) |
| 307 | expect(prot).to receive(:write_struct_begin).with("SpecNamespace::Xception") |
| 308 | expect(prot).to receive(:write_struct_end) |
| 309 | expect(prot).to receive(:write_field_begin).with('message', Thrift::Types::STRING, 1) |
| 310 | expect(prot).to receive(:write_string).with("something happened") |
| 311 | expect(prot).to receive(:write_field_begin).with('code', Thrift::Types::I32, 2) |
| 312 | expect(prot).to receive(:write_i32).with(5) |
| 313 | expect(prot).to receive(:write_field_stop) |
| 314 | expect(prot).to receive(:write_field_end).twice |
| Kevin Clark | 3af9287 | 2008-07-28 22:20:36 +0000 | [diff] [blame] | 315 | |
| 316 | e.write(prot) |
| 317 | end |
| 318 | end |
| Dmytro Shteflyuk | e9ac8e3 | 2025-11-19 23:33:23 -0500 | [diff] [blame] | 319 | |
| 320 | it "should handle UUID fields in structs" do |
| 321 | struct = SpecNamespace::Foo.new( |
| 322 | simple: 42, |
| 323 | words: 'test', |
| 324 | opt_uuid: '550e8400-e29b-41d4-a716-446655440000' |
| 325 | ) |
| 326 | |
| 327 | trans = Thrift::MemoryBufferTransport.new |
| 328 | prot = Thrift::BinaryProtocol.new(trans) |
| 329 | |
| 330 | struct.write(prot) |
| 331 | |
| 332 | result = SpecNamespace::Foo.new |
| 333 | result.read(prot) |
| 334 | |
| 335 | expect(result.simple).to eq(42) |
| 336 | expect(result.words).to eq('test') |
| 337 | expect(result.opt_uuid).to eq('550e8400-e29b-41d4-a716-446655440000') |
| 338 | end |
| 339 | |
| 340 | it "should handle optional UUID fields when unset" do |
| 341 | struct = SpecNamespace::Foo.new(simple: 42, words: 'test') |
| 342 | expect(struct.opt_uuid).to be_nil |
| 343 | expect(struct.opt_uuid?).to be_falsey |
| 344 | end |
| 345 | |
| 346 | it "should handle list of UUIDs in SimpleList" do |
| 347 | uuids = ['550e8400-e29b-41d4-a716-446655440000', '6ba7b810-9dad-11d1-80b4-00c04fd430c8'] |
| 348 | struct = SpecNamespace::SimpleList.new(uuids: uuids) |
| 349 | |
| 350 | trans = Thrift::MemoryBufferTransport.new |
| 351 | prot = Thrift::CompactProtocol.new(trans) |
| 352 | |
| 353 | struct.write(prot) |
| 354 | |
| 355 | result = SpecNamespace::SimpleList.new |
| 356 | result.read(prot) |
| 357 | |
| 358 | expect(result.uuids).to eq(uuids) |
| 359 | end |
| 360 | |
| 361 | it "should normalize UUID case to lowercase" do |
| 362 | struct = SpecNamespace::Foo.new(opt_uuid: '550E8400-E29B-41D4-A716-446655440000') |
| 363 | |
| 364 | trans = Thrift::MemoryBufferTransport.new |
| 365 | prot = Thrift::BinaryProtocol.new(trans) |
| 366 | |
| 367 | struct.write(prot) |
| 368 | |
| 369 | result = SpecNamespace::Foo.new |
| 370 | result.read(prot) |
| 371 | |
| 372 | expect(result.opt_uuid).to eq('550e8400-e29b-41d4-a716-446655440000') |
| 373 | end |
| 374 | |
| 375 | it "should handle UUID alongside other types in SimpleList" do |
| 376 | struct = SpecNamespace::SimpleList.new( |
| 377 | bools: [true, false], |
| 378 | i32s: [1, 2, 3], |
| 379 | strings: ['hello', 'world'], |
| 380 | uuids: ['550e8400-e29b-41d4-a716-446655440000', '00000000-0000-0000-0000-000000000000'] |
| 381 | ) |
| 382 | |
| 383 | trans = Thrift::MemoryBufferTransport.new |
| 384 | prot = Thrift::BinaryProtocol.new(trans) |
| 385 | |
| 386 | struct.write(prot) |
| 387 | |
| 388 | result = SpecNamespace::SimpleList.new |
| 389 | result.read(prot) |
| 390 | |
| 391 | expect(result.bools).to eq([true, false]) |
| 392 | expect(result.i32s).to eq([1, 2, 3]) |
| 393 | expect(result.strings).to eq(['hello', 'world']) |
| 394 | expect(result.uuids).to eq(['550e8400-e29b-41d4-a716-446655440000', '00000000-0000-0000-0000-000000000000']) |
| 395 | end |
| Kevin Clark | 090b69e | 2008-06-18 01:12:58 +0000 | [diff] [blame] | 396 | end |
| Kevin Clark | 03d7a47 | 2008-06-18 01:09:41 +0000 | [diff] [blame] | 397 | end |