From bfc7c0cdc64dd265005470550624d59895725d4d Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sat, 22 Dec 2012 22:51:14 -0500 Subject: [PATCH] Starting to goof around with a Go implementation of Aspen --- gotime/smplt/Makefile | 16 ++++++++++++++ gotime/smplt/simplate.go | 35 +++++++++++++++++++++++++++++++ gotime/smplt/smplt_test.go | 43 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+) create mode 100644 gotime/smplt/Makefile create mode 100644 gotime/smplt/simplate.go create mode 100644 gotime/smplt/smplt_test.go diff --git a/gotime/smplt/Makefile b/gotime/smplt/Makefile new file mode 100644 index 0000000..a8830cc --- /dev/null +++ b/gotime/smplt/Makefile @@ -0,0 +1,16 @@ +TARGETS = \ + github.com/meatballhat/box-o-sand/gotime/smplt + +test: build + go test -x -v $(TARGETS) + +build: deps + go install -x $(TARGETS) + +deps: + go get -n -x $(TARGETS) + +clean: + go clean -x -i $(TARGETS) + +.PHONY: test build clean fmt diff --git a/gotime/smplt/simplate.go b/gotime/smplt/simplate.go new file mode 100644 index 0000000..0fc9238 --- /dev/null +++ b/gotime/smplt/simplate.go @@ -0,0 +1,35 @@ +package smplt + +import ( + "strings" +) + +const ( + SIMPLATE_TYPE_RENDERED = "rendered" + SIMPLATE_TYPE_STATIC = "static" + SIMPLATE_TYPE_NEGOTIATED = "negotiated" +) + +type Simplate struct { + Type string +} + +func SimplateFromString(filename, content string) *Simplate { + nbreaks := strings.Count(content, " ") + + s := &Simplate{ + Type: SIMPLATE_TYPE_STATIC, + } + + if nbreaks == 2 { + s.Type = SIMPLATE_TYPE_RENDERED + return s + } + + if nbreaks > 2 { + s.Type = SIMPLATE_TYPE_NEGOTIATED + return s + } + + return s +} diff --git a/gotime/smplt/smplt_test.go b/gotime/smplt/smplt_test.go new file mode 100644 index 0000000..490a729 --- /dev/null +++ b/gotime/smplt/smplt_test.go @@ -0,0 +1,43 @@ +package smplt_test + +import ( + "testing" + + . "github.com/meatballhat/box-o-sand/gotime/smplt" +) + +const BASIC_RENDERED_TXT_SIMPLATE = ` +import ( + "time" +) + +type Dance struct { + Who string + When time.Time +} + +d := &Dance{ + Who: "Everybody", + When: time.Now(), +} + +{{d.Who}} Dance {{d.When}}! +` + +const BASIC_STATIC_TXT_SIMPLATE = ` +Everybody Dance Now! +` + +func TestDetectsRenderedSimplate(t *testing.T) { + s := SimplateFromString("basic-rendered.txt", BASIC_RENDERED_TXT_SIMPLATE) + if s.Type != SIMPLATE_TYPE_RENDERED { + t.Errorf("Simplate detected as %s instead of %s", s.Type, SIMPLATE_TYPE_RENDERED) + } +} + +func TestDetectsStaticSimplate(t *testing.T) { + s := SimplateFromString("basic-static.txt", BASIC_STATIC_TXT_SIMPLATE) + if s.Type != SIMPLATE_TYPE_STATIC { + t.Errorf("Simplate detected as %s instead of %s", s.Type, SIMPLATE_TYPE_STATIC) + } +}