-
Notifications
You must be signed in to change notification settings - Fork 13
/
troop_test.go
69 lines (60 loc) · 1.38 KB
/
troop_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package goof
import (
"errors"
"fmt"
"os"
"testing"
)
//go:noinline
func Barf(err error) int {
if err != nil {
return len(err.Error())
}
return 5
}
// no dead code elim
func init() { Barf(nil) }
func (suite) TestCall(t *testing.T) {
symbol := fmt.Sprintf("%s.Barf", importPath)
if _, err := troop.Call(symbol, errors.New("hi")); err != nil {
t.Fatal(err)
}
if _, err := troop.Call(symbol, nil); err != nil {
t.Fatal(err)
}
}
func (suite) TestCallFprintf(t *testing.T) {
fmt.Fprintf(os.Stdout, "hello world %d\n", 2)
if _, err := troop.Call("fmt.Fprintf", os.Stdout, "hello world %d\n", []interface{}{2}); err != nil {
t.Fatal(err)
}
}
func (suite) TestCallFailures(t *testing.T) {
symbol := fmt.Sprintf("%s.Barf", importPath)
type is []interface{}
cases := []struct {
name string
args []interface{}
}{
{symbol, is{nil, nil}}, // too many args
{symbol, is{false}}, // wrong arg kind
{symbol, is{"hello", 2}}, // wrong arg kind
}
for i, c := range cases {
out, err := troop.Call(c.name, c.args...)
if err == nil {
t.Logf("%d: %+v", i, c)
t.Errorf("expected an error. out: %#v", out)
}
}
}
func (suite) TestGlobals(t *testing.T) {
troop_rv, err := troop.Global(fmt.Sprintf("%s.troop", importPath))
if err != nil {
t.Fatal(err)
}
troop2 := troop_rv.Addr().Interface().(*Troop)
if troop2 != &troop {
t.Fatal("got a different troop")
}
}