Skip to content
This repository
tag: 1.3.1
Fetching contributors…

Cannot retrieve contributors at this time

file 127 lines (107 sloc) 3.544 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
require 'test/unit'

class TestMethodCache < Test::Unit::TestCase
  def test_simple_hierarchy
    obj = Object.new
    class << obj
      class A; def foo; 1; end; end
      class B < A; end
      class C < B; def bar; foo; end; end

      $test_simple_hierarchy1 = C.new.bar
      class B; def foo; 2; end; end
      $test_simple_hierarchy2 = C.new.bar
    end
    assert_equal(1, $test_simple_hierarchy1)
    assert_equal(2, $test_simple_hierarchy2)
  end

  def test_simple_include
    obj = Object.new
    class << obj
      class A; def foo; 1; end; end
      module B; end
      class C < A; include B; def bar; foo; end; end
      $test_simple_include1 = C.new.bar

      # modules included into modules don't cause flush
      module X; def foo; 2; end; end
      module B; include X; end
      $test_simple_include2 = C.new.bar
      module X; def foo; 3; end; end
      $test_simple_include3 = C.new.bar

      # changes in included modules do cause flush
      module B; def foo; 4; end; end
      $test_simple_include4 = C.new.bar
    end

    assert_equal(1, $test_simple_include1)
    assert_equal(1, $test_simple_include2)
    assert_equal(1, $test_simple_include3)
    assert_equal(4, $test_simple_include4)
  end

  def test_simple_hierarchy_send
    obj = Object.new
    class << obj
      class A; def foo; 1; end; end
      class B < A; end
      class C < B; def bar; foo; end; end

      $test_simple_hierarchy1 = C.new.send :bar
      class B; def foo; 2; end; end
      $test_simple_hierarchy2 = C.new.send :bar
    end
    assert_equal(1, $test_simple_hierarchy1)
    assert_equal(2, $test_simple_hierarchy2)
  end

  def test_simple_include_send
    obj = Object.new
    class << obj
      class A; def foo; 1; end; end
      module B; end
      class C < A; include B; def bar; foo; end; end
      $test_simple_include1 = C.new.send :bar

      # modules included into modules don't cause flush
      module X; def foo; 2; end; end
      module B; include X; end
      $test_simple_include2 = C.new.send :bar
      module X; def foo; 3; end; end
      $test_simple_include3 = C.new.send :bar

      # changes in included modules do cause flush
      module B; def foo; 4; end; end
      $test_simple_include4 = C.new.send :bar
    end

    assert_equal(1, $test_simple_include1)
    assert_equal(1, $test_simple_include2)
    assert_equal(1, $test_simple_include3)
    assert_equal(4, $test_simple_include4)
  end

  def test_simple_alias
    obj = Object.new
    class << obj
      class A; def foo; 1; end; end;
      class B < A; def bar; 2; end; end
      class C < B; end
      class D < C; def go; bar; end; end
      $test_simple_include1 = D.new.bar

      class A; alias bar foo; end
      $test_simple_include2 = D.new.bar

      class C; alias bar foo; end
      $test_simple_include3 = D.new.bar
    end

    assert_equal(2, $test_simple_include1)
    assert_equal(2, $test_simple_include2)
    assert_equal(1, $test_simple_include3)
  end

  def test_simple_alias_send
    obj = Object.new
    class << obj
      class A; def foo; 1; end; end;
      class B < A; def bar; 2; end; end
      class C < B; end
      class D < C; def go; bar; end; end
      $test_simple_include1 = D.new.send :bar

      class A; alias bar foo; end
      $test_simple_include2 = D.new.send :bar

      class C; alias bar foo; end
      $test_simple_include3 = D.new.send :bar
    end

    assert_equal(2, $test_simple_include1)
    assert_equal(2, $test_simple_include2)
    assert_equal(1, $test_simple_include3)
  end
end
Something went wrong with that request. Please try again.