rb: Implement type-checking in Thrift::Struct.new and field accessors
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@669015 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/rb/spec/gen-rb/NonblockingService.rb b/lib/rb/spec/gen-rb/NonblockingService.rb
index 46e7c60..411a6f0 100644
--- a/lib/rb/spec/gen-rb/NonblockingService.rb
+++ b/lib/rb/spec/gen-rb/NonblockingService.rb
@@ -115,7 +115,7 @@
class Greeting_args
include Thrift::Struct
- attr_accessor :english
+ Thrift::Struct.field_accessor self, :english
FIELDS = {
1 => {:type => Thrift::Types::BOOL, :name => 'english'}
}
@@ -123,7 +123,7 @@
class Greeting_result
include Thrift::Struct
- attr_accessor :success
+ Thrift::Struct.field_accessor self, :success
FIELDS = {
0 => {:type => Thrift::Types::STRUCT, :name => 'success', :class => Hello}
}
@@ -138,7 +138,7 @@
class Block_result
include Thrift::Struct
- attr_accessor :success
+ Thrift::Struct.field_accessor self, :success
FIELDS = {
0 => {:type => Thrift::Types::BOOL, :name => 'success'}
}
@@ -174,7 +174,7 @@
class Sleep_args
include Thrift::Struct
- attr_accessor :seconds
+ Thrift::Struct.field_accessor self, :seconds
FIELDS = {
1 => {:type => Thrift::Types::DOUBLE, :name => 'seconds'}
}
diff --git a/lib/rb/spec/gen-rb/ThriftSpec_types.rb b/lib/rb/spec/gen-rb/ThriftSpec_types.rb
index f34b7bc..b98ae7d 100644
--- a/lib/rb/spec/gen-rb/ThriftSpec_types.rb
+++ b/lib/rb/spec/gen-rb/ThriftSpec_types.rb
@@ -9,7 +9,7 @@
module SpecNamespace
class Hello
include Thrift::Struct
- attr_accessor :greeting
+ Thrift::Struct.field_accessor self, :greeting
FIELDS = {
1 => {:type => Thrift::Types::STRING, :name => 'greeting', :default => 'hello world'}
}
@@ -17,7 +17,7 @@
class Foo
include Thrift::Struct
- attr_accessor :simple, :words, :hello, :ints, :complex, :shorts
+ Thrift::Struct.field_accessor self, :simple, :words, :hello, :ints, :complex, :shorts
FIELDS = {
1 => {:type => Thrift::Types::I32, :name => 'simple', :default => 53},
2 => {:type => Thrift::Types::STRING, :name => 'words', :default => 'words'},
@@ -40,7 +40,7 @@
class BoolStruct
include Thrift::Struct
- attr_accessor :yesno
+ Thrift::Struct.field_accessor self, :yesno
FIELDS = {
1 => {:type => Thrift::Types::BOOL, :name => 'yesno', :default => true}
}
diff --git a/lib/rb/spec/struct_spec.rb b/lib/rb/spec/struct_spec.rb
index ef2354d..44f0b27 100644
--- a/lib/rb/spec/struct_spec.rb
+++ b/lib/rb/spec/struct_spec.rb
@@ -174,5 +174,30 @@
struct = Foo.new
lambda { struct.send :write_container, nil, nil, {:type => "foo"} }.should raise_error(StandardError, "Not a container type: foo")
end
+
+ 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")
+ ensure
+ Thrift.type_checking = false
+ end
+ lambda { Hello.new(:greeting => 3) }.should_not raise_error(TypeError)
+ end
+
+ it "should support optional type-checking in field accessors" do
+ Thrift.type_checking = true
+ begin
+ hello = Hello.new
+ lambda { hello.greeting = 3 }.should raise_error(TypeError, "Expected Types::STRING, received Fixnum")
+ ensure
+ Thrift.type_checking = false
+ end
+ lambda { hello.greeting = 3 }.should_not raise_error(TypeError)
+ end
+
+ it "should raise an exception when unknown types are given to Thrift::Struct.new" do
+ lambda { Hello.new(:fish => 'salmon') }.should raise_error(Exception, "Unknown arguments given to SpecNamespace::Hello.new")
+ end
end
end