Skip to content
This repository has been archived by the owner on Dec 9, 2020. It is now read-only.

Commit

Permalink
broken some tests out to make them easier to find
Browse files Browse the repository at this point in the history
  • Loading branch information
markbates committed Mar 15, 2018
1 parent 14e8d79 commit 279b913
Show file tree
Hide file tree
Showing 10 changed files with 445 additions and 383 deletions.
42 changes: 42 additions & 0 deletions escape_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package plush

import (
"html/template"
"testing"

"github.com/stretchr/testify/require"
)

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

input := `<p><%= "<script>alert('pwned')</script>" %></p>`
s, err := Render(input, NewContext())
r.NoError(err)
r.Equal("<p>&lt;script&gt;alert(&#39;pwned&#39;)&lt;/script&gt;</p>", s)
}

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

input := `<%= escapedHTML() %>|<%= unescapedHTML() %>|<%= raw("<b>unsafe</b>") %>`
s, err := Render(input, NewContextWith(map[string]interface{}{
"escapedHTML": func() string {
return "<b>unsafe</b>"
},
"unescapedHTML": func() template.HTML {
return "<b>unsafe</b>"
},
}))
r.NoError(err)
r.Equal("&lt;b&gt;unsafe&lt;/b&gt;|<b>unsafe</b>|<b>unsafe</b>", s)
}

func Test_Escaping_EscapeExpression(t *testing.T) {
r := require.New(t)
input := `C:\\<%= "temp" %>`

s, err := Render(input, NewContext())
r.NoError(err)
r.Equal(`C:\temp`, s)
}
61 changes: 61 additions & 0 deletions hashes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package plush

import (
"testing"

"github.com/stretchr/testify/require"
)

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

input := `<%= m["first"] + " " + m["last"] %>|<%= a[0+1] %>`
s, err := Render(input, NewContextWith(map[string]interface{}{
"m": map[string]string{"first": "Mark", "last": "Bates"},
"a": []string{"john", "paul"},
}))
r.NoError(err)
r.Equal("Mark Bates|paul", s)
}

func Test_Render_HashCall(t *testing.T) {
r := require.New(t)
input := `<%= m["a"] %>`
ctx := NewContext()
ctx.Set("m", map[string]string{
"a": "A",
})
s, err := Render(input, ctx)
r.NoError(err)
r.Equal("A", s)
}

func Test_Render_HashCall_OnAttribute(t *testing.T) {
r := require.New(t)
input := `<%= m.MyMap[key] %>`
ctx := NewContext()
ctx.Set("m", struct {
MyMap map[string]string
}{
MyMap: map[string]string{"a": "A"},
})
ctx.Set("key", "a")
s, err := Render(input, ctx)
r.NoError(err)
r.Equal("A", s)
}

func Test_Render_HashCall_OnAttribute_IntoFunction(t *testing.T) {
r := require.New(t)
input := `<%= debug(m.MyMap[key]) %>`
ctx := NewContext()
ctx.Set("m", struct {
MyMap map[string]string
}{
MyMap: map[string]string{"a": "A"},
})
ctx.Set("key", "a")
s, err := Render(input, ctx)
r.NoError(err)
r.Equal("<pre>A</pre>", s)
}
60 changes: 60 additions & 0 deletions line_number_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package plush

import (
"testing"

"github.com/stretchr/testify/require"
)

func Test_LineNumberErrors(t *testing.T) {
r := require.New(t)
input := `<p>
<%= f.Foo %>
</p>`

_, err := Render(input, NewContext())
r.Error(err)
r.Contains(err.Error(), "line 2:")
}

func Test_LineNumberErrors_ForLoop(t *testing.T) {
r := require.New(t)
input := `
<%= for (n) in numbers.Foo { %>
<%= n %>
<% } %>
`

_, err := Render(input, NewContext())
r.Error(err)
r.Contains(err.Error(), "line 2:")
}

func Test_LineNumberErrors_ForLoop2(t *testing.T) {
r := require.New(t)
input := `
<%= for (n in numbers.Foo { %>
<%= if (n == 3) { %>
<%= n %>
<% } %>
<% } %>
`

_, err := Parse(input)
r.Error(err)
r.Contains(err.Error(), "line 2:")
}

func Test_LineNumberErrors_InsideForLoop(t *testing.T) {
r := require.New(t)
input := `
<%= for (n) in numbers { %>
<%= n.Foo %>
<% } %>
`
ctx := NewContext()
ctx.Set("numbers", []int{1, 2})
_, err := Render(input, ctx)
r.Error(err)
r.Contains(err.Error(), "line 3:")
}
45 changes: 45 additions & 0 deletions numbers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package plush

import (
"testing"

"github.com/stretchr/testify/require"
)

// support identifiers containing digits, but not starting with a digits
func Test_Identifiers_With_Digits(t *testing.T) {
r := require.New(t)
input := `<%= my123greet %> <%= name3 %>`

ctx := NewContext()
ctx.Set("my123greet", "hi")
ctx.Set("name3", "mark")

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

func Test_Render_Var_ends_in_Number(t *testing.T) {
r := require.New(t)
ctx := NewContextWith(map[string]interface{}{
"myvar1": []string{"john", "paul"},
})
s, err := Render(`<%= for (n) in myvar1 {return n}`, ctx)
r.NoError(err)
r.Equal("johnpaul", s)
}

func Test_Render_AllowsManyNumericTypes(t *testing.T) {
r := require.New(t)
input := `<%= i32 %> <%= u32 %> <%= i8 %>`

ctx := NewContext()
ctx.Set("i32", int32(1))
ctx.Set("u32", uint32(2))
ctx.Set("i8", int8(3))

s, err := Render(input, ctx)
r.NoError(err)
r.Equal("1 2 3", s)
}
Loading

0 comments on commit 279b913

Please sign in to comment.