From dcb060b246fadd9ad17ba5d626bd23cdb1a4c10d Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sun, 23 Dec 2012 11:07:23 -0500 Subject: [PATCH] More assertions about simplate parsing beyond simple type detection. --- gotime/smplt/simplate.go | 11 ++++++++++- gotime/smplt/smplt_test.go | 40 +++++++++++++++++++++++++++++++------- 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/gotime/smplt/simplate.go b/gotime/smplt/simplate.go index d3061dd..2f3895d 100644 --- a/gotime/smplt/simplate.go +++ b/gotime/smplt/simplate.go @@ -17,10 +17,17 @@ type Simplate struct { Filename string Type string ContentType string + InitPage *SimplatePage + LogicPages []*SimplatePage +} + +type SimplatePage struct { + Body string } func SimplateFromString(filename, content string) *Simplate { - nbreaks := strings.Count(content, " ") + rawPages := strings.Split(content, " ") + nbreaks := len(rawPages) - 1 s := &Simplate{ Filename: filename, @@ -35,6 +42,8 @@ func SimplateFromString(filename, content string) *Simplate { if nbreaks == 2 { s.Type = SIMPLATE_TYPE_RENDERED + s.InitPage = &SimplatePage{Body: rawPages[0]} + s.LogicPages = append(s.LogicPages, &SimplatePage{Body: rawPages[1]}) return s } diff --git a/gotime/smplt/smplt_test.go b/gotime/smplt/smplt_test.go index a76ae31..4b0f694 100644 --- a/gotime/smplt/smplt_test.go +++ b/gotime/smplt/smplt_test.go @@ -18,12 +18,12 @@ type Dance struct { When time.Time } -d := &Dance{ +D := &Dance{ Who: "Everybody", When: time.Now(), } -{{d.Who}} Dance {{d.When}}! +{{.D.Who}} Dance {{.D.When}}! ` BASIC_STATIC_TXT_SIMPLATE = ` Everybody Dance Now! @@ -38,11 +38,11 @@ type Dance struct { When time.Time } -d := &Dance{ +D := &Dance{ Who: "Everybody", When: time.Now(), } -response.SetBody(d) +response.SetBody(D) ` BASIC_NEGOTIATED_SIMPLATE = ` import ( @@ -54,15 +54,15 @@ type Dance struct { When time.Time } -d := &Dance{ +D := &Dance{ Who: "Everybody", When: time.Now(), } text/plain -{{d.Who}} Dance {{d.When}}! +{{.D.Who}} Dance {{.D.When}}! 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) } } + +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)) + } +}