2024-09-12 18:13:16 -04:00
|
|
|
// Copyright 2021 Harran Ali <harran.m@gmail.com>. All rights reserved.
|
|
|
|
// Copyright (c) 2024 Zeni Kim <zenik@smarteching.com>
|
|
|
|
// Use of this source code is governed by MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2024-09-16 17:01:44 -04:00
|
|
|
func TestNewHooks(t *testing.T) {
|
|
|
|
mw := NewHooks()
|
|
|
|
if fmt.Sprintf("%T", mw) != "*core.Hooks" {
|
|
|
|
t.Errorf("failed testing new hook")
|
2024-09-12 18:13:16 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-16 17:01:44 -04:00
|
|
|
func TestResloveHooks(t *testing.T) {
|
|
|
|
NewHooks()
|
|
|
|
mw := ResolveHooks()
|
|
|
|
if fmt.Sprintf("%T", mw) != "*core.Hooks" {
|
|
|
|
t.Errorf("failed resolve hooks")
|
2024-09-12 18:13:16 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAttach(t *testing.T) {
|
2024-09-16 17:01:44 -04:00
|
|
|
mw := NewHooks()
|
|
|
|
tmw := Hook(func(c *Context) {
|
2024-09-12 18:13:16 -04:00
|
|
|
c.GetLogger().Info("Testing!")
|
|
|
|
})
|
|
|
|
mw.Attach(tmw)
|
|
|
|
mws := mw.getByIndex(0)
|
|
|
|
if reflect.ValueOf(tmw).Pointer() != reflect.ValueOf(mws).Pointer() {
|
2024-09-16 17:01:44 -04:00
|
|
|
t.Errorf("Failed testing attach hook")
|
2024-09-12 18:13:16 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-16 17:01:44 -04:00
|
|
|
func TestGetHooks(t *testing.T) {
|
|
|
|
mw := NewHooks()
|
|
|
|
t1 := Hook(func(c *Context) {
|
2024-09-12 18:13:16 -04:00
|
|
|
c.GetLogger().Info("testing1!")
|
|
|
|
})
|
2024-09-16 17:01:44 -04:00
|
|
|
t2 := Hook(func(c *Context) {
|
2024-09-12 18:13:16 -04:00
|
|
|
c.GetLogger().Info("testing2!")
|
|
|
|
})
|
|
|
|
mw.Attach(t1)
|
|
|
|
mw.Attach(t2)
|
2024-09-16 17:01:44 -04:00
|
|
|
if len(mw.GetHooks()) != 2 {
|
|
|
|
t.Errorf("failed testing get hooks")
|
2024-09-12 18:13:16 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-16 17:01:44 -04:00
|
|
|
func TestHookGetByIndex(t *testing.T) {
|
|
|
|
mw := NewHooks()
|
|
|
|
t1 := Hook(func(c *Context) {
|
2024-09-12 18:13:16 -04:00
|
|
|
c.GetLogger().Info("testing!")
|
|
|
|
})
|
|
|
|
mw.Attach(t1)
|
|
|
|
if reflect.ValueOf(mw.getByIndex(0)).Pointer() != reflect.ValueOf(t1).Pointer() {
|
|
|
|
t.Errorf("failed testing get by index")
|
|
|
|
}
|
|
|
|
}
|