Starting work on rendering out simplates to Go sources.

This commit is contained in:
Dan Buch 2012-12-24 00:12:11 -05:00
parent 8314d97ade
commit 8654aacea3
2 changed files with 73 additions and 0 deletions

View File

@ -1,6 +1,8 @@
package smplt package smplt
import ( import (
"bufio"
"io"
"mime" "mime"
"path" "path"
"strings" "strings"
@ -63,3 +65,15 @@ func SimplateFromString(filename, content string) *Simplate {
return s 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
}

View File

@ -1,8 +1,16 @@
package smplt_test package smplt_test
import ( import (
"bufio"
"bytes"
"fmt"
"go/parser"
"go/token"
"mime" "mime"
"os"
"path"
"testing" "testing"
"time"
. "github.com/meatballhat/box-o-sand/gotime/smplt" . "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) { func TestSimplateKnowsItsFilename(t *testing.T) {
s := SimplateFromString("hasty-decisions.txt", "herpherpderpherp") s := SimplateFromString("hasty-decisions.txt", "herpherpderpherp")
if s.Filename != "hasty-decisions.txt" { 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) 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
}
}