Making assertions about TemplatePage assignment

This commit is contained in:
Dan Buch 2012-12-23 12:11:48 -05:00
parent dcb060b246
commit 8314d97ade
2 changed files with 72 additions and 12 deletions

View File

@ -19,6 +19,7 @@ type Simplate struct {
ContentType string ContentType string
InitPage *SimplatePage InitPage *SimplatePage
LogicPages []*SimplatePage LogicPages []*SimplatePage
TemplatePage *SimplatePage
} }
type SimplatePage struct { type SimplatePage struct {
@ -35,20 +36,28 @@ func SimplateFromString(filename, content string) *Simplate {
ContentType: mime.TypeByExtension(path.Ext(filename)), ContentType: mime.TypeByExtension(path.Ext(filename)),
} }
if nbreaks == 1 && s.ContentType == "application/json" { if nbreaks == 1 || nbreaks == 2 {
s.Type = SIMPLATE_TYPE_JSON
return s
}
if nbreaks == 2 {
s.Type = SIMPLATE_TYPE_RENDERED
s.InitPage = &SimplatePage{Body: rawPages[0]} s.InitPage = &SimplatePage{Body: rawPages[0]}
s.LogicPages = append(s.LogicPages, &SimplatePage{Body: rawPages[1]}) s.LogicPages = append(s.LogicPages, &SimplatePage{Body: rawPages[1]})
if s.ContentType == "application/json" {
s.Type = SIMPLATE_TYPE_JSON
} else {
s.Type = SIMPLATE_TYPE_RENDERED
s.TemplatePage = &SimplatePage{Body: rawPages[2]}
}
return s return s
} }
if nbreaks > 2 { if nbreaks > 2 {
s.Type = SIMPLATE_TYPE_NEGOTIATED s.Type = SIMPLATE_TYPE_NEGOTIATED
s.InitPage = &SimplatePage{Body: rawPages[0]}
for _, rawPage := range rawPages {
s.LogicPages = append(s.LogicPages, &SimplatePage{Body: rawPage})
}
return s return s
} }

View File

@ -138,3 +138,54 @@ func TestAssignsOneLogicPageToRenderedSimplates(t *testing.T) {
"of logic pages assigned!: %v", len(s.LogicPages)) "of logic pages assigned!: %v", len(s.LogicPages))
} }
} }
func TestAssignsOneTemplatePageToRenderedSimplates(t *testing.T) {
s := SimplateFromString("basic-rendered.txt", BASIC_RENDERED_TXT_SIMPLATE)
if s.TemplatePage == nil {
t.Errorf("Rendered simplate had no template page assigned!: %v", s.TemplatePage)
}
}
func TestAssignsAnInitPageToJSONSimplates(t *testing.T) {
s := SimplateFromString("basic.json", BASIC_JSON_SIMPLATE)
if s.InitPage == nil {
t.Errorf("JSON simplate had no init page assigned!: %v", s.InitPage)
}
}
func TestAssignsOneLogicPageToJSONSimplates(t *testing.T) {
s := SimplateFromString("basic.json", BASIC_JSON_SIMPLATE)
if len(s.LogicPages) != 1 {
t.Errorf("Rendered simplate unexpected number "+
"of logic pages assigned!: %v", len(s.LogicPages))
}
}
func TestAssignsNoTemplatePageToJSONSimplates(t *testing.T) {
s := SimplateFromString("basic.json", BASIC_JSON_SIMPLATE)
if s.TemplatePage != nil {
t.Errorf("JSON simplate had a template page assigned!: %v", s.TemplatePage)
}
}
func TestAssignsAnInitPageToNegotiatedSimplates(t *testing.T) {
s := SimplateFromString("basic-negotiated.txt", BASIC_NEGOTIATED_SIMPLATE)
if s.InitPage == nil {
t.Errorf("Negotiated simplate had no init page assigned!: %v", s.InitPage)
}
}
func TestAssignsAtLeastOneLogicPageToNegotiatedSimplates(t *testing.T) {
s := SimplateFromString("basic-negotiated.txt", BASIC_NEGOTIATED_SIMPLATE)
if len(s.LogicPages) < 1 {
t.Errorf("Negotiated simplate unexpected number "+
"of logic pages assigned!: %v", len(s.LogicPages))
}
}
func TestAssignsNoTemplatePageToNegotiatedSimplates(t *testing.T) {
s := SimplateFromString("basic-negotiated.txt", BASIC_NEGOTIATED_SIMPLATE)
if s.TemplatePage != nil {
t.Errorf("Negotiated simplate had a template page assigned!: %v", s.TemplatePage)
}
}