如何在groovy中进行列表比较? [英] How to make a list comparison in groovy?

查看:356
本文介绍了如何在groovy中进行列表比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在位置ID列表和一个位置ID之间进行比较。实际上,我想说的是,如果在位置标识列表 [000000,000000,000000,000000,000000,000000,000000,000000, 000000] 。我可以为列表中的一个实例工作,但我如何检查所有实例?目前它正在检查一个实例,但我需要检查整个列表。



以下是常规代码:

  import groovy.json.JsonSlurper 
def response = messageExchange.response.responseContent
def json = new JsonSlurper()。parseText(response)

def location_id = json.reviews.location_id
assert location_id!= null
def location_id_response = location_id [0] .toString()
def location_id_request = messageExchange.modelItem.testStep。 testCase.getPropertyValue(locationid)
assert location_id_response == location_id_request

log.info location_id_request
log.info location_id_response
log.info location_id

以下是日志信息:

  log.info location_id_request ='000000'
log.info location_id_response ='000000'
log.info location_id = [000000,000000,000000,000000,000000]

V特别是 location_id_response [0] 正在从列表中选择第一个值,但我希望列表中的所有值都使用 location_id_request 与列表中的每个值进行比较,而不仅仅是第一个值。如果我从[0]中删除了'0',它会抛出一个数组错误。

解决方案



  assert location_id.every {it == location_id_request} 





pre $ assert location_id == [location_id_request] * location_id.size()



  assert location_id.distinct()== [location_id_request] 

或者

  assert location_id.distinct()。with {it.head()== location_id_request&& it.size()== 1} 


I want to perform a comparison between a list of location ids and one location id. Virtually I want to say if value '000000' should be displayed in each instance of the list of location ids [000000, 000000, 000000, 000000, 000000]. I can get it working for one instance for the list but how can I check all instances? At the moment it is checking one instance but I require the whole list to be checked.

Below is the groovy code:

import groovy.json.JsonSlurper 
def response = messageExchange.response.responseContent
def json = new JsonSlurper().parseText(response)

def location_id = json.reviews.location_id
assert location_id != null
def location_id_response = location_id[0].toString()
def location_id_request = messageExchange.modelItem.testStep.testCase.getPropertyValue("locationid")
assert location_id_response == location_id_request

log.info location_id_request
log.info location_id_response
log.info location_id

Below is the log information:

log.info location_id_request = '000000'
log.info location_id_response = '000000'
log.info location_id = [000000, 000000, 000000, 000000, 000000]

Virtually location_id_response[0] is picking the first value from the list, but I want all values from the list with location_id_requestbeing compared to each value in the list, not just the first value. If I remove the '0' from [0], it throws an array error.

解决方案

You just need

assert location_id.every { it == location_id_request }

Or

assert location_id == [location_id_request] * location_id.size()

Or

assert location_id.distinct() == [location_id_request]

Or

assert location_id.distinct().with { it.head() == location_id_request && it.size() == 1 }

这篇关于如何在groovy中进行列表比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