| 1 |
// Copyright 2016 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package objc |
| 6 |
|
| 7 |
import ( |
| 8 |
"reflect" |
| 9 |
"runtime" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"golang.org/x/mobile/internal/importers" |
| 13 |
) |
| 14 |
|
| 15 |
func TestImport(t *testing.T) { |
| 16 |
if runtime.GOOS != "darwin" { |
| 17 |
t.Skipf("can only parse objc types on darwin") |
| 18 |
} |
| 19 |
tests := []struct { |
| 20 |
ref importers.PkgRef |
| 21 |
name string |
| 22 |
methods []*Func |
| 23 |
}{ |
| 24 |
{ |
| 25 |
ref: importers.PkgRef{Pkg: "Foundation/NSObjectP", Name: "Hash"}, |
| 26 |
name: "NSObject", |
| 27 |
methods: []*Func{ |
| 28 |
&Func{Sig: "hash", GoName: "Hash", Ret: &Type{Kind: Uint, Decl: "NSUInteger"}}, |
| 29 |
}, |
| 30 |
}, |
| 31 |
{ |
| 32 |
ref: importers.PkgRef{Pkg: "Foundation/NSString", Name: "StringWithContentsOfFileEncodingError"}, |
| 33 |
name: "NSString", |
| 34 |
methods: []*Func{ |
| 35 |
&Func{ |
| 36 |
Sig: "stringWithContentsOfFile:encoding:error:", |
| 37 |
GoName: "StringWithContentsOfFileEncodingError", |
| 38 |
Params: []*Param{ |
| 39 |
&Param{Name: "path", Type: &Type{Kind: String, Decl: "NSString *"}}, |
| 40 |
&Param{Name: "enc", Type: &Type{Kind: Uint, Decl: "NSStringEncoding"}}, |
| 41 |
&Param{Name: "error", Type: &Type{Kind: Class, Name: "NSError", Decl: "NSError * * _Nullable", Indirect: true}}, |
| 42 |
}, |
| 43 |
Ret: &Type{Kind: 3, Decl: "NSString *"}, |
| 44 |
Static: true, |
| 45 |
}, |
| 46 |
}, |
| 47 |
}, |
| 48 |
} |
| 49 |
for _, test := range tests { |
| 50 |
refs := &importers.References{ |
| 51 |
Refs: []importers.PkgRef{test.ref}, |
| 52 |
Names: make(map[string]struct{}), |
| 53 |
} |
| 54 |
for _, m := range test.methods { |
| 55 |
refs.Names[m.GoName] = struct{}{} |
| 56 |
} |
| 57 |
types, err := Import(refs) |
| 58 |
if err != nil { |
| 59 |
t.Fatal(err) |
| 60 |
} |
| 61 |
if len(types) == 0 { |
| 62 |
t.Fatalf("got no types, expected at least 1") |
| 63 |
} |
| 64 |
n := types[0] |
| 65 |
if n.Name != test.name { |
| 66 |
t.Errorf("got class name %s, expected %s", n.Name, test.name) |
| 67 |
} |
| 68 |
loop: |
| 69 |
for _, exp := range test.methods { |
| 70 |
for _, got := range n.AllMethods { |
| 71 |
if reflect.DeepEqual(exp, got) { |
| 72 |
continue loop |
| 73 |
} |
| 74 |
} |
| 75 |
for _, got := range n.Funcs { |
| 76 |
if reflect.DeepEqual(exp, got) { |
| 77 |
continue loop |
| 78 |
} |
| 79 |
} |
| 80 |
t.Errorf("failed to find method: %+v", exp) |
| 81 |
} |
| 82 |
} |
| 83 |
} |