/* Copyright © 2023 Philipp Wolfer Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package backends_test import ( "reflect" "slices" "testing" "github.com/spf13/viper" "go.uploadedlobster.com/scotty/backends" ) 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) } } 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) } } 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) } } 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, ) } return // Finish the test } } // If we got here the "dump" backend was not included t.Errorf("GetBackends() did not return expected bacend \"dump\"") }