-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added partial helper (tweaked) (#73)
- Loading branch information
Showing
4 changed files
with
245 additions
and
0 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
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
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,49 @@ | ||
package plush | ||
|
||
import ( | ||
"html/template" | ||
"strings" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
// PartialFeeder is callback function should implemented on application side. | ||
type PartialFeeder func(string) (string, error) | ||
|
||
func partialHelper(name string, data map[string]interface{}, help HelperContext) (template.HTML, error) { | ||
if help.Context == nil || help.Context.data == nil { | ||
return "", errors.New("invalid context. abort") | ||
} | ||
for k, v := range data { | ||
help.Set(k, v) | ||
} | ||
|
||
pf, ok := help.Value("partialFeeder").(func(string) (string, error)) | ||
if !ok { | ||
return "", errors.New("could not found partial feeder from helpers") | ||
} | ||
|
||
var part string | ||
var err error | ||
if part, err = pf(name); err != nil { | ||
return "", err | ||
} | ||
|
||
if part, err = Render(part, help.Context); err != nil { | ||
return "", err | ||
} | ||
|
||
if layout, ok := data["layout"].(string); ok { | ||
return partialHelper( | ||
layout, | ||
map[string]interface{}{"yield": template.HTML(part)}, | ||
help) | ||
} | ||
|
||
if ct, ok := help.Value("contentType").(string); ok { | ||
if strings.Contains(ct, "javascript") && strings.HasSuffix(name, ".html") { | ||
part = template.JSEscapeString(string(part)) | ||
} | ||
} | ||
return template.HTML(part), err | ||
} |
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,190 @@ | ||
package plush | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_PartialHelper_Nil_Context(t *testing.T) { | ||
r := require.New(t) | ||
|
||
name := "index" | ||
data := map[string]interface{}{} | ||
help := HelperContext{} | ||
|
||
html, err := partialHelper(name, data, help) | ||
r.Error(err) | ||
r.Contains(err.Error(), "invalid context") | ||
r.Equal("", string(html)) | ||
} | ||
|
||
func Test_PartialHelper_Blank_Data(t *testing.T) { | ||
r := require.New(t) | ||
|
||
name := "index" | ||
data := map[string]interface{}{} | ||
help := HelperContext{Context: NewContext()} | ||
help.Context.data = nil | ||
|
||
html, err := partialHelper(name, data, help) | ||
r.Error(err) | ||
r.Contains(err.Error(), "invalid context") | ||
r.Equal("", string(html)) | ||
} | ||
|
||
func Test_PartialHelper_Blank_Context(t *testing.T) { | ||
r := require.New(t) | ||
|
||
name := "index" | ||
data := map[string]interface{}{} | ||
help := HelperContext{Context: NewContext()} | ||
|
||
html, err := partialHelper(name, data, help) | ||
r.Error(err) | ||
r.Contains(err.Error(), "could not found") | ||
r.Equal("", string(html)) | ||
} | ||
|
||
func Test_PartialHelper_Invalid_Feeder(t *testing.T) { | ||
r := require.New(t) | ||
|
||
name := "index" | ||
data := map[string]interface{}{} | ||
help := HelperContext{Context: NewContext()} | ||
help.Set("partialFeeder", "me-rong") | ||
|
||
html, err := partialHelper(name, data, help) | ||
r.Error(err) | ||
r.Contains(err.Error(), "could not found") | ||
r.Equal("", string(html)) | ||
} | ||
|
||
func Test_PartialHelper_Invalid_FeederFunction(t *testing.T) { | ||
r := require.New(t) | ||
|
||
name := "index" | ||
data := map[string]interface{}{} | ||
help := HelperContext{Context: NewContext()} | ||
help.Set("partialFeeder", func(string) string { | ||
return "me-rong" | ||
}) | ||
|
||
html, err := partialHelper(name, data, help) | ||
r.Error(err) | ||
r.Contains(err.Error(), "could not found") | ||
r.Equal("", string(html)) | ||
} | ||
|
||
func Test_PartialHelper_Feeder_Error(t *testing.T) { | ||
r := require.New(t) | ||
|
||
name := "index" | ||
data := map[string]interface{}{} | ||
help := HelperContext{Context: NewContext()} | ||
help.Set("partialFeeder", func(string) (string, error) { | ||
return "", errors.New("me-rong") | ||
}) | ||
|
||
_, err := partialHelper(name, data, help) | ||
r.Error(err) | ||
r.Contains(err.Error(), "me-rong") | ||
} | ||
|
||
func Test_PartialHelper_Good(t *testing.T) { | ||
r := require.New(t) | ||
|
||
name := "index" | ||
data := map[string]interface{}{} | ||
help := HelperContext{Context: NewContext()} | ||
help.Set("partialFeeder", func(string) (string, error) { | ||
return `<div class="test">Plush!</div>`, nil | ||
}) | ||
|
||
html, err := partialHelper(name, data, help) | ||
r.NoError(err) | ||
r.Equal(`<div class="test">Plush!</div>`, string(html)) | ||
} | ||
|
||
func Test_PartialHelper_With_Data(t *testing.T) { | ||
r := require.New(t) | ||
|
||
name := "index" | ||
data := map[string]interface{}{"name": "Yonghwan"} | ||
help := HelperContext{Context: NewContext()} | ||
help.Set("partialFeeder", func(string) (string, error) { | ||
return `<div class="test">Hello <%= name %></div>`, nil | ||
}) | ||
|
||
html, err := partialHelper(name, data, help) | ||
r.NoError(err) | ||
r.Equal(`<div class="test">Hello Yonghwan</div>`, string(html)) | ||
} | ||
|
||
func Test_PartialHelper_Render_Error(t *testing.T) { | ||
r := require.New(t) | ||
|
||
name := "index" | ||
data := map[string]interface{}{} | ||
help := HelperContext{Context: NewContext()} | ||
help.Set("partialFeeder", func(string) (string, error) { | ||
return `<div class="test">Hello <%= name </div>`, nil | ||
}) | ||
|
||
_, err := partialHelper(name, data, help) | ||
r.Error(err) | ||
} | ||
|
||
func Test_PartialHelper_With_Layout(t *testing.T) { | ||
r := require.New(t) | ||
|
||
name := "index" | ||
data := map[string]interface{}{ | ||
"name": "Yonghwan", | ||
"layout": "container", | ||
} | ||
help := HelperContext{Context: NewContext()} | ||
help.Set("partialFeeder", func(name string) (string, error) { | ||
if name == "container" { | ||
return `<html><%= yield %></html>`, nil | ||
} | ||
return `<div class="test">Hello <%= name %></div>`, nil | ||
}) | ||
|
||
html, err := partialHelper(name, data, help) | ||
r.NoError(err) | ||
r.Equal(`<html><div class="test">Hello Yonghwan</div></html>`, string(html)) | ||
} | ||
|
||
func Test_PartialHelper_JavaScript(t *testing.T) { | ||
r := require.New(t) | ||
|
||
name := "index.js" | ||
data := map[string]interface{}{} | ||
help := HelperContext{Context: NewContext()} | ||
help.Set("contentType", "application/javascript") | ||
help.Set("partialFeeder", func(string) (string, error) { | ||
return `alert('\'Hello\'');`, nil | ||
}) | ||
|
||
html, err := partialHelper(name, data, help) | ||
r.NoError(err) | ||
r.Equal(`alert('\'Hello\'');`, string(html)) | ||
} | ||
|
||
func Test_PartialHelper_Javascript_With_HTML(t *testing.T) { | ||
r := require.New(t) | ||
|
||
name := "index.html" | ||
data := map[string]interface{}{} | ||
help := HelperContext{Context: NewContext()} | ||
help.Set("contentType", "application/javascript") | ||
help.Set("partialFeeder", func(string) (string, error) { | ||
return `alert('\'Hello\'');`, nil | ||
}) | ||
|
||
html, err := partialHelper(name, data, help) | ||
r.NoError(err) | ||
r.Equal(`alert(\'\\\'Hello\\\'\');`, string(html)) | ||
} |