full unit testing and fixes

- mapToString is ordered according to setup
- setupSingle & teardownSingle no longer return "ok" states
- teardown functions support errors
- setup and teardown functions receive module
This commit is contained in:
Elijah Duffy
2025-06-03 14:49:00 -07:00
parent 5378c53537
commit ed8137647d
4 changed files with 695 additions and 65 deletions

61
module_test.go Normal file
View File

@@ -0,0 +1,61 @@
package app
import (
"log/slog"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewModule(t *testing.T) {
assert := assert.New(t)
// Test creating a module with no options
mod := NewModule("testModule", ModuleOpts{})
assert.NotNil(mod, "expected module to be created")
assert.Equal("testModule", mod.name, "expected module name to match")
assert.Nil(mod.setup, "expected setup function to be nil")
assert.Nil(mod.teardown, "expected teardown function to be nil")
assert.Empty(mod.depends, "expected dependencies to be empty")
assert.False(mod.loaded, "expected module to not be loaded")
// Test creating a module with setup and teardown functions and dependencies
setupFn := func(m *Module) error { return nil }
teardownFn := func(m *Module) error { return nil }
modWithOpts := NewModule("testModuleWithOpts", ModuleOpts{
Setup: setupFn,
Teardown: teardownFn,
Depends: []string{"dependency1", "dependency2"},
})
assert.NotNil(modWithOpts, "expected module with options to be created")
assert.NotNil(modWithOpts.setup, "expected setup function to be set")
assert.NotNil(modWithOpts.teardown, "expected teardown function to be set")
assert.Equal([]string{"dependency1", "dependency2"}, modWithOpts.depends, "expected dependencies to match")
}
func TestModule(t *testing.T) {
assert := assert.New(t)
// Create a module and set its logger
mod := NewModule("testModule", ModuleOpts{})
assert.Panics(func() { mod.Logger() }, "expected Logger to panic when logger is not set")
logger := slog.Default()
mod.logger = logger
// Test Logger method
assert.Equal(logger, mod.Logger(), "expected Logger to return the correct logger")
// Test Name method
assert.Equal("testModule", mod.Name(), "expected Name to return the module's name")
// Test Loaded method
assert.False(mod.Loaded(), "expected Loaded to return false initially")
// Test RequireLoaded method
assert.Panics(func() { mod.RequireLoaded() }, "expected RequireLoaded to panic when module is not loaded")
// Test setting the module as loaded
mod.loaded = true
assert.NotPanics(func() { mod.RequireLoaded() }, "expected RequireLoaded to not panic when module is loaded")
}