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