-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
registry_test.go
78 lines (61 loc) · 3.23 KB
/
registry_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
package sprout
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
func TestAddFunction(t *testing.T) {
funcsMap := make(FunctionMap)
// Define two different functions
testFunc1 := func() string { return "Hello, World!" }
testFunc2 := func() string { return "Should Not Overwrite" }
testFunc3 := func() string { return "Should Be Defined" }
// Test adding a new function
AddFunction(funcsMap, "testFunc", testFunc1)
assert.Contains(t, funcsMap, "testFunc", "Function 'testFunc' should be added to the FunctionMap")
// Call the function and check the result
result := funcsMap["testFunc"].(func() string)()
assert.Equal(t, "Hello, World!", result, "Function 'testFunc' should return 'Hello, World!'")
// Test trying to overwrite an existing function
AddFunction(funcsMap, "testFunc", testFunc2)
result = funcsMap["testFunc"].(func() string)()
assert.Equal(t, "Hello, World!", result, "Function 'testFunc' should not be overwritten and should still return 'Hello, World!'")
// Test adding a new function after the previous one
AddFunction(funcsMap, "testFunc2", testFunc3)
result = funcsMap["testFunc2"].(func() string)()
assert.Equal(t, "Should Be Defined", result, "Function 'testFunc2' should be added and return 'Should Be Defined'")
}
func TestAddAlias(t *testing.T) {
aliasMap := make(FunctionAliasMap)
// Test adding zero aliases
AddAlias(aliasMap, "originalFunc")
assert.NotContains(t, aliasMap, "originalFunc", "No aliases should be added if none are provided")
// Test adding aliases for an existing function
AddAlias(aliasMap, "originalFunc", "alias1", "alias2")
assert.Contains(t, aliasMap, "originalFunc", "Aliases should be added under 'originalFunc'")
assert.Equal(t, []string{"alias1", "alias2"}, aliasMap["originalFunc"], "Aliases should be correctly registered")
// Test adding more aliases to an existing function
AddAlias(aliasMap, "originalFunc", "alias3")
assert.Equal(t, []string{"alias1", "alias2", "alias3"}, aliasMap["originalFunc"], "New alias should be added to the existing aliases")
// Test adding an alias to a function that doesn't exist in the map yet
AddAlias(aliasMap, "nonExistentFunc", "aliasX")
assert.Contains(t, aliasMap, "nonExistentFunc", "Aliases should be added under 'nonExistentFunc' even if the function doesn't exist")
}
func TestWithRegistries(t *testing.T) {
// Define two registries with functions and aliases
mockRegistry1 := new(MockRegistry)
mockRegistry1.On("UID").Return("mockRegistry1")
mockRegistry1.On("LinkHandler", mock.Anything).Return(nil)
mockRegistry1.On("RegisterFunctions", mock.Anything).Return(nil)
mockRegistry2 := new(MockRegistry)
mockRegistry2.linkHandlerMustCrash = true
mockRegistry2.On("UID").Return("mockRegistry2")
mockRegistry2.On("LinkHandler", mock.Anything).Return(nil)
mockRegistry1.On("RegisterFunctions", mock.Anything).Return(nil)
// Create a handler with the registries
handler := New(WithRegistries(mockRegistry1, mockRegistry2))
handler.Build()
// Check that the functions and aliases are present in the handler
assert.Contains(t, handler.registries, mockRegistry1, "Registry 1 should be added to the handler")
assert.Contains(t, handler.registries, mockRegistry2, "Registry 2 should be added to the handler")
}