This repository has been archived by the owner on Dec 9, 2020. It is now read-only.
forked from gobuffalo/plush
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
broken some tests out to make them easier to find
- Loading branch information
Showing
10 changed files
with
445 additions
and
383 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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><script>alert('pwned')</script></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("<b>unsafe</b>|<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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.