From 6258824e807cba19eea2b6c48965cb4203d03eb8 Mon Sep 17 00:00:00 2001 From: Mark Bates Date: Fri, 22 Dec 2017 11:53:49 -0500 Subject: [PATCH] ok, i think this precedences stuff is finally under control --- if_test.go | 8 ++++++++ parser/parser_test.go | 4 ++++ parser/precedences.go | 5 +++-- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/if_test.go b/if_test.go index 8b0c1b1..c6b8ca4 100644 --- a/if_test.go +++ b/if_test.go @@ -54,6 +54,14 @@ func Test_Render_If_Or(t *testing.T) { r.Equal("hi", s) } +func Test_Render_If_Or_False_False(t *testing.T) { + r := require.New(t) + input := `<%= if (1 == 2 || 2 == 1) { return "hi" } else { return "bye" } %>` + s, err := Render(input, NewContext()) + r.NoError(err) + r.Equal("bye", s) +} + func Test_Render_If_Nil(t *testing.T) { r := require.New(t) input := `<%= if (names && len(names) >= 1) { %>hi<%} %>` diff --git a/parser/parser_test.go b/parser/parser_test.go index 271396b..5c30f68 100644 --- a/parser/parser_test.go +++ b/parser/parser_test.go @@ -298,6 +298,10 @@ func Test_OperatorPrecedence(t *testing.T) { "foo ~= bar", "(foo ~= bar)", }, + { + "1 != 2 || 2 != 1", + "((1 != 2) || (2 != 1))", + }, } for _, tt := range tests { diff --git a/parser/precedences.go b/parser/precedences.go index 59a0a2a..aa0e335 100644 --- a/parser/precedences.go +++ b/parser/precedences.go @@ -5,6 +5,7 @@ import "github.com/gobuffalo/plush/token" const ( _ int = iota LOWEST // + ANDOR // || or && EQUALS // == LESSGREATER // > or < SUM // + @@ -18,8 +19,8 @@ var precedences = map[token.Type]int{ token.EQ: EQUALS, token.NOT_EQ: EQUALS, token.MATCHES: EQUALS, - token.AND: EQUALS, - token.OR: EQUALS, + token.AND: ANDOR, + token.OR: ANDOR, token.LT: LESSGREATER, token.LTEQ: LESSGREATER, token.GT: LESSGREATER,