~mna/snow unlisted

snow/pkg/semantic/translate_pass_test.go -rw-r--r-- 2.0 KiB
424066c5Martin Angers doc: v0.0.5 1 year, 7 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package semantic_test

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

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

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

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

	for _, fi := range filetest.SourceFiles(t, baseDir) {
		t.Run(fi.Name(), func(t *testing.T) {
			unit, err := semantic.Run(semantic.TranslatePass, filepath.Join(baseDir, fi.Name()))

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

			if unit == nil && err != nil {
				t.Fatal(ebuf.String())
			}

			var buf bytes.Buffer
			unit.Scope().WriteTo(&buf, 0, true)

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

			// sanity check: input files are all present in the unit, in the same order
			// (well, currently all tests are only single files, but...)
			if len(unit.Files) != 1 {
				t.Fatalf("want unit to have 1 file, got %d", len(unit.Files))
			}
			file := unit.FileSet.File(unit.Files[0].Pos())
			if file == nil {
				t.Fatalf("unit.FileSet.File returned nil")
			}
			wantNm := filepath.Join(baseDir, fi.Name())
			if got := file.Name(); got != wantNm {
				t.Fatalf("want unit.FileSet.File to have return filename %s, got %s", wantNm, got)
			}

			// sanity check: all nodes have a scope
			var hasScope visitorFunc
			hasScope = func(n semantic.Node) semantic.Visitor {
				if n == nil {
					return nil
				}
				if n.Scope() == nil {
					lpos := unit.FileSet.Position(n.Pos())
					t.Fatalf("%s: no scope for %s", lpos, semantic.TypeIdentString(n))
				}
				return hasScope
			}
			semantic.Walk(hasScope, unit)
		})
	}
}

type visitorFunc func(semantic.Node) semantic.Visitor

func (v visitorFunc) Visit(n semantic.Node) semantic.Visitor {
	return v(n)
}