Moving output name calculation into method of Simplate

This commit is contained in:
Dan Buch 2012-12-24 12:03:21 -05:00
parent 8654aacea3
commit 3dd1fc9635
2 changed files with 38 additions and 7 deletions

View File

@ -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"
}

View File

@ -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)