More tests for simplate type detection

This commit is contained in:
Dan Buch 2012-12-22 23:32:26 -05:00
parent bfc7c0cdc6
commit f61d845eca
2 changed files with 89 additions and 6 deletions

View File

@ -1,6 +1,8 @@
package smplt package smplt
import ( import (
"mime"
"path"
"strings" "strings"
) )
@ -8,17 +10,27 @@ const (
SIMPLATE_TYPE_RENDERED = "rendered" SIMPLATE_TYPE_RENDERED = "rendered"
SIMPLATE_TYPE_STATIC = "static" SIMPLATE_TYPE_STATIC = "static"
SIMPLATE_TYPE_NEGOTIATED = "negotiated" SIMPLATE_TYPE_NEGOTIATED = "negotiated"
SIMPLATE_TYPE_JSON = "json"
) )
type Simplate struct { type Simplate struct {
Filename string
Type string Type string
ContentType string
} }
func SimplateFromString(filename, content string) *Simplate { func SimplateFromString(filename, content string) *Simplate {
nbreaks := strings.Count(content, " ") nbreaks := strings.Count(content, " ")
s := &Simplate{ s := &Simplate{
Filename: filename,
Type: SIMPLATE_TYPE_STATIC, Type: SIMPLATE_TYPE_STATIC,
ContentType: mime.TypeByExtension(path.Ext(filename)),
}
if nbreaks == 1 && s.ContentType == "application/json" {
s.Type = SIMPLATE_TYPE_JSON
return s
} }
if nbreaks == 2 { if nbreaks == 2 {

View File

@ -1,12 +1,14 @@
package smplt_test package smplt_test
import ( import (
"mime"
"testing" "testing"
. "github.com/meatballhat/box-o-sand/gotime/smplt" . "github.com/meatballhat/box-o-sand/gotime/smplt"
) )
const BASIC_RENDERED_TXT_SIMPLATE = ` const (
BASIC_RENDERED_TXT_SIMPLATE = `
import ( import (
"time" "time"
) )
@ -23,10 +25,64 @@ d := &Dance{
{{d.Who}} Dance {{d.When}}! {{d.Who}} Dance {{d.When}}!
` `
BASIC_STATIC_TXT_SIMPLATE = `
const BASIC_STATIC_TXT_SIMPLATE = `
Everybody Dance Now! Everybody Dance Now!
` `
BASIC_JSON_SIMPLATE = `
import (
"time"
)
type Dance struct {
Who string
When time.Time
}
d := &Dance{
Who: "Everybody",
When: time.Now(),
}
response.SetBody(d)
`
BASIC_NEGOTIATED_SIMPLATE = `
import (
"time"
)
type Dance struct {
Who string
When time.Time
}
d := &Dance{
Who: "Everybody",
When: time.Now(),
}
text/plain
{{d.Who}} Dance {{d.When}}!
application/json
{"who":"{{d.Who}}","when":"{{d.When}}"}
`
)
func TestSimplateKnowsItsFilename(t *testing.T) {
s := SimplateFromString("hasty-decisions.txt", "herpherpderpherp")
if s.Filename != "hasty-decisions.txt" {
t.Errorf("Simplate filename incorrectly assigned as %s instead of %s",
s.Filename, "hasty-decisions.txt")
}
}
func TestSimplateKnowsItsContentType(t *testing.T) {
s := SimplateFromString("hasty-decisions.js", "function herp() { return 'derp'; }")
expected := mime.TypeByExtension(".js")
if s.ContentType != expected {
t.Errorf("Simplate content type incorrectly assigned as %s instead of %s",
s.ContentType, expected)
}
}
func TestDetectsRenderedSimplate(t *testing.T) { func TestDetectsRenderedSimplate(t *testing.T) {
s := SimplateFromString("basic-rendered.txt", BASIC_RENDERED_TXT_SIMPLATE) s := SimplateFromString("basic-rendered.txt", BASIC_RENDERED_TXT_SIMPLATE)
@ -41,3 +97,18 @@ func TestDetectsStaticSimplate(t *testing.T) {
t.Errorf("Simplate detected as %s instead of %s", s.Type, SIMPLATE_TYPE_STATIC) t.Errorf("Simplate detected as %s instead of %s", s.Type, SIMPLATE_TYPE_STATIC)
} }
} }
func TestDetectsJSONSimplates(t *testing.T) {
s := SimplateFromString("basic.json", BASIC_JSON_SIMPLATE)
if s.Type != SIMPLATE_TYPE_JSON {
t.Errorf("Simplate detected as %s instead of %s", s.Type, SIMPLATE_TYPE_JSON)
}
}
func TestDetectsNegotiatedSimplates(t *testing.T) {
s := SimplateFromString("hork", BASIC_NEGOTIATED_SIMPLATE)
if s.Type != SIMPLATE_TYPE_NEGOTIATED {
t.Errorf("Simplate detected as %s instead of %s",
s.Type, SIMPLATE_TYPE_NEGOTIATED)
}
}