-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathevent_test.go
79 lines (64 loc) · 2.49 KB
/
event_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
// Package eventdispatcher contains a set of tools making up a simple and
// reliable event dispatcher
package eventdispatcher
import (
"fmt"
"github.com/stretchr/testify/assert"
"testing"
)
// Tests the name getter
func TestName(t *testing.T) {
assert := assert.New(t)
e := getTestEvent()
n := getTestEventName()
assert.Equal(n, e.Name(), fmt.Sprintf("The event name provided %q is different than the one taken from the event: %q", n, e.Name()))
}
func getTestEvent() ParamsEvent {
e := NewParamsEvent(getTestEventName())
return *e
}
func getTestEventName() string {
n := "test_name"
return n
}
func TestStopPropagation(t *testing.T) {
assert := assert.New(t)
e := getTestEvent()
assert.False(e.IsPropagationStopped(), fmt.Sprintf("The event propagation is stopped before calling %q", "IsPropagationStopped"))
e.StopPropagation()
assert.True(e.IsPropagationStopped(), fmt.Sprintf("The event propagation is not stopped after calling %q", "IsPropagationStopped"))
}
type testType struct {
foo string
bar int64
}
func TestParams(t *testing.T) {
assert := assert.New(t)
e := getTestEvent()
p := "test value"
k := "test_key"
// Has nonexisting param
assert.False(e.HasParam(k), fmt.Sprintf("The event does not contain the param %s", k))
// Get nonexisting param
v, ok := e.GetParam(k)
assert.Equal("", v, fmt.Sprintf("Expected value for nonexisting key is `%s`, returned value %s", "", v))
assert.False(ok, fmt.Sprintf("%s expects second returned value to be false if param does not exists.", "GetParam"))
re := e.SetParam(k, p)
assert.Equal(&e, re, fmt.Sprintf("The %s method should return same event instance for chaining!", "SetParam"))
// Has existing param
assert.True(e.HasParam(k), fmt.Sprintf("The event should contain the param %s with value %s", k, p))
// Get existing param
v, ok = e.GetParam(k)
assert.Equal(p, v.(string), fmt.Sprintf("Expected value %s, returned value %s", p, v.(string)))
assert.True(ok, fmt.Sprintf("%s expects second returned value to be true if param exists.", "GetParam"))
// Remove param
re = e.RemoveParam(k)
assert.Equal(&e, re, fmt.Sprintf("The %s method should return same event instance for chaining!", "SetParam"))
// Again check nonexisting params
assert.False(e.HasParam(k), fmt.Sprintf("The event does not contain the param %s", k))
// Testing setting parameter with non-string value
ns := testType{"test", 1}
e.SetParam(k, ns)
rns, _ := e.GetParam(k)
assert.Equal(ns, rns.(testType), fmt.Sprintf("The event does not contain valid param %s", k))
}