PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/gems/facets-2.4.5/test/more/test_typecast.rb

https://bitbucket.org/mediashelf/fedora-migrator
Ruby | 54 lines | 41 code | 13 blank | 0 comment | 0 complexity | 0f9e08708efc30847cc9a810d3496b93 MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0, IPL-1.0, AGPL-1.0, LGPL-3.0
  1. require 'facets/typecast.rb'
  2. require 'test/unit'
  3. class TC_TypeCast < Test::Unit::TestCase
  4. class TestClass
  5. attr_accessor :my_var
  6. def initialize(my_var); @my_var = my_var; end
  7. def to_string(options={})
  8. @my_var
  9. end
  10. class << self
  11. def from_string(string, options={})
  12. self.new( string )
  13. end
  14. end
  15. end
  16. def setup
  17. @test_string = "this is a test"
  18. @test_class = TestClass.new(@test_string)
  19. end
  20. def test_to_string
  21. assert_equal( '1234', 1234.cast_to(String) )
  22. end
  23. def test_custom_to_string
  24. assert_equal( @test_string, @test_class.cast_to(String) )
  25. end
  26. def test_custom_from_string
  27. assert_equal( @test_class.my_var, @test_string.cast_to(TestClass).my_var )
  28. end
  29. def test_string_to_class
  30. assert_equal( Test::Unit::TestCase, "Test::Unit::TestCase".cast_to(Class) )
  31. end
  32. def test_string_to_time
  33. assert_equal( "Mon Oct 10 00:00:00 2005", "2005-10-10".cast_to(Time).localtime.strftime("%a %b %d %H:%M:%S %Y") )
  34. end
  35. def test_no_converter
  36. "sfddsf".cast_to( ::Regexp )
  37. assert(1+1==3, 'should not get here')
  38. rescue TypeCastException => ex
  39. assert_equal(TypeCastException, ex.class)
  40. end
  41. end