From 0f544bf2781d9e7282479d887b263313f7c8840d Mon Sep 17 00:00:00 2001 From: David Bariod Date: Thu, 17 Jan 2019 13:42:01 +0100 Subject: [PATCH] Add a unit test to ensure hook are called in their registration order --- hook_test.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/hook_test.go b/hook_test.go index b967593..28f193d 100644 --- a/hook_test.go +++ b/hook_test.go @@ -190,3 +190,27 @@ func TestAddHookRace(t *testing.T) { // actually assert on the hook }) } + +type HookCallFunc struct { + F func() +} + +func (h *HookCallFunc) Levels() []Level { + return AllLevels +} + +func (h *HookCallFunc) Fire(e *Entry) error { + h.F() + return nil +} + +func TestHookFireOrder(t *testing.T) { + checkers := []string{} + h := LevelHooks{} + h.Add(&HookCallFunc{F: func() { checkers = append(checkers, "first hook") }}) + h.Add(&HookCallFunc{F: func() { checkers = append(checkers, "second hook") }}) + h.Add(&HookCallFunc{F: func() { checkers = append(checkers, "third hook") }}) + + h.Fire(InfoLevel, &Entry{}) + require.Equal(t, []string{"first hook", "second hook", "third hook"}, checkers) +}