github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/testing/base/compare.go (about) 1 package base 2 3 import "github.com/MontFerret/ferret/pkg/runtime/core" 4 5 type CompareOperator int 6 7 const ( 8 NotEqualOp CompareOperator = 0 9 EqualOp CompareOperator = 1 10 LessOp CompareOperator = 2 11 LessOrEqualOp CompareOperator = 3 12 GreaterOp CompareOperator = 4 13 GreaterOrEqualOp CompareOperator = 5 14 ) 15 16 func (op CompareOperator) String() string { 17 switch op { 18 case NotEqualOp: 19 return "not equal to" 20 case EqualOp: 21 return "equal to" 22 case LessOp: 23 return "less than" 24 case LessOrEqualOp: 25 return "less than or equal to" 26 case GreaterOp: 27 return "greater than" 28 default: 29 return "greater than or equal to" 30 } 31 } 32 33 func (op CompareOperator) Compare(args []core.Value) (bool, error) { 34 err := core.ValidateArgs(args, 2, 3) 35 36 if err != nil { 37 return false, err 38 } 39 40 actual := args[0] 41 expected := args[1] 42 43 var result bool 44 45 switch op { 46 case NotEqualOp: 47 result = actual.Compare(expected) != 0 48 case EqualOp: 49 result = actual.Compare(expected) == 0 50 case LessOp: 51 result = actual.Compare(expected) == -1 52 case LessOrEqualOp: 53 c := actual.Compare(expected) 54 result = c == -1 || c == 0 55 case GreaterOp: 56 result = actual.Compare(expected) == 1 57 default: 58 c := actual.Compare(expected) 59 result = c == 1 || c == 0 60 } 61 62 return result, nil 63 }