-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathglib_test.go
109 lines (87 loc) · 3.11 KB
/
glib_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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package glib
import (
"fmt"
"testing"
)
// GLib values
func TestValue(t *testing.T) {
v1 := uint64(0xdeadbeaf)
a := ValueOf(v1)
b := NewValue(TYPE_UINT64)
a.Copy(b)
t.Logf("a = %s(%s), b = %s(%s)", a.Type(), a, b.Type(), b)
if b.Get().(uint64) != v1 {
t.Error("TYPE_UINT64")
}
v2 := -1
a = ValueOf(v2) // TYPE_GO_INT == TYPE_LONG
b = NewValue(TYPE_GO_INT32) // TYPE_GO_INT32 == TYPE_INT
a.Transform(b)
t.Logf("a = %s(%s), b = %s(%s)", a.Type(), a, b.Type(), b)
if b.Get().(int32) != int32(v2) {
t.Error("TYPE_GO_INT32 (TYPE_INT)")
}
}
// Signals by ID
func TestSignal(t *testing.T) {
// An object on which the signal will be emitted
o := NewObject(TYPE_OBJECT, nil)
// The new signal without return value, which can be emitted on TYPE_OBJECT
// and accepts one parameter of type TYPE_GO_INT
s := NewSignal("sig1", TYPE_NONE, o.Type(), TYPE_GO_INT)
t.Logf("Signal: %s", s)
// The Go variable of type A that have some methods. These methods will be
// used as signal handlers.
a := A("Test with IDs")
// Connect a.handler(*Object, int) to the signal.
// o will be passed as its first argument. Second argument of type int will
// be passed from second argument passed to the Emit function.
o.ConnectSid(s, 0, (*A).handler, &a)
// Connect a.noiHandler(int) to the signal.
// o will not be passed to the method. An argument of type int will be
// passed from second argument passed to the Emit function.
o.ConnectSidNoi(s, 0, (*A).noiHandler, &a)
// Connect funcHandler(*Object, int)to the signal.
// o will be passed as its first argument. Second argument of type int
// will be passed from second argument passed to the Emit function.
o.ConnectSid(s, 0, funcHandler, nil)
// Connect funcNoiHandler(int) to the signal.
// o will not be passed to the function. An argument of type int will be
// passed from second argument passed to the Emit function.
o.ConnectSidNoi(s, 0, funcNoiHandler, nil)
// Connect funcHandlerParam0(A, *Object, int) to the signal.
// &a will be passed as its first argument, o will be passed as its second
// argument. The thrid argument of type int will be from second argument
// passed to the Emit function.
o.ConnectSid(s, 0, funcHandlerParam0, &a)
// Emit signal with 123 integer as argument.
o.EmitById(s, 0, 123)
}
// Like TestSignal but uses signal names
func TestSignalName(t *testing.T) {
o := NewObject(TYPE_OBJECT, nil)
NewSignal("sig2", TYPE_NONE, TYPE_OBJECT, TYPE_GO_INT)
a := A("Test with names")
o.Connect("sig2", (*A).handler, &a)
o.ConnectNoi("sig2", (*A).noiHandler, &a)
o.Connect("sig2", funcHandler, nil)
o.ConnectNoi("sig2", funcNoiHandler, nil)
o.Connect("sig2", funcHandlerParam0, &a)
o.Emit("sig2", 456)
}
type A string
func (a *A) handler(o *Object, i int) {
fmt.Printf("handler: %s, %v, %d\n", *a, o, i)
}
func (a *A) noiHandler(i int) {
fmt.Printf("noiHandler: %s, %d\n", *a, i)
}
func funcHandler(o *Object, i int) {
fmt.Printf("funcHandler: %v, %d\n", o, i)
}
func funcNoiHandler(i int) {
fmt.Printf("funcNoiHandler: %d\n", i)
}
func funcHandlerParam0(a *A, o *Object, i int) {
fmt.Printf("funcHandlerParam0: %s %v %d\n", *a, o, i)
}