【问题标题】:Karate match contains deep not working as you would expect空手道比赛包含深度无法正常工作
【发布时间】:2021-10-14 19:56:32
【问题描述】:

我们来自这里:https://github.com/intuit/karate#match-contains-deep

正如它所说:

这会修改 match contains 的行为,以便处理嵌套列表或对象以进行“深度包含”匹配,...,您只想检查各种数据“树”中的某些值

所以让我们试着玩一下,找到一些 {e: 5},它会在树的某个深处,当original = { a: 1, b: 2, c: 3, d: { a: 1, b: 2, e: 5 } }

Feature: Match contains deep

 # match contains deep test (works ok, but this is not "deep" by any stretch of imagination, this is guiding the whole path in the search)
 Scenario: match contains deep test modified original
   * def original = { a: 1, b: 2, c: 3, d: { a: 1, b: 2, e: 5 } }
   * def expected = { d: { e: 5 } }
   * match original contains deep expected

 #Thats was not deep, this is deep (fails, and this is what anyone would understand by "deep")
 Scenario: match contains deep
   * def original = { a: 1, b: 2, c: 3, d: { a: 1, b: 2, e: 5 } }
   * def expected = { e: 5 }
   * match original contains deep expected

 #So you dont need deep (fails)
 Scenario: match contains test modified original without deep
   * def original = { a: 1, b: 2, c: 3, d: { a: 1, b: 2, e: 5 } }
   * def expected = { d: { e: 5 } }
   * match original contains expected

 #So maybe it works with any (fails)
 Scenario: match contains test modified original
   * def original = { a: 1, b: 2, c: 3, d: { a: 1, b: 2, e: 5 } }
   * def expected = { e: 5 }
   * match original contains any expected

 #Maybe I'm tripping with syntax (fails)
 Scenario: match contains deep test my test #2
   * def original = { a: 1, b: 2, c: 3, d: { a: 1, b: 2, e: 5 } }
   * def expected = e: 5
   * match original contains deep expected

 #So maybe I'm tripping with syntax and semantics, and its any (fails)
 Scenario: match contains deep test my test #2
   * def original = { a: 1, b: 2, c: 3, d: { a: 1, b: 2, e: 5 } }
   * def expected = e: 5
   * match original contains any expected
 

所以,要么我没有真正得到这个东西,要么它没有按预期工作,即我想检查放置在树中任何位置的现有键值对。

如果有人能对此有所启发,那就太好了。正如我所看到的,@PeterThomas 正在回答大多数带有空手道标签的问题,我要感谢他为将这个工具交到社区手中所做的巨大努力。

【问题讨论】:

    标签: karate


    【解决方案1】:

    作为该工具的创建者,这就是我对“深度”的定义,所以我想您必须处理它;)顺便说一句,您是第一个发现它具有误导性的人 - 并且以我的诚实观点,它确实按您期望的方式工作。你有 JSON 的“子集”——但你仍然想“修复”路径。这也是大多数人想要的,因为当您处理 JSON 时,您永远不会想要“惊喜”,比如某些值会跳来跳去并出现在其他地方。

    也就是说,您似乎正在寻找的是 JsonPath:

    * def original = { a: 1, b: 2, c: 3, d: { a: 1, b: 2, e: 5 } }
    * match original..* contains deep { e: 5 }
    

    我把它作为一个练习留给你,让你弄清楚它是如何工作的。但请随时寻求提示:P

    【讨论】:

      【解决方案2】:

      @Greco,对不起,迟到了你是对的,最初我还认为 deep 能够在任何地方找到键值对,但遗憾的是它不是这样打算的。 我理解这些术语的方式是:

      contains - 在根对象中找到提到的键,但它们的值完全匹配。 contains deep - 在根对象中找到提到的键,然后可以允许这些键对其值进行部分匹配,就像 contains 允许根对象进行部分匹配一样。

      就像@Peter 所说,你必须形成一条路径,但它可以到达任何级别。

      * def original = { a: 1, b: 2, c: 3, d: { a: 1, b: 2, e: {f:5,g:6} } }
      * def expected = { d: { e: {f:5} } }
      * match original contains deep expected
      

      【讨论】:

      • 提示:我们一直在寻找可以贡献代码的人;)
      • @PeterThomas 呵呵呵呵。我希望我能。我不是核心程序员/开发人员。只是使用工具完成工作的人,拥有一些知识,可以使用足以用于测试目的的代码。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-03
      • 2017-06-12
      • 2020-09-29
      • 1970-01-01
      • 2020-11-30
      • 1970-01-01
      • 2019-03-28
      相关资源
      最近更新 更多