Files
job-search/internal/config/config_test.go
T

69 lines
2.0 KiB
Go

package config
import (
"reflect"
"testing"
)
// Kinds that LoadFromViper knows how to read.
var viperKinds = map[reflect.Kind]bool{
reflect.String: true, reflect.Int: true, reflect.Bool: true,
}
// Every key round-trips through TypeOf/ValueOf/SetValue and has a type that
// LoadFromViper can read.
func TestKeysRoundTrip(t *testing.T) {
var conf JobSearchConfig
if err := applyDefaults(&conf); err != nil {
t.Fatalf("defaults: %v", err)
}
for key, want := range conf.Flatten() {
kind, err := conf.TypeOf(key)
if err != nil {
t.Errorf("TypeOf(%q): %v", key, err)
} else if !viperKinds[kind] {
t.Errorf("TypeOf(%q) = %v, which LoadFromViper does not handle", key, kind)
}
got, err := conf.ValueOf(key)
if err != nil || !reflect.DeepEqual(got, want) {
t.Errorf("ValueOf(%q) = %#v, %v; want %#v", key, got, err, want)
}
if err := conf.SetValue(key, got); err != nil {
t.Errorf("SetValue(%q, %#v): %v", key, got, err)
}
}
}
// Every section has the full method set, and its Flatten keys (which use the
// section's JSCK* constant) line up with its cfg tag in the parent.
func TestSections(t *testing.T) {
var conf JobSearchConfig
all := conf.Flatten()
checkSections(t, all, "", reflect.ValueOf(&conf).Elem())
}
func checkSections(t *testing.T, all map[string]any, prefix string, section reflect.Value) {
for _, f := range fieldsOf(section.Type()) {
if !f.section {
continue
}
fv := section.FieldByIndex(f.index)
name := fv.Type().Name()
for _, m := range []string{"Map", "Flatten", "TypeOf", "ValueOf", "SetValue"} {
if !fv.Addr().MethodByName(m).IsValid() {
t.Errorf("%s is missing method %s", name, m)
}
}
if flatten := fv.MethodByName("Flatten"); flatten.IsValid() {
keys := flatten.Call(nil)[0].Interface().(map[string]any)
for k := range keys {
if _, ok := all[joinKey(prefix, k)]; !ok {
t.Errorf("%s.Flatten() key %q does not match its cfg tag %q",
name, k, joinKey(prefix, f.key))
}
}
}
checkSections(t, all, joinKey(prefix, f.key), fv)
}
}