blob: e45aa42d4af096e54356f6024a83a1a7bb33839f [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
Jake Farrella87810f2012-09-28 01:59:04 +000023 describe Thrift::Union do
Bryan Duxbury33e190c2010-02-16 21:19:01 +000024 it "should return nil value in unset union" do
Jake Farrella87810f2012-09-28 01:59:04 +000025 union = SpecNamespace::My_union.new
James E. King III27247072018-03-22 20:50:23 -040026 expect(union.get_set_field).to eq(nil)
27 expect(union.get_value).to eq(nil)
Bryan Duxbury33e190c2010-02-16 21:19:01 +000028 end
29
30 it "should set a field and be accessible through get_value and the named field accessor" do
Jake Farrella87810f2012-09-28 01:59:04 +000031 union = SpecNamespace::My_union.new
Bryan Duxbury33e190c2010-02-16 21:19:01 +000032 union.integer32 = 25
James E. King III27247072018-03-22 20:50:23 -040033 expect(union.get_set_field).to eq(:integer32)
34 expect(union.get_value).to eq(25)
35 expect(union.integer32).to eq(25)
Bryan Duxbury33e190c2010-02-16 21:19:01 +000036 end
37
38 it "should work correctly when instantiated with static field constructors" do
Jake Farrella87810f2012-09-28 01:59:04 +000039 union = SpecNamespace::My_union.integer32(5)
James E. King III27247072018-03-22 20:50:23 -040040 expect(union.get_set_field).to eq(:integer32)
41 expect(union.integer32).to eq(5)
Bryan Duxbury33e190c2010-02-16 21:19:01 +000042 end
43
44 it "should raise for wrong set field" do
Jake Farrella87810f2012-09-28 01:59:04 +000045 union = SpecNamespace::My_union.new
Bryan Duxbury33e190c2010-02-16 21:19:01 +000046 union.integer32 = 25
James E. King III27247072018-03-22 20:50:23 -040047 expect { union.some_characters }.to raise_error(RuntimeError, "some_characters is not union's set field.")
Bryan Duxbury33e190c2010-02-16 21:19:01 +000048 end
Jake Farrella87810f2012-09-28 01:59:04 +000049
Joe Ennever5b15f8c2015-08-31 19:20:36 +000050 it "should raise for wrong set field when hash initialized and type checking is off" do
51 Thrift.type_checking = false
52 union = SpecNamespace::My_union.new({incorrect_field: :incorrect})
Dmytro Shteflyuk32776c02026-02-10 12:25:07 -050053 expect { Thrift::Serializer.new.serialize(union) }.to raise_error(Thrift::ProtocolException, "set_field is not valid for this union!") { |error|
54 expect(error.type).to eq(Thrift::ProtocolException::INVALID_DATA)
55 }
Joe Ennever5b15f8c2015-08-31 19:20:36 +000056 end
57
Bryan Duxbury33e190c2010-02-16 21:19:01 +000058 it "should not be equal to nil" do
Jake Farrella87810f2012-09-28 01:59:04 +000059 union = SpecNamespace::My_union.new
James E. King III27247072018-03-22 20:50:23 -040060 expect(union).not_to eq(nil)
Bryan Duxbury33e190c2010-02-16 21:19:01 +000061 end
Jake Farrella87810f2012-09-28 01:59:04 +000062
Roger Meier56d38fb2015-06-01 22:01:09 +020063 it "should not be equal with an empty String" do
64 union = SpecNamespace::My_union.new
James E. King III27247072018-03-22 20:50:23 -040065 expect(union).not_to eq('')
Roger Meier56d38fb2015-06-01 22:01:09 +020066 end
67
Bryan Duxbury33e190c2010-02-16 21:19:01 +000068 it "should not equate two different unions, i32 vs. string" do
Jake Farrella87810f2012-09-28 01:59:04 +000069 union = SpecNamespace::My_union.new(:integer32, 25)
70 other_union = SpecNamespace::My_union.new(:some_characters, "blah!")
James E. King III27247072018-03-22 20:50:23 -040071 expect(union).not_to eq(other_union)
Bryan Duxbury33e190c2010-02-16 21:19:01 +000072 end
73
74 it "should properly reset setfield and setvalue" do
Jake Farrella87810f2012-09-28 01:59:04 +000075 union = SpecNamespace::My_union.new(:integer32, 25)
James E. King III27247072018-03-22 20:50:23 -040076 expect(union.get_set_field).to eq(:integer32)
Bryan Duxbury33e190c2010-02-16 21:19:01 +000077 union.some_characters = "blah!"
James E. King III27247072018-03-22 20:50:23 -040078 expect(union.get_set_field).to eq(:some_characters)
79 expect(union.get_value).to eq("blah!")
80 expect { union.integer32 }.to raise_error(RuntimeError, "integer32 is not union's set field.")
Bryan Duxbury33e190c2010-02-16 21:19:01 +000081 end
82
83 it "should not equate two different unions with different values" do
Jake Farrella87810f2012-09-28 01:59:04 +000084 union = SpecNamespace::My_union.new(:integer32, 25)
85 other_union = SpecNamespace::My_union.new(:integer32, 400)
James E. King III27247072018-03-22 20:50:23 -040086 expect(union).not_to eq(other_union)
Bryan Duxbury33e190c2010-02-16 21:19:01 +000087 end
88
89 it "should not equate two different unions with different fields" do
Jake Farrella87810f2012-09-28 01:59:04 +000090 union = SpecNamespace::My_union.new(:integer32, 25)
91 other_union = SpecNamespace::My_union.new(:other_i32, 25)
James E. King III27247072018-03-22 20:50:23 -040092 expect(union).not_to eq(other_union)
Bryan Duxbury33e190c2010-02-16 21:19:01 +000093 end
94
Dmytro Shteflyuke9ac8e32025-11-19 23:33:23 -050095 it "should equate two unions with the same UUID value" do
96 union = SpecNamespace::My_union.new(:unique_id, '550e8400-e29b-41d4-a716-446655440000')
97 other_union = SpecNamespace::My_union.new(:unique_id, '550e8400-e29b-41d4-a716-446655440000')
98 expect(union).to eq(other_union)
99 end
100
101 it "should not equate two unions with different UUID values" do
102 union = SpecNamespace::My_union.new(:unique_id, '550e8400-e29b-41d4-a716-446655440000')
103 other_union = SpecNamespace::My_union.new(:unique_id, '6ba7b810-9dad-11d1-80b4-00c04fd430c8')
104 expect(union).not_to eq(other_union)
105 end
106
107 it "should not equate UUID union with different field type" do
108 union = SpecNamespace::My_union.new(:unique_id, '550e8400-e29b-41d4-a716-446655440000')
109 other_union = SpecNamespace::My_union.new(:some_characters, '550e8400-e29b-41d4-a716-446655440000')
110 expect(union).not_to eq(other_union)
111 end
112
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000113 it "should inspect properly" do
Jake Farrella87810f2012-09-28 01:59:04 +0000114 union = SpecNamespace::My_union.new(:integer32, 25)
James E. King III27247072018-03-22 20:50:23 -0400115 expect(union.inspect).to eq("<SpecNamespace::My_union integer32: 25>")
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000116 end
117
118 it "should not allow setting with instance_variable_set" do
Jake Farrella87810f2012-09-28 01:59:04 +0000119 union = SpecNamespace::My_union.new(:integer32, 27)
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000120 union.instance_variable_set(:@some_characters, "hallo!")
James E. King III27247072018-03-22 20:50:23 -0400121 expect(union.get_set_field).to eq(:integer32)
122 expect(union.get_value).to eq(27)
123 expect { union.some_characters }.to raise_error(RuntimeError, "some_characters is not union's set field.")
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000124 end
125
Jens Geyera9e33bf2012-12-12 23:11:04 +0100126 it "should serialize to binary correctly" do
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000127 trans = Thrift::MemoryBufferTransport.new
128 proto = Thrift::BinaryProtocol.new(trans)
129
Jake Farrella87810f2012-09-28 01:59:04 +0000130 union = SpecNamespace::My_union.new(:integer32, 25)
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000131 union.write(proto)
132
Jake Farrella87810f2012-09-28 01:59:04 +0000133 other_union = SpecNamespace::My_union.new(:integer32, 25)
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000134 other_union.read(proto)
James E. King III27247072018-03-22 20:50:23 -0400135 expect(other_union).to eq(union)
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000136 end
137
Jens Geyera9e33bf2012-12-12 23:11:04 +0100138 it "should serialize to json correctly" do
139 trans = Thrift::MemoryBufferTransport.new
140 proto = Thrift::JsonProtocol.new(trans)
141
142 union = SpecNamespace::My_union.new(:integer32, 25)
143 union.write(proto)
144
145 other_union = SpecNamespace::My_union.new(:integer32, 25)
146 other_union.read(proto)
James E. King III27247072018-03-22 20:50:23 -0400147 expect(other_union).to eq(union)
Jens Geyera9e33bf2012-12-12 23:11:04 +0100148 end
149
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000150 it "should raise when validating unset union" do
Jake Farrella87810f2012-09-28 01:59:04 +0000151 union = SpecNamespace::My_union.new
Dmytro Shteflyuk32776c02026-02-10 12:25:07 -0500152 expect { union.validate }.to raise_error(Thrift::ProtocolException, "Union fields are not set.") { |error|
153 expect(error.type).to eq(Thrift::ProtocolException::INVALID_DATA)
154 }
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000155
Jake Farrella87810f2012-09-28 01:59:04 +0000156 other_union = SpecNamespace::My_union.new(:integer32, 1)
James E. King III27247072018-03-22 20:50:23 -0400157 expect { other_union.validate }.not_to raise_error
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000158 end
159
160 it "should validate an enum field properly" do
Jake Farrella87810f2012-09-28 01:59:04 +0000161 union = SpecNamespace::TestUnion.new(:enum_field, 3)
James E. King III27247072018-03-22 20:50:23 -0400162 expect(union.get_set_field).to eq(:enum_field)
Dmytro Shteflyuk32776c02026-02-10 12:25:07 -0500163 expect { union.validate }.to raise_error(Thrift::ProtocolException, "Invalid value of field enum_field!") { |error|
164 expect(error.type).to eq(Thrift::ProtocolException::INVALID_DATA)
165 }
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000166
Jake Farrella87810f2012-09-28 01:59:04 +0000167 other_union = SpecNamespace::TestUnion.new(:enum_field, 1)
James E. King III27247072018-03-22 20:50:23 -0400168 expect { other_union.validate }.not_to raise_error
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000169 end
170
171 it "should properly serialize and match structs with a union" do
Jake Farrella87810f2012-09-28 01:59:04 +0000172 union = SpecNamespace::My_union.new(:integer32, 26)
173 swu = SpecNamespace::Struct_with_union.new(:fun_union => union)
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000174
175 trans = Thrift::MemoryBufferTransport.new
176 proto = Thrift::CompactProtocol.new(trans)
177
178 swu.write(proto)
179
Jake Farrella87810f2012-09-28 01:59:04 +0000180 other_union = SpecNamespace::My_union.new(:some_characters, "hello there")
181 swu2 = SpecNamespace::Struct_with_union.new(:fun_union => other_union)
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000182
James E. King III27247072018-03-22 20:50:23 -0400183 expect(swu2).not_to eq(swu)
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000184
185 swu2.read(proto)
James E. King III27247072018-03-22 20:50:23 -0400186 expect(swu2).to eq(swu)
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000187 end
Bryan Duxbury205e4502010-02-18 23:19:42 +0000188
Bryan Duxbury5af64f02010-02-17 22:27:27 +0000189 it "should support old style constructor" do
Jake Farrella87810f2012-09-28 01:59:04 +0000190 union = SpecNamespace::My_union.new(:integer32 => 26)
James E. King III27247072018-03-22 20:50:23 -0400191 expect(union.get_set_field).to eq(:integer32)
192 expect(union.get_value).to eq(26)
Bryan Duxbury5af64f02010-02-17 22:27:27 +0000193 end
Bryan Duxbury205e4502010-02-18 23:19:42 +0000194
195 it "should not throw an error when inspected and unset" do
Dmytro Shteflyuk3b0ab4d2026-03-11 17:46:48 -0400196 expect{ SpecNamespace::TestUnion.new().inspect }.not_to raise_error
Bryan Duxbury205e4502010-02-18 23:19:42 +0000197 end
198
Bryan Duxbury3d03c522010-02-18 17:42:06 +0000199 it "should print enum value name when inspected" do
James E. King III27247072018-03-22 20:50:23 -0400200 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 +0000201
James E. King III27247072018-03-22 20:50:23 -0400202 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 +0000203 end
Bryan Duxbury205e4502010-02-18 23:19:42 +0000204
Bryan Duxbury0e4920c2010-02-18 20:28:27 +0000205 it "should offer field? methods" do
James E. King III27247072018-03-22 20:50:23 -0400206 expect(SpecNamespace::My_union.new.some_enum?).to be_falsey
207 expect(SpecNamespace::My_union.new(:some_enum => SpecNamespace::SomeEnum::ONE).some_enum?).to be_truthy
208 expect(SpecNamespace::My_union.new(:im_true => false).im_true?).to be_truthy
209 expect(SpecNamespace::My_union.new(:im_true => true).im_true?).to be_truthy
Bryan Duxbury0e4920c2010-02-18 20:28:27 +0000210 end
Bryan Duxbury205e4502010-02-18 23:19:42 +0000211
Bryan Duxbury39dadd62010-02-18 22:00:45 +0000212 it "should pretty print binary fields" do
James E. King III27247072018-03-22 20:50:23 -0400213 expect(SpecNamespace::TestUnion.new(:binary_field => "\001\002\003").inspect).to eq("<SpecNamespace::TestUnion binary_field: 010203>")
Bryan Duxbury39dadd62010-02-18 22:00:45 +0000214 end
Bryan Duxbury205e4502010-02-18 23:19:42 +0000215
216 it "should be comparable" do
217 relationships = [
Dmytro Shteflyuke9ac8e32025-11-19 23:33:23 -0500218 [0, -1, -1, -1, -1, -1],
219 [1, 0, -1, -1, -1, -1],
220 [1, 1, 0, -1, -1, -1],
221 [1, 1, 1, 0, -1, -1],
222 [1, 1, 1, 1, 0, -1],
223 [1, 1, 1, 1, 1, 0]]
Bryan Duxbury205e4502010-02-18 23:19:42 +0000224
225 objs = [
Jake Farrella87810f2012-09-28 01:59:04 +0000226 SpecNamespace::TestUnion.new(:string_field, "blah"),
227 SpecNamespace::TestUnion.new(:string_field, "blahblah"),
228 SpecNamespace::TestUnion.new(:i32_field, 1),
Dmytro Shteflyuke9ac8e32025-11-19 23:33:23 -0500229 SpecNamespace::TestUnion.new(:uuid_field, '550e8400-e29b-41d4-a716-446655440000'),
230 SpecNamespace::TestUnion.new(:uuid_field, '6ba7b810-9dad-11d1-80b4-00c04fd430c8'),
Jake Farrella87810f2012-09-28 01:59:04 +0000231 SpecNamespace::TestUnion.new()]
Bryan Duxbury205e4502010-02-18 23:19:42 +0000232
Dmytro Shteflyuke9ac8e32025-11-19 23:33:23 -0500233 objs.size.times do |y|
234 objs.size.times do |x|
Bryan Duxbury205e4502010-02-18 23:19:42 +0000235 # puts "#{objs[y].inspect} <=> #{objs[x].inspect} should == #{relationships[y][x]}"
James E. King III27247072018-03-22 20:50:23 -0400236 expect(objs[y] <=> objs[x]).to eq(relationships[y][x])
Bryan Duxbury205e4502010-02-18 23:19:42 +0000237 end
238 end
239 end
Dmytro Shteflyuke9ac8e32025-11-19 23:33:23 -0500240
241 it "should handle UUID as union value" do
242 union = SpecNamespace::My_union.new
243 union.unique_id = 'ffffffff-ffff-ffff-ffff-ffffffffffff'
244
245 trans = Thrift::MemoryBufferTransport.new
246 prot = Thrift::CompactProtocol.new(trans)
247
248 union.write(prot)
249
250 result = SpecNamespace::My_union.new
251 result.read(prot)
252
253 expect(result.unique_id).to eq('ffffffff-ffff-ffff-ffff-ffffffffffff')
254 expect(result.get_set_field).to eq(:unique_id)
255 end
256
257 it "should normalize UUID case in union" do
258 union = SpecNamespace::My_union.new
259 union.unique_id = '550E8400-E29B-41D4-A716-446655440000'
260
261 trans = Thrift::MemoryBufferTransport.new
262 prot = Thrift::BinaryProtocol.new(trans)
263
264 union.write(prot)
265
266 result = SpecNamespace::My_union.new
267 result.read(prot)
268
269 expect(result.unique_id).to eq('550e8400-e29b-41d4-a716-446655440000')
270 end
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000271 end
272end