docreflect/docs_test.go
2025-08-08 22:05:05 +02:00

117 lines
2.5 KiB
Go

package docreflect_test
import (
"code.squareroundforest.org/arpio/docreflect"
"code.squareroundforest.org/arpio/docreflect/internal/tests/src/testpackage"
"reflect"
"strings"
"testing"
)
func Test(t *testing.T) {
t.Run("unregistered", func(t *testing.T) {
d := docreflect.Docs("foo/bar/baz.qux")
if d != "" {
t.Fatal()
}
})
t.Run("package", func(t *testing.T) {
d := docreflect.Docs("code.squareroundforest.org/arpio/docreflect/internal/tests/src/testpackage")
if !strings.Contains(d, "Package testpackage") {
t.Fatal()
}
})
t.Run("function by path", func(t *testing.T) {
d := docreflect.Docs("code.squareroundforest.org/arpio/docreflect/internal/tests/src/testpackage.ExportedFunc")
if !strings.Contains(d, "ExportedFunc has documentation") {
t.Fatal()
}
if strings.Contains(d, "func(p1, p2)") {
t.Fatal()
}
})
t.Run("function by value", func(t *testing.T) {
d := docreflect.Function(reflect.ValueOf(testpackage.ExportedFunc))
if !strings.Contains(d, "ExportedFunc has documentation") {
t.Fatal(d)
}
if strings.Contains(d, "func(p1, p2)") {
t.Fatal()
}
})
t.Run("function params", func(t *testing.T) {
d := docreflect.FunctionParams(reflect.ValueOf(testpackage.ExportedFunc))
if len(d) != 2 {
t.Fatal(d)
}
if d[0] != "p1" {
t.Fatal(d)
}
if d[1] != "p2" {
t.Fatal(d)
}
})
t.Run("method as function value", func(t *testing.T) {
s := testpackage.ExportedType{}
m := s.Method
d := docreflect.Function(reflect.ValueOf(m))
if !strings.Contains(d, "Method is a method of ExportedType") {
t.Fatal(d)
}
p := docreflect.FunctionParams(reflect.ValueOf(m))
if len(p) != 3 {
t.Fatal()
}
if p[0] != "p1" || p[1] != "p2" || p[2] != "p3" {
t.Fatal()
}
})
t.Run("type", func(t *testing.T) {
s := testpackage.ExportedType{}
typ := reflect.TypeOf(s)
d := docreflect.Type(typ)
if !strings.Contains(d, "ExportedType has docs") {
t.Fatal()
}
})
t.Run("field", func(t *testing.T) {
s := testpackage.ExportedType{}
typ := reflect.TypeOf(s)
d := docreflect.Field(typ, "Foo")
if !strings.Contains(d, "Foo is a field") {
t.Fatal()
}
})
t.Run("method", func(t *testing.T) {
s := testpackage.ExportedType{}
typ := reflect.TypeOf(s)
d := docreflect.Method(typ, "Method")
if !strings.Contains(d, "Method is a method of ExportedType") {
t.Fatal()
}
p := docreflect.MethodParams(typ, "Method")
if len(p) != 3 {
t.Fatal()
}
if p[0] != "p1" || p[1] != "p2" || p[2] != "p3" {
t.Fatal()
}
})
}