You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by kc...@apache.org on 2008/07/19 00:03:48 UTC

svn commit: r678059 - in /incubator/thrift/trunk/lib/rb: lib/thrift/struct.rb lib/thrift/types.rb spec/struct_spec.rb spec/types_spec.rb

Author: kclark
Date: Fri Jul 18 15:03:48 2008
New Revision: 678059

URL: http://svn.apache.org/viewvc?rev=678059&view=rev
Log:
rb: Display field name in type-checking error [THRIFT-78]

Author: Kevin Ballard <ke...@rapleaf.com>

Modified:
    incubator/thrift/trunk/lib/rb/lib/thrift/struct.rb
    incubator/thrift/trunk/lib/rb/lib/thrift/types.rb
    incubator/thrift/trunk/lib/rb/spec/struct_spec.rb
    incubator/thrift/trunk/lib/rb/spec/types_spec.rb

Modified: incubator/thrift/trunk/lib/rb/lib/thrift/struct.rb
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/lib/thrift/struct.rb?rev=678059&r1=678058&r2=678059&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/lib/thrift/struct.rb (original)
+++ incubator/thrift/trunk/lib/rb/lib/thrift/struct.rb Fri Jul 18 15:03:48 2008
@@ -6,7 +6,7 @@
     def initialize(d={})
       each_field do |fid, type, name, default|
         value = d.delete(name.to_s) { d.delete(name.to_sym) { default.dup rescue default } }
-        Thrift.check_type(value, type) if Thrift.type_checking
+        Thrift.check_type(value, type, name) if Thrift.type_checking
         instance_variable_set("@#{name}", value)
       end
       raise Exception, "Unknown keys given to #{self.class}.new: #{d.keys.join(", ")}" unless d.empty?
@@ -72,7 +72,7 @@
       fields.each do |field|
         klass.send :attr_reader, field
         klass.send :define_method, "#{field}=" do |value|
-          Thrift.check_type(value, klass::FIELDS.values.find { |f| f[:name].to_s == field.to_s }[:type] ) if Thrift.type_checking
+          Thrift.check_type(value, klass::FIELDS.values.find { |f| f[:name].to_s == field.to_s }[:type], field) if Thrift.type_checking
           instance_variable_set("@#{field}", value)
         end
       end

Modified: incubator/thrift/trunk/lib/rb/lib/thrift/types.rb
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/lib/thrift/types.rb?rev=678059&r1=678058&r2=678059&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/lib/thrift/types.rb (original)
+++ incubator/thrift/trunk/lib/rb/lib/thrift/types.rb Fri Jul 18 15:03:48 2008
@@ -25,7 +25,7 @@
   class TypeError < Exception
   end
 
-  def self.check_type(value, type)
+  def self.check_type(value, type, name)
     return if value.nil?
     klasses = case type
               when Types::VOID
@@ -48,7 +48,7 @@
                 Array
               end
     valid = klasses && [*klasses].any? { |klass| klass === value }
-    raise TypeError, "Expected #{type_name(type)}, received #{value.class}" unless valid
+    raise TypeError, "Expected #{type_name(type)}, received #{value.class} for field #{name}" unless valid
   end
 
   def self.type_name(type)

Modified: incubator/thrift/trunk/lib/rb/spec/struct_spec.rb
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/spec/struct_spec.rb?rev=678059&r1=678058&r2=678059&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/spec/struct_spec.rb (original)
+++ incubator/thrift/trunk/lib/rb/spec/struct_spec.rb Fri Jul 18 15:03:48 2008
@@ -177,7 +177,7 @@
     it "should support optional type-checking in Thrift::Struct.new" do
       Thrift.type_checking = true
       begin
-        lambda { Hello.new(:greeting => 3) }.should raise_error(TypeError, "Expected Types::STRING, received Fixnum")
+        lambda { Hello.new(:greeting => 3) }.should raise_error(TypeError, "Expected Types::STRING, received Fixnum for field greeting")
       ensure
         Thrift.type_checking = false
       end
@@ -188,7 +188,7 @@
       Thrift.type_checking = true
       begin
         hello = Hello.new
-        lambda { hello.greeting = 3 }.should raise_error(TypeError, "Expected Types::STRING, received Fixnum")
+        lambda { hello.greeting = 3 }.should raise_error(TypeError, "Expected Types::STRING, received Fixnum for field greeting")
       ensure
         Thrift.type_checking = false
       end

Modified: incubator/thrift/trunk/lib/rb/spec/types_spec.rb
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/spec/types_spec.rb?rev=678059&r1=678058&r2=678059&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/spec/types_spec.rb (original)
+++ incubator/thrift/trunk/lib/rb/spec/types_spec.rb Fri Jul 18 15:03:48 2008
@@ -16,31 +16,40 @@
       Thrift.type_checking = true
       begin
         # lambda { Thrift.check_type(nil, Types::STOP) }.should raise_error(TypeError)
