blob: a4270906d0a0491394a4e2a67656ea1a7ebbd92c [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
Bryan Duxbury33e190c2010-02-16 21:19:01 +000027 union.get_set_field.should == nil
28 union.get_value.should == nil
29 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
34 union.get_set_field.should == :integer32
35 union.get_value.should == 25
36 union.integer32.should == 25
37 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)
Bryan Duxbury33e190c2010-02-16 21:19:01 +000041 union.get_set_field.should == :integer32
42 union.integer32.should == 5
43 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
48 lambda { union.some_characters }.should raise_error(RuntimeError, "some_characters is not union's set field.")
49 end
Jake Farrella87810f2012-09-28 01:59:04 +000050
Bryan Duxbury33e190c2010-02-16 21:19:01 +000051 it "should not be equal to nil" do
Jake Farrella87810f2012-09-28 01:59:04 +000052 union = SpecNamespace::My_union.new
Bryan Duxbury33e190c2010-02-16 21:19:01 +000053 union.should_not == nil
54 end
Jake Farrella87810f2012-09-28 01:59:04 +000055
Roger Meier56d38fb2015-06-01 22:01:09 +020056 it "should not be equal with an empty String" do
57 union = SpecNamespace::My_union.new
58 union.should_not == ''
59 end
60
Bryan Duxbury33e190c2010-02-16 21:19:01 +000061 it "should not equate two different unions, i32 vs. string" do
Jake Farrella87810f2012-09-28 01:59:04 +000062 union = SpecNamespace::My_union.new(:integer32, 25)
63 other_union = SpecNamespace::My_union.new(:some_characters, "blah!")
Bryan Duxbury33e190c2010-02-16 21:19:01 +000064 union.should_not == other_union
65 end
66
67 it "should properly reset setfield and setvalue" do
Jake Farrella87810f2012-09-28 01:59:04 +000068 union = SpecNamespace::My_union.new(:integer32, 25)
Bryan Duxbury33e190c2010-02-16 21:19:01 +000069 union.get_set_field.should == :integer32
70 union.some_characters = "blah!"
71 union.get_set_field.should == :some_characters
72 union.get_value.should == "blah!"
73 lambda { union.integer32 }.should raise_error(RuntimeError, "integer32 is not union's set field.")
74 end
75
76 it "should not equate two different unions with different values" do
Jake Farrella87810f2012-09-28 01:59:04 +000077 union = SpecNamespace::My_union.new(:integer32, 25)
78 other_union = SpecNamespace::My_union.new(:integer32, 400)
Bryan Duxbury33e190c2010-02-16 21:19:01 +000079 union.should_not == other_union
80 end
81
82 it "should not equate two different unions with different fields" do
Jake Farrella87810f2012-09-28 01:59:04 +000083 union = SpecNamespace::My_union.new(:integer32, 25)
84 other_union = SpecNamespace::My_union.new(:other_i32, 25)
Bryan Duxbury33e190c2010-02-16 21:19:01 +000085 union.should_not == other_union
86 end
87
88 it "should inspect properly" do
Jake Farrella87810f2012-09-28 01:59:04 +000089 union = SpecNamespace::My_union.new(:integer32, 25)
Bryan Duxbury33e190c2010-02-16 21:19:01 +000090 union.inspect.should == "<SpecNamespace::My_union integer32: 25>"
91 end
92
93 it "should not allow setting with instance_variable_set" do
Jake Farrella87810f2012-09-28 01:59:04 +000094 union = SpecNamespace::My_union.new(:integer32, 27)
Bryan Duxbury33e190c2010-02-16 21:19:01 +000095 union.instance_variable_set(:@some_characters, "hallo!")
96 union.get_set_field.should == :integer32
97 union.get_value.should == 27
98 lambda { union.some_characters }.should raise_error(RuntimeError, "some_characters is not union's set field.")
99 end
100
Jens Geyera9e33bf2012-12-12 23:11:04 +0100101 it "should serialize to binary correctly" do
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000102 trans = Thrift::MemoryBufferTransport.new
103 proto = Thrift::BinaryProtocol.new(trans)
104
Jake Farrella87810f2012-09-28 01:59:04 +0000105 union = SpecNamespace::My_union.new(:integer32, 25)
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000106 union.write(proto)
107
Jake Farrella87810f2012-09-28 01:59:04 +0000108 other_union = SpecNamespace::My_union.new(:integer32, 25)
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000109 other_union.read(proto)
110 other_union.should == union
111 end
112
Jens Geyera9e33bf2012-12-12 23:11:04 +0100113 it "should serialize to json correctly" do
114 trans = Thrift::MemoryBufferTransport.new
115 proto = Thrift::JsonProtocol.new(trans)
116
117 union = SpecNamespace::My_union.new(:integer32, 25)
118 union.write(proto)
119
120 other_union = SpecNamespace::My_union.new(:integer32, 25)
121 other_union.read(proto)
122 other_union.should == union
123 end
124
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000125 it "should raise when validating unset union" do
Jake Farrella87810f2012-09-28 01:59:04 +0000126 union = SpecNamespace::My_union.new
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000127 lambda { union.validate }.should raise_error(StandardError, "Union fields are not set.")
128
Jake Farrella87810f2012-09-28 01:59:04 +0000129 other_union = SpecNamespace::My_union.new(:integer32, 1)
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000130 lambda { other_union.validate }.should_not raise_error(StandardError, "Union fields are not set.")
131 end
132
133 it "should validate an enum field properly" do
Jake Farrella87810f2012-09-28 01:59:04 +0000134 union = SpecNamespace::TestUnion.new(:enum_field, 3)
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000135 union.get_set_field.should == :enum_field
Jake Farrella87810f2012-09-28 01:59:04 +0000136 lambda { union.validate }.should raise_error(Thrift::ProtocolException, "Invalid value of field enum_field!")
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000137
Jake Farrella87810f2012-09-28 01:59:04 +0000138 other_union = SpecNamespace::TestUnion.new(:enum_field, 1)
139 lambda { other_union.validate }.should_not raise_error(Thrift::ProtocolException, "Invalid value of field enum_field!")
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000140 end
141
142 it "should properly serialize and match structs with a union" do
Jake Farrella87810f2012-09-28 01:59:04 +0000143 union = SpecNamespace::My_union.new(:integer32, 26)
144 swu = SpecNamespace::Struct_with_union.new(:fun_union => union)
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000145
146 trans = Thrift::MemoryBufferTransport.new
147 proto = Thrift::CompactProtocol.new(trans)
148
149 swu.write(proto)
150
Jake Farrella87810f2012-09-28 01:59:04 +0000151 other_union = SpecNamespace::My_union.new(:some_characters, "hello there")
152 swu2 = SpecNamespace::Struct_with_union.new(:fun_union => other_union)
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000153
154 swu2.should_not == swu
155
156 swu2.read(proto)
157 swu2.should == swu
158 end
Bryan Duxbury205e4502010-02-18 23:19:42 +0000159
Bryan Duxbury5af64f02010-02-17 22:27:27 +0000160 it "should support old style constructor" do
Jake Farrella87810f2012-09-28 01:59:04 +0000161 union = SpecNamespace::My_union.new(:integer32 => 26)
Bryan Duxbury5af64f02010-02-17 22:27:27 +0000162 union.get_set_field.should == :integer32
163 union.get_value.should == 26
164 end
Bryan Duxbury205e4502010-02-18 23:19:42 +0000165
166 it "should not throw an error when inspected and unset" do
Jake Farrella87810f2012-09-28 01:59:04 +0000167 lambda{SpecNamespace::TestUnion.new().inspect}.should_not raise_error
Bryan Duxbury205e4502010-02-18 23:19:42 +0000168 end
169
Bryan Duxbury3d03c522010-02-18 17:42:06 +0000170 it "should print enum value name when inspected" do
Jake Farrella87810f2012-09-28 01:59:04 +0000171 SpecNamespace::My_union.new(:some_enum => SpecNamespace::SomeEnum::ONE).inspect.should == "<SpecNamespace::My_union some_enum: ONE (0)>"
Bryan Duxbury205e4502010-02-18 23:19:42 +0000172
Jake Farrella87810f2012-09-28 01:59:04 +0000173 SpecNamespace::My_union.new(:my_map => {SpecNamespace::SomeEnum::ONE => [SpecNamespace::SomeEnum::TWO]}).inspect.should == "<SpecNamespace::My_union my_map: {ONE (0): [TWO (1)]}>"
Bryan Duxbury3d03c522010-02-18 17:42:06 +0000174 end
Bryan Duxbury205e4502010-02-18 23:19:42 +0000175
Bryan Duxbury0e4920c2010-02-18 20:28:27 +0000176 it "should offer field? methods" do
Jake Farrella87810f2012-09-28 01:59:04 +0000177 SpecNamespace::My_union.new.some_enum?.should be_false
178 SpecNamespace::My_union.new(:some_enum => SpecNamespace::SomeEnum::ONE).some_enum?.should be_true
179 SpecNamespace::My_union.new(:im_true => false).im_true?.should be_true
180 SpecNamespace::My_union.new(:im_true => true).im_true?.should be_true
Bryan Duxbury0e4920c2010-02-18 20:28:27 +0000181 end
Bryan Duxbury205e4502010-02-18 23:19:42 +0000182
Bryan Duxbury39dadd62010-02-18 22:00:45 +0000183 it "should pretty print binary fields" do
Jake Farrella87810f2012-09-28 01:59:04 +0000184 SpecNamespace::TestUnion.new(:binary_field => "\001\002\003").inspect.should == "<SpecNamespace::TestUnion binary_field: 010203>"
Bryan Duxbury39dadd62010-02-18 22:00:45 +0000185 end
Bryan Duxbury205e4502010-02-18 23:19:42 +0000186
187 it "should be comparable" do
188 relationships = [
189 [0, -1, -1, -1],
190 [1, 0, -1, -1],
191 [1, 1, 0, -1],
192 [1, 1, 1, 0]]
193
194 objs = [
Jake Farrella87810f2012-09-28 01:59:04 +0000195 SpecNamespace::TestUnion.new(:string_field, "blah"),
196 SpecNamespace::TestUnion.new(:string_field, "blahblah"),
197 SpecNamespace::TestUnion.new(:i32_field, 1),
198 SpecNamespace::TestUnion.new()]
Bryan Duxbury205e4502010-02-18 23:19:42 +0000199
200 for y in 0..3
201 for x in 0..3
202 # puts "#{objs[y].inspect} <=> #{objs[x].inspect} should == #{relationships[y][x]}"
203 (objs[y] <=> objs[x]).should == relationships[y][x]
204 end
205 end
206 end
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000207 end
208end