2019-01-13 01:14:02 +00:00
|
|
|
package mrun
|
|
|
|
|
|
|
|
import (
|
2019-02-05 20:18:17 +00:00
|
|
|
"context"
|
2019-01-13 01:14:02 +00:00
|
|
|
. "testing"
|
|
|
|
|
2019-06-15 23:13:00 +00:00
|
|
|
"github.com/mediocregopher/mediocre-go-lib/mcmp"
|
2019-01-13 01:14:02 +00:00
|
|
|
"github.com/mediocregopher/mediocre-go-lib/mtest/massert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestHooks(t *T) {
|
2019-02-05 20:18:17 +00:00
|
|
|
var out []int
|
2019-01-13 01:14:02 +00:00
|
|
|
mkHook := func(i int) Hook {
|
2019-02-05 20:18:17 +00:00
|
|
|
return func(context.Context) error {
|
|
|
|
out = append(out, i)
|
2019-01-13 01:14:02 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-15 23:13:00 +00:00
|
|
|
cmp := new(mcmp.Component)
|
|
|
|
AddHook(cmp, 0, mkHook(1))
|
|
|
|
AddHook(cmp, 0, mkHook(2))
|
2019-02-05 20:18:17 +00:00
|
|
|
|
2019-06-15 23:13:00 +00:00
|
|
|
cmpA := cmp.Child("a")
|
|
|
|
AddHook(cmpA, 0, mkHook(3))
|
|
|
|
AddHook(cmpA, 999, mkHook(999)) // different key
|
2019-01-13 01:14:02 +00:00
|
|
|
|
2019-06-15 23:13:00 +00:00
|
|
|
AddHook(cmp, 0, mkHook(4))
|
2019-01-13 01:14:02 +00:00
|
|
|
|
2019-06-15 23:13:00 +00:00
|
|
|
cmpB := cmp.Child("b")
|
|
|
|
AddHook(cmpB, 0, mkHook(5))
|
|
|
|
cmpB1 := cmpB.Child("1")
|
|
|
|
AddHook(cmpB1, 0, mkHook(6))
|
2019-01-13 01:14:02 +00:00
|
|
|
|
2019-06-15 23:13:00 +00:00
|
|
|
AddHook(cmp, 0, mkHook(7))
|
2019-03-03 20:34:44 +00:00
|
|
|
|
2019-03-10 23:23:37 +00:00
|
|
|
massert.Require(t,
|
2019-06-15 23:13:00 +00:00
|
|
|
massert.Nil(TriggerHooks(context.Background(), cmp, 0)),
|
2019-03-03 20:34:44 +00:00
|
|
|
massert.Equal([]int{1, 2, 3, 4, 5, 6, 7}, out),
|
2019-03-10 23:23:37 +00:00
|
|
|
)
|
2019-01-13 01:14:02 +00:00
|
|
|
|
2019-02-05 20:18:17 +00:00
|
|
|
out = nil
|
2019-03-10 23:23:37 +00:00
|
|
|
massert.Require(t,
|
2019-06-15 23:13:00 +00:00
|
|
|
massert.Nil(TriggerHooksReverse(context.Background(), cmp, 0)),
|
2019-03-03 20:34:44 +00:00
|
|
|
massert.Equal([]int{7, 6, 5, 4, 3, 2, 1}, out),
|
2019-03-10 23:23:37 +00:00
|
|
|
)
|
2019-01-13 01:14:02 +00:00
|
|
|
}
|