blob: 4835288474db1bdc6f507870fcf0ff703133a1f8 [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
20require File.dirname(__FILE__) + '/spec_helper'
21
22class ThriftUnionSpec < Spec::ExampleGroup
23 include Thrift
24 include SpecNamespace
25
26 describe Union do
27 it "should return nil value in unset union" do
28 union = My_union.new
29 union.get_set_field.should == nil
30 union.get_value.should == nil
31 end
32
33 it "should set a field and be accessible through get_value and the named field accessor" do
34 union = My_union.new
35 union.integer32 = 25
36 union.get_set_field.should == :integer32
37 union.get_value.should == 25
38 union.integer32.should == 25
39 end
40
41 it "should work correctly when instantiated with static field constructors" do
42 union = My_union.integer32(5)
43 union.get_set_field.should == :integer32
44 union.integer32.should == 5
45 end
46
47 it "should raise for wrong set field" do
48 union = My_union.new
49 union.integer32 = 25
50 lambda { union.some_characters }.should raise_error(RuntimeError, "some_characters is not union's set field.")
51 end
52
53 it "should not be equal to nil" do
54 union = My_union.new
55 union.should_not == nil
56 end
57
58 it "should not equate two different unions, i32 vs. string" do
59 union = My_union.new(:integer32, 25)
60 other_union = My_union.new(:some_characters, "blah!")
61 union.should_not == other_union
62 end
63
64 it "should properly reset setfield and setvalue" do
65 union = My_union.new(:integer32, 25)
66 union.get_set_field.should == :integer32
67 union.some_characters = "blah!"
68 union.get_set_field.should == :some_characters
69 union.get_value.should == "blah!"
70 lambda { union.integer32 }.should raise_error(RuntimeError, "integer32 is not union's set field.")
71 end
72
73 it "should not equate two different unions with different values" do
74 union = My_union.new(:integer32, 25)
75 other_union = My_union.new(:integer32, 400)
76 union.should_not == other_union
77 end
78
79 it "should not equate two different unions with different fields" do
80 union = My_union.new(:integer32, 25)
81 other_union = My_union.new(:other_i32, 25)
82 union.should_not == other_union
83 end
84
85 it "should inspect properly" do
86 union = My_union.new(:integer32, 25)
87 union.inspect.should == "<SpecNamespace::My_union integer32: 25>"
88 end
89
90 it "should not allow setting with instance_variable_set" do
91 union = My_union.new(:integer32, 27)
92 union.instance_variable_set(:@some_characters, "hallo!")
93 union.get_set_field.should == :integer32
94 union.get_value.should == 27
95 lambda { union.some_characters }.should raise_error(RuntimeError, "some_characters is not union's set field.")
96 end
97
98 it "should serialize correctly" do
99 trans = Thrift::MemoryBufferTransport.new
100 proto = Thrift::BinaryProtocol.new(trans)
101
102 union = My_union.new(:integer32, 25)
103 union.write(proto)
104
105 other_union = My_union.new(:integer32, 25)
106 other_union.read(proto)
107 other_union.should == union
108 end
109
110 it "should raise when validating unset union" do
111 union = My_union.new
112 lambda { union.validate }.should raise_error(StandardError, "Union fields are not set.")
113
114 other_union = My_union.new(:integer32, 1)
115 lambda { other_union.validate }.should_not raise_error(StandardError, "Union fields are not set.")
116 end
117
118 it "should validate an enum field properly" do
119 union = TestUnion.new(:enum_field, 3)
120 union.get_set_field.should == :enum_field
121 lambda { union.validate }.should raise_error(ProtocolException, "Invalid value of field enum_field!")
122
123 other_union = TestUnion.new(:enum_field, 1)
124 lambda { other_union.validate }.should_not raise_error(ProtocolException, "Invalid value of field enum_field!")
125 end
126
127 it "should properly serialize and match structs with a union" do
128 union = My_union.new(:integer32, 26)
129 swu = Struct_with_union.new(:fun_union => union)
130
131 trans = Thrift::MemoryBufferTransport.new
132 proto = Thrift::CompactProtocol.new(trans)
133
134 swu.write(proto)
135
136 other_union = My_union.new(:some_characters, "hello there")
137 swu2 = Struct_with_union.new(:fun_union => other_union)
138
139 swu2.should_not == swu
140
141 swu2.read(proto)
142 swu2.should == swu
143 end
144 end
145end