From 8654aacea3fb6937e8b1d421f8dd2caad422e8b6 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Mon, 24 Dec 2012 00:12:11 -0500 Subject: [PATCH] Starting work on rendering out simplates to Go sources. --- gotime/smplt/simplate.go | 14 +++++++++ gotime/smplt/smplt_test.go | 59 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) diff --git a/gotime/smplt/simplate.go b/gotime/smplt/simplate.go index ec7ac31..3ca88f8 100644 --- a/gotime/smplt/simplate.go +++ b/gotime/smplt/simplate.go @@ -1,6 +1,8 @@ package smplt import ( + "bufio" + "io" "mime" "path" "strings" @@ -63,3 +65,15 @@ func SimplateFromString(filename, content string) *Simplate { return s } + +func (me *Simplate) Execute(wr io.Writer, data interface{}) error { + outbuf := bufio.NewWriter(wr) + defer outbuf.Flush() + + _, err := outbuf.WriteString("package smplt_gen\n") + if err != nil { + return err + } + + return nil +} diff --git a/gotime/smplt/smplt_test.go b/gotime/smplt/smplt_test.go index b633b79..8a465c0 100644 --- a/gotime/smplt/smplt_test.go +++ b/gotime/smplt/smplt_test.go @@ -1,8 +1,16 @@ package smplt_test import ( + "bufio" + "bytes" + "fmt" + "go/parser" + "go/token" "mime" + "os" + "path" "testing" + "time" . "github.com/meatballhat/box-o-sand/gotime/smplt" ) @@ -66,6 +74,25 @@ D := &Dance{ ` ) +var ( + tmpdir = path.Join(os.TempDir(), + fmt.Sprintf("smplt_test-%d", time.Now().UTC().UnixNano())) +) + +func mkTmpDir() { + err := os.MkdirAll(tmpdir, os.ModeDir|os.ModePerm) + if err != nil { + panic(err) + } +} + +func rmTmpDir() { + err := os.RemoveAll(tmpdir) + if err != nil { + panic(err) + } +} + func TestSimplateKnowsItsFilename(t *testing.T) { s := SimplateFromString("hasty-decisions.txt", "herpherpderpherp") if s.Filename != "hasty-decisions.txt" { @@ -189,3 +216,35 @@ func TestAssignsNoTemplatePageToNegotiatedSimplates(t *testing.T) { t.Errorf("Negotiated simplate had a template page assigned!: %v", s.TemplatePage) } } + +func TestRenderedSimplateCanExecuteToWriter(t *testing.T) { + s := SimplateFromString("basic-rendered.txt", BASIC_RENDERED_TXT_SIMPLATE) + var out bytes.Buffer + err := s.Execute(bufio.NewWriter(&out), "no data needed") + if err != nil { + t.Error(err) + } +} + +func TestRenderedSimplateOutputIsValidGoSource(t *testing.T) { + mkTmpDir() + defer rmTmpDir() + + s := SimplateFromString("basic-rendered.txt", BASIC_RENDERED_TXT_SIMPLATE) + outfile_name := path.Join(tmpdir, "basic_rendered.go") + outf, err := os.Create(outfile_name) + if err != nil { + t.Error(err) + return + } + + s.Execute(outf, "nothing here") + outf.Close() + + fset := token.NewFileSet() + _, err = parser.ParseFile(fset, outfile_name, nil, parser.DeclarationErrors) + if err != nil { + t.Error(err) + return + } +}