Sha256: 86861a87c1dcb160132aaa9cebc0b8efa8cbe581d8d4ef3c93767b7d8a6cb773

Contents?: true

Size: 1.62 KB

Versions: 1

Compression:

Stored size: 1.62 KB

Contents

$: << File.expand_path(File.dirname(__FILE__) + '/../lib')

require 'test/unit'
require 'valuable.rb'
require 'date'
require File.dirname(__FILE__) + '/../examples/phone_number'
class Person < Valuable
  has_value :dob, :klass => :date
end

class Chemical < Valuable
  has_value :ph, :klass => :decimal
end

class TypicalTest < Test::Unit::TestCase

  def test_that_dates_can_be_set_directly
    born_on = Date.civil(1976, 07, 26)
    me = Person.new( :dob => born_on )
    assert_equal( born_on, me.dob )
  end

  def test_that_dates_are_parsed_from_strings
    neil_born_on = 'August 5, 1930'
    neil = Person.new( :dob => neil_born_on )
    assert_equal( Date.civil( 1930, 8, 5 ), neil.dob )
  end

  def test_that_a_date_might_not_be_set_yet_and_that_can_be_ok
    dr_who = Person.new( :dob => nil )
    assert_nil( dr_who.dob )
  end

  def test_that_collections_are_typified
    people = Class.new(Valuable)
    people.has_collection( :phones, :klass => PhoneNumber )

    person = people.new(:phones => ['8668675309'])
    assert_kind_of( Array, person.phones )
    assert_kind_of( PhoneNumber, person.phones.first )
  end

  def test_that_it_discovers_an_invalid_klass
    animal = Class.new(Valuable)
    assert_raises ArgumentError, "Animal doesn't know how to format species with :klass => 'invalid'" do
      animal.has_value :species, :klass => :invalid
    end
  end

  def test_that_decimals_typified
    lemon_juice = Chemical.new(:ph => 1.8)
    assert_kind_of BigDecimal, lemon_juice.ph
  end

  def test_that_nil_input_is_preserved_for_decimals
    lemon_juice = Chemical.new(:ph => nil)
    assert_equal nil, lemon_juice.ph
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
valuable-0.9.1 test/typical_test.rb