forked from cucumber/godog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast_test.go
76 lines (58 loc) · 1.6 KB
/
ast_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
package godog
import (
"go/parser"
"go/token"
"testing"
)
var astContextSrc = `package main
import (
"github.com/DATA-DOG/godog"
)
func MyContext(s *godog.Suite) {
}`
var astTwoContextSrc = `package lib
import (
"github.com/DATA-DOG/godog"
)
func ApiContext(s *godog.Suite) {
}
func DBContext(s *godog.Suite) {
}`
func astContextParse(src string, t *testing.T) []string {
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "", []byte(src), 0)
if err != nil {
t.Fatalf("unexpected error while parsing ast: %v", err)
}
return astContexts(f)
}
func TestShouldGetSingleContextFromSource(t *testing.T) {
actual := astContextParse(astContextSrc, t)
expect := []string{"MyContext"}
if len(actual) != len(expect) {
t.Fatalf("number of found contexts do not match, expected %d, but got %d", len(expect), len(actual))
}
for i, c := range expect {
if c != actual[i] {
t.Fatalf("expected context '%s' at pos %d, but got: '%s'", c, i, actual[i])
}
}
}
func TestShouldGetTwoContextsFromSource(t *testing.T) {
actual := astContextParse(astTwoContextSrc, t)
expect := []string{"ApiContext", "DBContext"}
if len(actual) != len(expect) {
t.Fatalf("number of found contexts do not match, expected %d, but got %d", len(expect), len(actual))
}
for i, c := range expect {
if c != actual[i] {
t.Fatalf("expected context '%s' at pos %d, but got: '%s'", c, i, actual[i])
}
}
}
func TestShouldNotFindAnyContextsInEmptyFile(t *testing.T) {
actual := astContextParse(`package main`, t)
if len(actual) != 0 {
t.Fatalf("expected no contexts to be found, but there was some: %v", actual)
}
}