2024-06-01 16:31:50 +02:00
|
|
|
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
|
|
|
// All rights reserved.
|
|
|
|
|
|
|
|
// t_func-base_test.go
|
|
|
|
package expr
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFuncBase(t *testing.T) {
|
|
|
|
section := "Func-Base"
|
|
|
|
|
|
|
|
inputs := []inputType{
|
|
|
|
/* 1 */ {`isNil(nil)`, true, nil},
|
|
|
|
/* 2 */ {`v=nil; isNil(v)`, true, nil},
|
|
|
|
/* 3 */ {`v=5; isNil(v)`, false, nil},
|
|
|
|
/* 4 */ {`int(true)`, int64(1), nil},
|
|
|
|
/* 5 */ {`int(false)`, int64(0), nil},
|
|
|
|
/* 6 */ {`int(3.1)`, int64(3), nil},
|
|
|
|
/* 7 */ {`int(3.9)`, int64(3), nil},
|
|
|
|
/* 8 */ {`int("432")`, int64(432), nil},
|
|
|
|
/* 9 */ {`int("1.5")`, nil, errors.New(`strconv.Atoi: parsing "1.5": invalid syntax`)},
|
|
|
|
/* 10 */ {`int("432", 4)`, nil, errors.New(`int(): too much params -- expected 1, got 2`)},
|
2024-06-05 05:52:27 +02:00
|
|
|
/* 11 */ {`int(nil)`, nil, errors.New(`int(): can't convert nil to int`)},
|
2024-06-01 16:31:50 +02:00
|
|
|
/* 12 */ {`isInt(2+1)`, true, nil},
|
|
|
|
/* 13 */ {`isInt(3.1)`, false, nil},
|
|
|
|
/* 14 */ {`isFloat(3.1)`, true, nil},
|
|
|
|
/* 15 */ {`isString("3.1")`, true, nil},
|
|
|
|
/* 16 */ {`isString("3" + 1)`, true, nil},
|
|
|
|
/* 17 */ {`isList(["3", 1])`, true, nil},
|
|
|
|
/* 18 */ {`isDict({"a":"3", "b":1})`, true, nil},
|
|
|
|
/* 19 */ {`isFract(1|3)`, true, nil},
|
|
|
|
/* 20 */ {`isFract(3|1)`, false, nil},
|
|
|
|
/* 21 */ {`isRational(3|1)`, true, nil},
|
|
|
|
/* 22 */ {`fract("2.2(3)")`, newFraction(67, 30), nil},
|
|
|
|
/* 23 */ {`fract("1.21(3)")`, newFraction(91, 75), nil},
|
|
|
|
/* 24 */ {`fract(1.21(3))`, newFraction(91, 75), nil},
|
|
|
|
/* 25 */ {`fract(1.21)`, newFraction(121, 100), nil},
|
|
|
|
/* 26 */ {`dec(2)`, float64(2), nil},
|
|
|
|
/* 27 */ {`dec(2.0)`, float64(2), nil},
|
|
|
|
/* 28 */ {`dec("2.0")`, float64(2), nil},
|
|
|
|
/* 29 */ {`dec(true)`, float64(1), nil},
|
|
|
|
/* 30 */ {`dec(true")`, nil, errors.New("[1:11] missing string termination \"")},
|
|
|
|
/* 31 */ {`dec()`, nil, errors.New(`dec(): too few params -- expected 1, got 0`)},
|
|
|
|
/* 32 */ {`dec(1,2,3)`, nil, errors.New(`dec(): too much params -- expected 1, got 3`)},
|
|
|
|
/* 33 */ {`isBool(false)`, true, nil},
|
|
|
|
/* 34 */ {`fract(1|2)`, newFraction(1, 2), nil},
|
|
|
|
/* 35 */ {`fract(12,2)`, newFraction(6, 1), nil},
|
|
|
|
/* 36 */ {`bool(2)`, true, nil},
|
|
|
|
/* 37 */ {`bool(1|2)`, true, nil},
|
|
|
|
/* 38 */ {`bool(1.0)`, true, nil},
|
|
|
|
/* 39 */ {`bool("1")`, true, nil},
|
|
|
|
/* 40 */ {`bool(false)`, false, nil},
|
|
|
|
/* 41 */ {`bool([1])`, nil, errors.New(`bool(): can't convert list to bool`)},
|
|
|
|
/* 42 */ {`dec(false)`, float64(0), nil},
|
|
|
|
/* 43 */ {`dec(1|2)`, float64(0.5), nil},
|
|
|
|
/* 44 */ {`dec([1])`, nil, errors.New(`dec(): can't convert list to float`)},
|
|
|
|
// /* 45 */ {`string([1])`, nil, errors.New(`string(): can't convert list to string`)},
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Setenv("EXPR_PATH", ".")
|
|
|
|
|
|
|
|
// parserTestSpec(t, section, inputs, 2)
|
|
|
|
parserTest(t, section, inputs)
|
|
|
|
}
|