godog/internal/builder/ast_test.go
Softonik f6ed69097f
Некоторые проверки провалились
test / test (1.16.x) (push) Has been cancelled
test / test (1.17.x) (push) Has been cancelled
Модуль переименован для публикации
2025-04-03 05:36:58 +03:00

76 строки
1,6 КиБ
Go

package builder
import (
"go/parser"
"go/token"
"testing"
)
var astContextSrc = `package main
import (
"git.golang1.ru/softonik/godog"
)
func MyContext(s *godog.Suite) {
}`
var astTwoContextSrc = `package lib
import (
"git.golang1.ru/softonik/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, "Suite")
}
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)
}
}