我可以在 Scala 中从一组非逗号分隔的标记创建元组吗?

发布于 2024-11-15 02:39:55 字数 951 浏览 3 评论 0原文

我正在继续我的测试 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暗恋未遂 2024-11-22 02:39:55

您需要“令牌”之间的方法/运算符。对于成对来说,已经有 ->,例如

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

You need a method/operator between the "tokens". For pairs there is already ->, e.g.

println("hello" -> 12 -> '!' -> 12.0)
//--> (((hello,12),!),12.0)
香橙ぽ 2024-11-22 02:39:55

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

obj method obj method obj ...

许多 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:

obj method obj method obj ...

Many DSL implementations (like specs) utilize this syntax to create more "text-like" syntax.

高跟鞋的旋律 2024-11-22 02:39:55

不确定它是否有效:

如前所述,您可以用运算符表示法调用一个参数方法。还有允许动态调用方法的动态特征: 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 ..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文