-
Notifications
You must be signed in to change notification settings - Fork 5
/
typecheck_test.go
67 lines (57 loc) · 1.41 KB
/
typecheck_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
package main
import (
"context"
"os"
"reflect"
"testing"
sitter "github.com/smacker/go-tree-sitter"
"github.com/smacker/go-tree-sitter/java"
)
func loadFile(fileName string) ([]byte, *sitter.Tree) {
parser := sitter.NewParser()
parser.SetLanguage(java.GetLanguage())
source, err := os.ReadFile(fileName)
if err != nil {
panic(err)
}
tree, err := parser.ParseCtx(context.Background(), nil, source)
if err != nil {
panic(err)
}
return source, tree
}
func TestSimpleDeclaration(t *testing.T) {
source, tree := loadFile("testfiles/typechecks/SimpleDeclaration.java")
expected := TypeInformation{
types: map[string]string{
"main": "",
"args": "[]string",
"variable": "int32",
},
}
info, err := ExtractTypeInformation(tree.RootNode(), source)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(info, expected) {
t.Errorf("Actual: %v did not meet expected: %v", info, expected)
}
}
func TestMethodDeclaration(t *testing.T) {
source, tree := loadFile("testfiles/typechecks/MethodConstructorDeclaration.java")
expected := TypeInformation{
types: map[string]string{
"sayHello": "string",
"squared": "int32",
"n": "int32",
"someNum": "float64",
},
}
info, err := ExtractTypeInformation(tree.RootNode(), source)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(info, expected) {
t.Errorf("Actual: %v did not meet expected: %v", info, expected)
}
}