diff options
author | Leon Breedt <bitserf@gmail.com> | 2005-04-02 21:03:36 +0000 |
---|---|---|
committer | Leon Breedt <bitserf@gmail.com> | 2005-04-02 21:03:36 +0000 |
commit | aaea48fe9826b9e5d2d5b92795a297b8f238c58d (patch) | |
tree | e7c01c7f95d467f837c1f96d58dac74c3c902610 /actionwebservice/test/casting_test.rb | |
parent | aa09c770e9b5400683be11952673017295246de7 (diff) | |
download | rails-aaea48fe9826b9e5d2d5b92795a297b8f238c58d.tar.gz rails-aaea48fe9826b9e5d2d5b92795a297b8f238c58d.tar.bz2 rails-aaea48fe9826b9e5d2d5b92795a297b8f238c58d.zip |
* collapse 'ws' back into protocols, it just added complexity and indirection, and was hard to extend.
* extract casting into seperate support file
* ensure casting always does the right thing for return values, should fix interoperability issues with Ecto and possibly other XML-RPC clients
* add functional unit tests for scaffolding
* represent signature items with classes instead of symbols/Class objects, much more flexible
* tweak logging to always show casted versions of parameters and return values, if possible.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1072 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionwebservice/test/casting_test.rb')
-rw-r--r-- | actionwebservice/test/casting_test.rb | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/actionwebservice/test/casting_test.rb b/actionwebservice/test/casting_test.rb new file mode 100644 index 0000000000..223313564b --- /dev/null +++ b/actionwebservice/test/casting_test.rb @@ -0,0 +1,82 @@ +require File.dirname(__FILE__) + '/abstract_unit' + +module CastingTest + class API < ActionWebService::API::Base + api_method :int, :expects => [:int] + api_method :str, :expects => [:string] + api_method :bool, :expects => [:bool] + api_method :float, :expects => [:float] + api_method :time, :expects => [:time] + api_method :datetime, :expects => [:datetime] + api_method :date, :expects => [:date] + + api_method :int_array, :expects => [[:int]] + api_method :str_array, :expects => [[:string]] + api_method :bool_array, :expects => [[:bool]] + end +end + +class TC_Casting < Test::Unit::TestCase + include CastingTest + + def test_base_type_casting_valid + assert_equal 10000, cast_expects(:int, '10000')[0] + assert_equal '10000', cast_expects(:str, 10000)[0] + [1, '1', 'true', 'y', 'yes'].each do |val| + assert_equal true, cast_expects(:bool, val)[0] + end + [0, '0', 'false', 'n', 'no'].each do |val| + assert_equal false, cast_expects(:bool, val)[0] + end + assert_equal 3.14159, cast_expects(:float, '3.14159')[0] + now = Time.at(Time.now.tv_sec) + casted = cast_expects(:time, now.to_s)[0] + assert_equal now, casted + now = DateTime.now + assert_equal now.to_s, cast_expects(:datetime, now.to_s)[0].to_s + today = Date.today + assert_equal today, cast_expects(:date, today.to_s)[0] + end + + def test_base_type_casting_invalid + assert_raises ArgumentError do + cast_expects(:int, 'this is not a number') + end + assert_raises ActionWebService::Casting::CastingError do + # neither true or false ;) + cast_expects(:bool, 'i always lie') + end + assert_raises ArgumentError do + cast_expects(:float, 'not a float') + end + assert_raises ArgumentError do + cast_expects(:time, '111111111111111111111111111111111') + end + assert_raises ArgumentError do + cast_expects(:datetime, '-1') + end + assert_raises ArgumentError do + cast_expects(:date, '') + end + end + + def test_array_type_casting + assert_equal [1, 2, 3213992, 4], cast_expects(:int_array, ['1', '2', '3213992', '4'])[0] + assert_equal ['one', 'two', '5.0', '200', '', 'true'], cast_expects(:str_array, [:one, 'two', 5.0, 200, nil, true])[0] + assert_equal [true, false, true, true, false], cast_expects(:bool_array, ['1', nil, 'y', true, 'false'])[0] + end + + def test_array_type_casting_failure + assert_raises ActionWebService::Casting::CastingError do + cast_expects(:bool_array, ['false', 'blahblah']) + end + assert_raises ArgumentError do + cast_expects(:int_array, ['1', '2.021', '4']) + end + end + + private + def cast_expects(method_name, *args) + API.api_method_instance(method_name.to_sym).cast_expects([*args]) + end +end |