我做错了什么:
assert 'foo' == 'foo' //PASS
assert '500' == '500' //PASS
assert '500' < '1000' //FAIL <-- Supposed to pass
assert '500' <= '1000' //FAIL <-- Supposed to pass
assert '1000' > '500' //FAIL <-- Supposed to pass
assert '1000' >= '500' //FAIL <-- Supposed to pass
它适用于可定制的“条件”对象:
class Condition {
static def compareClosure = [
'==' : { a, b -> a == b},
'!=' : { a, b -> a != b},
'<' : { a, b -> a < b},
'<=' : { a, b -> a <= b},
'>' : { a, b -> a > b},
'>=' : { a, b -> a >= b}
]
String comparator
def value
Condition(String comparator, String value) {
this.value = value
this.comparator = comparator
}
boolean isSatisfiedBy(def value) {
compareClosure[comparator](value, this.value)
}
}
所以
assert new Condition('<=', '1000').isSatisfiedBy('500') //FAIL
有没有办法在不将值转换为数字类型的情况下做到这一点?