Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +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' |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 21 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 22 | describe 'Union' do |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 23 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 24 | describe Thrift::Union do |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 25 | it "should return nil value in unset union" do |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 26 | union = SpecNamespace::My_union.new |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 27 | expect(union.get_set_field).to eq(nil) |
| 28 | expect(union.get_value).to eq(nil) |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 29 | end |
| 30 | |
| 31 | it "should set a field and be accessible through get_value and the named field accessor" do |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 32 | union = SpecNamespace::My_union.new |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 33 | union.integer32 = 25 |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 34 | expect(union.get_set_field).to eq(:integer32) |
| 35 | expect(union.get_value).to eq(25) |
| 36 | expect(union.integer32).to eq(25) |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 37 | end |
| 38 | |
| 39 | it "should work correctly when instantiated with static field constructors" do |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 40 | union = SpecNamespace::My_union.integer32(5) |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 41 | expect(union.get_set_field).to eq(:integer32) |
| 42 | expect(union.integer32).to eq(5) |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 43 | end |
| 44 | |
| 45 | it "should raise for wrong set field" do |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 46 | union = SpecNamespace::My_union.new |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 47 | union.integer32 = 25 |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 48 | expect { union.some_characters }.to raise_error(RuntimeError, "some_characters is not union's set field.") |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 49 | end |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 50 | |
Joe Ennever | 5b15f8c | 2015-08-31 19:20:36 +0000 | [diff] [blame] | 51 | 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}) |
| 54 | example = lambda { Thrift::Serializer.new.serialize(union) } |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 55 | expect(example).to raise_error(RuntimeError, "set_field is not valid for this union!") |
Joe Ennever | 5b15f8c | 2015-08-31 19:20:36 +0000 | [diff] [blame] | 56 | end |
| 57 | |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 58 | it "should not be equal to nil" do |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 59 | union = SpecNamespace::My_union.new |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 60 | expect(union).not_to eq(nil) |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 61 | end |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 62 | |
Roger Meier | 56d38fb | 2015-06-01 22:01:09 +0200 | [diff] [blame] | 63 | it "should not be equal with an empty String" do |
| 64 | union = SpecNamespace::My_union.new |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 65 | expect(union).not_to eq('') |
Roger Meier | 56d38fb | 2015-06-01 22:01:09 +0200 | [diff] [blame] | 66 | end |
| 67 | |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 68 | it "should not equate two different unions, i32 vs. string" do |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 69 | union = SpecNamespace::My_union.new(:integer32, 25) |
| 70 | other_union = SpecNamespace::My_union.new(:some_characters, "blah!") |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 71 | expect(union).not_to eq(other_union) |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 72 | end |
| 73 | |
| 74 | it "should properly reset setfield and setvalue" do |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 75 | union = SpecNamespace::My_union.new(:integer32, 25) |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 76 | expect(union.get_set_field).to eq(:integer32) |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 77 | union.some_characters = "blah!" |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 78 | 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 Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 81 | end |
| 82 | |
| 83 | it "should not equate two different unions with different values" do |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 84 | union = SpecNamespace::My_union.new(:integer32, 25) |
| 85 | other_union = SpecNamespace::My_union.new(:integer32, 400) |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 86 | expect(union).not_to eq(other_union) |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 87 | end |
| 88 | |
| 89 | it "should not equate two different unions with different fields" do |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 90 | union = SpecNamespace::My_union.new(:integer32, 25) |
| 91 | other_union = SpecNamespace::My_union.new(:other_i32, 25) |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 92 | expect(union).not_to eq(other_union) |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 93 | end |
| 94 | |
| 95 | it "should inspect properly" do |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 96 | union = SpecNamespace::My_union.new(:integer32, 25) |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 97 | expect(union.inspect).to eq("<SpecNamespace::My_union integer32: 25>") |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 98 | end |
| 99 | |
| 100 | it "should not allow setting with instance_variable_set" do |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 101 | union = SpecNamespace::My_union.new(:integer32, 27) |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 102 | union.instance_variable_set(:@some_characters, "hallo!") |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 103 | expect(union.get_set_field).to eq(:integer32) |
| 104 | expect(union.get_value).to eq(27) |
| 105 | expect { union.some_characters }.to raise_error(RuntimeError, "some_characters is not union's set field.") |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 106 | end |
| 107 | |
Jens Geyer | a9e33bf | 2012-12-12 23:11:04 +0100 | [diff] [blame] | 108 | it "should serialize to binary correctly" do |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 109 | trans = Thrift::MemoryBufferTransport.new |
| 110 | proto = Thrift::BinaryProtocol.new(trans) |
| 111 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 112 | union = SpecNamespace::My_union.new(:integer32, 25) |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 113 | union.write(proto) |
| 114 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 115 | other_union = SpecNamespace::My_union.new(:integer32, 25) |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 116 | other_union.read(proto) |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 117 | expect(other_union).to eq(union) |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 118 | end |
| 119 | |
Jens Geyer | a9e33bf | 2012-12-12 23:11:04 +0100 | [diff] [blame] | 120 | it "should serialize to json correctly" do |
| 121 | trans = Thrift::MemoryBufferTransport.new |
| 122 | proto = Thrift::JsonProtocol.new(trans) |
| 123 | |
| 124 | union = SpecNamespace::My_union.new(:integer32, 25) |
| 125 | union.write(proto) |
| 126 | |
| 127 | other_union = SpecNamespace::My_union.new(:integer32, 25) |
| 128 | other_union.read(proto) |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 129 | expect(other_union).to eq(union) |
Jens Geyer | a9e33bf | 2012-12-12 23:11:04 +0100 | [diff] [blame] | 130 | end |
| 131 | |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 132 | it "should raise when validating unset union" do |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 133 | union = SpecNamespace::My_union.new |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 134 | expect { union.validate }.to raise_error(StandardError, "Union fields are not set.") |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 135 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 136 | other_union = SpecNamespace::My_union.new(:integer32, 1) |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 137 | expect { other_union.validate }.not_to raise_error |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 138 | end |
| 139 | |
| 140 | it "should validate an enum field properly" do |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 141 | union = SpecNamespace::TestUnion.new(:enum_field, 3) |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 142 | expect(union.get_set_field).to eq(:enum_field) |
| 143 | expect { union.validate }.to raise_error(Thrift::ProtocolException, "Invalid value of field enum_field!") |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 144 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 145 | other_union = SpecNamespace::TestUnion.new(:enum_field, 1) |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 146 | expect { other_union.validate }.not_to raise_error |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 147 | end |
| 148 | |
| 149 | it "should properly serialize and match structs with a union" do |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 150 | union = SpecNamespace::My_union.new(:integer32, 26) |
| 151 | swu = SpecNamespace::Struct_with_union.new(:fun_union => union) |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 152 | |
| 153 | trans = Thrift::MemoryBufferTransport.new |
| 154 | proto = Thrift::CompactProtocol.new(trans) |
| 155 | |
| 156 | swu.write(proto) |
| 157 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 158 | other_union = SpecNamespace::My_union.new(:some_characters, "hello there") |
| 159 | swu2 = SpecNamespace::Struct_with_union.new(:fun_union => other_union) |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 160 | |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 161 | expect(swu2).not_to eq(swu) |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 162 | |
| 163 | swu2.read(proto) |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 164 | expect(swu2).to eq(swu) |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 165 | end |
Bryan Duxbury | 205e450 | 2010-02-18 23:19:42 +0000 | [diff] [blame] | 166 | |
Bryan Duxbury | 5af64f0 | 2010-02-17 22:27:27 +0000 | [diff] [blame] | 167 | it "should support old style constructor" do |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 168 | union = SpecNamespace::My_union.new(:integer32 => 26) |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 169 | expect(union.get_set_field).to eq(:integer32) |
| 170 | expect(union.get_value).to eq(26) |
Bryan Duxbury | 5af64f0 | 2010-02-17 22:27:27 +0000 | [diff] [blame] | 171 | end |
Bryan Duxbury | 205e450 | 2010-02-18 23:19:42 +0000 | [diff] [blame] | 172 | |
| 173 | it "should not throw an error when inspected and unset" do |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 174 | expect{SpecNamespace::TestUnion.new().inspect}.not_to raise_error |
Bryan Duxbury | 205e450 | 2010-02-18 23:19:42 +0000 | [diff] [blame] | 175 | end |
| 176 | |
Bryan Duxbury | 3d03c52 | 2010-02-18 17:42:06 +0000 | [diff] [blame] | 177 | it "should print enum value name when inspected" do |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 178 | expect(SpecNamespace::My_union.new(:some_enum => SpecNamespace::SomeEnum::ONE).inspect).to eq("<SpecNamespace::My_union some_enum: ONE (0)>") |
Bryan Duxbury | 205e450 | 2010-02-18 23:19:42 +0000 | [diff] [blame] | 179 | |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 180 | 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 Duxbury | 3d03c52 | 2010-02-18 17:42:06 +0000 | [diff] [blame] | 181 | end |
Bryan Duxbury | 205e450 | 2010-02-18 23:19:42 +0000 | [diff] [blame] | 182 | |
Bryan Duxbury | 0e4920c | 2010-02-18 20:28:27 +0000 | [diff] [blame] | 183 | it "should offer field? methods" do |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 184 | expect(SpecNamespace::My_union.new.some_enum?).to be_falsey |
| 185 | expect(SpecNamespace::My_union.new(:some_enum => SpecNamespace::SomeEnum::ONE).some_enum?).to be_truthy |
| 186 | expect(SpecNamespace::My_union.new(:im_true => false).im_true?).to be_truthy |
| 187 | expect(SpecNamespace::My_union.new(:im_true => true).im_true?).to be_truthy |
Bryan Duxbury | 0e4920c | 2010-02-18 20:28:27 +0000 | [diff] [blame] | 188 | end |
Bryan Duxbury | 205e450 | 2010-02-18 23:19:42 +0000 | [diff] [blame] | 189 | |
Bryan Duxbury | 39dadd6 | 2010-02-18 22:00:45 +0000 | [diff] [blame] | 190 | it "should pretty print binary fields" do |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 191 | expect(SpecNamespace::TestUnion.new(:binary_field => "\001\002\003").inspect).to eq("<SpecNamespace::TestUnion binary_field: 010203>") |
Bryan Duxbury | 39dadd6 | 2010-02-18 22:00:45 +0000 | [diff] [blame] | 192 | end |
Bryan Duxbury | 205e450 | 2010-02-18 23:19:42 +0000 | [diff] [blame] | 193 | |
| 194 | it "should be comparable" do |
| 195 | relationships = [ |
| 196 | [0, -1, -1, -1], |
| 197 | [1, 0, -1, -1], |
| 198 | [1, 1, 0, -1], |
| 199 | [1, 1, 1, 0]] |
| 200 | |
| 201 | objs = [ |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 202 | SpecNamespace::TestUnion.new(:string_field, "blah"), |
| 203 | SpecNamespace::TestUnion.new(:string_field, "blahblah"), |
| 204 | SpecNamespace::TestUnion.new(:i32_field, 1), |
| 205 | SpecNamespace::TestUnion.new()] |
Bryan Duxbury | 205e450 | 2010-02-18 23:19:42 +0000 | [diff] [blame] | 206 | |
| 207 | for y in 0..3 |
| 208 | for x in 0..3 |
| 209 | # puts "#{objs[y].inspect} <=> #{objs[x].inspect} should == #{relationships[y][x]}" |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 210 | expect(objs[y] <=> objs[x]).to eq(relationships[y][x]) |
Bryan Duxbury | 205e450 | 2010-02-18 23:19:42 +0000 | [diff] [blame] | 211 | end |
| 212 | end |
| 213 | end |
Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 214 | end |
| 215 | end |