Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce TestWriteDeclsAndStmts #86

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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