Skip to content

Commit

Permalink
simplified some of test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
sio4 committed Oct 4, 2022
1 parent 452ed6b commit 73b2405
Show file tree
Hide file tree
Showing 4 changed files with 273 additions and 445 deletions.
361 changes: 108 additions & 253 deletions if_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,192 +6,111 @@ import (
"github.com/stretchr/testify/require"
)

func Test_Render_If(t *testing.T) {
r := require.New(t)
input := `<% if (true) { return "hi"} %>`
s, err := Render(input, NewContext())
r.NoError(err)
r.Equal("", s)
}

func Test_Render_If_ReturnTrue(t *testing.T) {
r := require.New(t)
input := `<%= if (true == true) { return "hi"} %>`
s, err := Render(input, NewContext())
r.NoError(err)
r.Equal("hi", s)
}

func Test_Render_If_ReturnFalse_No_Output(t *testing.T) {
r := require.New(t)
input := `<%= if (true != true) { return "hi"} %>`
s, err := Render(input, NewContext())
r.NoError(err)
r.Equal("", s)
}

func Test_Render_If_True_EqualFalse(t *testing.T) {
r := require.New(t)
input := `<% let test = true %><%= if (test == false) { return "hi"} %>`
s, err := Render(input, NewContext())
r.NoError(err)
r.Equal("", s)
}

func Test_Render_If_Is_Test_True(t *testing.T) {
r := require.New(t)
input := `<% let test = true %><%= if (test == true) { return "hi"} %>`
s, err := Render(input, NewContext())
r.NoError(err)
r.Equal("hi", s)
}

func Test_Render_If_Test_Is_Not_True(t *testing.T) {
r := require.New(t)
input := `<% let test = true %><%= if (test != true) { return "hi"} %>`
s, err := Render(input, NewContext())
r.NoError(err)
r.Equal("", s)
}

func Test_Render_If_Test_Is_Not_Equal_One(t *testing.T) {
r := require.New(t)
input := `<% let test = 1 %><%= if (test != true) { return "hi"} %>`
_, err := Render(input, NewContext())
r.Error(err, "line 1: unable to operate (!=) on int and bool")

}

func Test_Render_If_Test_Is_Equal_One(t *testing.T) {
r := require.New(t)
input := `<% let test = 1 %><%= if (test == 1) { return "hi"} %>`
s, err := Render(input, NewContext())
r.NoError(err)
r.Equal("hi", s)

}

func Test_Render_If_Return(t *testing.T) {
r := require.New(t)
input := `<%= if (true) { return "hi"} %>`
s, err := Render(input, NewContext())
r.NoError(err)
r.Equal("hi", s)
}

func Test_Render_If_Return_HTML(t *testing.T) {
r := require.New(t)
input := `<%= if (true) { %>hi<%} %>`
s, err := Render(input, NewContext())
r.NoError(err)
r.Equal("hi", s)
}

func Test_Render_If_And(t *testing.T) {
r := require.New(t)
input := `<%= if (false && true) { %> hi <%} %>`
s, err := Render(input, NewContext())
r.NoError(err)
r.Equal("", s)
}

func Test_Render_If_And_True_True(t *testing.T) {
r := require.New(t)
input := `<%= if (2 == 2 && 1 == 1) { return "hi" } %>`
s, err := Render(input, NewContext())
r.NoError(err)
r.Equal("hi", s)
}

