Sha256: 30c897486e3bb5547181d15909dee7a08022168916f922a6a8c4602f39e53ee9

Contents?: true

Size: 1.76 KB

Versions: 6

Compression:

Stored size: 1.76 KB

Contents

require 'test/unit'
require 'rubygems'
require File.join(File.dirname(__FILE__), '../init')

class CoercedTest < Test::Unit::TestCase
  def test_is_a_feature
    assert_equal 1, 1.must.be.coerced(Integer)
    assert_equal 1, 1.must.be.coerced(Integer, String)
    assert_equal 1, 1.must.be.coerced(String, Numeric)

    assert_raises(Invalid)     { "1".must.be.coerced(Integer) }
    assert_raises(Invalid)     { "1".must.be.coerced(Integer, Range) }
    assert_raises(NotImplementedError) { "1".must.be.coerced(Integer){raise NotImplementedError} }
  end

  def test_coecing_succeeded
    assert_equal 1,   1.must.be.coerced(Integer, String=>proc{|i|i.to_i})
    assert_equal 1, "1".must.be.coerced(Integer, String=>proc{|i|i.to_i})
    assert_equal 1, "1".must.be.coerced(Integer, String=>proc{|i|i.to_i}, Range=>proc{|i|i.first})
    assert_equal 1, "1".must.be.coerced(Integer, Range, String=>proc{|i|i.to_i}, Range=>proc{|i|i.first})
    assert_equal 1, "1".must.be.coerced(File, Integer, Range, String=>proc{|i|i.to_i}, Range=>proc{|i|i.first})
  end

  def test_coecing_failed
    assert_raises(Invalid)     { "1".must.be.coerced(Integer, String=>proc{|i| /a/}) }
    assert_raises(NotImplementedError) { "1".must.be.coerced(Integer, String=>proc{|i| /a/}){raise NotImplementedError} }
  end

  def test_cascaded_coecing_succeeded
    assert_equal 1,    1.must.be.coerced(Integer, Symbol=>:to_s, String=>:to_i)
    assert_equal 1,  "1".must.be.coerced(Integer, Symbol=>:to_s, String=>:to_i)
    assert_equal 1, :"1".must.be.coerced(Integer, Symbol=>:to_s, String=>:to_i)
  end

  def test_detect_livelock
    assert_raises(Invalid){ "1".must.be.coerced(Integer, Symbol=>:to_s, String=>:intern)}
    assert_raises(Invalid){:"1".must.be.coerced(Integer, Symbol=>:to_s, String=>:intern)}
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
must-0.2.8 test/coerced_test.rb
must-0.2.7 test/coerced_test.rb
must-0.2.6 test/coerced_test.rb
must-0.2.5 test/coerced_test.rb
must-0.2.4 test/coerced_test.rb
must-0.2.3 test/coerced_test.rb