#==============================================================================# # $Id: test_kagemusha.rb 27 2007-08-15 14:35:31Z yuyakato $ #==============================================================================# require File.dirname(__FILE__) + "/test_helper.rb" #==============================================================================# class TestKagemusha < Test::Unit::TestCase def setup @musha = Kagemusha.new(String) end def test_initialize klass = String meta = class << klass; self; end assert_equal(klass, @musha.instance_eval { @klass }) assert_equal(meta, @musha.instance_eval { @meta }) assert_equal({}, @musha.instance_eval { @class_methods }) assert_equal({}, @musha.instance_eval { @instance_methods }) end def test_define_class_method_and_undefine block1 = proc { 1 } block2 = proc { 2 } assert_equal(nil, @musha.instance_eval { @class_methods[:foo] }) assert_equal(nil, @musha.instance_eval { @class_methods[:bar] }) @musha.define_class_method(:foo, &block1) assert_equal(block1, @musha.instance_eval { @class_methods[:foo] }) assert_equal(nil, @musha.instance_eval { @class_methods[:bar] }) @musha.define_class_method("bar", &block2) assert_equal(block1, @musha.instance_eval { @class_methods[:foo] }) assert_equal(block2, @musha.instance_eval { @class_methods[:bar] }) @musha.undefine_class_method("foo") assert_equal(nil, @musha.instance_eval { @class_methods[:foo] }) assert_equal(block2, @musha.instance_eval { @class_methods[:bar] }) @musha.undefine_class_method(:bar) assert_equal(nil, @musha.instance_eval { @class_methods[:foo] }) assert_equal(nil, @musha.instance_eval { @class_methods[:bar] }) end def test_defs_and_undefs block = proc { 1 } assert_equal(nil, @musha.instance_eval { @class_methods[:foo] }) @musha.defs(:foo, &block) assert_equal(block, @musha.instance_eval { @class_methods[:foo] }) @musha.undefs(:foo) assert_equal(nil, @musha.instance_eval { @class_methods[:foo] }) end def test_define_instance_method_and_undefine block1 = proc { 1 } block2 = proc { 2 } assert_equal(nil, @musha.instance_eval { @instance_methods[:foo] }) assert_equal(nil, @musha.instance_eval { @instance_methods[:bar] }) @musha.define_instance_method(:foo, &block1) assert_equal(block1, @musha.instance_eval { @instance_methods[:foo] }) assert_equal(nil, @musha.instance_eval { @instance_methods[:bar] }) @musha.define_instance_method("bar", &block2) assert_equal(block1, @musha.instance_eval { @instance_methods[:foo] }) assert_equal(block2, @musha.instance_eval { @instance_methods[:bar] }) @musha.undefine_instance_method("foo") assert_equal(nil, @musha.instance_eval { @instance_methods[:foo] }) assert_equal(block2, @musha.instance_eval { @instance_methods[:bar] }) @musha.undefine_instance_method(:bar) assert_equal(nil, @musha.instance_eval { @instance_methods[:foo] }) assert_equal(nil, @musha.instance_eval { @instance_methods[:bar] }) end def test_def_and_undef block = proc { 1 } assert_equal(nil, @musha.instance_eval { @instance_methods[:foo] }) @musha.def(:foo, &block) assert_equal(block, @musha.instance_eval { @instance_methods[:foo] }) @musha.undef(:foo) assert_equal(nil, @musha.instance_eval { @instance_methods[:foo] }) end def test_swap_class_method @musha = Kagemusha.new(Regexp) @musha.defs(:compile) { /bar/ } assert_equal(/foo/, Regexp.compile("foo")) @musha.swap { assert_equal(/bar/, Regexp.compile("foo")) } assert_equal(/foo/, Regexp.compile("foo")) end def test_swap_instance_method target = "foo" @musha.def(:to_s) { "bar" } assert_equal("foo", target.to_s) @musha.swap { assert_equal("bar", target.to_s) } assert_equal("foo", target.to_s) end end #==============================================================================# #==============================================================================#