如何在groovy中进行列表比较?

时间:2017-01-07 18:03:00

标签: groovy

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

下面是groovy代码:

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]

实际上location_id_response[0]正在从列表中选择第一个值,但我希望列表中包含location_id_request的所有值与列表中的每个值进行比较,而不仅仅是第一个值。如果我删除了' 0' 0从[0]开始,它会抛出一个数组错误。

1 个答案:

答案 0 :(得分:1)

你只需要

assert location_id.every { it == location_id_request }

或者

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 }