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

Can't get simple test case to work #69

Closed
payneio opened this issue Sep 26, 2019 · 5 comments
Closed

Can't get simple test case to work #69

payneio opened this issue Sep 26, 2019 · 5 comments

Comments

@payneio
Copy link

payneio commented Sep 26, 2019

Seems straightforward, but for the life of me, can't get this to work. Keep getting ca_test.go:19: Expected no errors. Got: undefined: beliefs:

func TestExpr(t *testing.T) {
	expression := "beliefs['temp'] > 60 and beliefs['temp'] < 76"
	env := map[string]interface{}{
		`beliefs`: map[string]interface{}{
			`temp`: 58.6,
		},
	}
	out, err := expr.Eval(expression, expr.Env(env))
	if err != nil {
		t.Errorf("Expected no errors. Got: %v", err)
	}
	if out != false {
		t.Errorf("Expecte `false`, got: %v", out)
	}
}
@antonmedv
Copy link
Member

Hi,

Try this fix:

	expression := "beliefs['temp'] > 60 and beliefs['temp'] < 76"
	env := map[string]interface{}{
		`beliefs`: map[string]interface{}{
			`temp`: 58.6,
		},
	}
-	out, err := expr.Eval(expression, expr.Env(env))
+	out, err := expr.Eval(expression, env)
	if err != nil {
		t.Errorf("Expected no errors. Got: %v", err)
	}
	if out != false {
		t.Errorf("Expecte `false`, got: %v", out)
	}

@antonmedv
Copy link
Member

antonmedv commented Sep 26, 2019

Also, I've added check what in future this misuse should be notified: 4d40254

@payneio
Copy link
Author

payneio commented Sep 26, 2019

Yes... I've been working it today without the Env wrapper and it works fine. What is the point of the Env wrapper then? It creates a type table of some sort?

@antonmedv
Copy link
Member

Yes, expr.Env creates type tables for type checker. Take a look at this example: https://godoc.org/github.com/antonmedv/expr#example-Compile
Compile ahead of time will allow faster execution and type checking of expression before going to production.

@payneio
Copy link
Author

payneio commented Sep 26, 2019 via email

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

No branches or pull requests

2 participants