docreflect/docs_test.go

170 lines
3.8 KiB
Go
Raw Permalink Normal View History

2025-08-08 21:44:31 +02:00
package docreflect_test
import (
"code.squareroundforest.org/arpio/docreflect"
2025-08-08 22:05:05 +02:00
"code.squareroundforest.org/arpio/docreflect/internal/tests/src/testpackage"
2025-08-08 21:44:31 +02:00
"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) {
2025-08-08 22:05:05 +02:00
d := docreflect.Docs("code.squareroundforest.org/arpio/docreflect/internal/tests/src/testpackage")
2025-08-08 21:44:31 +02:00
if !strings.Contains(d, "Package testpackage") {
t.Fatal()
}
})
t.Run("function by path", func(t *testing.T) {
2025-08-08 22:05:05 +02:00
d := docreflect.Docs("code.squareroundforest.org/arpio/docreflect/internal/tests/src/testpackage.ExportedFunc")
2025-08-08 21:44:31 +02:00
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()
}
})
2025-08-09 20:39:45 +02:00
t.Run("field in inline struct type", func(t *testing.T) {
s := &testpackage.ExportedType{}
typ := reflect.TypeOf(s)
d := docreflect.Field(typ, "Bar", "Baz")
if !strings.Contains(d, "Baz is another field") {
t.Fatal(d)
}
})
2025-08-17 18:33:25 +02:00
t.Run("field in inline pointer struct type", func(t *testing.T) {
s := &testpackage.ExportedType{}
typ := reflect.TypeOf(s)
d := docreflect.Field(typ, "Qux", "Quux")
if !strings.Contains(d, "Quux is a number") {
t.Fatal(d)
}
})
2025-08-09 20:39:45 +02:00
t.Run("field in another type", func(t *testing.T) {
s := &testpackage.ExportedType{}
typ := reflect.TypeOf(s)
d := docreflect.Field(typ, "Baz", "Foo")
if !strings.Contains(d, "Foo is a field in ExportedType2") {
t.Fatal(d)
}
})
2025-08-17 18:33:25 +02:00
t.Run("field in an anon struct field", func(t *testing.T) {
s := &testpackage.ExportedType3{}
typ := reflect.TypeOf(s)
d := docreflect.Field(typ, "Foo")
if !strings.Contains(d, "Foo is a field in ExportedType2") {
t.Fatal(d)
}
})
2025-08-08 21:44:31 +02:00
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()
}
})
2025-08-17 18:33:25 +02:00
t.Run("method as function value", func(t *testing.T) {
s := testpackage.ExportedType{}
d := docreflect.Function(reflect.ValueOf(s.Method))
if !strings.Contains(d, "Method is a method of ExportedType") {
t.Fatal()
}
p := docreflect.FunctionParams(reflect.ValueOf(s.Method))
if len(p) != 3 {
t.Fatal()
}
if p[0] != "p1" || p[1] != "p2" || p[2] != "p3" {
t.Fatal()
}
})
2025-08-08 21:44:31 +02:00
}