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

Language Ruby Lines 55
MD5 Hash 0f9e08708efc30847cc9a810d3496b93 Estimated Cost $944 (why?)
Repository https://bitbucket.org/mediashelf/fedora-migrator View Raw File
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
require 'facets/typecast.rb'
require 'test/unit'

class TC_TypeCast < Test::Unit::TestCase

  class TestClass
    attr_accessor :my_var
    def initialize(my_var); @my_var = my_var; end

    def to_string(options={})
      @my_var
    end

    class << self
      def from_string(string, options={})
        self.new( string )
      end
    end
  end


  def setup
    @test_string = "this is a test"
    @test_class = TestClass.new(@test_string)
  end

  def test_to_string
    assert_equal( '1234', 1234.cast_to(String) )
  end

  def test_custom_to_string
    assert_equal( @test_string, @test_class.cast_to(String) )
  end

  def test_custom_from_string
    assert_equal( @test_class.my_var, @test_string.cast_to(TestClass).my_var )
  end

  def test_string_to_class
    assert_equal( Test::Unit::TestCase, "Test::Unit::TestCase".cast_to(Class) )
  end

  def test_string_to_time
    assert_equal( "Mon Oct 10 00:00:00 2005", "2005-10-10".cast_to(Time).localtime.strftime("%a %b %d %H:%M:%S %Y") )
  end

  def test_no_converter
    "sfddsf".cast_to( ::Regexp )
    assert(1+1==3, 'should not get here')
  rescue TypeCastException => ex
    assert_equal(TypeCastException, ex.class)
  end
end
Back to Top