Forum Discussion
nmrao
2 years agoChampion Level 3
Here is the another example for your reference with Map data type unlike list earlier.
It is noticeable in the below that here containsKey is used, and also in used to find if the key / word is there in the dictionary or not.
Also see how any, every keywords are used at the end.
Please follow in-line comments below:
//Let us assume, there is a dictionary and one would like to check
//if certain word is there in it or not before know the meaning of that word.
//Define the dictionary
def dictionary = ['word1': 'Meaning of word 1', 'word2': 'Meaning of word 2', 'wordx': 'Meaning of word x', 'wordz': 'Meaning of word z']
//Closure to check if a word found or not with logging
def checkForWord = {
if (dictionary.containsKey(it)) {
log.info("$it found")
}
else {
log.info ("$it not found")
}
}
//Actually checking for words
checkForWord('wordx')
checkForWord('wordp')
//Another way of check using assert - this is how it should be done in the automated tests
//Check if at least one word of the list is found in the dictionary
assert ['wordp', 'wordz'].any { it in dictionary.keySet()}, 'Not even one word found'
//Check if all the words of the list are present in the dictionary
assert ['word1', 'word3'].every { it in dictionary.keySet()}, 'Not all words found'
Related Content
- 2 years ago
- 4 years ago
- 2 years ago
- 7 years ago
Recent Discussions
- 18 hours ago
- 17 days ago