* feat: Add TDD tests for core package Adds a new `tdd/` directory for TDD-style contract tests. Implements a comprehensive test suite for the `pkg/core` package, covering: - `New()` - `WithService()` - `WithName()` - `WithWails()` - `WithAssets()` - `WithServiceLock()` - `RegisterService()` - `Service()` - `ServiceFor()` - `MustServiceFor()` - `ACTION()` - `RegisterAction()` - `RegisterActions()` To support testing, a public `Assets()` method was added to the `Core` struct. * feat: Add TDD tests for e, io, runtime, and config packages Adds comprehensive TDD tests to the `tdd/` directory for the following packages: - `pkg/e` - `pkg/io` - `pkg/runtime` - `pkg/config` This significantly improves the test coverage of the project. To support testing the `runtime` package, the `newWithFactories` function was exported as `NewWithFactories`. The existing tests for the `config` package were moved from the `internal` package to the `tdd/` directory and adapted to use the public API. * fix: Update tdd tests for config, core, and runtime Updates the TDD tests for the `config`, `core`, and `runtime` packages to improve their coverage and correctness. - In `tdd/config_test.go`, the `TestIsFeatureEnabled` test is updated to use `s.Set` to modify the `features` slice, ensuring that the persistence logic is exercised. - In `tdd/core_test.go`, the `TestCore_WithAssets_Good` test is updated to use a real embedded filesystem with `//go:embed` to verify the contents of a test file. - In `tdd/runtime_test.go`, the `TestNew_Good` test is converted to a table-driven test to cover the happy path, error cases, and a case with a non-nil `application.App`. * fix: Fix build and improve test coverage This commit fixes a build failure in the `pkg/runtime` tests and significantly improves the test coverage for several packages. - Fixes a build failure in `pkg/runtime/runtime_test.go` by updating a call to an exported function. - Moves TDD tests for `config` and `e` packages into their respective package directories to ensure accurate coverage reporting. - Adds a new test suite for the `pkg/i18n` package, including a test helper to inject a mock i18n bundle. - Moves and updates tests for the `pkg/crypt` package to use its public API. - The coverage for `config` and `e` is now 100%. - The coverage for `crypt` and `i18n` has been significantly improved. --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
160 lines
3.5 KiB
Go
160 lines
3.5 KiB
Go
package tdd
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/Snider/Core/pkg/io"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
type MockMedium struct {
|
|
ReadFileFunc func(path string) (string, error)
|
|
WriteFileFunc func(path, content string) error
|
|
EnsureDirFunc func(path string) error
|
|
IsFileFunc func(path string) bool
|
|
}
|
|
|
|
func (m *MockMedium) Read(path string) (string, error) {
|
|
if m.ReadFileFunc != nil {
|
|
return m.ReadFileFunc(path)
|
|
}
|
|
return "", errors.New("not implemented")
|
|
}
|
|
|
|
func (m *MockMedium) Write(path, content string) error {
|
|
if m.WriteFileFunc != nil {
|
|
return m.WriteFileFunc(path, content)
|
|
}
|
|
return errors.New("not implemented")
|
|
}
|
|
|
|
func (m *MockMedium) EnsureDir(path string) error {
|
|
if m.EnsureDirFunc != nil {
|
|
return m.EnsureDirFunc(path)
|
|
}
|
|
return errors.New("not implemented")
|
|
}
|
|
|
|
func (m *MockMedium) IsFile(path string) bool {
|
|
if m.IsFileFunc != nil {
|
|
return m.IsFileFunc(path)
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (m *MockMedium) FileGet(path string) (string, error) {
|
|
return m.Read(path)
|
|
}
|
|
|
|
func (m *MockMedium) FileSet(path, content string) error {
|
|
return m.Write(path, content)
|
|
}
|
|
|
|
func TestIO_Read_Good(t *testing.T) {
|
|
medium := &MockMedium{
|
|
ReadFileFunc: func(path string) (string, error) {
|
|
assert.Equal(t, "test.txt", path)
|
|
return "hello", nil
|
|
},
|
|
}
|
|
content, err := io.Read(medium, "test.txt")
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, "hello", content)
|
|
}
|
|
|
|
func TestIO_Read_Bad(t *testing.T) {
|
|
medium := &MockMedium{
|
|
ReadFileFunc: func(path string) (string, error) {
|
|
return "", assert.AnError
|
|
},
|
|
}
|
|
_, err := io.Read(medium, "test.txt")
|
|
assert.Error(t, err)
|
|
assert.ErrorIs(t, err, assert.AnError)
|
|
}
|
|
|
|
func TestIO_Write_Good(t *testing.T) {
|
|
medium := &MockMedium{
|
|
WriteFileFunc: func(path, content string) error {
|
|
assert.Equal(t, "test.txt", path)
|
|
assert.Equal(t, "hello", content)
|
|
return nil
|
|
},
|
|
}
|
|
err := io.Write(medium, "test.txt", "hello")
|
|
assert.NoError(t, err)
|
|
}
|
|
|
|
func TestIO_Write_Bad(t *testing.T) {
|
|
medium := &MockMedium{
|
|
WriteFileFunc: func(path, content string) error {
|
|
return assert.AnError
|
|
},
|
|
}
|
|
err := io.Write(medium, "test.txt", "hello")
|
|
assert.Error(t, err)
|
|
assert.ErrorIs(t, err, assert.AnError)
|
|
}
|
|
|
|
func TestIO_EnsureDir_Good(t *testing.T) {
|
|
medium := &MockMedium{
|
|
EnsureDirFunc: func(path string) error {
|
|
assert.Equal(t, "testdir", path)
|
|
return nil
|
|
},
|
|
}
|
|
err := io.EnsureDir(medium, "testdir")
|
|
assert.NoError(t, err)
|
|
}
|
|
|
|
func TestIO_EnsureDir_Bad(t *testing.T) {
|
|
medium := &MockMedium{
|
|
EnsureDirFunc: func(path string) error {
|
|
return assert.AnError
|
|
},
|
|
}
|
|
err := io.EnsureDir(medium, "testdir")
|
|
assert.Error(t, err)
|
|
assert.ErrorIs(t, err, assert.AnError)
|
|
}
|
|
|
|
func TestIO_IsFile_Good(t *testing.T) {
|
|
medium := &MockMedium{
|
|
IsFileFunc: func(path string) bool {
|
|
assert.Equal(t, "test.txt", path)
|
|
return true
|
|
},
|
|
}
|
|
assert.True(t, io.IsFile(medium, "test.txt"))
|
|
}
|
|
|
|
func TestIO_Copy_Good(t *testing.T) {
|
|
source := &MockMedium{
|
|
ReadFileFunc: func(path string) (string, error) {
|
|
assert.Equal(t, "source.txt", path)
|
|
return "hello", nil
|
|
},
|
|
}
|
|
dest := &MockMedium{
|
|
WriteFileFunc: func(path, content string) error {
|
|
assert.Equal(t, "dest.txt", path)
|
|
assert.Equal(t, "hello", content)
|
|
return nil
|
|
},
|
|
}
|
|
err := io.Copy(source, "source.txt", dest, "dest.txt")
|
|
assert.NoError(t, err)
|
|
}
|
|
|
|
func TestIO_Copy_Bad(t *testing.T) {
|
|
source := &MockMedium{
|
|
ReadFileFunc: func(path string) (string, error) {
|
|
return "", assert.AnError
|
|
},
|
|
}
|
|
dest := &MockMedium{}
|
|
err := io.Copy(source, "source.txt", dest, "dest.txt")
|
|
assert.Error(t, err)
|
|
assert.ErrorIs(t, err, assert.AnError)
|
|
}
|