From 95d944613bffce1cee3783568ae229e116168ba4 Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Sun, 29 Dec 2019 13:12:52 -0700 Subject: Export Replacer and use concrete type instead of interface The interface was only making things difficult; a concrete pointer is probably best. --- replacer_test.go | 90 +++++++++++++++++++++++++++----------------------------- 1 file changed, 43 insertions(+), 47 deletions(-) (limited to 'replacer_test.go') diff --git a/replacer_test.go b/replacer_test.go index 88e83f5..42e9ee1 100644 --- a/replacer_test.go +++ b/replacer_test.go @@ -132,7 +132,7 @@ func TestReplacerSet(t *testing.T) { } func TestReplacerReplaceKnown(t *testing.T) { - rep := replacer{ + rep := Replacer{ providers: []ReplacerFunc{ // split our possible vars to two functions (to test if both functions are called) func(key string) (val string, ok bool) { @@ -204,7 +204,7 @@ func TestReplacerReplaceKnown(t *testing.T) { } func TestReplacerDelete(t *testing.T) { - rep := replacer{ + rep := Replacer{ static: map[string]string{ "key1": "val1", "key2": "val2", @@ -264,59 +264,55 @@ func TestReplacerMap(t *testing.T) { } func TestReplacerNew(t *testing.T) { - var tc = NewReplacer() + rep := NewReplacer() - rep, ok := tc.(*replacer) - if ok { - if len(rep.providers) != 2 { - t.Errorf("Expected providers length '%v' got length '%v'", 2, len(rep.providers)) - } else { - // test if default global replacements are added as the first provider - hostname, _ := os.Hostname() - os.Setenv("CADDY_REPLACER_TEST", "envtest") - defer os.Setenv("CADDY_REPLACER_TEST", "") + if len(rep.providers) != 2 { + t.Errorf("Expected providers length '%v' got length '%v'", 2, len(rep.providers)) + } else { + // test if default global replacements are added as the first provider + hostname, _ := os.Hostname() + os.Setenv("CADDY_REPLACER_TEST", "envtest") + defer os.Setenv("CADDY_REPLACER_TEST", "") - for _, tc := range []struct { - variable string - value string - }{ - { - variable: "system.hostname", - value: hostname, - }, - { - variable: "system.slash", - value: string(filepath.Separator), - }, - { - variable: "system.os", - value: runtime.GOOS, - }, - { - variable: "system.arch", - value: runtime.GOARCH, - }, - { - variable: "env.CADDY_REPLACER_TEST", - value: "envtest", - }, - } { - if val, ok := rep.providers[0](tc.variable); ok { - if val != tc.value { - t.Errorf("Expected value '%s' for key '%s' got '%s'", tc.value, tc.variable, val) - } - } else { - t.Errorf("Expected key '%s' to be recognized by first provider", tc.variable) + for _, tc := range []struct { + variable string + value string + }{ + { + variable: "system.hostname", + value: hostname, + }, + { + variable: "system.slash", + value: string(filepath.Separator), + }, + { + variable: "system.os", + value: runtime.GOOS, + }, + { + variable: "system.arch", + value: runtime.GOARCH, + }, + { + variable: "env.CADDY_REPLACER_TEST", + value: "envtest", + }, + } { + if val, ok := rep.providers[0](tc.variable); ok { + if val != tc.value { + t.Errorf("Expected value '%s' for key '%s' got '%s'", tc.value, tc.variable, val) } + } else { + t.Errorf("Expected key '%s' to be recognized by first provider", tc.variable) } } - } else { - t.Errorf("Expected type of replacer %T got %T ", &replacer{}, tc) } + } -func testReplacer() replacer { - return replacer{ +func testReplacer() Replacer { + return Replacer{ providers: make([]ReplacerFunc, 0), static: make(map[string]string), } -- cgit v1.2.3