忽略数组顺序时匹配响应 [英] match response while ignoring array order
本文介绍了忽略数组顺序时匹配响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给出以下响应:
* def resp = {"a":[{"c": 2},{"c": 1}, {"c":3}], "b":[6,5,4]}
无论顺序数组在哪里,我都想匹配它.
I want to match it no matter the order arrays are in.
我知道我可以使用contains
指令来做到这一点,但是我认为这需要我执行多种匹配模式,例如:
I know I could do it with contains
directive but I think that would require me to do multiple match patterns like:
* match resp.a.[*].c contains [3,2,1]
* match resp.b contains [5,4,6]
是否存在诸如全局配置之类的东西来全部忽略数组顺序,还是我必须滚动包含对象?
Is there something like global config to ignore array order all together or do I have to roll with contains?
推荐答案
以下工作.例如,您也可以指定数组的大小.
The following works. You could also specify the size of the array if you want for example.
* def response = {"a":[2,1,3], "b":[6,5,4]}
* match response.a contains [3,2,1]
* match response.b contains [5,4,6]
* match response == {"a": "#[] #? _ <4, _ >0", "b": "#[] #? _ <7, _ >3"}
编辑以适合您更详细的情况:
Edit to match your more detailed case :
* def resp = {"a":[{"c": 2},{"c": 1}, {"c":3}], "b":[6,5,4]}
* def inside = {"c" : "#number"}
* def schema = { "a": "#[] #(inside)", "b": "#[] #number" }
* match resp == schema
这篇关于忽略数组顺序时匹配响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文