Sha256: a88e3566ddcaae66215815873fadfb364b5f98e2d48c333861da1d7a288f19d6

Contents?: true

Size: 1.1 KB

Versions: 3

Compression:

Stored size: 1.1 KB

Contents

require File.expand_path(File.dirname(__FILE__)) + '/helper'

class TestBlockInit < Test::Unit::TestCase
  
  class Settable
    include Tracksperanto::BlockInit
    
    attr_accessor :foo, :bar
    
    private
    def privatized=(something)
    end
  end
  
  def test_block_init_with_hash
    s = Settable.new(:foo => "x", :bar => "y")
    assert_equal "x", s.foo
    assert_equal "y", s.bar
  end
  
  def test_block_init_with_hash_raises_on_unknown_attribute
    assert_raise(NoMethodError) { Settable.new(:nonexistent => true) }
  end
  
  def test_block_init_with_block_yields_the_object
    within_blk = nil
    s = Settable.new do | f |
      within_blk = f
      f.foo = "x"
    end
    
    assert_equal s, within_blk, "Should have yielded the object to the blk"
    assert_equal "x", s.foo
  end

  def test_block_init_attributes_overwrite_hash_attributes
    s = Settable.new(:foo => "bar") { |f| f.foo = "x" }
    assert_equal "x", s.foo
  end
  
  if RUBY_VERSION > '1.9'
    def test_block_init_uses_public_send
      assert_raise(NoMethodError) { Settable.new(:privatized => true) }
    end
  end
  
  
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
tracksperanto-4.1.2 test/test_block_init.rb
tracksperanto-4.1.0 test/test_block_init.rb
tracksperanto-4.0.0 test/test_block_init.rb