2017-01-13 28 views

回答

2

一些它看起來並不像StringSpec公開此信息,但可以延長StringSpec這樣做。例如: -

class Spec : StringSpec() { 
    init { 
     "triangle.stl" { testCase -> 
      println(testCase.name) 
     } 
    } 

    operator fun String.invoke(test: (TestCase) -> Unit): TestCase { 
     var tc: TestCase? = null 
     tc = invoke(fun() { test(tc!!) }) 
     return tc 
    } 
} 

或避免與exsting String.invoke,你可以用自己的語法擴展它的功能衝突。例如:

class Spec : StringSpec() { 
    init { 
     "triangle.stl" testCase { 
      println(name) 
     } 
    } 

    infix fun String.testCase(test: TestCase.() -> Unit): TestCase { 
     var tc: TestCase? = null 
     tc = invoke { test(tc!!) } 
     return tc 
    } 
} 
1

您將不得不親自存儲對該字符串的引用。像

class stl : StringSpec() { 
    init { 
     val spek = "triangle.stl" 
     spek { 
      // use spek in here 
     } 
    } 
} 
相關問題