nswrap/types/convert_test.go

390 lines
9.4 KiB
Go
Raw Normal View History

2019-06-04 11:27:46 -04:00
package types
import (
"fmt"
"reflect"
"testing"
)
func isNil(x interface{}) bool {
return x == nil ||
(reflect.ValueOf(x).Kind() == reflect.Ptr &&
reflect.ValueOf(x).IsNil())
2019-06-04 11:27:46 -04:00
}
func TestType(t *testing.T) {
i := 1
var str string
var n *Node
var tp *Type
chk := func(actual, expected interface{}) {
t.Run(fmt.Sprintf("TestType%d", i), func(t *testing.T) {
if isNil(actual) && isNil(expected) {
return
}
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Test failed for %s\n", str)
fmt.Println("Actual:\n", actual)
fmt.Println("Expected:\n", expected)
2019-06-04 11:27:46 -04:00
}
})
}
chk_newtype := func() {
tp = NewTypeFromString(str, "")
chk(tp, &Type{Node: n, Class: ""})
2019-06-04 11:27:46 -04:00
}
//tests on nil Type pointers:
chk(tp.BaseType(), nil)
chk(tp.CType(), "")
chk(tp.IsFunction(), false)
chk(tp.IsPointer(), false)
chk(tp.IsFunctionPtr(), false)
chk(tp.IsValist(), false)
2019-06-04 11:27:46 -04:00
str = "int"
n = &Node{"TypeName", "", []*Node{
&Node{"TypeSpecifier", "int", []*Node{}}}}
2019-06-04 11:27:46 -04:00
chk_newtype()
tint := tp
str = "id"
n = &Node{"TypeName", "", []*Node{
&Node{"TypedefName", "id", []*Node{}}}}
chk_newtype()
//nsid := tp
2019-06-04 11:27:46 -04:00
str = "NSObject"
n = &Node{"TypeName", "", []*Node{
&Node{"TypedefName", "NSObject", []*Node{}}}}
2019-06-04 11:27:46 -04:00
chk_newtype()
nso := tp
str = "NSString"
n = &Node{"TypeName", "", []*Node{
&Node{"TypedefName", "NSString", []*Node{}}}}
2019-06-04 11:27:46 -04:00
chk_newtype()
nst := tp
str = "NSString**"
n = &Node{"TypeName", "", []*Node{
&Node{"TypedefName", "NSString", []*Node{}},
&Node{"Pointer", "*", []*Node{}},
&Node{"Pointer", "*", []*Node{}}}}
chk_newtype()
chk(tp.IsPointer(), true)
chk(tp.Typedef(), nil)
nstpp := tp
str = "NSObject**"
n = &Node{"TypeName", "", []*Node{
&Node{"TypedefName", "NSObject", []*Node{}},
&Node{"Pointer", "*", []*Node{}},
&Node{"Pointer", "*", []*Node{}}}}
chk_newtype()
chk(tp.IsPointer(), true)
nsopp := tp
str = "NSObject*"
n = &Node{"TypeName", "", []*Node{
&Node{"TypedefName", "NSObject", []*Node{}},
&Node{"Pointer", "*", []*Node{}}}}
chk_newtype()
chk(tp.IsPointer(), true)
nsop := tp
2019-06-04 11:27:46 -04:00
str = "NSString*"
n = &Node{"TypeName", "", []*Node{
&Node{"TypedefName", "NSString", []*Node{}},
&Node{"Pointer", "*", []*Node{}}}}
2019-06-04 11:27:46 -04:00
chk_newtype()
chk(tp.IsPointer(), true)
chk(tp.Typedef(), nil)
nstp := tp
2019-06-04 11:27:46 -04:00
str = "myTypedef"
AddTypedef("myTypedef", tp)
n = &Node{"TypeName", "", []*Node{
&Node{"TypedefName", "myTypedef", []*Node{}}}}
2019-06-04 11:27:46 -04:00
chk_newtype()
chk(tp.Typedef(), nstp)
2019-06-04 11:27:46 -04:00
str = "const NSArray <ObjectType * _Nonnull> *"
n = &Node{"TypeName", "", []*Node{
&Node{"TypeQualifier", "const", []*Node{}},
&Node{"TypedefName", "NSArray", []*Node{}},
&Node{"GenericList", "", []*Node{
&Node{"Generic", "", []*Node{
&Node{"TypedefName", "ObjectType", []*Node{}},
&Node{"Pointer", "*", []*Node{}},
&Node{"NullableAnnotation", "_Nonnull", []*Node{}}}}}},
&Node{"Pointer", "*", []*Node{}}}}
2019-06-04 11:27:46 -04:00
chk_newtype()
chk(tp.CType(), "NSArray*")
chk(tp.CTypeAttrib(), "const NSArray*")
chk(tp.CGoType(), "*C.NSArray")
chk(tp.GoType(), "*NSArray")
2019-06-04 11:27:46 -04:00
SetTypeParam("MyClass", "ObjectType", "MyClass")
2019-06-04 11:27:46 -04:00
str = "id<ObjectType *>"
n = &Node{"TypeName", "", []*Node{
&Node{"TypedefName", "id", []*Node{}},
&Node{"GenericList", "", []*Node{
&Node{"Generic", "", []*Node{
&Node{"TypedefName", "ObjectType", []*Node{}},
&Node{"Pointer", "*", []*Node{}}}}}}}}
2019-06-04 11:27:46 -04:00
chk_newtype()
tp = NewType(n, "MyClass")
chk(tp.CType(), "NSObject*")
2019-06-04 11:27:46 -04:00
chk(tp.String(),
`<TypeName> ''
2019-06-04 11:27:46 -04:00
-<TypedefName> 'id'
-<GenericList> ''
--<Generic> ''
---<TypedefName> 'MyClass'
---<Pointer> '*'
`)
x, _ := clean(nil, "MyClass")
chk(x, nil)
SetSuper("NSString", "NSObject")
chk(Super("NSString"), "NSObject")
2019-06-04 11:27:46 -04:00
tp2 := tp.CloneToClass("NSObject")
chk(tp2.Class, "NSObject")
2019-06-04 11:27:46 -04:00
str = "you can't parse this"
tp = NewTypeFromString(str, "")
2019-06-04 11:27:46 -04:00
tp2 = &Type{}
chk(tp, tp2)
2019-06-04 11:27:46 -04:00
str = "id<ObjectType *>"
tp2 = NewTypeFromString(str, "MyClass")
chk(tp.BaseType(), tp)
2019-06-04 11:27:46 -04:00
str = "id<ObjectType *>*"
tp = NewTypeFromString(str, "MyClass")
chk(tp.BaseType(), tp2)
chk(tp.PointsTo(), tp2)
AddTypedef("myTypedef", tp)
2019-06-04 11:27:46 -04:00
str = "myTypedef"
n = &Node{"TypeName", "", []*Node{
&Node{"TypedefName", "myTypedef", []*Node{}}}}
2019-06-04 11:27:46 -04:00
chk_newtype()
chk(tp.PointsTo(), tp2)
chk(tp2.PointsTo(), nil)
2019-06-04 11:27:46 -04:00
chk(tp.GoTypeDecl(false), `
2019-06-04 11:27:46 -04:00
type MyTypedef **C.NSObject
`)
str = "void"
n = &Node{"TypeName", "", []*Node{
&Node{"TypeSpecifier", "void", []*Node{}}}}
2019-06-04 11:27:46 -04:00
chk_newtype()
chk(tp.GoTypeDecl(false), "")
2019-06-04 11:27:46 -04:00
void := tp
str = "BOOL"
n, _ = Parse(str)
2019-06-04 11:27:46 -04:00
chk_newtype()
bl := tp
str = "void**"
n, _ = Parse(str)
2019-06-04 11:27:46 -04:00
chk_newtype()
chk(tp.GoType(), "*unsafe.Pointer")
chk(tp.CToGo("var"), "(*unsafe.Pointer)(unsafe.Pointer(var))")
2019-06-04 11:27:46 -04:00
voidpp := tp
Wrap("NSObject")
str = "NSObject*"
n, _ = Parse(str)
2019-06-04 11:27:46 -04:00
chk_newtype()
chk(tp.GoType(), "*NSObject")
2019-06-04 11:27:46 -04:00
Wrap("NSString")
chk(nso.GoTypeDecl(false), `
2019-06-04 11:27:46 -04:00
type NSObject interface {
Ptr() unsafe.Pointer
}
`)
chk(nso.GoType(), "NSObject")
2019-06-04 11:27:46 -04:00
chk(nst.GoTypeDecl(false), `
2019-06-04 11:27:46 -04:00
type NSString struct { Id }
func (o *NSString) Ptr() unsafe.Pointer { if o == nil { return nil }; return o.ptr }
func (o *Id) NSString() *NSString {
return (*NSString)(unsafe.Pointer(o))
2019-06-04 11:27:46 -04:00
}
`)
str = "int(void)"
n, _ = Parse(str)
2019-06-04 11:27:46 -04:00
chk_newtype()
chk(tp.IsFunction(), true)
chk(tp.IsFunctionPtr(), false)
chk(tp.ReturnType().CType(), "int")
2019-06-04 11:27:46 -04:00
str = "int *(void)"
n, _ = Parse(str)
2019-06-04 11:27:46 -04:00
chk_newtype()
chk(tp.IsFunction(), true)
chk(tp.ReturnType().CType(), "int*")
chk(tp.ReturnType().GoType(), "*Int")
2019-06-04 11:27:46 -04:00
fn := tp
AddTypedef("myTypedef", fn)
2019-06-04 11:27:46 -04:00
str = "myTypedef"
n, _ = Parse(str)
2019-06-04 11:27:46 -04:00
chk_newtype()
chk(tp.IsFunction(), true)
chk(tp.IsFunctionPtr(), false)
chk(tp.ReturnType().CType(), "int*")
2019-06-04 11:27:46 -04:00
str = "int (*)(void)"
n, _ = Parse(str)
2019-06-04 11:27:46 -04:00
chk_newtype()
chk(tp.IsFunction(), false)
chk(tp.IsPointer(), true)
chk(tp.IsFunctionPtr(), true)
chk(tp.ReturnType(), nil)
2019-06-04 11:27:46 -04:00
fnptr := tp
AddTypedef("myTypedef", fnptr)
2019-06-04 11:27:46 -04:00
str = "myTypedef"
n, _ = Parse(str)
2019-06-04 11:27:46 -04:00
chk_newtype()
chk(tp.IsFunction(), false)
chk(tp.IsPointer(), true)
chk(tp.IsFunctionPtr(), true)
chk(tp.ReturnType(), nil)
2019-06-04 11:27:46 -04:00
chk(tp.IsValist(), false)
2019-06-04 11:27:46 -04:00
str = "__va_list_tag"
n, _ = Parse(str)
2019-06-04 11:27:46 -04:00
chk_newtype()
chk(tp.IsValist(), true)
2019-06-04 11:27:46 -04:00
str = "GoToC"
var rtype *Type
ptypes := []*Type{nsop, nstp, tint, voidpp}
pnames := []string{"p1", "p2", "p3", "p4"}
snames := []string{"", "", "", ""}
goImports := make(map[string]bool)
2019-06-04 11:27:46 -04:00
chk_gotoc := func(expected string) {
chk(GoToC("myFun", "myFun", pnames, snames, rtype, ptypes, false, false, false, goImports), expected)
2019-06-04 11:27:46 -04:00
}
chk_gotoc("")
rtype = void
chk_gotoc(`C.myFun(p1.Ptr(), p2.Ptr(), (C.int)(p3), unsafe.Pointer(p4))
runtime.KeepAlive(o)`)
2019-06-04 11:27:46 -04:00
rtype = bl
chk_gotoc(
`ret := (C.myFun(p1.Ptr(), p2.Ptr(), (C.int)(p3), unsafe.Pointer(p4))) != 0
runtime.KeepAlive(o)
2019-06-04 11:27:46 -04:00
return ret`)
rtype = voidpp
chk_gotoc(
`ret := (*unsafe.Pointer)(unsafe.Pointer(C.myFun(p1.Ptr(), p2.Ptr(), (C.int)(p3), unsafe.Pointer(p4))))
runtime.KeepAlive(o)
2019-06-04 11:27:46 -04:00
return ret`)
rtype = nstp
2019-06-04 11:27:46 -04:00
chk_gotoc(
`ret := &NSString{}
ret.ptr = unsafe.Pointer(C.myFun(p1.Ptr(), p2.Ptr(), (C.int)(p3), unsafe.Pointer(p4)))
if ret.ptr == nil { runtime.KeepAlive(o); return ret }
if ret.ptr == o.ptr { runtime.KeepAlive(o); return (*NSString)(unsafe.Pointer(o)) }
runtime.KeepAlive(o)
2019-06-04 11:27:46 -04:00
return ret`)
rtype = nsop
2019-06-04 11:27:46 -04:00
chk_gotoc(
`ret := &Id{}
ret.ptr = unsafe.Pointer(C.myFun(p1.Ptr(), p2.Ptr(), (C.int)(p3), unsafe.Pointer(p4)))
if ret.ptr == nil { runtime.KeepAlive(o); return ret }
if ret.ptr == o.ptr { runtime.KeepAlive(o); return (*Id)(unsafe.Pointer(o)) }
runtime.KeepAlive(o)
2019-06-04 11:27:46 -04:00
return ret`)
ptypes[1].Variadic = true
ptypes[0].Variadic = false
2019-06-04 11:27:46 -04:00
chk_gotoc(
`ret := &Id{}
ret.ptr = unsafe.Pointer(C.myFun(p1.Ptr(), unsafe.Pointer(&p2), (C.int)(p3), unsafe.Pointer(p4)))
if ret.ptr == nil { runtime.KeepAlive(o); return ret }
if ret.ptr == o.ptr { runtime.KeepAlive(o); return (*Id)(unsafe.Pointer(o)) }
runtime.KeepAlive(o)
2019-06-04 11:27:46 -04:00
return ret`)
ptypes[1].Variadic = false
2019-06-04 11:27:46 -04:00
snames[1] = "p2p"
ptypes[1] = nsopp
2019-06-04 11:27:46 -04:00
chk_gotoc(
`ret := &Id{}
ret.ptr = unsafe.Pointer(C.myFun(p1.Ptr(), (*unsafe.Pointer)(unsafe.Pointer(&p2p[0])), (C.int)(p3), unsafe.Pointer(p4)))
2019-06-04 11:27:46 -04:00
(*p2) = (*p2)[:cap(*p2)]
for i := 0; i < len(*p2); i++ {
if p2p[i] == nil {
(*p2) = (*p2)[:i]
break
}
if (*p2)[i] == nil {
(*p2)[i] = &Id{}
}
2019-06-04 11:27:46 -04:00
(*p2)[i].ptr = p2p[i]
}
if ret.ptr == nil { runtime.KeepAlive(o); return ret }
if ret.ptr == o.ptr { runtime.KeepAlive(o); return (*Id)(unsafe.Pointer(o)) }
runtime.KeepAlive(o)
2019-06-04 11:27:46 -04:00
return ret`)
snames[1] = ""
snames[2] = "p3p"
ptypes[1] = nsop
ptypes[2] = nstpp
2019-06-04 11:27:46 -04:00
chk_gotoc(
`ret := &Id{}
ret.ptr = unsafe.Pointer(C.myFun(p1.Ptr(), p2.Ptr(), (*unsafe.Pointer)(unsafe.Pointer(&p3p[0])), unsafe.Pointer(p4)))
2019-06-04 11:27:46 -04:00
(*p3) = (*p3)[:cap(*p3)]
for i := 0; i < len(*p3); i++ {
if p3p[i] == nil {
(*p3) = (*p3)[:i]
break
}
if (*p3)[i] == nil {
(*p3)[i] = &NSString{}
}
2019-06-04 11:27:46 -04:00
(*p3)[i].ptr = p3p[i]
}
if ret.ptr == nil { runtime.KeepAlive(o); return ret }
if ret.ptr == o.ptr { runtime.KeepAlive(o); return (*Id)(unsafe.Pointer(o)) }
runtime.KeepAlive(o)
2019-06-04 11:27:46 -04:00
return ret`)
chk(GoToC("myFun", "myFun", pnames, snames, rtype, ptypes, true, false, false, goImports),
`ret := &Id{}
ret.ptr = unsafe.Pointer(C.myFun(p1.Ptr(), p2.Ptr(), (*unsafe.Pointer)(unsafe.Pointer(&p3p[0])), p4))
2019-06-04 11:27:46 -04:00
(*p3) = (*p3)[:cap(*p3)]
for i := 0; i < len(*p3); i++ {
if p3p[i] == nil {
(*p3) = (*p3)[:i]
break
}
if (*p3)[i] == nil {
(*p3)[i] = &NSString{}
}
2019-06-04 11:27:46 -04:00
(*p3)[i].ptr = p3p[i]
}
if ret.ptr == nil { runtime.KeepAlive(o); return ret }
if ret.ptr == o.ptr { runtime.KeepAlive(o); return (*Id)(unsafe.Pointer(o)) }
runtime.KeepAlive(o)
2019-06-04 11:27:46 -04:00
return ret`)
}