rb: Thrift.check_type should check struct classes [THRIFT-185]
Author: Piotr Kozikowski
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@726156 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/rb/lib/thrift/types.rb b/lib/rb/lib/thrift/types.rb
index 3a83a9f..460f80f 100644
--- a/lib/rb/lib/thrift/types.rb
+++ b/lib/rb/lib/thrift/types.rb
@@ -60,6 +60,8 @@
value.each do |el|
check_type(el, field[:element], "#{name}.element", false)
end
+ when Types::STRUCT
+ raise TypeError, "Expected #{field[:class]}, received #{value.class} for field #{name}" unless field[:class] == value.class
end
end
diff --git a/lib/rb/spec/types_spec.rb b/lib/rb/spec/types_spec.rb
index 183a354..873db30 100644
--- a/lib/rb/spec/types_spec.rb
+++ b/lib/rb/spec/types_spec.rb
@@ -37,7 +37,7 @@
lambda { Thrift.check_type("3", {:type => Types::STRING}, :foo) }.should_not raise_error(TypeError)
lambda { Thrift.check_type(3, {:type => Types::STRING}, :foo) }.should raise_error(TypeError)
hello = SpecNamespace::Hello.new
- lambda { Thrift.check_type(hello, {:type => Types::STRUCT}, :foo) }.should_not raise_error(TypeError)
+ lambda { Thrift.check_type(hello, {:type => Types::STRUCT, :class => SpecNamespace::Hello}, :foo) }.should_not raise_error(TypeError)
lambda { Thrift.check_type("foo", {:type => Types::STRUCT}, :foo) }.should raise_error(TypeError)
lambda { Thrift.check_type({:foo => 1}, {:type => Types::MAP}, :foo) }.should_not raise_error(TypeError)
lambda { Thrift.check_type([1], {:type => Types::MAP}, :foo) }.should raise_error(TypeError)
@@ -74,6 +74,9 @@
lambda { Thrift.check_type(Set.new([1, 2]), field, :foo) }.should_not raise_error(TypeError)
lambda { Thrift.check_type(Set.new([1, nil, 2]), field, :foo) }.should raise_error(TypeError)
lambda { Thrift.check_type(Set.new([1, 2.3, 2]), field, :foo) }.should raise_error(TypeError)
+
+ field = {:type => Types::STRUCT, :class => SpecNamespace::Hello}
+ lambda { Thrift.check_type(SpecNamespace::BoolStruct, field, :foo) }.should raise_error(TypeError)
end
it "should give the TypeError a readable message" do