From 083c07a26579e685bd62b085471f8720ef0aa822 Mon Sep 17 00:00:00 2001 From: Kevin Clark Date: Sat, 13 Dec 2008 02:39:20 +0000 Subject: [PATCH] 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 --- lib/rb/lib/thrift/types.rb | 2 ++ lib/rb/spec/types_spec.rb | 5 ++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/rb/lib/thrift/types.rb b/lib/rb/lib/thrift/types.rb index 3a83a9f3..460f80f8 100644 --- a/lib/rb/lib/thrift/types.rb +++ b/lib/rb/lib/thrift/types.rb @@ -60,6 +60,8 @@ module Thrift 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 183a3544..873db300 100644 --- a/lib/rb/spec/types_spec.rb +++ b/lib/rb/spec/types_spec.rb @@ -37,7 +37,7 @@ class ThriftTypesSpec < Spec::ExampleGroup 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 @@ class ThriftTypesSpec < Spec::ExampleGroup 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 -- 2.17.1