~mna/snow unlisted

snow/pkg/parser/parser_test.go -rw-r--r-- 1.1 KiB
424066c5Martin Angers doc: v0.0.5 1 year, 3 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package parser_test

import (
	"bytes"
	"flag"
	"path/filepath"
	"testing"

	"git.sr.ht/~mna/snow/pkg/internal/filetest"
	"git.sr.ht/~mna/snow/pkg/parser"
	"git.sr.ht/~mna/snow/pkg/printer"
	"git.sr.ht/~mna/snow/pkg/scanner"
)

var testUpdateParserTests = flag.Bool("test.update-parser-tests", false, "If set, replace expected parser test results with actual results.")

func TestParser(t *testing.T) {
	baseDir := "testdata"
	for _, fi := range filetest.SourceFiles(t, baseDir) {
		t.Run(fi.Name(), func(t *testing.T) {
			fset, files, err := parser.ParseFiles(filepath.Join(baseDir, fi.Name()))

			var ebuf bytes.Buffer
			scanner.PrintError(&ebuf, err)
			if len(files) != 1 {
				t.Fatalf("want 1 file, got %d", len(files))
			}

			var buf bytes.Buffer
			pp := printer.Printer{W: &buf, Pos: printer.PosOffsets, WithComments: true}
			if err := pp.PrintAST(files[0], fset.File(files[0].Pos())); err != nil {
				t.Fatal(err)
			}

			filetest.DiffOutput(t, fi, buf.String(), baseDir, testUpdateParserTests)
			filetest.DiffErrors(t, fi, ebuf.String(), baseDir, testUpdateParserTests)
		})
	}
}