More assertions about simplate parsing

beyond simple type detection.
This commit is contained in:
Dan Buch 2012-12-23 11:07:23 -05:00
parent f61d845eca
commit dcb060b246
2 changed files with 43 additions and 8 deletions

View File

@ -17,10 +17,17 @@ type Simplate struct {
Filename string Filename string
Type string Type string
ContentType string ContentType string
InitPage *SimplatePage
LogicPages []*SimplatePage
}
type SimplatePage struct {
Body string
} }
func SimplateFromString(filename, content string) *Simplate { func SimplateFromString(filename, content string) *Simplate {
nbreaks := strings.Count(content, " ") rawPages := strings.Split(content, " ")
nbreaks := len(rawPages) - 1
s := &Simplate{ s := &Simplate{
Filename: filename, Filename: filename,
@ -35,6 +42,8 @@ func SimplateFromString(filename, content string) *Simplate {
if nbreaks == 2 { if nbreaks == 2 {
s.Type = SIMPLATE_TYPE_RENDERED s.Type = SIMPLATE_TYPE_RENDERED
s.InitPage = &SimplatePage{Body: rawPages[0]}
s.LogicPages = append(s.LogicPages, &SimplatePage{Body: rawPages[1]})
return s return s
} }

View File

@ -18,12 +18,12 @@ type Dance struct {
When time.Time When time.Time
} }
d := &Dance{ D := &Dance{
Who: "Everybody", Who: "Everybody",
When: time.Now(), When: time.Now(),
} }
{{d.Who}} Dance {{d.When}}! {{.D.Who}} Dance {{.D.When}}!
` `
BASIC_STATIC_TXT_SIMPLATE = ` BASIC_STATIC_TXT_SIMPLATE = `
Everybody Dance Now! Everybody Dance Now!
@ -38,11 +38,11 @@ type Dance struct {
When time.Time When time.Time
} }
d := &Dance{ D := &Dance{
Who: "Everybody", Who: "Everybody",
When: time.Now(), When: time.Now(),
} }
response.SetBody(d) response.SetBody(D)
` `
BASIC_NEGOTIATED_SIMPLATE = ` BASIC_NEGOTIATED_SIMPLATE = `
import ( import (
@ -54,15 +54,15 @@ type Dance struct {
When time.Time When time.Time
} }
d := &Dance{ D := &Dance{
Who: "Everybody", Who: "Everybody",
When: time.Now(), When: time.Now(),
} }
text/plain text/plain
{{d.Who}} Dance {{d.When}}! {{.D.Who}} Dance {{.D.When}}!
application/json application/json
{"who":"{{d.Who}}","when":"{{d.When}}"} {"who":"{{.D.Who}}","when":"{{.D.When}}"}
` `
) )
@ -112,3 +112,29 @@ func TestDetectsNegotiatedSimplates(t *testing.T) {
s.Type, SIMPLATE_TYPE_NEGOTIATED) s.Type, SIMPLATE_TYPE_NEGOTIATED)
} }
} }
func TestAssignsNoGoPagesToStaticSimplates(t *testing.T) {
s := SimplateFromString("basic-static.txt", BASIC_STATIC_TXT_SIMPLATE)
if s.InitPage != nil {
t.Errorf("Static simplate had init page assigned!: %v", s.InitPage)
}
if len(s.LogicPages) > 0 {
t.Errorf("Static simplate had logic pages assigned!: %v", s.LogicPages)
}
}
func TestAssignsAnInitPageToRenderedSimplates(t *testing.T) {
s := SimplateFromString("basic-rendered.txt", BASIC_RENDERED_TXT_SIMPLATE)
if s.InitPage == nil {
t.Errorf("Rendered simplate had no init page assigned!: %v", s.InitPage)
}
}
func TestAssignsOneLogicPageToRenderedSimplates(t *testing.T) {
s := SimplateFromString("basic-rendered.txt", BASIC_RENDERED_TXT_SIMPLATE)
if len(s.LogicPages) != 1 {
t.Errorf("Rendered simplate unexpected number "+
"of logic pages assigned!: %v", len(s.LogicPages))
}
}