mock: Add mock config interface

This commit is contained in:
Sasha Koshka 2025-01-28 13:45:48 -05:00
parent ea41137c9c
commit 7e5a78fef7
3 changed files with 75 additions and 0 deletions

26
mock/config.go Normal file
View File

@ -0,0 +1,26 @@
package mock
import "iter"
import "git.tebibyte.media/sashakoshka/camfish"
var _ camfish.Config = new(Config)
// Config implements camfish.Config.
type Config map[string] []string
func (config Config) Get(key string) string {
list, ok := config[key]
if !ok { return "" }
if len(list) == 0 { return "" }
return list[0]
}
func (config Config) GetAll(key string) iter.Seq2[int, string] {
return func(yield func(int, string) bool) {
list, ok := config[key]
if !ok { return }
for index, value := range list {
yield(index, value)
}
}
}

46
mock/config_test.go Normal file
View File

@ -0,0 +1,46 @@
package mock
import "testing"
func TestConfig(test *testing.T) {
config := Config {
"single": []string { "aslkdjasd" },
"multiple": []string { "item0", "item1" },
"empty": []string { },
}
if correct, got := config.Get("single"), "aslkdjasd"; correct != got {
test.Fatal("not equal:", got)
}
if correct, got := config.Get("multiple"), "item0"; correct != got {
test.Fatal("not equal:", got)
}
if correct, got := config.Get("empty"), ""; correct != got {
test.Fatal("not equal:", got)
}
if correct, got := config.Get("non-existent"), ""; correct != got {
test.Fatal("not equal:", got)
}
for index, value := range config.GetAll("single") {
if index < 0 { test.Fatal("index too small") }
if index > 0 { test.Fatal("index too large") }
if value != "aslkdjasd" {
test.Fatal("not equal:", value)
}
}
for index, value := range config.GetAll("multiple") {
if index < 0 { test.Fatal("index too small") }
if index > 1 { test.Fatal("index too large") }
switch value {
case "item0": if index != 0 { test.Fatal("not equal:", index, value) }
case "item1": if index != 1 { test.Fatal("not equal:", index, value) }
default:
test.Fatal("should not have value:", index, value)
}
}
for index, value := range config.GetAll("empty") {
test.Fatal("should not have value:", index, value)
}
for index, value := range config.GetAll("non-existent") {
test.Fatal("should not have value:", index, value)
}
}

3
mock/doc.go Normal file
View File

@ -0,0 +1,3 @@
// Package mock implements mock interface implementations and other utilities
// for testing actors.
package mock