You can use the Groovy Collection.intersect(Collection right) method and check if the returned collection is as large as the argument passed as the argument.
You should use the String.tokenize() method earlier to generate a List from String instead of String.split() , which returns an array of String:
def sublist = ["My", "Homer"] def list = "Hi My Name is Homer".tokenize() assert sublist.size() == list.intersect(sublist).size()
Alternatively, you can use the Groovy Object.every(Closure closure) method and check to see if each item has a sublist in the list:
assert sublist.every { list.contains(it) }
However, the shortest way is to use the standard Java Collection API:
assert list.containsAll(sublist)
Christoph metzendorf
source share