~mna/snow unlisted

snow/pkg/semantic/analysis_pass_test.go -rw-r--r-- 1.4 KiB
424066c5Martin Angers doc: v0.0.5 1 year, 6 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package semantic_test

import (
	"bytes"
	"flag"
	"fmt"
	"go/scanner"
	"path/filepath"
	"sort"
	"testing"

	"git.sr.ht/~mna/snow/pkg/internal/filetest"
	"git.sr.ht/~mna/snow/pkg/semantic"
)

var testUpdateStaticTests = flag.Bool("test.update-static-tests", false, "If set, replace expected semantic static analysis test results with actual results.")

func TestStatic(t *testing.T) {
	baseDir := "testdata"
	expectDir := filepath.Join(baseDir, "static")

	for _, fi := range filetest.SourceFiles(t, baseDir) {
		t.Run(fi.Name(), func(t *testing.T) {
			unit, err := semantic.Run(semantic.AnalysisPass, filepath.Join(baseDir, fi.Name()))
			if unit == nil && err != nil {
				t.Fatal(err)
			}

			var ebuf bytes.Buffer
			scanner.PrintError(&ebuf, err)

			var buf bytes.Buffer
			type pair struct {
				id   *semantic.Ident
				decl semantic.Decl
			}
			pairs := make([]pair, 0, len(unit.ValueUses))
			for id, decl := range unit.ValueUses {
				pairs = append(pairs, pair{id, decl})
			}
			sort.Slice(pairs, func(i, j int) bool {
				l, r := pairs[i].id, pairs[j].id
				return l.Pos() < r.Pos()
			})
			for _, p := range pairs {
				fmt.Fprintf(&buf, "%s: %s\n", unit.FileSet.Position(p.id.Pos()), p.decl.Ident())
			}

			filetest.DiffOutput(t, fi, buf.String(), expectDir, testUpdateStaticTests)
			filetest.DiffErrors(t, fi, ebuf.String(), expectDir, testUpdateStaticTests)
		})
	}
}