| 1 |
package symboltable |
| 2 |
|
| 3 |
import ( |
| 4 |
"os" |
| 5 |
"path/filepath" |
| 6 |
"strings" |
| 7 |
"testing" |
| 8 |
|
| 9 |
qt "github.com/go-quicktest/qt" |
| 10 |
) |
| 11 |
|
| 12 |
func TestCreateSymbolTableFile(t *testing.T) { |
| 13 |
dir := t.TempDir() |
| 14 |
path := filepath.Join(dir, "test.symbols.tsv") |
| 15 |
|
| 16 |
st, err := CreateSymbolTableFile(path, "mymodule") |
| 17 |
qt.Assert(t, qt.IsNil(err)) |
| 18 |
qt.Check(t, qt.Equals(st.Path, path)) |
| 19 |
st.Close() |
| 20 |
|
| 21 |
content, err := os.ReadFile(path) |
| 22 |
qt.Assert(t, qt.IsNil(err)) |
| 23 |
|
| 24 |
lines := strings.Split(strings.TrimRight(string(content), "\n"), "\n") |
| 25 |
qt.Assert(t, qt.HasLen(lines, 4)) |
| 26 |
qt.Check(t, qt.Equals(lines[0], "# language = Go")) |
| 27 |
qt.Check(t, qt.StringContains(lines[1], "# instrumentor = ")) |
| 28 |
qt.Check(t, qt.Equals(lines[2], "# module = mymodule")) |
| 29 |
qt.Check(t, qt.Equals(lines[3], "file\tfunction\tbegin_line\tbegin_column\tend_line\tend_column\taddress")) |
| 30 |
} |
| 31 |
|
| 32 |
func TestWritePosition(t *testing.T) { |
| 33 |
dir := t.TempDir() |
| 34 |
path := filepath.Join(dir, "test.symbols.tsv") |
| 35 |
|
| 36 |
st, err := CreateSymbolTableFile(path, "mymodule") |
| 37 |
qt.Assert(t, qt.IsNil(err)) |
| 38 |
|
| 39 |
err = st.WritePosition(SymbolTablePosition{ |
| 40 |
Path: "/src/main.go", |
| 41 |
Function: "Foo", |
| 42 |
StartLine: 10, |
| 43 |
StartColumn: 1, |
| 44 |
EndLine: 15, |
| 45 |
EndColumn: 2, |
| 46 |
Edge: 42, |
| 47 |
}) |
| 48 |
qt.Assert(t, qt.IsNil(err)) |
| 49 |
st.Close() |
| 50 |
|
| 51 |
content, err := os.ReadFile(path) |
| 52 |
qt.Assert(t, qt.IsNil(err)) |
| 53 |
|
| 54 |
lines := strings.Split(strings.TrimRight(string(content), "\n"), "\n") |
| 55 |
// 4 header lines + 1 position line |
| 56 |
qt.Assert(t, qt.HasLen(lines, 5)) |
| 57 |
qt.Check(t, qt.Equals(lines[4], "/src/main.go\tFoo\t10\t1\t15\t2\t42")) |
| 58 |
} |