supermethod2bug.groovy

来自「Groovy动态语言 运行在JVM中的动态语言 可以方便的处理业务逻辑变化大的业」· GROOVY 代码 · 共 128 行

GROOVY
128
字号
/**
 * @version $Revision: 3827 $
 */
 
class SuperMethod2Bug extends GroovyTestCase {
     
    void testBug() {
    	def base = new SuperBase()
    	def value = base.doSomething()
    	assert value == "TestBase"
    	
    	
    	base = new SuperDerived()
    	value = base.doSomething()
    	assert value == "TestDerivedTestBase"
    }

    void testBug2() {
    	def base = new SuperBase()
    	def value = base.foo(2)
    	assert value == "TestBase2"
    	
    	
    	base = new SuperDerived()
    	value = base.foo(3)
    	assert value == "TestDerived3TestBase3"
    }

    void testBug3() {
    	def base = new SuperBase()
    	def value = base.foo(2,3)
    	assert value == "foo(x,y)Base2,3"
    	
    	
    	base = new SuperDerived()
    	value = base.foo(3,4)
    	assert value == "foo(x,y)Derived3,4foo(x,y)Base3,4"
    }

    void testBug4() {
    	def base = new SuperBase("Cheese")
    	def value = base.name
    	assert value == "Cheese"
    	
    	
    	base = new SuperDerived("Cheese")
    	value = base.name
    	assert value == "CheeseDerived"
    }
    
    void testCallsToSuperMethodsReturningPrimitives(){
       def base = new SuperBase("super cheese")
       assert base.longMethod() == 1
       assert base.intMethod() == 1
       assert base.boolMethod() == true
       
       base = new SuperDerived("derived super cheese")
       assert base.longMethod() == 1
       assert base.intMethod() == 1
       assert base.boolMethod() == true       
    }
}

class SuperBase {
    String name

    SuperBase() {
    }
    
    SuperBase(String name) {
        this.name = name
    }
    
    def doSomething() {
    	"TestBase"
    }

    def foo(param) {
    	"TestBase" + param
    }
    
    def foo(x, y) {
    	"foo(x,y)Base" + x + "," + y
    }
    
    boolean boolMethod(){true}
    long longMethod(){1l}
    int intMethod(){1i}
}

class SuperDerived extends SuperBase {
    
	def calls = 0
	
	SuperDerived() {
	}
	
	SuperDerived(String name) {
	    super(name + "Derived")
	}
	
    def doSomething() {
    	/** @todo ++calls causes bug */
    	//calls++
    	/*
    	calls = calls + 1
    	assert calls < 3
    	*/
    	
    	"TestDerived" + super.doSomething()
    }
	
    def foo(param) {
    	"TestDerived" + param + super.foo(param)
    }
	
    def foo(x, y) {
    	"foo(x,y)Derived" + x + "," + y + super.foo(x, y)
    }
    
    // we want to ensure that a call with super, which is directly added into 
    // bytecode without calling MetaClass does correct boxing
    boolean booMethod(){super.boolMethod()}
    int intMethod(){super.intMethod()}
    long longMethod(){super.longMethod()}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?