generated from LinuxSuRen/github-go
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add replacement in the package config
- Loading branch information
1 parent
7a27747
commit 6186ed5
Showing
7 changed files
with
94 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package cmd | ||
|
||
func getOrDefault(key, def string, data map[string]string) (result string) { | ||
var ok bool | ||
if result, ok = data[key]; !ok { | ||
result = def | ||
} | ||
return | ||
} | ||
|
||
func getReplacement(key string, data map[string]string) (result string) { | ||
return getOrDefault(key, key, data) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"github.com/magiconair/properties/assert" | ||
"testing" | ||
) | ||
|
||
func TestGetOrDefault(t *testing.T) { | ||
table := []testCase{ | ||
{key: "fake", def: "def", data: map[string]string{}, expected: "def"}, | ||
{key: "fake", def: "def", data: nil, expected: "def"}, | ||
{key: "fake", def: "def", data: map[string]string{ | ||
"fake": "good", | ||
}, expected: "good"}, | ||
} | ||
|
||
for index, item := range table { | ||
result := getOrDefault(item.key, item.def, item.data) | ||
assert.Equal(t, result, item.expected, fmt.Sprintf("test failed with '%d'", index)) | ||
} | ||
} | ||
|
||
func TestGetReplacement(t *testing.T) { | ||
table := []testCase{ | ||
{key: "fake", data: map[string]string{}, expected: "fake"}, | ||
{key: "fake", data: nil, expected: "fake"}, | ||
{key: "fake", data: map[string]string{ | ||
"fake": "good", | ||
}, expected: "good"}, | ||
} | ||
|
||
for index, item := range table { | ||
result := getReplacement(item.key, item.data) | ||
assert.Equal(t, result, item.expected, fmt.Sprintf("test failed with '%d'", index)) | ||
} | ||
} | ||
|
||
type testCase struct { | ||
key string | ||
def string | ||
data map[string]string | ||
expected string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters