File: test_class_intercepter.rb

Project: CI: Class Interception

#!/usr/bin/env ruby

require 'test/unit'
require 'class_intercepter'

class Bark
  def value
    "woof"
  end
end

class Barf
  def value
    "yech"
  end
end

class Dog
  def talk
    Bark.new
  end
end

class TestClassIntercepter < Test::Unit::TestCase
  def test_normal
    d = Dog.new
    assert_equal "woof", d.talk.value
  end

  def test_intercepted
    d = Dog.new
    assert_equal "woof", d.talk.value
    Dog.use_class(:Bark, Barf)
    assert_equal "yech", d.talk.value
    Dog.use_class(:Bark, Bark)
    assert_equal "woof", d.talk.value
  end

  def test_intercepted_in_block
    d = Dog.new
    assert_equal "woof", d.talk.value
    Dog.use_class(:Bark, Barf) do 
      assert_equal "yech", d.talk.value
    end
    assert_equal "woof", d.talk.value
  end

  def test_intercepted_in_block_with_error
    d = Dog.new
    assert_equal "woof", d.talk.value
    Dog.use_class(:Bark, Barf) do 
      fail "Oops"
    end rescue nil
    assert_equal "woof", d.talk.value
  end
end


[ Index ][ Presentation ]
Generated by [ source2html ]