blob: 7bf4ca04091b6a2c4a584c9d8b3895206e6cc5da [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})
Kengo Seki5a8cd9d2019-10-06 00:13:14 +090054 expect { Thrift::Serializer.new.serialize(union) }.to raise_error(RuntimeError, "set_field is not valid for this union!")
Joe Ennever5b15f8c2015-08-31 19:20:36 +000055 end
56
Bryan Duxbury33e190c2010-02-16 21:19:01 +000057 it "should not be equal to nil" do
Jake Farrella87810f2012-09-28 01:59:04 +000058 union = SpecNamespace::My_union.new
James E. King III27247072018-03-22 20:50:23 -040059 expect(union).not_to eq(nil)
Bryan Duxbury33e190c2010-02-16 21:19:01 +000060 end
Jake Farrella87810f2012-09-28 01:59:04 +000061
Roger Meier56d38fb2015-06-01 22:01:09 +020062 it "should not be equal with an empty String" do
63 union = SpecNamespace::My_union.new
James E. King III27247072018-03-22 20:50:23 -040064 expect(union).not_to eq('')
Roger Meier56d38fb2015-06-01 22:01:09 +020065 end
66
Bryan Duxbury33e190c2010-02-16 21:19:01 +000067 it "should not equate two different unions, i32 vs. string" do
Jake Farrella87810f2012-09-28 01:59:04 +000068 union = SpecNamespace::My_union.new(:integer32, 25)
69 other_union = SpecNamespace::My_union.new(:some_characters, "blah!")
James E. King III27247072018-03-22 20:50:23 -040070 expect(union).not_to eq(other_union)
Bryan Duxbury33e190c2010-02-16 21:19:01 +000071 end
72
73 it "should properly reset setfield and setvalue" do
Jake Farrella87810f2012-09-28 01:59:04 +000074 union = SpecNamespace::My_union.new(:integer32, 25)
James E. King III27247072018-03-22 20:50:23 -040075 expect(union.get_set_field).to eq(:integer32)
Bryan Duxbury33e190c2010-02-16 21:19:01 +000076 union.some_characters = "blah!"
James E. King III27247072018-03-22 20:50:23 -040077 expect(union.get_set_field).to eq(:some_characters)
78 expect(union.get_value).to eq("blah!")
79 expect { union.integer32 }.to raise_error(RuntimeError, "integer32 is not union's set field.")
Bryan Duxbury33e190c2010-02-16 21:19:01 +000080 end
81
82 it "should not equate two different unions with different values" do
Jake Farrella87810f2012-09-28 01:59:04 +000083 union = SpecNamespace::My_union.new(:integer32, 25)
84 other_union = SpecNamespace::My_union.new(:integer32, 400)
James E. King III27247072018-03-22 20:50:23 -040085 expect(union).not_to eq(other_union)
Bryan Duxbury33e190c2010-02-16 21:19:01 +000086 end
87
88 it "should not equate two different unions with different fields" do
Jake Farrella87810f2012-09-28 01:59:04 +000089 union = SpecNamespace::My_union.new(:integer32, 25)
90 other_union = SpecNamespace::My_union.new(:other_i32, 25)
James E. King III27247072018-03-22 20:50:23 -040091 expect(union).not_to eq(other_union)
Bryan Duxbury33e190c2010-02-16 21:19:01 +000092 end
93
Dmytro Shteflyuke9ac8e32025-11-19 23:33:23 -050094 it "should equate two unions with the same UUID value" do
95 union = SpecNamespace::My_union.new(:unique_id, '550e8400-e29b-41d4-a716-446655440000')
96 other_union = SpecNamespace::My_union.new(:unique_id, '550e8400-e29b-41d4-a716-446655440000')
97 expect(union).to eq(other_union)
98 end
99
100 it "should not equate two unions with different UUID values" do
101 union = SpecNamespace::My_union.new(:unique_id, '550e8400-e29b-41d4-a716-446655440000')
102 other_union = SpecNamespace::My_union.new(:unique_id, '6ba7b810-9dad-11d1-80b4-00c04fd430c8')
103 expect(union).not_to eq(other_union)
104 end
105
106 it "should not equate UUID union with different field type" do
107 union = SpecNamespace::My_union.new(:unique_id, '550e8400-e29b-41d4-a716-446655440000')
108 other_union = SpecNamespace::My_union.new(:some_characters, '550e8400-e29b-41d4-a716-446655440000')
109 expect(union).not_to eq(other_union)
110 end
111
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000112 it "should inspect properly" do
Jake Farrella87810f2012-09-28 01:59:04 +0000113 union = SpecNamespace::My_union.new(:integer32, 25)
James E. King III27247072018-03-22 20:50:23 -0400114 expect(union.inspect).to eq("<SpecNamespace::My_union integer32: 25>")
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000115 end
116
117 it "should not allow setting with instance_variable_set" do
Jake Farrella87810f2012-09-28 01:59:04 +0000118 union = SpecNamespace::My_union.new(:integer32, 27)
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000119 union.instance_variable_set(:@some_characters, "hallo!")
James E. King III27247072018-03-22 20:50:23 -0400120 expect(union.get_set_field).to eq(:integer32)
121 expect(union.get_value).to eq(27)
122 expect { union.some_characters }.to raise_error(RuntimeError, "some_characters is not union's set field.")
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000123 end
124
Jens Geyera9e33bf2012-12-12 23:11:04 +0100125 it "should serialize to binary correctly" do
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000126 trans = Thrift::MemoryBufferTransport.new
127 proto = Thrift::BinaryProtocol.new(trans)
128
Jake Farrella87810f2012-09-28 01:59:04 +0000129 union = SpecNamespace::My_union.new(:integer32, 25)
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000130 union.write(proto)
131
Jake Farrella87810f2012-09-28 01:59:04 +0000132 other_union = SpecNamespace::My_union.new(:integer32, 25)
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000133 other_union.read(proto)
James E. King III27247072018-03-22 20:50:23 -0400134 expect(other_union).to eq(union)
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000135 end
136
Jens Geyera9e33bf2012-12-12 23:11:04 +0100137 it "should serialize to json correctly" do
138 trans = Thrift::MemoryBufferTransport.new
139 proto = Thrift::JsonProtocol.new(trans)
140
141 union = SpecNamespace::My_union.new(:integer32, 25)
142 union.write(proto)
143
144 other_union = SpecNamespace::My_union.new(:integer32, 25)
145 other_union.read(proto)
James E. King III27247072018-03-22 20:50:23 -0400146 expect(other_union).to eq(union)
Jens Geyera9e33bf2012-12-12 23:11:04 +0100147 end
148
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000149 it "should raise when validating unset union" do
Jake Farrella87810f2012-09-28 01:59:04 +0000150 union = SpecNamespace::My_union.new
James E. King III27247072018-03-22 20:50:23 -0400151 expect { union.validate }.to raise_error(StandardError, "Union fields are not set.")
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000152
Jake Farrella87810f2012-09-28 01:59:04 +0000153 other_union = SpecNamespace::My_union.new(:integer32, 1)
James E. King III27247072018-03-22 20:50:23 -0400154 expect { other_union.validate }.not_to raise_error
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000155 end
156
157 it "should validate an enum field properly" do
Jake Farrella87810f2012-09-28 01:59:04 +0000158 union = SpecNamespace::TestUnion.new(:enum_field, 3)
James E. King III27247072018-03-22 20:50:23 -0400159 expect(union.get_set_field).to eq(:enum_field)
160 expect { union.validate }.to raise_error(Thrift::ProtocolException, "Invalid value of field enum_field!")
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000161
Jake Farrella87810f2012-09-28 01:59:04 +0000162 other_union = SpecNamespace::TestUnion.new(:enum_field, 1)
James E. King III27247072018-03-22 20:50:23 -0400163 expect { other_union.validate }.not_to raise_error
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000164 end
165
166 it "should properly serialize and match structs with a union" do
Jake Farrella87810f2012-09-28 01:59:04 +0000167 union = SpecNamespace::My_union.new(:integer32, 26)
168 swu = SpecNamespace::Struct_with_union.new(:fun_union => union)
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000169
170 trans = Thrift::MemoryBufferTransport.new
171 proto = Thrift::CompactProtocol.new(trans)
172
173 swu.write(proto)
174
Jake Farrella87810f2012-09-28 01:59:04 +0000175 other_union = SpecNamespace::My_union.new(:some_characters, "hello there")
176 swu2 = SpecNamespace::Struct_with_union.new(:fun_union => other_union)
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000177
James E. King III27247072018-03-22 20:50:23 -0400178 expect(swu2).not_to eq(swu)
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000179
180 swu2.read(proto)
James E. King III27247072018-03-22 20:50:23 -0400181 expect(swu2).to eq(swu)
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000182 end
Bryan Duxbury205e4502010-02-18 23:19:42 +0000183
Bryan Duxbury5af64f02010-02-17 22:27:27 +0000184 it "should support old style constructor" do
Jake Farrella87810f2012-09-28 01:59:04 +0000185 union = SpecNamespace::My_union.new(:integer32 => 26)
James E. King III27247072018-03-22 20:50:23 -0400186 expect(union.get_set_field).to eq(:integer32)
187 expect(union.get_value).to eq(26)
Bryan Duxbury5af64f02010-02-17 22:27:27 +0000188 end
Bryan Duxbury205e4502010-02-18 23:19:42 +0000189
190 it "should not throw an error when inspected and unset" do
James E. King III27247072018-03-22 20:50:23 -0400191 expect{SpecNamespace::TestUnion.new().inspect}.not_to raise_error
Bryan Duxbury205e4502010-02-18 23:19:42 +0000192 end
193
Bryan Duxbury3d03c522010-02-18 17:42:06 +0000194 it "should print enum value name when inspected" do
James E. King III27247072018-03-22 20:50:23 -0400195 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 +0000196
James E. King III27247072018-03-22 20:50:23 -0400197 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 +0000198 end
Bryan Duxbury205e4502010-02-18 23:19:42 +0000199
Bryan Duxbury0e4920c2010-02-18 20:28:27 +0000200 it "should offer field? methods" do
James E. King III27247072018-03-22 20:50:23 -0400201 expect(SpecNamespace::My_union.new.some_enum?).to be_falsey
202 expect(SpecNamespace::My_union.new(:some_enum => SpecNamespace::SomeEnum::ONE).some_enum?).to be_truthy
203 expect(SpecNamespace::My_union.new(:im_true => false).im_true?).to be_truthy
204 expect(SpecNamespace::My_union.new(:im_true => true).im_true?).to be_truthy
Bryan Duxbury0e4920c2010-02-18 20:28:27 +0000205 end
Bryan Duxbury205e4502010-02-18 23:19:42 +0000206
Bryan Duxbury39dadd62010-02-18 22:00:45 +0000207 it "should pretty print binary fields" do
James E. King III27247072018-03-22 20:50:23 -0400208 expect(SpecNamespace::TestUnion.new(:binary_field => "\001\002\003").inspect).to eq("<SpecNamespace::TestUnion binary_field: 010203>")
Bryan Duxbury39dadd62010-02-18 22:00:45 +0000209 end
Bryan Duxbury205e4502010-02-18 23:19:42 +0000210
211 it "should be comparable" do
212 relationships = [
Dmytro Shteflyuke9ac8e32025-11-19 23:33:23 -0500213 [0, -1, -1, -1, -1, -1],
214 [1, 0, -1, -1, -1, -1],
215 [1, 1, 0, -1, -1, -1],
216 [1, 1, 1, 0, -1, -1],
217 [1, 1, 1, 1, 0, -1],
218 [1, 1, 1, 1, 1, 0]]
Bryan Duxbury205e4502010-02-18 23:19:42 +0000219
220 objs = [
Jake Farrella87810f2012-09-28 01:59:04 +0000221 SpecNamespace::TestUnion.new(:string_field, "blah"),
222 SpecNamespace::TestUnion.new(:string_field, "blahblah"),
223 SpecNamespace::TestUnion.new(:i32_field, 1),
Dmytro Shteflyuke9ac8e32025-11-19 23:33:23 -0500224 SpecNamespace::TestUnion.new(:uuid_field, '550e8400-e29b-41d4-a716-446655440000'),
225 SpecNamespace::TestUnion.new(:uuid_field, '6ba7b810-9dad-11d1-80b4-00c04fd430c8'),
Jake Farrella87810f2012-09-28 01:59:04 +0000226 SpecNamespace::TestUnion.new()]
Bryan Duxbury205e4502010-02-18 23:19:42 +0000227
Dmytro Shteflyuke9ac8e32025-11-19 23:33:23 -0500228 objs.size.times do |y|
229 objs.size.times do |x|
Bryan Duxbury205e4502010-02-18 23:19:42 +0000230 # puts "#{objs[y].inspect} <=> #{objs[x].inspect} should == #{relationships[y][x]}"
James E. King III27247072018-03-22 20:50:23 -0400231 expect(objs[y] <=> objs[x]).to eq(relationships[y][x])
Bryan Duxbury205e4502010-02-18 23:19:42 +0000232 end
233 end
234 end
Dmytro Shteflyuke9ac8e32025-11-19 23:33:23 -0500235
236 it "should handle UUID as union value" do
237 union = SpecNamespace::My_union.new
238 union.unique_id = 'ffffffff-ffff-ffff-ffff-ffffffffffff'
239
240 trans = Thrift::MemoryBufferTransport.new
241 prot = Thrift::CompactProtocol.new(trans)
242
243 union.write(prot)
244
245 result = SpecNamespace::My_union.new
246 result.read(prot)
247
248 expect(result.unique_id).to eq('ffffffff-ffff-ffff-ffff-ffffffffffff')
249 expect(result.get_set_field).to eq(:unique_id)
250 end
251
252 it "should normalize UUID case in union" do
253 union = SpecNamespace::My_union.new
254 union.unique_id = '550E8400-E29B-41D4-A716-446655440000'
255
256 trans = Thrift::MemoryBufferTransport.new
257 prot = Thrift::BinaryProtocol.new(trans)
258
259 union.write(prot)
260
261 result = SpecNamespace::My_union.new
262 result.read(prot)
263
264 expect(result.unique_id).to eq('550e8400-e29b-41d4-a716-446655440000')
265 end
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000266 end
267end