Sha256: b0ad511f44f860e97d1b9670cafbefbb0a0c09990b2653345c4fa1f4d13322f5

Contents?: true

Size: 1.65 KB

Versions: 33

Compression:

Stored size: 1.65 KB

Contents

# -*- encoding : utf-8 -*-
require File.expand_path(File.dirname(__FILE__)) + '/helper'

class TestCasts < Test::Unit::TestCase
  D = 0.0001
  
  class Testable
    include Tracksperanto::Casts
    attr_accessor :vanilla, :str_attr, :int_attr, :float_attr
    cast_to_string :str_attr
    cast_to_int :int_attr
    cast_to_float :float_attr
    cast_to_bool :truthy
  end
  
  class Junk
    def to_s
      "Random"
    end
  end
  
  def test_cast_to_float
    t = Testable.new
    assert_kind_of Float, t.float_attr, "Uninitialized float attr should be a float"
    assert_in_delta 0, t.float_attr, D
    
    t.float_attr = "3"
    assert_kind_of Float, t.float_attr
    assert_in_delta 3.0, t.float_attr, D
    
    t.float_attr = "a"
    assert_kind_of Float, t.float_attr
    assert_in_delta 0, t.float_attr, D
  end
  
  def test_cast_to_bool
    t = Testable.new
    assert_equal false, t.truthy
    
    t.truthy = nil
    assert_equal false, t.truthy
    
    t.truthy = false
    assert_equal false, t.truthy
    
    t.truthy = "yes"
    assert_equal true, t.truthy
    
    t.truthy = 1
    assert_equal true, t.truthy
  end
  
  def test_cast_to_int
    t = Testable.new
    
    assert_equal 0, t.int_attr, "Uninitialized int attr should be an int"
    
    t.int_attr = "3.1"
    assert_kind_of Fixnum, t.int_attr
    assert_equal 3, t.int_attr
    
    t.float_attr = 3.1
    assert_kind_of Fixnum, t.int_attr
    assert_equal 3, t.int_attr
  end
  
  def test_cast_to_string
    t = Testable.new
    
    assert_equal '', t.str_attr
    t.str_attr = 3.123
    assert_equal "3.123", t.str_attr
    
    t.str_attr = Junk.new
    assert_equal "Random", t.str_attr
  end
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
tracksperanto-3.2.2 test/test_casts.rb
tracksperanto-3.2.1 test/test_casts.rb
tracksperanto-3.2.0 test/test_casts.rb
tracksperanto-3.1.0 test/test_casts.rb
tracksperanto-3.0.1 test/test_casts.rb
tracksperanto-3.0.0 test/test_casts.rb
tracksperanto-2.12.0 test/test_casts.rb
tracksperanto-2.11.3 test/test_casts.rb
tracksperanto-2.11.2 test/test_casts.rb
tracksperanto-2.11.1 test/test_casts.rb
tracksperanto-2.11.0 test/test_casts.rb
tracksperanto-2.10.0 test/test_casts.rb
tracksperanto-2.9.9 test/test_casts.rb