cancel
Showing results for 
Search instead for 
Did you mean: 

Assert int value in an array

SOLVED
Highlighted
Contributor

Assert int value in an array

As on the image my responce data contains an array like this :300, 300, 300, 305, 310, 310, 310, 315, 320.

I need to verify whether the each valu is greater than or equal to 299.

But im getting this error.

Is there any way to resolve this?



1-11-2019 9-10-16 PM.png

1 ACCEPTED SOLUTION

Accepted Solutions
Contributor

Re: Assert int value in an array

int count = 0
price.each {
    if (it > 299)
        count++
}
if (count > 0) assert false, "Not all price is greater than 299"
5 REPLIES 5
Contributor

Re: Assert int value in an array

int count = 0
price.each {
    if (it > 299)
        count++
}
if (count > 0) assert false, "Not all price is greater than 299"
Contributor

Re: Assert int value in an array

What is it??? i cant understand what is the meaning on "it" in the context of groovy scripting


@aaronpliu wrote:
int count = 0
price.each {
    if (it > 299)
        count++
}
if (count > 0) assert false, "Not all price is greater than 299"


.Robot Mad

Community Hero

Re: Assert int value in an array

Goovy is a powerful yet simple to understand.

 

The requirement can be achieved easily using below statement.

 

//Define the list of numbers to be checked
def listToBeChecked = [300, 300, 300, 305, 310, 310, 310, 315, 320]

//Following will check every element against 299 and show error otherwise
assert listToBeChecked.every{element -> element > 299 }, 'check failed'

 



Regards,
Rao.
Community Hero

Re: Assert int value in an array

The piece of code is being "iterated" over the list and "it" is a reserved keyword and refers to the current element of the list during iteration.

 

 



Regards,
Rao.
Contributor

Re: Assert int value in an array

Hi @chathurad,

 

As @nmrao said, 'Groovy' language is simple and groovy, you are able to take the better way to deal with your requirements. Groovy has many extended methods based on Java Class / method.

 

as for "it", a closure using an implicit parameter. you can also specify a parameter like {param ->  xxxxx}

(Example)

for list, often using each / collect / any / every / find / findAll / eachWithIndex...etc

 

def alist = [100, 200, 300, 400, 500]

alist.each { 
    println(it) //output: 100, 200, 300, 400, 500
}

alist.each {element ->
    println(element) //output: 100, 200, 300, 400, 500
}

def blist = alist.collect {
    if (it > 300)
        it
}
println(blist.findAll {it != null}) //output: [400, 500]

def clist = alist.find {
    it > 300
}
println(clist) //output: 400

def dlist = alist.findAll {
    it > 300
}
println(dlist) //output: [400, 500]

def t = alist.every {
    it > 99
}
println(t) //output: true

def t2 = alist.any {
    it > 400
}
println(t2) //output: true

alist.eachWithIndex {element, index ->
    if (element > 300)
        println("$index: $element") //output: 3: 400 / 4: 500
}

you would access official website: http://www.groovy-lang.org/single-page-documentation.html to understand more. and check API: http://www.groovy-lang.org/api.html to understand more usage based on language.

 

Thanks,

/Aaron

New Here?
Join us and watch the welcome video:
Watch the new Interview
Top Kudoed Authors