mirror of
https://git.sr.ht/~phw/scotty
synced 2025-04-25 22:07:56 +02:00
Use testify to simplify tests
This commit is contained in:
parent
f9ac0328a7
commit
c6331328d4
3 changed files with 19 additions and 28 deletions
|
@ -23,11 +23,10 @@ THE SOFTWARE.
|
|||
package backends_test
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"go.uploadedlobster.com/scotty/backends"
|
||||
)
|
||||
|
||||
|
@ -35,50 +34,32 @@ func TestResolveBackend(t *testing.T) {
|
|||
config := viper.New()
|
||||
config.Set("backend", "dump")
|
||||
backend, err := backends.ResolveBackend[backends.ListenImport](config)
|
||||
if err != nil {
|
||||
t.Errorf("ResolveBackend failed unexpected: %v", err)
|
||||
}
|
||||
realType := reflect.TypeOf(backend)
|
||||
if realType.Name() != "DumpBackend" {
|
||||
t.Errorf("ResolveBackend did not return a DumpBackend, unexpected type %v", realType)
|
||||
}
|
||||
assert.NoError(t, err)
|
||||
assert.IsType(t, backends.DumpBackend{}, backend)
|
||||
}
|
||||
|
||||
func TestResolveBackendUnknown(t *testing.T) {
|
||||
config := viper.New()
|
||||
config.Set("backend", "foo")
|
||||
_, err := backends.ResolveBackend[backends.ListenImport](config)
|
||||
if err == nil {
|
||||
t.Error("Expected ResolveBackend to fail", err)
|
||||
} else if err.Error() != "Unknown backend foo" {
|
||||
t.Errorf("Expected \"Unknown backend foo\", got \"%s\"", err)
|
||||
}
|
||||
assert.EqualError(t, err, "Unknown backend foo")
|
||||
}
|
||||
|
||||
func TestResolveBackendInvalidInterface(t *testing.T) {
|
||||
config := viper.New()
|
||||
config.Set("backend", "dump")
|
||||
_, err := backends.ResolveBackend[backends.ListenExport](config)
|
||||
if err == nil {
|
||||
t.Error("Expected ResolveBackend to fail", err)
|
||||
} else if err.Error() != "Backend dump does not implement ListenExport" {
|
||||
t.Errorf("Expected \"Backend dump does not implement ListenExport\", got \"%s\"", err)
|
||||
}
|
||||
assert.EqualError(t, err, "Backend dump does not implement ListenExport")
|
||||
}
|
||||
|
||||
func TestGetBackends(t *testing.T) {
|
||||
backends := backends.GetBackends()
|
||||
for _, info := range backends {
|
||||
if info.Name == "dump" {
|
||||
if !slices.Contains[[]string](info.ImportCapabilities, "listen") {
|
||||
t.Error("Backend \"dump\" is expected to provide listen import capability")
|
||||
}
|
||||
if len(info.ExportCapabilities) > 0 {
|
||||
t.Errorf(
|
||||
"Backend \"dump\" is not expected to provide any export capabilities, provides %s",
|
||||
info.ExportCapabilities,
|
||||
)
|
||||
}
|
||||
assert.Containsf(t, info.ImportCapabilities, "listen",
|
||||
"ImportCapabilities for \"dump\" expected to contain \"listens\"")
|
||||
assert.Emptyf(t, info.ExportCapabilities,
|
||||
"ExportCapabilities for \"dump\" expected to be empty")
|
||||
return // Finish the test
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue