feat(demo): add comprehensive public API tests to gotypes and gotoken
Expanded demo tests to cover more public methods and functions from go/types and go/token packages, as discovered via `go doc`. **go/types additions:** - Type comparison functions: Identical, AssignableTo, Comparable, ConvertibleTo - Interface checking: Implements, AssertableTo - String formatting: TypeString, ObjectString with qualifiers - Lookup utilities: LookupFieldOrMethod, NewMethodSet - Type utilities: IsInterface, Default, Id **go/token additions:** - Utility functions: IsExported, IsIdentifier, IsKeyword, Lookup These additions provide better coverage of the packages' public APIs and demonstrate proper usage patterns for interface conversions and method calls. Generated with [codeagent](https://github.com/qbox/codeagent) Co-authored-by: luoliwoshang <51194195+luoliwoshang@users.noreply.github.com>
This commit is contained in:
@@ -13,6 +13,7 @@ func main() {
|
||||
testPosition()
|
||||
testTokenPrecedence()
|
||||
testTokenKeywords()
|
||||
testUtilityFunctions()
|
||||
}
|
||||
|
||||
func testPos() {
|
||||
@@ -236,3 +237,31 @@ func testPosition() {
|
||||
|
||||
fmt.Println("SUCCESS: Position operations work correctly\n")
|
||||
}
|
||||
|
||||
func testUtilityFunctions() {
|
||||
fmt.Println("\n=== Test Utility Functions ===")
|
||||
|
||||
fmt.Printf("IsExported(\"Foo\"): %v\n", token.IsExported("Foo"))
|
||||
fmt.Printf("IsExported(\"foo\"): %v\n", token.IsExported("foo"))
|
||||
fmt.Printf("IsExported(\"_foo\"): %v\n", token.IsExported("_foo"))
|
||||
|
||||
fmt.Printf("IsIdentifier(\"foo\"): %v\n", token.IsIdentifier("foo"))
|
||||
fmt.Printf("IsIdentifier(\"foo123\"): %v\n", token.IsIdentifier("foo123"))
|
||||
fmt.Printf("IsIdentifier(\"123foo\"): %v\n", token.IsIdentifier("123foo"))
|
||||
fmt.Printf("IsIdentifier(\"foo-bar\"): %v\n", token.IsIdentifier("foo-bar"))
|
||||
|
||||
fmt.Printf("IsKeyword(\"func\"): %v\n", token.IsKeyword("func"))
|
||||
fmt.Printf("IsKeyword(\"if\"): %v\n", token.IsKeyword("if"))
|
||||
fmt.Printf("IsKeyword(\"foo\"): %v\n", token.IsKeyword("foo"))
|
||||
|
||||
lookupFunc := token.Lookup("func")
|
||||
fmt.Printf("Lookup(\"func\"): %s\n", lookupFunc)
|
||||
|
||||
lookupIdent := token.Lookup("myVar")
|
||||
fmt.Printf("Lookup(\"myVar\"): %s\n", lookupIdent)
|
||||
|
||||
lookupFor := token.Lookup("for")
|
||||
fmt.Printf("Lookup(\"for\"): %s\n", lookupFor)
|
||||
|
||||
fmt.Println("SUCCESS: Utility functions work correctly\n")
|
||||
}
|
||||
|
||||
@@ -21,6 +21,11 @@ func main() {
|
||||
testPointer()
|
||||
testMap()
|
||||
testChan()
|
||||
testTypeComparison()
|
||||
testTypeChecking()
|
||||
testStringFunctions()
|
||||
testLookupFunctions()
|
||||
testUtilityFunctions()
|
||||
}
|
||||
|
||||
func testBasicTypes() {
|
||||
@@ -253,3 +258,129 @@ func testChan() {
|
||||
|
||||
fmt.Println("SUCCESS: Chan operations work correctly\n")
|
||||
}
|
||||
|
||||
func testTypeComparison() {
|
||||
fmt.Println("\n=== Test Type Comparison Functions ===")
|
||||
|
||||
t1 := types.Typ[types.Int]
|
||||
t2 := types.Typ[types.Int]
|
||||
t3 := types.Typ[types.String]
|
||||
|
||||
if !types.Identical(t1, t2) {
|
||||
panic("Identical failed: int should be identical to int")
|
||||
}
|
||||
fmt.Printf("Identical(int, int): %v\n", types.Identical(t1, t2))
|
||||
fmt.Printf("Identical(int, string): %v\n", types.Identical(t1, t3))
|
||||
|
||||
if !types.AssignableTo(t1, t2) {
|
||||
panic("AssignableTo failed")
|
||||
}
|
||||
fmt.Printf("AssignableTo(int, int): %v\n", types.AssignableTo(t1, t2))
|
||||
fmt.Printf("AssignableTo(int, string): %v\n", types.AssignableTo(t1, t3))
|
||||
|
||||
fmt.Printf("Comparable(int): %v\n", types.Comparable(t1))
|
||||
fmt.Printf("Comparable(string): %v\n", types.Comparable(t3))
|
||||
|
||||
fmt.Printf("ConvertibleTo(int, int): %v\n", types.ConvertibleTo(t1, t2))
|
||||
|
||||
fmt.Println("SUCCESS: Type comparison functions work correctly\n")
|
||||
}
|
||||
|
||||
func testTypeChecking() {
|
||||
fmt.Println("\n=== Test Type Checking Functions ===")
|
||||
|
||||
pkg := types.NewPackage("example.com/test", "test")
|
||||
|
||||
m1 := types.NewFunc(token.NoPos, pkg, "Method1", types.NewSignatureType(nil, nil, nil, nil, nil, false))
|
||||
m2 := types.NewFunc(token.NoPos, pkg, "Method2", types.NewSignatureType(nil, nil, nil, nil, nil, false))
|
||||
iface := types.NewInterfaceType([]*types.Func{m1, m2}, nil)
|
||||
iface.Complete()
|
||||
|
||||
fields := []*types.Var{
|
||||
types.NewField(token.NoPos, nil, "x", types.Typ[types.Int], false),
|
||||
}
|
||||
structType := types.NewStruct(fields, nil)
|
||||
|
||||
fmt.Printf("Implements(struct, interface): %v\n", types.Implements(structType, iface))
|
||||
fmt.Printf("Implements(int, interface): %v\n", types.Implements(types.Typ[types.Int], iface))
|
||||
|
||||
emptyIface := types.NewInterfaceType(nil, nil)
|
||||
emptyIface.Complete()
|
||||
fmt.Printf("Implements(int, empty interface): %v\n", types.Implements(types.Typ[types.Int], emptyIface))
|
||||
|
||||
fmt.Printf("AssertableTo(interface, int): %v\n", types.AssertableTo(iface, types.Typ[types.Int]))
|
||||
|
||||
fmt.Println("SUCCESS: Type checking functions work correctly\n")
|
||||
}
|
||||
|
||||
func testStringFunctions() {
|
||||
fmt.Println("\n=== Test String Functions ===")
|
||||
|
||||
pkg := types.NewPackage("example.com/test", "test")
|
||||
varObj := types.NewVar(token.NoPos, pkg, "myVar", types.Typ[types.Int])
|
||||
|
||||
objStr := types.ObjectString(varObj, nil)
|
||||
fmt.Printf("ObjectString: %s\n", objStr)
|
||||
|
||||
objStrQual := types.ObjectString(varObj, types.RelativeTo(pkg))
|
||||
fmt.Printf("ObjectString (qualified): %s\n", objStrQual)
|
||||
|
||||
typeStr := types.TypeString(types.Typ[types.Int], nil)
|
||||
fmt.Printf("TypeString(int): %s\n", typeStr)
|
||||
|
||||
sliceType := types.NewSlice(types.Typ[types.String])
|
||||
sliceStr := types.TypeString(sliceType, nil)
|
||||
fmt.Printf("TypeString([]string): %s\n", sliceStr)
|
||||
|
||||
fmt.Println("SUCCESS: String functions work correctly\n")
|
||||
}
|
||||
|
||||
func testLookupFunctions() {
|
||||
fmt.Println("\n=== Test Lookup Functions ===")
|
||||
|
||||
pkg := types.NewPackage("example.com/test", "test")
|
||||
|
||||
fields := []*types.Var{
|
||||
types.NewField(token.NoPos, pkg, "X", types.Typ[types.Int], false),
|
||||
types.NewField(token.NoPos, pkg, "Y", types.Typ[types.String], false),
|
||||
}
|
||||
structType := types.NewStruct(fields, nil)
|
||||
|
||||
obj, index, indirect := types.LookupFieldOrMethod(structType, false, pkg, "X")
|
||||
if obj == nil {
|
||||
panic("LookupFieldOrMethod failed to find X")
|
||||
}
|
||||
fmt.Printf("LookupFieldOrMethod found: %s, index: %v, indirect: %v\n", obj.Name(), index, indirect)
|
||||
|
||||
obj2, index2, indirect2 := types.LookupFieldOrMethod(structType, false, pkg, "NonExistent")
|
||||
fmt.Printf("LookupFieldOrMethod (non-existent): found=%v, index=%v, indirect=%v\n", obj2 != nil, index2, indirect2)
|
||||
|
||||
mset := types.NewMethodSet(structType)
|
||||
fmt.Printf("NewMethodSet: %d methods\n", mset.Len())
|
||||
|
||||
fmt.Println("SUCCESS: Lookup functions work correctly\n")
|
||||
}
|
||||
|
||||
func testUtilityFunctions() {
|
||||
fmt.Println("\n=== Test Utility Functions ===")
|
||||
|
||||
pkg := types.NewPackage("example.com/test", "test")
|
||||
|
||||
iface := types.NewInterfaceType(nil, nil)
|
||||
iface.Complete()
|
||||
|
||||
fmt.Printf("IsInterface(interface): %v\n", types.IsInterface(iface))
|
||||
fmt.Printf("IsInterface(int): %v\n", types.IsInterface(types.Typ[types.Int]))
|
||||
|
||||
typedNil := types.Typ[types.UntypedNil]
|
||||
defaultType := types.Default(typedNil)
|
||||
fmt.Printf("Default(UntypedNil): %v\n", defaultType)
|
||||
|
||||
intDefault := types.Default(types.Typ[types.Int])
|
||||
fmt.Printf("Default(int): %v\n", intDefault)
|
||||
|
||||
idStr := types.Id(pkg, "MyType")
|
||||
fmt.Printf("Id(pkg, \"MyType\"): %s\n", idStr)
|
||||
|
||||
fmt.Println("SUCCESS: Utility functions work correctly\n")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user