From 7e5a78fef7cf0b84c194de55bd35d7e1776ff053 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Tue, 28 Jan 2025 13:45:48 -0500 Subject: [PATCH] mock: Add mock config interface --- mock/config.go | 26 +++++++++++++++++++++++++ mock/config_test.go | 46 +++++++++++++++++++++++++++++++++++++++++++++ mock/doc.go | 3 +++ 3 files changed, 75 insertions(+) create mode 100644 mock/config.go create mode 100644 mock/config_test.go create mode 100644 mock/doc.go diff --git a/mock/config.go b/mock/config.go new file mode 100644 index 0000000..5cf7457 --- /dev/null +++ b/mock/config.go @@ -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) + } + } +} diff --git a/mock/config_test.go b/mock/config_test.go new file mode 100644 index 0000000..02eaa6f --- /dev/null +++ b/mock/config_test.go @@ -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) + } +} diff --git a/mock/doc.go b/mock/doc.go new file mode 100644 index 0000000..22cec64 --- /dev/null +++ b/mock/doc.go @@ -0,0 +1,3 @@ +// Package mock implements mock interface implementations and other utilities +// for testing actors. +package mock