-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkspace_test.go
137 lines (111 loc) · 3.21 KB
/
workspace_test.go
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package langd
// workspace_test.go does not contain actual tests, but rather utility
// functions that support testing workspace methods.
import (
"bytes"
"fmt"
"go/token"
"os"
"path/filepath"
"runtime"
"testing"
"github.com/google/uuid"
"github.com/object88/langd/log"
"github.com/spf13/afero"
)
func createOverlay(files map[string]string) (string, afero.Fs) {
fs := afero.NewMemMapFs()
rootPath := filepath.Join(string(os.PathSeparator), uuid.New().String(), "go", "src")
for path, contents := range files {
completePath := filepath.Join(rootPath, path)
fs.MkdirAll(filepath.Dir(completePath), 0644)
fh, _ := fs.Create(completePath)
fh.WriteString(contents)
fh.Close()
}
return rootPath, fs
}
func workspaceSetup(t *testing.T, startingPath string, overlayFs afero.Fs, expectFailure bool) (*Workspace, func()) {
le := NewLoaderEngine()
l := NewLoader(le, runtime.GOOS, runtime.GOARCH, runtime.GOROOT(), func(l *Loader) {
l.fs = afero.NewCopyOnWriteFs(l.fs, overlayFs)
})
w := CreateWorkspace(le, log.CreateLog(os.Stdout))
w.AssignLoader(l)
w.log.SetLevel(log.Verbose)
t.Logf("About to load directory '%s'\n", startingPath)
err := l.LoadDirectory(startingPath)
if err != nil {
t.Fatalf("Error while loading directory '%s': %s", startingPath, err.Error())
}
t.Logf("Finished loading directory\n")
t.Logf("Waiting for complete\n")
l.Wait()
t.Logf("Complete\n")
if expectFailure {
errCount := 0
w.Loader.Errors(func(file string, errs []FileError) {
errCount += len(errs)
})
if errCount == 0 {
t.Fatal("Expected errors, but got none\n")
}
} else {
errCount := 0
var buf bytes.Buffer
w.Loader.Errors(func(file string, errs []FileError) {
buf.WriteString(fmt.Sprintf("Loading error in %s:\n", file))
for k, err := range errs {
buf.WriteString(fmt.Sprintf("\t%02d: %s:%d %s\n", k, err.Filename, err.Line, err.Message))
}
errCount += len(errs)
})
if errCount != 0 {
buf.WriteString(fmt.Sprintf("Total: %d errors\n", errCount))
t.Fatal(buf.String())
}
}
return w, func() { le.Close() }
}
func testDeclaration(t *testing.T, w *Workspace, usagePosition, expectedDeclPosition *token.Position) {
declPosition, err := w.LocateDeclaration(usagePosition)
if err != nil {
t.Fatal(err.Error())
}
testPosition(t, declPosition, expectedDeclPosition)
}
func testPosition(t *testing.T, actual, expected *token.Position) {
if actual == nil {
t.Fatalf("actual is nil")
}
if !actual.IsValid() {
t.Fatalf("Returned position '%s' is not valid", actual.String())
}
if actual.Filename != expected.Filename {
t.Fatalf("Incorrect filename: got '%s', expected '%s'", actual.Filename, expected.Filename)
}
if actual.Line != expected.Line {
t.Fatalf("Incorrect line: got %d, expected %d", actual.Line, expected.Line)
}
if actual.Column != expected.Column {
t.Fatalf("Incorrect column: got %d, expected %d", actual.Column, expected.Column)
}
}
func comparePosition(actual, expected *token.Position) bool {
if actual == nil {
return false
}
if !actual.IsValid() {
return false
}
if actual.Filename != expected.Filename {
return false
}
if actual.Line != expected.Line {
return false
}
if actual.Column != expected.Column {
return false
}
return true
}