From 3dd1fc96350846ce4437f9db2a3cf688813a8201 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Mon, 24 Dec 2012 12:03:21 -0500 Subject: [PATCH] Moving output name calculation into method of `Simplate` --- gotime/smplt/simplate.go | 19 +++++++++++++++++-- gotime/smplt/smplt_test.go | 26 +++++++++++++++++++++----- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/gotime/smplt/simplate.go b/gotime/smplt/simplate.go index 3ca88f8..02abb65 100644 --- a/gotime/smplt/simplate.go +++ b/gotime/smplt/simplate.go @@ -66,14 +66,29 @@ func SimplateFromString(filename, content string) *Simplate { return s } -func (me *Simplate) Execute(wr io.Writer, data interface{}) error { +func (me *Simplate) Execute(wr io.Writer) error { outbuf := bufio.NewWriter(wr) - defer outbuf.Flush() _, err := outbuf.WriteString("package smplt_gen\n") if err != nil { return err } + err = outbuf.Flush() + if err != nil { + return err + } + return nil } + +func (me *Simplate) OutputName() string { + if me.Type == SIMPLATE_TYPE_STATIC { + return me.Filename + } + + lessDots := strings.Replace(me.Filename, ".", "-DOT-", -1) + lessSlashes := strings.Replace(lessDots, "/", "-SLASH-", -1) + lessSpaces := strings.Replace(lessSlashes, " ", "-SPACE-", -1) + return lessSpaces + ".go" +} diff --git a/gotime/smplt/smplt_test.go b/gotime/smplt/smplt_test.go index 8a465c0..47e7aeb 100644 --- a/gotime/smplt/smplt_test.go +++ b/gotime/smplt/smplt_test.go @@ -1,7 +1,6 @@ package smplt_test import ( - "bufio" "bytes" "fmt" "go/parser" @@ -111,6 +110,20 @@ func TestSimplateKnowsItsContentType(t *testing.T) { } } +func TestStaticSimplateKnowsItsOutputName(t *testing.T) { + s := SimplateFromString("nothing.txt", "foo\nham\n") + if s.OutputName() != "nothing.txt" { + t.Errorf("Static simplate output name is wrong!: %v", s.OutputName()) + } +} + +func TestRenderedSimplateKnowsItsOutputName(t *testing.T) { + s := SimplateFromString("flip/dippy slippy/snork.d/basic-rendered.txt", BASIC_RENDERED_TXT_SIMPLATE) + if s.OutputName() != "flip-SLASH-dippy-SPACE-slippy-SLASH-snork-DOT-d-SLASH-basic-rendered-DOT-txt.go" { + t.Errorf("Rendered simplate output name is wrong!: %v", s.OutputName()) + } +} + func TestDetectsRenderedSimplate(t *testing.T) { s := SimplateFromString("basic-rendered.txt", BASIC_RENDERED_TXT_SIMPLATE) if s.Type != SIMPLATE_TYPE_RENDERED { @@ -220,7 +233,7 @@ func TestAssignsNoTemplatePageToNegotiatedSimplates(t *testing.T) { 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") + err := s.Execute(&out) if err != nil { t.Error(err) } @@ -231,15 +244,18 @@ func TestRenderedSimplateOutputIsValidGoSource(t *testing.T) { defer rmTmpDir() s := SimplateFromString("basic-rendered.txt", BASIC_RENDERED_TXT_SIMPLATE) - outfile_name := path.Join(tmpdir, "basic_rendered.go") + outfile_name := path.Join(tmpdir, s.OutputName()) outf, err := os.Create(outfile_name) if err != nil { t.Error(err) return } - s.Execute(outf, "nothing here") - outf.Close() + s.Execute(outf) + err = outf.Close() + if err != nil { + t.Error(err) + } fset := token.NewFileSet() _, err = parser.ParseFile(fset, outfile_name, nil, parser.DeclarationErrors)