Skip to content

Commit

Permalink
Introduce TestWriteDeclsAndStmts
Browse files Browse the repository at this point in the history
I wanted a way to quickly add example gomacro files as test cases.

This change adds TestWriteDeclsAndStmts that essentially constructs a
new fast interpreter, calls EvalFile and then WriteDeclsToFile.  The
testdata directory is searched for test cases; every test case must have
a .gomacro file, a .go file and optionally a .err file.  The .gomacro
file will be passed to EvalFile and its resulting error will be compared
against contents of any .err file.  Then the contents of the .go file
will be compared to the output of WriteDeclsToStream.  To create a test
just add the .gomacro file and then go test can be called with the
UPDATE_SNAPSHOTS envvar set, e.g.

   UPDATE_SNAPSHOTS=on go test ./cmd -run=TestWriteDeclsAndStmts

This will call EvalFile and WriteDeclsToFile and generate the .go and
.err files.  Note the tests will fail because we are generating, not
testing against the fixture.

The first test added is failing and demonstrates issue #84.

Signed-off-by: Corin Lawson <[email protected]>
  • Loading branch information
au-phiware authored and Corin Lawson committed Jan 12, 2020
1 parent 8d335e3 commit ee65b10
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 0 deletions.
77 changes: 77 additions & 0 deletions cmd/cmd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package cmd_test

import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"

. "github.com/cosmos72/gomacro/base"
. "github.com/cosmos72/gomacro/fast"
"github.com/kylelemons/godebug/diff"
)

func TestWriteDeclsAndStmts(t *testing.T) {
testdata, err := ioutil.ReadDir("testdata")
if err != nil {
t.Fatal(err)
}

for _, file := range testdata {
if !strings.HasSuffix(file.Name(), ".gomacro") {
continue
}
test := strings.TrimSuffix(file.Name(), ".gomacro")
t.Run(test, func(t *testing.T) {
gomacroFile := filepath.Join("testdata", test+".gomacro")
goFile := filepath.Join("testdata", test+".go")
errFile := filepath.Join("testdata", test+".err")
ir := New()
g := &ir.Comp.Globals
g.Options |= OptCollectDeclarations | OptCollectStatements

comments, got := ir.EvalFile(gomacroFile)

if os.Getenv("UPDATE_SNAPSHOTS") != "" {
if got != nil {
if err := ioutil.WriteFile(errFile, []byte(got.Error()), 0644); err != nil {
t.Fatal(err)
}
}

g.WriteDeclsToFile(goFile, comments)

t.Fail() // Test not performed
} else {
buf, err := ioutil.ReadFile(errFile)
if os.IsNotExist(err) {
if got != nil {
t.Errorf("EvalFile returned an error: %q", got)
}
} else if err == nil {
if got == nil {
t.Errorf("EvalFile didn't return an expected error:\nwant %q", string(buf))
} else if string(buf) != got.Error() {
t.Errorf("EvalFile returned an unexpected error:\nwant %q;\n got %q", string(buf), got)
}
} else {
t.Fatal(err)
}

if buf, err = ioutil.ReadFile(goFile); err != nil {
if os.IsNotExist(err) {
t.Fatalf("%s, try setting UPDATE_SNAPSHOTS=on", err)
}
t.Fatal(err)
}
out := &strings.Builder{}
out.WriteString(comments)
g.WriteDeclsToStream(out)
if diff := diff.Diff(string(buf), out.String()); diff != "" {
t.Errorf("EvalFile produced unexpected AST (lines starting with '-' are missing, '+' lines are superfluous):\n%s", diff)
}
}
})
}
}
3 changes: 3 additions & 0 deletions cmd/testdata/type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package main

type Name struct{}
7 changes: 7 additions & 0 deletions cmd/testdata/type.gomacro
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
:macro make() interface{} {
return ~"{
type Name struct{}
}
}

make
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/cosmos72/gomacro
go 1.9

require (
github.com/kylelemons/godebug v1.1.0
github.com/mattn/go-runewidth v0.0.7
github.com/peterh/liner v1.1.0
golang.org/x/tools v0.0.0-20191206204035-259af5ff87bd
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/mattn/go-runewidth v0.0.3/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/mattn/go-runewidth v0.0.7 h1:Ei8KR0497xHyKJPAv59M1dkC+rOZCMBJ+t3fZ+twI54=
github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
Expand Down

0 comments on commit ee65b10

Please sign in to comment.