gname1test.groovy

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

GROOVY
145
字号
package gls.ch06.s05;

import gls.ch06.s05.testClasses.Tt1cgi;
import gls.ch06.s05.testClasses.Tt1cgo;
import gls.ch06.s05.testClasses.Tt1gi;
import gls.ch06.s05.testClasses.Tt1go;
import gls.ch06.s05.testClasses.Tt1;
import gls.ch06.s05.testClasses.Tt1c;

class GName1Test extends GroovyTestCase {
  void testObjectSupportNameHandling() {
    Tt1  obj = new Tt1()  // Test POJO
    def newX = "new x"
    def newX1 = "new x1"
    def newX2 = "new x2"
    
    assert obj.x == "property"
    assert obj.@x == "field"
    assert obj.x() == "method"
    
    obj.x = newX
    obj.@x = newX1
    
    assert obj.x == newX
    assert obj.@x == newX1
    
    obj.setX newX2
    
    assert obj.x == newX2
    assert obj.@x == newX1
  }
  
  void testObjectSupportNameHandling1() {
    Tt1go  obj = new Tt1go()  // Test class subclassing GroovyObjectSupport
    def newX = "new x"
    def newX1 = "new x1"
    def newX2 = "new x2"
    
    assert obj.x == "property"
    assert obj.@x == "field"
    assert obj.x() == "method"
    
    obj.x = newX
    obj.@x = newX1
    
    assert obj.x == newX
    assert obj.@x == newX1
    
    obj.setX newX2
    
    assert obj.x == newX2
    assert obj.@x == newX1
  }
  
  void testObjectSupportNameHandling2() {
    Tt1  obj = new Tt1gi()  // Test POJO implementing GroovyObject
    def newX = "new x"
    def newX1 = "new x1"
    def newX2 = "new x2"
    
    assert obj.x == "dynamic property"
    assert obj.@x == "field"
    assert obj.x() == "dynamic method"
    
    obj.x = newX
    obj.@x = newX1
    
    assert obj.x == "dynamic property"
    assert obj.@x == newX1
    
    obj.setX newX2
    
    assert obj.x == "dynamic property"
    assert obj.@x == newX1
  }
  
  void testObjectSupportNameHandlingWitnClosureValues() {
    Tt1c obj = new Tt1c()  // Test POJO
    def newX = {"new x"}
    def newX1 = {"new x1"}
    def newX2 = {"new x2"}
    
    assert (obj.x)() == "property"
    assert obj.@x() == "field"
    assert obj.x() == "method"
    
      
    obj.x = newX
    obj.@x = newX1
    
    assert (obj.x)() == newX()
    assert obj.@x() == newX1()
    
    obj.setX newX2
    
    assert (obj.x)() == newX2()
    assert obj.@x() == newX1()
  }
  
  void testObjectSupportNameHandlingWitnClosureValues1() {
    Tt1cgo obj = new Tt1cgo()  // class subclassing GroovyObjectSupport
    def newX = {"new x"}
    def newX1 = {"new x1"}
    def newX2 = {"new x2"}
    
    assert (obj.x)() == "property"
    assert obj.@x() == "field"
    assert obj.x() == "method"
    
      
    obj.x = newX
    obj.@x = newX1
    
    assert (obj.x)() == newX()
    assert (obj.@x)() == newX1()
    
    obj.setX newX2
    
    assert (obj.x)() == newX2()
    assert (obj.@x)() == newX1()
  }
  
  void testObjectSupportNameHandlingWitnClosureValues2() {
    Tt1c obj = new Tt1cgi()  // Test POJO implementing GroovyObject
    def newX = {"new x"}
    def newX1 = {"new x1"}
    def newX2 = {"new x2"}
    
    assert (obj.x)() == "property"
    assert (obj.@x)() == "field"  // can't write obj.@x() - syntax error
    assert obj.x() == "method"
    
      
    obj.x = newX
    obj.@x = newX1
    
    assert (obj.x)() == newX()
    assert (obj.@x)() == newX1()
    
    obj.setX newX2
    
    assert (obj.x)() == newX2()
    assert (obj.@x)() == newX1()
  }
}

⌨️ 快捷键说明

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