Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type checking for function's arguments #474

Closed
mralves opened this issue Nov 22, 2023 · 0 comments
Closed

Type checking for function's arguments #474

mralves opened this issue Nov 22, 2023 · 0 comments
Labels

Comments

@mralves
Copy link

mralves commented Nov 22, 2023

When calling a function that receives a single float (any other type seems to work properly), expr.Compile now allows the argument to be of any type.
These tests reflect the issue:

func TestIssue(t *testing.T) {
	t.Parallel()
	testCases := []struct {
		name string
		code string
		fail bool
	}{
		{
			name: `when the parameter is a string`,
			code: `func("invalid")`,
			fail: true,
		},
		{
			name: `when the parameter is a bool`,
			code: `func(true)`,
			fail: true,
		},
		{
			name: `when the parameter is an array`,
			code: `func([])`,
			fail: true,
		},
		{
			name: `when the parameter is a map`,
			code: `func({})`,
			fail: true,
		},
		{
			name: `when the parameter is an integer`,
			code: `func(1)`,
			fail: false,
		},
		{
			name: `when the parameter is a float`,
			code: `func(1.5)`,
			fail: false,
		},
	}

	for _, tc := range testCases {
		ltc := tc
		t.Run(ltc.name, func(t *testing.T) {
			t.Parallel()
			function := expr.Function("func", func(params ...any) (any, error) {
				return true, nil
			}, new(func(float64) bool))
			_, err := expr.Compile(ltc.code, function)
			if ltc.fail {
				if err == nil {
					t.Error("expected an error, but it was nil")
					t.FailNow()
				}
			} else {
				if err != nil {
					t.Errorf("expected nil, but it was %v", err)
					t.FailNow()
				}
			}
		})
	}
}

The behavior was changed in v1.15.0 and still persists in v1.15.4.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants