From 8314d97ade2942a056fc0f2546c2ec8a4ac59525 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sun, 23 Dec 2012 12:11:48 -0500 Subject: [PATCH] Making assertions about `TemplatePage` assignment --- gotime/smplt/simplate.go | 33 +++++++++++++++--------- gotime/smplt/smplt_test.go | 51 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 12 deletions(-) diff --git a/gotime/smplt/simplate.go b/gotime/smplt/simplate.go index 2f3895d..ec7ac31 100644 --- a/gotime/smplt/simplate.go +++ b/gotime/smplt/simplate.go @@ -14,11 +14,12 @@ const ( ) type Simplate struct { - Filename string - Type string - ContentType string - InitPage *SimplatePage - LogicPages []*SimplatePage + Filename string + Type string + ContentType string + InitPage *SimplatePage + LogicPages []*SimplatePage + TemplatePage *SimplatePage } type SimplatePage struct { @@ -35,20 +36,28 @@ func SimplateFromString(filename, content string) *Simplate { ContentType: mime.TypeByExtension(path.Ext(filename)), } - if nbreaks == 1 && s.ContentType == "application/json" { - s.Type = SIMPLATE_TYPE_JSON - return s - } - - if nbreaks == 2 { - s.Type = SIMPLATE_TYPE_RENDERED + if nbreaks == 1 || nbreaks == 2 { s.InitPage = &SimplatePage{Body: rawPages[0]} 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 } if nbreaks > 2 { 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 } diff --git a/gotime/smplt/smplt_test.go b/gotime/smplt/smplt_test.go index 4b0f694..b633b79 100644 --- a/gotime/smplt/smplt_test.go +++ b/gotime/smplt/smplt_test.go @@ -138,3 +138,54 @@ func TestAssignsOneLogicPageToRenderedSimplates(t *testing.T) { "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) + } +}