Skip to content

Commit

Permalink
Merge pull request #178 from gobuffalo/task-test-package
Browse files Browse the repository at this point in the history
task: moving the tests to a _test package
  • Loading branch information
paganotoni authored Jul 27, 2023
2 parents 082e553 + 7381a86 commit 4814270
Show file tree
Hide file tree
Showing 34 changed files with 440 additions and 379 deletions.
19 changes: 10 additions & 9 deletions ast/ast_test.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
package ast
package ast_test

import (
"testing"

"github.com/gobuffalo/plush/v4/ast"
"github.com/gobuffalo/plush/v4/token"
"github.com/stretchr/testify/require"
)

func Test_Program_String(t *testing.T) {
r := require.New(t)
program := &Program{
Statements: []Statement{
&LetStatement{
TokenAble: TokenAble{token.Token{Type: token.LET, Literal: "let"}},
Name: &Identifier{
TokenAble: TokenAble{token.Token{Type: token.IDENT, Literal: "myVar"}},
program := &ast.Program{
Statements: []ast.Statement{
&ast.LetStatement{
TokenAble: ast.TokenAble{token.Token{Type: token.LET, Literal: "let"}},
Name: &ast.Identifier{
TokenAble: ast.TokenAble{token.Token{Type: token.IDENT, Literal: "myVar"}},
Value: "myVar",
},
Value: &Identifier{
TokenAble: TokenAble{token.Token{Type: token.IDENT, Literal: "anotherVar"}},
Value: &ast.Identifier{
TokenAble: ast.TokenAble{token.Token{Type: token.IDENT, Literal: "anotherVar"}},
Value: "anotherVar",
},
},
Expand Down
7 changes: 4 additions & 3 deletions comments_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package plush
package plush_test

import (
"testing"

"github.com/gobuffalo/plush/v4"
"github.com/stretchr/testify/require"
)

Expand All @@ -20,7 +21,7 @@ func Test_Comment(t *testing.T) {
}

for _, test := range input {
s, err := Render(test, NewContext())
s, err := plush.Render(test, plush.NewContext())
r.NoError(err)
r.Contains(s, "Hi")
r.NotContains(s, "this is a comment")
Expand All @@ -42,7 +43,7 @@ func Test_BlockComment(t *testing.T) {
}

for _, test := range input {
s, err := Render(test, NewContext())
s, err := plush.Render(test, plush.NewContext())
r.NoError(err)
r.Contains(s, "Hi")
r.NotContains(s, []string{"this is", "a block comment"})
Expand Down
15 changes: 8 additions & 7 deletions context_test.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
package plush
package plush_test

import (
"html/template"
"testing"

"golang.org/x/sync/errgroup"

"github.com/gobuffalo/plush/v4"
"github.com/stretchr/testify/require"
)

func Test_Context_Set(t *testing.T) {
r := require.New(t)
c := NewContext()
c := plush.NewContext()
r.Nil(c.Value("foo"))
c.Set("foo", "bar")
r.NotNil(c.Value("foo"))
}

func Test_Context_Set_Concurrency(t *testing.T) {
r := require.New(t)
c := NewContext()
c := plush.NewContext()

wg := errgroup.Group{}
f := func() error {
Expand All @@ -35,7 +36,7 @@ func Test_Context_Set_Concurrency(t *testing.T) {

func Test_Context_Get(t *testing.T) {
r := require.New(t)
c := NewContext()
c := plush.NewContext()
r.Nil(c.Value("foo"))
c.Set("foo", "bar")
r.Equal("bar", c.Value("foo"))
Expand All @@ -44,7 +45,7 @@ func Test_Context_Get(t *testing.T) {
func Test_NewSubContext_Set(t *testing.T) {
r := require.New(t)

c := NewContext()
c := plush.NewContext()
r.Nil(c.Value("foo"))

sc := c.New()
Expand All @@ -58,7 +59,7 @@ func Test_NewSubContext_Set(t *testing.T) {
func Test_NewSubContext_Get(t *testing.T) {
r := require.New(t)

c := NewContext()
c := plush.NewContext()
c.Set("foo", "bar")

sc := c.New()
Expand All @@ -67,7 +68,7 @@ func Test_NewSubContext_Get(t *testing.T) {

func Test_Context_Override_Helper(t *testing.T) {
r := require.New(t)
c := NewContext()
c := plush.NewContext()
c.Set("debug", func(i interface{}) template.HTML {
return template.HTML("DEBUG")
})
Expand Down
7 changes: 4 additions & 3 deletions error_test.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
package plush
package plush_test

import (
"database/sql"

"errors"
"testing"

"github.com/gobuffalo/plush/v4"
"github.com/stretchr/testify/require"
)

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

ctx := NewContext()
ctx := plush.NewContext()
ctx.Set("sqlError", func() error {
return sql.ErrNoRows
})

_, err := Render(`<%= sqlError() %>`, ctx)
_, err := plush.Render(`<%= sqlError() %>`, ctx)
r.True(errors.Is(err, sql.ErrNoRows))
}
9 changes: 5 additions & 4 deletions escape_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package plush
package plush_test

import (
"html/template"
"testing"

"github.com/gobuffalo/plush/v4"
"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())
s, err := plush.Render(input, plush.NewContext())
r.NoError(err)
r.Equal("<p>&lt;script&gt;alert(&#39;pwned&#39;)&lt;/script&gt;</p>", s)
}
Expand All @@ -20,7 +21,7 @@ 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{}{
s, err := plush.Render(input, plush.NewContextWith(map[string]interface{}{
"escapedHTML": func() string {
return "<b>unsafe</b>"
},
Expand All @@ -36,7 +37,7 @@ func Test_Escaping_EscapeExpression(t *testing.T) {
r := require.New(t)
input := `C:\\<%= "temp" %>`

s, err := Render(input, NewContext())
s, err := plush.Render(input, plush.NewContext())
r.NoError(err)
r.Equal(`C:\temp`, s)
}
18 changes: 10 additions & 8 deletions example_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package plush
package plush_test

import (
"fmt"
"html/template"
"log"

"github.com/gobuffalo/plush/v4"
)

// ExampleRender using `if`, `for`, `else`, functions, etc...
Expand All @@ -20,10 +22,10 @@ func ExampleRender() {
<% } %>
</html>`

ctx := NewContext()
ctx := plush.NewContext()
ctx.Set("names", []string{"john", "paul", "george", "ringo"})

s, err := Render(html, ctx)
s, err := plush.Render(html, ctx)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -55,7 +57,7 @@ let greet = fn(n) {
%>
<h1><%= greet(h["name"]) %></h1>`

s, err := Render(html, NewContext())
s, err := plush.Render(html, plush.NewContext())
if err != nil {
log.Fatal(err)
}
Expand All @@ -74,22 +76,22 @@ func ExampleRender_customHelperFunctions() {
<% } %>
`

ctx := NewContext()
ctx := plush.NewContext()
ctx.Set("one", func() int {
return 1
})
ctx.Set("greet", func(s string) string {
return fmt.Sprintf("Hi %s", s)
})
ctx.Set("can", func(s string, help HelperContext) (template.HTML, error) {
ctx.Set("can", func(s string, help plush.HelperContext) (template.HTML, error) {
if s == "update" {
h, err := help.Block()
return template.HTML(h), err
}
return "", nil
})

s, err := Render(html, ctx)
s, err := plush.Render(html, ctx)
if err != nil {
log.Fatal(err)
}
Expand All @@ -103,7 +105,7 @@ func ExampleRender_customHelperFunctions() {
func ExampleRender_forIterator() {
html := `<%= for (v) in between(3,6) { %><%=v%><% } %>`

s, err := Render(html, NewContext())
s, err := plush.Render(html, plush.NewContext())
if err != nil {
log.Fatal(err)
}
Expand Down
Loading

0 comments on commit 4814270

Please sign in to comment.