-        lambda { Thrift.check_type(3, Types::STOP) }.should raise_error(TypeError)
-        lambda { Thrift.check_type(nil, Types::VOID) }.should_not raise_error(TypeError)
-        lambda { Thrift.check_type(3, Types::VOID) }.should raise_error(TypeError)
-        lambda { Thrift.check_type(true, Types::BOOL) }.should_not raise_error(TypeError)
-        lambda { Thrift.check_type(3, Types::BOOL) }.should raise_error(TypeError)
-        # lambda { Thrift.check_type(nil, Types::BOOL) }.should raise_error(TypeError)
-        lambda { Thrift.check_type(42, Types::BYTE) }.should_not raise_error(TypeError)
-        lambda { Thrift.check_type(42, Types::I16) }.should_not raise_error(TypeError)
-        lambda { Thrift.check_type(42, Types::I32) }.should_not raise_error(TypeError)
-        lambda { Thrift.check_type(42, Types::I64) }.should_not raise_error(TypeError)
-        lambda { Thrift.check_type(3.14, Types::I32) }.should raise_error(TypeError)
-        lambda { Thrift.check_type(3.14, Types::DOUBLE) }.should_not raise_error(TypeError)
-        lambda { Thrift.check_type(3, Types::DOUBLE) }.should raise_error(TypeError)
-        lambda { Thrift.check_type("3", Types::STRING) }.should_not raise_error(TypeError)
-        lambda { Thrift.check_type(3, Types::STRING) }.should raise_error(TypeError)
+        lambda { Thrift.check_type(3, Types::STOP, :foo) }.should raise_error(TypeError)
+        lambda { Thrift.check_type(nil, Types::VOID, :foo) }.should_not raise_error(TypeError)
+        lambda { Thrift.check_type(3, Types::VOID, :foo) }.should raise_error(TypeError)
+        lambda { Thrift.check_type(true, Types::BOOL, :foo) }.should_not raise_error(TypeError)
+        lambda { Thrift.check_type(3, Types::BOOL, :foo) }.should raise_error(TypeError)
+        # lambda { Thrift.check_type(nil, Types::BOOL, :foo) }.should raise_error(TypeError)
+        lambda { Thrift.check_type(42, Types::BYTE, :foo) }.should_not raise_error(TypeError)
+        lambda { Thrift.check_type(42, Types::I16, :foo) }.should_not raise_error(TypeError)
+        lambda { Thrift.check_type(42, Types::I32, :foo) }.should_not raise_error(TypeError)
+        lambda { Thrift.check_type(42, Types::I64, :foo) }.should_not raise_error(TypeError)
+        lambda { Thrift.check_type(3.14, Types::I32, :foo) }.should raise_error(TypeError)
+        lambda { Thrift.check_type(3.14, Types::DOUBLE, :foo) }.should_not raise_error(TypeError)
+        lambda { Thrift.check_type(3, Types::DOUBLE, :foo) }.should raise_error(TypeError)
+        lambda { Thrift.check_type("3", Types::STRING, :foo) }.should_not raise_error(TypeError)
+        lambda { Thrift.check_type(3, Types::STRING, :foo) }.should raise_error(TypeError)
         hello = SpecNamespace::Hello.new
-        lambda { Thrift.check_type(hello, Types::STRUCT) }.should_not raise_error(TypeError)
-        lambda { Thrift.check_type("foo", Types::STRUCT) }.should raise_error(TypeError)
-        lambda { Thrift.check_type({:foo => 1}, Types::MAP) }.should_not raise_error(TypeError)
-        lambda { Thrift.check_type([1], Types::MAP) }.should raise_error(TypeError)
-        lambda { Thrift.check_type([1], Types::LIST) }.should_not raise_error(TypeError)
-        lambda { Thrift.check_type({:foo => 1}, Types::LIST) }.should raise_error(TypeError)
-        lambda { Thrift.check_type(Set.new([1,2]), Types::SET) }.should_not raise_error(TypeError)
-        lambda { Thrift.check_type([1,2], Types::SET) }.should raise_error(TypeError)
-        lambda { Thrift.check_type({:foo => true}, Types::SET) }.should raise_error(TypeError)
+        lambda { Thrift.check_type(hello, Types::STRUCT, :foo) }.should_not raise_error(TypeError)
+        lambda { Thrift.check_type("foo", Types::STRUCT, :foo) }.should raise_error(TypeError)
+        lambda { Thrift.check_type({:foo => 1}, Types::MAP, :foo) }.should_not raise_error(TypeError)
+        lambda { Thrift.check_type([1], Types::MAP, :foo) }.should raise_error(TypeError)
+        lambda { Thrift.check_type([1], Types::LIST, :foo) }.should_not raise_error(TypeError)
+        lambda { Thrift.check_type({:foo => 1}, Types::LIST, :foo) }.should raise_error(TypeError)
+        lambda { Thrift.check_type(Set.new([1,2]), Types::SET, :foo) }.should_not raise_error(TypeError)
+        lambda { Thrift.check_type([1,2], Types::SET, :foo) }.should raise_error(TypeError)
+        lambda { Thrift.check_type({:foo => true}, Types::SET, :foo) }.should raise_error(TypeError)
+      ensure
+        Thrift.type_checking = false
+      end
+    end
+
+    it "should give the TypeError a readable message" do
+      Thrift.type_checking = true
+      begin
+        lambda { Thrift.check_type(3, Types::STRING, :foo) }.should raise_error(TypeError, "Expected Types::STRING, received Fixnum for field foo")
       ensure
         Thrift.type_checking = false
       end