aboutsummaryrefslogtreecommitdiffstats
path: root/eventer/eventer_test.go
blob: b35267af6a576d06814a97cd953f60a7bd84c428 (plain) (blame)
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
package eventer

import "testing"

func TestChannel(t *testing.T) {
    eventer := New(nil)

    c := make(Channel, 1)
    eventer.RegisterChannel("test", c)
    eventer.Post("test", "hello world")

    res := <-c

    if res.Data.(string) != "hello world" {
        t.Error("Expected event with data 'hello world'. Got", res.Data)
    }
}

func TestFunction(t *testing.T) {
    eventer := New(nil)

    var data string
    eventer.RegisterFunc("test", func(ev Event) {
        data = ev.Data.(string)
    })
    eventer.Post("test", "hello world")

    if data != "hello world" {
        t.Error("Expected event with data 'hello world'. Got", data)
    }
}

func TestRegister(t *testing.T) {
    eventer := New(nil)

    c := eventer.Register("test")
    eventer.Post("test", "hello world")

    res := <-c

    if res.Data.(string) != "hello world" {
        t.Error("Expected event with data 'hello world'. Got", res.Data)
    }
}

func TestOn(t *testing.T) {
    eventer := New(nil)

    c := make(Channel, 1)
    eventer.On("test", c)

    var data string
    eventer.On("test", func(ev Event) {
        data = ev.Data.(string)
    })
    eventer.Post("test", "hello world")

    res := <-c
    if res.Data.(string) != "hello world" {
        t.Error("Expected channel event with data 'hello world'. Got", res.Data)
    }

    if data != "hello world" {
        t.Error("Expected function event with data 'hello world'. Got", data)
    }
}