-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
8d335e3
commit ee65b10
Showing
5 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package main | ||
|
||
type Name struct{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
:macro make() interface{} { | ||
return ~"{ | ||
type Name struct{} | ||
} | ||
} | ||
|
||
make |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters