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

Conversation

au-phiware
Copy link
Contributor

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, so that issue should be resolved before merging.

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 cosmos72#84.

Signed-off-by: Corin Lawson <corin@responsight.com>
@au-phiware
Copy link
Contributor Author

I've just notice that you've made use of the 'golden file' pattern in printer_test.go (or I suspect you've copy'n'pasted from the go standard lib). Would it be better to rename the go files to golden files?

Also, what are your thoughts on pulling in the third party package, github.com/kylelemons/godebug/diff? (It's okay to say, no more third party packages, you've done a great job keeping the external deps small, it seems frivolous to add one just for some pretty diffs in a test.)

@au-phiware
Copy link
Contributor Author

I've just realised that it's very important to limit the dependencies of gomacro (see comment on #82). So I'll pull it out and just use the diff function here.

@cosmos72
Copy link
Owner

cosmos72 commented Feb 2, 2020

The gomacro/go/printer package is just a fork of the go/printer package from std library. I did not bother to update its tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants