require 'test/yarvtest'

class TestClass < YarvTestBase

  def test_simple
    ae %q(
      class C
        def m(a,b)
          a+b
        end
      end
      C.new.m(1,2)
    ) do
      remove_const(:C)
    end

    ae %q(
      class A
      end
      class A::B
        def m
          A::B.name
        end
      end
      A::B.new.m
    ) do
      remove_const(:A)
    end

    #ae %q(
    #  class (class C;self; end)::D < C
    #    self.name
    #  end
    #) do
    #  remove_const(:C)
    #end
    
  end
  
  def test_sub
    ae %q(
      class A
        def m
          123
        end
      end
      
      class B < A
      end
      
      B.new.m
    ) do
      remove_const(:A)
      remove_const(:B)
    end
    
    ae %q(
      class A
        class B
          class C
            def m
              456
            end
          end
        end
      end
      
      class A::BB < A::B::C
      end
      
      A::BB.new.m
    ) do
      remove_const(:A)
    end
  end

  def test_attr
    ae %q(
      class C
        def set
          @a = 1
        end
        def get
          @a
        end
      end
      c = C.new
      c.set
      c.get
    ) do
      remove_const(:C)
    end
  end

  def test_initialize
    ae %q{
      class C
        def initialize
          @a = :C
        end
        def a
          @a
        end
      end
      
      C.new.a
    } do
      remove_const(:C)
    end
  end

  def test_to_s
    ae %q{
      class C
        def to_s
          "hoge"
        end
      end
      
      "ab#{C.new}cd"
    } do
      remove_const(:C)
    end
    
  end

  def test_attr_accessor
    ae %q{
      class C
        attr_accessor :a
        attr_reader   :b
        attr_writer   :c
        def b_write
          @b = 'huga'
        end
        def m a
          'test_attr_accessor' + @b + @c
        end
      end

      c = C.new
      c.a = true
      c.c = 'hoge'
      c.b_write
      c.m(c.b)
    } do
      remove_const(:C)
    end
  end

  def test_super
    ae %q{
      class C
        def m1
          100
        end
        
        def m2 a
          a + 100
        end
      end
      
      class CC < C
        def m1
          super() * 100
        end
        
        def m2
          super(200) * 100
        end
      end

      a = CC.new
      a.m1 + a.m2
    } do
      remove_const(:C)
      remove_const(:CC)
    end
  end

  def test_zsuper
    ae %q{
      class C
        def m1
          100
        end
        
        def m2 a
          a + 100
        end

        def m3 a
          a + 200
        end
      end
      
      class CC < C
        def m1
          super * 100
        end
        
        def m2 a
          super  * 100
        end

        def m3 a
          a = 400
          super * 100
        end
      end

      a = CC.new
      a.m1 + a.m2(200) + a.m3(300)
    } do
      remove_const(:C)
      remove_const(:CC)
    end
  end

  def test_zsuper2
    ae %q{
      class C1
        def m
          10
        end
      end

      class C2 < C1
        def m
          20 + super
        end
      end
      
      class C3 < C2
        def m
          30 + super
        end
      end
      
      C3.new.m
    } do
      remove_const(:C1)
      remove_const(:C2)
      remove_const(:C3)
    end
  end
end