func Test_Render_If_Or(t *testing.T) {
r := require.New(t)
input := `<%= if (false || true) { %>hi<%} %>`
s, err := Render(input, NewContext())
r.NoError(err)
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<%} %>`
s, err := Render(input, NewContext())
r.NoError(err)
r.Equal("", s)
}

func Test_Render_If_NotNil(t *testing.T) {
r := require.New(t)
input := `<%= if (!names) { %>hi<%} %>`
s, err := Render(input, NewContext())
r.NoError(err)
r.Equal("hi", s)
}

func Test_Render_If_Nil_Else(t *testing.T) {
r := require.New(t)
input := `<%= if (names && len(names) >= 1) { %>hi<%} else { %>something else<% } %>`
s, err := Render(input, NewContext())
r.NoError(err)
r.Equal("something else", s)
}

func Test_Render_If_Else_Return(t *testing.T) {
r := require.New(t)
input := `<p><%= if (false) { return "hi"} else { return "bye"} %></p>`
s, err := Render(input, NewContext())
r.NoError(err)
r.Equal("<p>bye</p>", s)
}

func Test_Render_If_LessThan(t *testing.T) {
r := require.New(t)
input := `<p><%= if (1 < 2) { return "hi"} else { return "bye"} %></p>`
s, err := Render(input, NewContext())
r.NoError(err)
r.Equal("<p>hi</p>", s)
}

func Test_Render_If_BangFalse(t *testing.T) {
r := require.New(t)
input := `<p><%= if (!false) { return "hi"} else { return "bye"} %></p>`
s, err := Render(input, NewContext())
r.NoError(err)
r.Equal("<p>hi</p>", s)
}

func Test_Render_If_NotEq(t *testing.T) {
r := require.New(t)
input := `<p><%= if (1 != 2) { return "hi"} else { return "bye"} %></p>`
s, err := Render(input, NewContext())
r.NoError(err)
r.Equal("<p>hi</p>", s)
}

func Test_Render_If_GtEq(t *testing.T) {
r := require.New(t)
input := `<p><%= if (1 >= 2) { return "hi"} else { return "bye"} %></p>`
s, err := Render(input, NewContext())
r.NoError(err)
r.Equal("<p>bye</p>", s)
}

func Test_Render_If_Else_True(t *testing.T) {
r := require.New(t)
input := `<p><%= if (true) { %>hi<% } else { %>bye<% } %></p>`
s, err := Render(input, NewContext())
r.NoError(err)
r.Equal("<p>hi</p>", s)
}

func Test_Render_If_Else_If_Else_True(t *testing.T) {
func Test_If_Condition(t *testing.T) {
tests := []struct {
input string
expected string
name string
success bool
}{
{`<%= if (true) { return "good"} else { return "bad"} %>`, "good", "if_else_true", true},
{`<%= if (false) { return "good"} else { return "bad"} %>`, "bad", "if_else_false", true},
{`<% if (true) { return "good"} else { return "bad"} %>`, "", "missing=", true},
{`<%= if (true) { %>good<% } %>`, "good", "value_from_template_html", true},
{`<%= if (false) { return "good"} %>`, "", "if_false", true},
{`<%= if (!false) { return "good"} %>`, "good", "if_bang_false", true},
{`<%= if (true == true) { return "good"} else { return "bad"} %>`, "good", "bool_true_is_true", true},
{`<%= if (true != true) { return "good"} else { return "bad"} %>`, "bad", "bool_true_is_not_true", true},
{`<% let test = true %><%= if (test == false) { return "good"} %>`, "", "let_var_is_false", true},
{`<% let test = true %><%= if (test == true) { return "good"} %>`, "good", "let_var_is_true", true},
{`<% let test = true %><%= if (test != true) { return "good"} %>`, "", "let_var_is_not_true", true},
{`<% let test = 1 %><%= if (test != true) { return "good"} %>`, "line 1: unable to operate (!=) on int and bool", "let_var_is_not_bool", false},
{`<% let test = 1 %><%= if (test == 1) { return "good"} %>`, "good", "let_var_is_1", true},
{`<%= if (false && true) { %>good<% } %>`, "", "logical_false_and_true", true},
{`<%= if (2 == 2 && 1 == 1) { %>good<% } %>`, "good", "logical_true_and_true", true},
{`<%= if (false || true) { %>good<% } %>`, "good", "logical_false_or_true", true},
{`<%= if (1 == 2 || 2 == 1) { %>good<% } %>`, "", "logical_false_or_false", true},
{`<%= if (names && len(names) >= 1) { %>good<% } %>`, "", "nil_and", true},
{`<%= if (names && len(names) >= 1) { %>good<% } else { %>else<% } %>`, "else", "nil_and_else", true},
{`<%= if (names) { %>good<% } %>`, "", "nil", true},
{`<%= if (!names) { %>good<% } %>`, "good", "not_nil", true},
{`<%= if (1 == 2) { %>good<% } %>`, "", "compare_equal_to", true},
{`<%= if (1 != 2) { %>good<% } %>`, "good", "compare_not_equal_to", true},
{`<%= if (1 < 2) { %>good<% } %>`, "good", "compare_less_than", true},
{`<%= if (1 <= 2) { %>good<% } %>`, "good", "compare_less_than_equal_to", true},
{`<%= if (1 > 2) { %>good<% } %>`, "", "compare_greater_than", true},
{`<%= if (1 >= 2) { %>good<% } %>`, "", "compare_greater_than_equal_to", true},
{`<%= if ("foo" ~= "bar") { %>good<% } %>`, "", "if_match_foo_bar", true},
{`<%= if ("foo" ~= "^fo") { %>good<% } %>`, "good", "if_match_foo_^fo", true},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
r := require.New(t)
s, err := Render(tc.input, NewContext())
if tc.success {
r.NoError(err)
r.Equal(tc.expected, s)
} else {
r.Error(err, tc.expected)
}
})
}
}

func Test_Condition_Only(t *testing.T) {
ctx_empty := NewContext()

ctx_with_paths := NewContext()
ctx_with_paths.Set("paths", "cart")

tests := []struct {
input string
expected string
name string
success bool
context *Context
}{
{`<%= paths == nil %>`, "true", "unknown_equal_to_nil", true, ctx_empty},
{`<%= nil == paths %>`, "true", "nil_equal_to_unknown", true, ctx_empty},

{`<%= !paths %>`, "false", "NOT SET", true, ctx_with_paths},
{`<%= !pages %>`, "true", "NOT UNKNOWN", true, ctx_with_paths},

{`<%= paths || pages %>`, "true", "SET_or_unknown", true, ctx_with_paths}, // fast return
{`<%= pages || paths %>`, "true", "unknown_or_SET", true, ctx_with_paths},
{`<%= paths || pages == "cart" %>`, "true", "SET_or_unknown_equal", true, ctx_with_paths}, // fast return
{`<%= pages == "cart" || paths %>`, "true", "unknown_equal_or_SET", true, ctx_with_paths},
{`<%= paths == "cart" || pages %>`, "true", "EQUAL_or_unknown", true, ctx_with_paths}, // fast return
{`<%= pages || paths == "cart" %>`, "true", "unknown_or_EQUAL", true, ctx_with_paths},
{`<%= paths == "cart" || pages == "cart" %>`, "true", "EQUAL_or_unknown_equal", true, ctx_with_paths}, // fast return
{`<%= pages == "cart" || paths == "cart" %>`, "true", "unknown_equal_or_EQUAL", true, ctx_with_paths},

{`<%= paths && pages %>`, "false", "set_and_UNKNOWN==false", true, ctx_with_paths},
{`<%= pages && paths %>`, "false", "UNKNOWN_and_set==false", true, ctx_with_paths}, // fast return
{`<%= paths && pages == "cart" %>`, "false", "set_and_UNKNOWN_equal==false", true, ctx_with_paths},
{`<%= pages == "cart" && paths %>`, "false", "UNKNOWN_equal_and_set==false", true, ctx_with_paths}, // fast return
{`<%= paths == "cart" && pages %>`, "false", "equal_and_UNKNOWN", true, ctx_with_paths},
{`<%= pages && paths == "cart" %>`, "false", "UNKNOWN_and_equal", true, ctx_with_paths}, // fast return
{`<%= paths == "cart" && pages == "cart" %>`, "false", "equal_and_UNKNOWN_equal", true, ctx_with_paths},
{`<%= pages == "cart" && paths == "cart" %>`, "false", "UNKNOWN_equal_and_equal", true, ctx_with_paths}, // fast return
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
r := require.New(t)
s, err := Render(tc.input, tc.context)
if tc.success {
r.NoError(err)
r.Equal(tc.expected, s)
} else {
r.Error(err, tc.expected)
}
})
}
}

func Test_If_Else_If_Else_True(t *testing.T) {
r := require.New(t)
ctx := NewContext()
input := `<p><%= if (state == "foo") { %>hi foo<% } else if (state == "bar") { %>hi bar<% } else if (state == "fizz") { %>hi fizz<% } else { %>hi buzz<% } %></p>`
Expand All @@ -217,14 +136,6 @@ func Test_Render_If_Else_If_Else_True(t *testing.T) {
r.Equal("<p>hi buzz</p>", s)
}

func Test_Render_If_Matches(t *testing.T) {
r := require.New(t)
input := `<p><%= if ("foo" ~= "bar") { return "hi" } else { return "bye" } %></p>`
s, err := Render(input, NewContext())
r.NoError(err)
r.Equal("<p>bye</p>", s)
}

func Test_If_String_Truthy(t *testing.T) {
r := require.New(t)

Expand All @@ -242,26 +153,7 @@ func Test_If_String_Truthy(t *testing.T) {
r.Equal("<p>hi</p>", s)
}

func Test_Render_If_Variable_Is_Set(t *testing.T) {
r := require.New(t)
input := `<%= if (names) { %>hi<%} %>`
ctx := NewContext()
ctx.Set("names", "123")
s, err := Render(input, ctx)
r.NoError(err)
r.Equal("hi", s)
}

func Test_Render_If_Variable_Not_Set(t *testing.T) {
r := require.New(t)
input := `<%= if (names) { %>hi<%} %>`

s, err := Render(input, NewContext())
r.NoError(err)
r.Equal("", s)
}

func Test_Render_If_Variable_Not_Set_But_Or_Condition_Is_True(t *testing.T) {
func Test_If_Variable_Not_Set_But_Or_Condition_Is_True_Complex(t *testing.T) {
r := require.New(t)
ctx := NewContext()
ctx.Set("path", "cart")
Expand All @@ -273,7 +165,7 @@ func Test_Render_If_Variable_Not_Set_But_Or_Condition_Is_True(t *testing.T) {
r.Equal("hi", s)
}

func Test_Render_If_Variable_Not_Set_But_Or_Condition_While_Node_Is_True_Includes_Syntax_Error_Last_Node(t *testing.T) {
func Test_If_Syntax_Error_On_Last_Node(t *testing.T) {
r := require.New(t)
ctx := NewContext()
ctx.Set("paths", "cart")
Expand All @@ -283,7 +175,7 @@ func Test_Render_If_Variable_Not_Set_But_Or_Condition_While_Node_Is_True_Include
r.Error(err)
}

func Test_Render_If_Variable_Not_Set_But_Or_Condition_While_Node_Is_True_Includes_Syntax_Error_First_Node(t *testing.T) {
func Test_If_Syntax_Error_On_First_Node(t *testing.T) {
r := require.New(t)
ctx := NewContext()
ctx.Set("paths", "cart")
Expand All @@ -292,40 +184,3 @@ func Test_Render_If_Variable_Not_Set_But_Or_Condition_While_Node_Is_True_Include
_, err := Render(input, ctx)
r.Error(err)
}

func Test_Render_If_Variable_Not_Set_But_Or_Condition_Left_Node_Is_True(t *testing.T) {
r := require.New(t)
ctx := NewContext()
ctx.Set("paths", "cart")
input := `<%= if ( paths == "cart" || pages ) { %>hi<%} %>`

s, err := Render(input, ctx)
r.NoError(err)
r.Equal("hi", s)
}

func Test_Render_If_Variable_Not_Set_But_Or_Condition_Right_Node_Is_True(t *testing.T) {
r := require.New(t)
ctx := NewContext()
ctx.Set("pages", "cart")
input := `<%= if ( paths == "cart" || pages ) { %>hi<%} %>`

s, err := Render(input, ctx)
r.NoError(err)
r.Equal("hi", s)
}

func Test_Condition_UnsetIsNil(t *testing.T) {
r := require.New(t)
ctx := NewContext()

input := `<%= paths == nil %>`
s, err := Render(input, ctx)
r.NoError(err)
r.Equal("true", s)

input = `<%= nil == paths %>`
s, err = Render(input, ctx)
r.NoError(err)
r.Equal("true", s)
}
Loading

0 comments on commit 73b2405

Please sign in to comment.