2011-06-10 38 views
2

我正在继续我的测试DSL的堆栈溢出驱动编程 - 感谢迄今为止所做的贡献!我可以从Scala中的非逗号分隔的一组令牌创建一个元组吗?

此刻我的DSL读这样​​

scenario("Incorrect password") { 
    given("user visits", the[AdminHomePage]) 
    then(the[SignInPage], "is displayed")  

    when("username", "admin", "and password", "wrongpassword", "are entered") 
    then(the[SignInPage], "is displayed")  
    and("Error message is", "Sign in failed") 
} 
给出

,当再是采取Any方法,所谓这样它们传递的参数的元组的时候 - Why and how is Scala treating a tuple specially when calling a one arg function?

理想我就把你的逗号,所以它的内容更漂亮,只用空格分隔令牌

scenario("Incorrect password") { 
    given("user visits" the[AdminHomePage]) 
    then(the[SignInPage] "is displayed")  

    when("username" "admin" "and password" "wrongpassword" "are entered") 
    then(the[SignInPage] "is displayed")  
    and("Error message is" "Sign in failed") 
} 

谁能想到它可以让我实现这个目标,又或者是去任何技术对于内部DSL来说太远了?

回答

1

您需要在“代币”之间有一个方法/操作符。对于已经有->,例如

println("hello" -> 12 -> '!' -> 12.0) 
//--> (((hello,12),!),12.0) 
+0

我曾考虑使用|或者类似的东西,但是一切似乎至少和逗号一样突兀,因为需要更多的工作。 – 2011-06-10 11:57:03

+1

通常“is”似乎在你的情况下工作得很好:'然后([SignInPage]是“显示”)','和(“ErrorMessage”是“Sign in failed”) – Landei 2011-06-10 12:39:59

1

不,您不能从空格分隔的标记创建元组(尽管您可以使用自定义运算符作为分隔符而不是逗号)。你可以做的是使用点免费语法是这样的:

obj method obj method obj ... 

许多DSL实现(如规格)利用这个语法来创建更多的“文字样”的语法。

+0

我在想,如果这是一个路径,但其它令牌必须是方法的名称? – 2011-06-10 10:58:30

+0

是的,所以你可以添加“扩展方法”字符串类型,但方法名称本身不能是字符串,例如: '“hello”is“displayed”and“groovy”' – 2011-06-10 14:06:13

1

不知道是否它的工作原理:

正如之前提到的,你可以拨打运营商的符号一个参数的方法。也有动态特质允许的方法动态调用:http://www.scala-lang.org/api/current/index.html#scala.Dynamic

所以如果你开始实现动态特性它可能工作的对象..

+0

但我仍然需要其他令牌可以解释为方法名称...? – 2011-06-10 11:58:06

+0

它将被解释为方法名称,但该方法不需要存在。 – 2011-06-10 12:01:07

+0

确实,但这意味着没有空格,不能只使用字符串等 – 2011-06-10 12:17:58

相关问题