blob: f38093d73077de51a2641171bfc97e481b6433bb [file] [log] [blame]
Bryan Duxbury33e190c2010-02-16 21:19:01 +00001#
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 Farrella87810f2012-09-28 01:59:04 +000020require 'spec_helper'
Bryan Duxbury33e190c2010-02-16 21:19:01 +000021
Jake Farrella87810f2012-09-28 01:59:04 +000022describe 'Union' do
Bryan Duxbury33e190c2010-02-16 21:19:01 +000023
Jake Farrella87810f2012-09-28 01:59:04 +000024 describe Thrift::Union do
Bryan Duxbury33e190c2010-02-16 21:19:01 +000025 it "should return nil value in unset union" do
Jake Farrella87810f2012-09-28 01:59:04 +000026 union = SpecNamespace::My_union.new
James E. King III27247072018-03-22 20:50:23 -040027 expect(union.get_set_field).to eq(nil)
28 expect(union.get_value).to eq(nil)
Bryan Duxbury33e190c2010-02-16 21:19:01 +000029 end
30
31 it "should set a field and be accessible through get_value and the named field accessor" do
Jake Farrella87810f2012-09-28 01:59:04 +000032 union = SpecNamespace::My_union.new
Bryan Duxbury33e190c2010-02-16 21:19:01 +000033 union.integer32 = 25
James E. King III27247072018-03-22 20:50:23 -040034 expect(union.get_set_field).to eq(:integer32)
35 expect(union.get_value).to eq(25)
36 expect(union.integer32).to eq(25)
Bryan Duxbury33e190c2010-02-16 21:19:01 +000037 end
38
39 it "should work correctly when instantiated with static field constructors" do
Jake Farrella87810f2012-09-28 01:59:04 +000040 union = SpecNamespace::My_union.integer32(5)
James E. King III27247072018-03-22 20:50:23 -040041 expect(union.get_set_field).to eq(:integer32)
42 expect(union.integer32).to eq(5)
Bryan Duxbury33e190c2010-02-16 21:19:01 +000043 end
44
45 it "should raise for wrong set field" do
Jake Farrella87810f2012-09-28 01:59:04 +000046 union = SpecNamespace::My_union.new
Bryan Duxbury33e190c2010-02-16 21:19:01 +000047 union.integer32 = 25
James E. King III27247072018-03-22 20:50:23 -040048 expect { union.some_characters }.to raise_error(RuntimeError, "some_characters is not union's set field.")
Bryan Duxbury33e190c2010-02-16 21:19:01 +000049 end
Jake Farrella87810f2012-09-28 01:59:04 +000050
Joe Ennever5b15f8c2015-08-31 19:20:36 +000051 it "should raise for wrong set field when hash initialized and type checking is off" do
52 Thrift.type_checking = false
53 union = SpecNamespace::My_union.new({incorrect_field: :incorrect})
Dmytro Shteflyuk32776c02026-02-10 12:25:07 -050054 expect { Thrift::Serializer.new.serialize(union) }.to raise_error(Thrift::ProtocolException, "set_field is not valid for this union!") { |error|
55 expect(error.type).to eq(Thrift::ProtocolException::INVALID_DATA)
56 }
Joe Ennever5b15f8c2015-08-31 19:20:36 +000057 end
58
Bryan Duxbury33e190c2010-02-16 21:19:01 +000059 it "should not be equal to nil" do
Jake Farrella87810f2012-09-28 01:59:04 +000060 union = SpecNamespace::My_union.new
James E. King III27247072018-03-22 20:50:23 -040061 expect(union).not_to eq(nil)
Bryan Duxbury33e190c2010-02-16 21:19:01 +000062 end
Jake Farrella87810f2012-09-28 01:59:04 +000063
Roger Meier56d38fb2015-06-01 22:01:09 +020064 it "should not be equal with an empty String" do
65 union = SpecNamespace::My_union.new
James E. King III27247072018-03-22 20:50:23 -040066 expect(union).not_to eq('')
Roger Meier56d38fb2015-06-01 22:01:09 +020067 end
68
Bryan Duxbury33e190c2010-02-16 21:19:01 +000069 it "should not equate two different unions, i32 vs. string" do
Jake Farrella87810f2012-09-28 01:59:04 +000070 union = SpecNamespace::My_union.new(:integer32, 25)
71 other_union = SpecNamespace::My_union.new(:some_characters, "blah!")
James E. King III27247072018-03-22 20:50:23 -040072 expect(union).not_to eq(other_union)
Bryan Duxbury33e190c2010-02-16 21:19:01 +000073 end
74
75 it "should properly reset setfield and setvalue" do
Jake Farrella87810f2012-09-28 01:59:04 +000076 union = SpecNamespace::My_union.new(:integer32, 25)
James E. King III27247072018-03-22 20:50:23 -040077 expect(union.get_set_field).to eq(:integer32)
Bryan Duxbury33e190c2010-02-16 21:19:01 +000078 union.some_characters = "blah!"
James E. King III27247072018-03-22 20:50:23 -040079 expect(union.get_set_field).to eq(:some_characters)
80 expect(union.get_value).to eq("blah!")
81 expect { union.integer32 }.to raise_error(RuntimeError, "integer32 is not union's set field.")
Bryan Duxbury33e190c2010-02-16 21:19:01 +000082 end
83
84 it "should not equate two different unions with different values" do
Jake Farrella87810f2012-09-28 01:59:04 +000085 union = SpecNamespace::My_union.new(:integer32, 25)
86 other_union = SpecNamespace::My_union.new(:integer32, 400)
James E. King III27247072018-03-22 20:50:23 -040087 expect(union).not_to eq(other_union)
Bryan Duxbury33e190c2010-02-16 21:19:01 +000088 end
89
90 it "should not equate two different unions with different fields" do
Jake Farrella87810f2012-09-28 01:59:04 +000091 union = SpecNamespace::My_union.new(:integer32, 25)
92 other_union = SpecNamespace::My_union.new(:other_i32, 25)
James E. King III27247072018-03-22 20:50:23 -040093 expect(union).not_to eq(other_union)
Bryan Duxbury33e190c2010-02-16 21:19:01 +000094 end
95
Dmytro Shteflyuke9ac8e32025-11-19 23:33:23 -050096 it "should equate two unions with the same UUID value" do
97 union = SpecNamespace::My_union.new(:unique_id, '550e8400-e29b-41d4-a716-446655440000')
98 other_union = SpecNamespace::My_union.new(:unique_id, '550e8400-e29b-41d4-a716-446655440000')
99 expect(union).to eq(other_union)
100 end
101
102 it "should not equate two unions with different UUID values" do
103 union = SpecNamespace::My_union.new(:unique_id, '550e8400-e29b-41d4-a716-446655440000')
104 other_union = SpecNamespace::My_union.new(:unique_id, '6ba7b810-9dad-11d1-80b4-00c04fd430c8')
105 expect(union).not_to eq(other_union)
106 end
107
108 it "should not equate UUID union with different field type" do
109 union = SpecNamespace::My_union.new(:unique_id, '550e8400-e29b-41d4-a716-446655440000')
110 other_union = SpecNamespace::My_union.new(:some_characters, '550e8400-e29b-41d4-a716-446655440000')
111 expect(union).not_to eq(other_union)
112 end
113
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000114 it "should inspect properly" do
Jake Farrella87810f2012-09-28 01:59:04 +0000115 union = SpecNamespace::My_union.new(:integer32, 25)
James E. King III27247072018-03-22 20:50:23 -0400116 expect(union.inspect).to eq("<SpecNamespace::My_union integer32: 25>")
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000117 end
118
119 it "should not allow setting with instance_variable_set" do
Jake Farrella87810f2012-09-28 01:59:04 +0000120 union = SpecNamespace::My_union.new(:integer32, 27)
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000121 union.instance_variable_set(:@some_characters, "hallo!")
James E. King III27247072018-03-22 20:50:23 -0400122 expect(union.get_set_field).to eq(:integer32)
123 expect(union.get_value).to eq(27)
124 expect { union.some_characters }.to raise_error(RuntimeError, "some_characters is not union's set field.")
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000125 end
126
Jens Geyera9e33bf2012-12-12 23:11:04 +0100127 it "should serialize to binary correctly" do
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000128 trans = Thrift::MemoryBufferTransport.new
129 proto = Thrift::BinaryProtocol.new(trans)
130
Jake Farrella87810f2012-09-28 01:59:04 +0000131 union = SpecNamespace::My_union.new(:integer32, 25)
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000132 union.write(proto)
133
Jake Farrella87810f2012-09-28 01:59:04 +0000134 other_union = SpecNamespace::My_union.new(:integer32, 25)
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000135 other_union.read(proto)
James E. King III27247072018-03-22 20:50:23 -0400136 expect(other_union).to eq(union)
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000137 end
138
Jens Geyera9e33bf2012-12-12 23:11:04 +0100139 it "should serialize to json correctly" do
140 trans = Thrift::MemoryBufferTransport.new
141 proto = Thrift::JsonProtocol.new(trans)
142
143 union = SpecNamespace::My_union.new(:integer32, 25)
144 union.write(proto)
145
146 other_union = SpecNamespace::My_union.new(:integer32, 25)
147 other_union.read(proto)
James E. King III27247072018-03-22 20:50:23 -0400148 expect(other_union).to eq(union)
Jens Geyera9e33bf2012-12-12 23:11:04 +0100149 end
150
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000151 it "should raise when validating unset union" do
Jake Farrella87810f2012-09-28 01:59:04 +0000152 union = SpecNamespace::My_union.new
Dmytro Shteflyuk32776c02026-02-10 12:25:07 -0500153 expect { union.validate }.to raise_error(Thrift::ProtocolException, "Union fields are not set.") { |error|
154 expect(error.type).to eq(Thrift::ProtocolException::INVALID_DATA)
155 }
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000156
Jake Farrella87810f2012-09-28 01:59:04 +0000157 other_union = SpecNamespace::My_union.new(:integer32, 1)
James E. King III27247072018-03-22 20:50:23 -0400158 expect { other_union.validate }.not_to raise_error
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000159 end
160
161 it "should validate an enum field properly" do
Jake Farrella87810f2012-09-28 01:59:04 +0000162 union = SpecNamespace::TestUnion.new(:enum_field, 3)
James E. King III27247072018-03-22 20:50:23 -0400163 expect(union.get_set_field).to eq(:enum_field)
Dmytro Shteflyuk32776c02026-02-10 12:25:07 -0500164 expect { union.validate }.to raise_error(Thrift::ProtocolException, "Invalid value of field enum_field!") { |error|
165 expect(error.type).to eq(Thrift::ProtocolException::INVALID_DATA)
166 }
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000167
Jake Farrella87810f2012-09-28 01:59:04 +0000168 other_union = SpecNamespace::TestUnion.new(:enum_field, 1)
James E. King III27247072018-03-22 20:50:23 -0400169 expect { other_union.validate }.not_to raise_error
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000170 end
171
172 it "should properly serialize and match structs with a union" do
Jake Farrella87810f2012-09-28 01:59:04 +0000173 union = SpecNamespace::My_union.new(:integer32, 26)
174 swu = SpecNamespace::Struct_with_union.new(:fun_union => union)
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000175
176 trans = Thrift::MemoryBufferTransport.new
177 proto = Thrift::CompactProtocol.new(trans)
178
179 swu.write(proto)
180
Jake Farrella87810f2012-09-28 01:59:04 +0000181 other_union = SpecNamespace::My_union.new(:some_characters, "hello there")
182 swu2 = SpecNamespace::Struct_with_union.new(:fun_union => other_union)
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000183
James E. King III27247072018-03-22 20:50:23 -0400184 expect(swu2).not_to eq(swu)
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000185
186 swu2.read(proto)
James E. King III27247072018-03-22 20:50:23 -0400187 expect(swu2).to eq(swu)
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000188 end
Bryan Duxbury205e4502010-02-18 23:19:42 +0000189
Bryan Duxbury5af64f02010-02-17 22:27:27 +0000190 it "should support old style constructor" do
Jake Farrella87810f2012-09-28 01:59:04 +0000191 union = SpecNamespace::My_union.new(:integer32 => 26)
James E. King III27247072018-03-22 20:50:23 -0400192 expect(union.get_set_field).to eq(:integer32)
193 expect(union.get_value).to eq(26)
Bryan Duxbury5af64f02010-02-17 22:27:27 +0000194 end
Bryan Duxbury205e4502010-02-18 23:19:42 +0000195
196 it "should not throw an error when inspected and unset" do
James E. King III27247072018-03-22 20:50:23 -0400197 expect{SpecNamespace::TestUnion.new().inspect}.not_to raise_error
Bryan Duxbury205e4502010-02-18 23:19:42 +0000198 end
199
Bryan Duxbury3d03c522010-02-18 17:42:06 +0000200 it "should print enum value name when inspected" do
James E. King III27247072018-03-22 20:50:23 -0400201 expect(SpecNamespace::My_union.new(:some_enum => SpecNamespace::SomeEnum::ONE).inspect).to eq("<SpecNamespace::My_union some_enum: ONE (0)>")
Bryan Duxbury205e4502010-02-18 23:19:42 +0000202
James E. King III27247072018-03-22 20:50:23 -0400203 expect(SpecNamespace::My_union.new(:my_map => {SpecNamespace::SomeEnum::ONE => [SpecNamespace::SomeEnum::TWO]}).inspect).to eq("<SpecNamespace::My_union my_map: {ONE (0): [TWO (1)]}>")
Bryan Duxbury3d03c522010-02-18 17:42:06 +0000204 end
Bryan Duxbury205e4502010-02-18 23:19:42 +0000205
Bryan Duxbury0e4920c2010-02-18 20:28:27 +0000206 it "should offer field? methods" do
James E. King III27247072018-03-22 20:50:23 -0400207 expect(SpecNamespace::My_union.new.some_enum?).to be_falsey
208 expect(SpecNamespace::My_union.new(:some_enum => SpecNamespace::SomeEnum::ONE).some_enum?).to be_truthy
209 expect(SpecNamespace::My_union.new(:im_true => false).im_true?).to be_truthy
210 expect(SpecNamespace::My_union.new(:im_true => true).im_true?).to be_truthy
Bryan Duxbury0e4920c2010-02-18 20:28:27 +0000211 end
Bryan Duxbury205e4502010-02-18 23:19:42 +0000212
Bryan Duxbury39dadd62010-02-18 22:00:45 +0000213 it "should pretty print binary fields" do
James E. King III27247072018-03-22 20:50:23 -0400214 expect(SpecNamespace::TestUnion.new(:binary_field => "\001\002\003").inspect).to eq("<SpecNamespace::TestUnion binary_field: 010203>")
Bryan Duxbury39dadd62010-02-18 22:00:45 +0000215 end
Bryan Duxbury205e4502010-02-18 23:19:42 +0000216
217 it "should be comparable" do
218 relationships = [
Dmytro Shteflyuke9ac8e32025-11-19 23:33:23 -0500219 [0, -1, -1, -1, -1, -1],
220 [1, 0, -1, -1, -1, -1],
221 [1, 1, 0, -1, -1, -1],
222 [1, 1, 1, 0, -1, -1],
223 [1, 1, 1, 1, 0, -1],
224 [1, 1, 1, 1, 1, 0]]
Bryan Duxbury205e4502010-02-18 23:19:42 +0000225
226 objs = [
Jake Farrella87810f2012-09-28 01:59:04 +0000227 SpecNamespace::TestUnion.new(:string_field, "blah"),
228 SpecNamespace::TestUnion.new(:string_field, "blahblah"),
229 SpecNamespace::TestUnion.new(:i32_field, 1),
Dmytro Shteflyuke9ac8e32025-11-19 23:33:23 -0500230 SpecNamespace::TestUnion.new(:uuid_field, '550e8400-e29b-41d4-a716-446655440000'),
231 SpecNamespace::TestUnion.new(:uuid_field, '6ba7b810-9dad-11d1-80b4-00c04fd430c8'),
Jake Farrella87810f2012-09-28 01:59:04 +0000232 SpecNamespace::TestUnion.new()]
Bryan Duxbury205e4502010-02-18 23:19:42 +0000233
Dmytro Shteflyuke9ac8e32025-11-19 23:33:23 -0500234 objs.size.times do |y|
235 objs.size.times do |x|
Bryan Duxbury205e4502010-02-18 23:19:42 +0000236 # puts "#{objs[y].inspect} <=> #{objs[x].inspect} should == #{relationships[y][x]}"
James E. King III27247072018-03-22 20:50:23 -0400237 expect(objs[y] <=> objs[x]).to eq(relationships[y][x])
Bryan Duxbury205e4502010-02-18 23:19:42 +0000238 end
239 end
240 end
Dmytro Shteflyuke9ac8e32025-11-19 23:33:23 -0500241
242 it "should handle UUID as union value" do
243 union = SpecNamespace::My_union.new
244 union.unique_id = 'ffffffff-ffff-ffff-ffff-ffffffffffff'
245
246 trans = Thrift::MemoryBufferTransport.new
247 prot = Thrift::CompactProtocol.new(trans)
248
249 union.write(prot)
250
251 result = SpecNamespace::My_union.new
252 result.read(prot)
253
254 expect(result.unique_id).to eq('ffffffff-ffff-ffff-ffff-ffffffffffff')
255 expect(result.get_set_field).to eq(:unique_id)
256 end
257
258 it "should normalize UUID case in union" do
259 union = SpecNamespace::My_union.new
260 union.unique_id = '550E8400-E29B-41D4-A716-446655440000'
261
262 trans = Thrift::MemoryBufferTransport.new
263 prot = Thrift::BinaryProtocol.new(trans)
264
265 union.write(prot)
266
267 result = SpecNamespace::My_union.new
268 result.read(prot)
269
270 expect(result.unique_id).to eq('550e8400-e29b-41d4-a716-446655440000')
271 end
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000272 end
273end