Sha256: a76306e4fa330b4ebe5fc6579eae93a969c69f33e002b9ff14969bd080ba2c37

Contents?: true

Size: 1.8 KB

Versions: 2

Compression:

Stored size: 1.8 KB

Contents

require "helper"

class TestTypes < Test::Unit::TestCase

  test "cast to boolean" do
    assert_equal true, Boolean.typecast(true)
    assert_equal true, Boolean.typecast("true")
    assert_equal true, Boolean.typecast(1)
    assert_equal true, Boolean.typecast("1")
    assert_equal false, Boolean.typecast(false)
    assert_equal false, Boolean.typecast("false")
    assert_equal false, Boolean.typecast(0)
    assert_equal false, Boolean.typecast("0")
    assert_nil Boolean.typecast(123)
    assert_nil Boolean.typecast("huh?")
  end

  test "cast to float" do
    assert_equal 35.5, Float.typecast("35.5")
    assert_nil Float.typecast("huh?")
  end

  test "cast to integer" do
    assert_equal 35, Integer.typecast(35)
    assert_equal 35, Integer.typecast("35")
    assert_equal 35, Integer.typecast("35.5")
    assert_nil Integer.typecast("huh?")
  end

  test "cast to string" do
    assert_equal "35", String.typecast(35)
    assert_equal "true", String.typecast(true)
    assert_equal "false", String.typecast(false)
    assert_equal "anything", String.typecast("anything")
    assert_nil String.typecast(nil)
  end

  test "cast to time" do
    assert_equal Time.local(2009, 12, 28, 10, 35, 25), Time.typecast({
      :year => "2009", :month => "12", :day => "28", :hour => "10", :minute => "35", :second => "25"
    })
    assert_equal Time.utc(2009, 12, 28, 9, 35, 25), Time.typecast("Mon Dec 28 11:35:25 +0200 2009")
    assert_equal Time.local(2009, 12, 28, 9, 35), Time.typecast("28/12/2009 09:35", :format => "%d/%m/%Y %H:%M")
    assert_equal Time.local(2009, 12, 28, 10, 35, 25), Time.typecast(Time.local(2009, 12, 28, 10, 35, 25))
    assert_equal Time.utc(2009, 12, 28, 10, 35, 25), Time.typecast(Time.utc(2009, 12, 28, 10, 35, 25))
    assert_equal Time.local(2009, 12, 28), Time.typecast(Date.new(2009, 12, 28))
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
presenter-0.1.1 test/test_types.rb
presenter-0.1.0 test/test_types.rb