diff --git a/compiler/compiler.go b/compiler/compiler.go index 6f0713b4..4831a310 100644 --- a/compiler/compiler.go +++ b/compiler/compiler.go @@ -368,7 +368,7 @@ func (c *compiler) BinaryNode(node *ast.BinaryNode) { c.compile(node.Left) c.derefInNeeded(node.Left) c.compile(node.Right) - c.derefInNeeded(node.Left) + c.derefInNeeded(node.Right) if l == r && l == reflect.Int && leftAndRightAreSimple { c.emit(OpEqualInt) @@ -382,7 +382,7 @@ func (c *compiler) BinaryNode(node *ast.BinaryNode) { c.compile(node.Left) c.derefInNeeded(node.Left) c.compile(node.Right) - c.derefInNeeded(node.Left) + c.derefInNeeded(node.Right) c.emit(OpEqual) c.emit(OpNot) diff --git a/test/deref/deref_test.go b/test/deref/deref_test.go index e3bfb794..684794a0 100644 --- a/test/deref/deref_test.go +++ b/test/deref/deref_test.go @@ -4,8 +4,9 @@ import ( "context" "testing" - "github.com/expr-lang/expr" "github.com/stretchr/testify/require" + + "github.com/expr-lang/expr" ) func TestDeref_binary(t *testing.T) { @@ -200,3 +201,39 @@ func TestDeref_nil_in_pointer_of_interface(t *testing.T) { require.Equal(t, true, output) }) } + +func TestDeref_сommutative(t *testing.T) { + a := "ok" + b := "ok" + + type Env struct { + A string + B *string + } + + env := Env{ + A: a, + B: &b, + } + + tests := []struct { + code string + want bool + }{ + {`A == B`, true}, + {`B == A`, true}, + {`A != B`, false}, + {`B != A`, false}, + } + + for _, test := range tests { + t.Run(test.code, func(t *testing.T) { + program, err := expr.Compile(test.code, expr.Env(env)) + require.NoError(t, err) + + out, err := expr.Run(program, env) + require.NoError(t, err) + require.Equal(t, test.want, out) + }) + } +}