我可以在 Scala 中从一组非逗号分隔的标记创建元组吗?
我正在继续我的测试 DSL 的堆栈溢出驱动编程 - 感谢迄今为止所有做出贡献的人!
目前,我的 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")
}
,when 和 then 是采用 Any
的方法,因此当像这样调用时,它们会传递一个参数元组 - Scala 在调用单参数函数时为何以及如何特殊对待元组? .
理想情况下,我会去掉逗号,这样读起来会更好,只用空格分隔标记。
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 来说它是否太过分了?
I'm continuing my Stack-Overflow-Driven Programming of a testing DSL - thanks to all who have contributed so far!
At the moment my DSL reads like this
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")
}
given, when and then are methods that take Any
, so when called like this they are passed a tuple of the arguments - Why and how is Scala treating a tuple specially when calling a one arg function? .
Ideally I'd drop the commas, so that it reads much nicer, with just spaces separating the tokens
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")
}
Can anyone think of any technique that would let me achieve this goal, or is it going too far for an internal DSL?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要“令牌”之间的方法/运算符。对于成对来说,已经有
->
,例如You need a method/operator between the "tokens". For pairs there is already
->
, e.g.不,您不能从空格分隔的标记创建元组(尽管您可以使用自定义运算符作为分隔符而不是逗号)。您可以做的是使用像这样的无点语法:
许多 DSL 实现(如规范)利用此语法来创建更多“类似文本”的语法。
No, you can't create tuples from space separated tokens (although you can use a custom operator as separator instead of comma). What you can do is to use the dot free syntax like this:
Many DSL implementations (like specs) utilize this syntax to create more "text-like" syntax.
不确定它是否有效:
如前所述,您可以用运算符表示法调用一个参数方法。还有允许动态调用方法的动态特征: http: //www.scala-lang.org/api/current/index.html#scala.Dynamic
因此,如果您从实现动态特征的对象开始,它可能会起作用..
Not sure if it works:
As mentioned before you can call one argument methods in operator notation. There is also the Dynamic Trait which allows dynamic invocation of methods: http://www.scala-lang.org/api/current/index.html#scala.Dynamic
So if you start with an object implementing the dynamic trait it might work ..