reorgniaze the repo
This commit is contained in:
50
helloworld/hello.go
Normal file
50
helloworld/hello.go
Normal file
@ -0,0 +1,50 @@
|
||||
package helloworld
|
||||
|
||||
const (
|
||||
english = "English"
|
||||
spanish = "Spanish"
|
||||
)
|
||||
|
||||
var (
|
||||
helloPrefixMap map[string]string
|
||||
defaultWorldMap map[string]string
|
||||
)
|
||||
|
||||
func initDefaults() {
|
||||
initPrefixMap()
|
||||
initWorldMap()
|
||||
}
|
||||
|
||||
func initPrefixMap() {
|
||||
helloPrefixMap = make(map[string]string)
|
||||
helloPrefixMap[english] = "Hello "
|
||||
helloPrefixMap[spanish] = "Hola "
|
||||
}
|
||||
|
||||
func initWorldMap() {
|
||||
defaultWorldMap = make(map[string]string)
|
||||
defaultWorldMap[english] = "world"
|
||||
defaultWorldMap[spanish] = "Mundo"
|
||||
}
|
||||
|
||||
func getDefaultWorldByLanguage(language string) string {
|
||||
return defaultWorldMap[language]
|
||||
}
|
||||
|
||||
func getHelloPrefixByLanguage(language string) string {
|
||||
return helloPrefixMap[language]
|
||||
}
|
||||
|
||||
func Hello(name, language string) string {
|
||||
prefix := getHelloPrefixByLanguage(language)
|
||||
|
||||
if name == "" {
|
||||
return prefix + getDefaultWorldByLanguage(language)
|
||||
}
|
||||
return prefix + name
|
||||
}
|
||||
|
||||
// func main() {
|
||||
// initDefaults()
|
||||
// fmt.Println(Hello("Mary", "English"))
|
||||
// }
|
31
helloworld/hello_test.go
Normal file
31
helloworld/hello_test.go
Normal file
@ -0,0 +1,31 @@
|
||||
package helloworld
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestHello(t *testing.T) {
|
||||
initDefaults()
|
||||
t.Run("Hello someone", func(t *testing.T) {
|
||||
got := Hello("Jason", "English")
|
||||
exp := "Hello Jason"
|
||||
assertCorrectMsg(t, got, exp)
|
||||
})
|
||||
|
||||
t.Run("Hello default", func(t *testing.T) {
|
||||
got := Hello("", "English")
|
||||
exp := "Hello world"
|
||||
assertCorrectMsg(t, got, exp)
|
||||
})
|
||||
|
||||
t.Run("Hello default", func(t *testing.T) {
|
||||
got := Hello("Elodie", "Spanish")
|
||||
exp := "Hola Elodie"
|
||||
assertCorrectMsg(t, got, exp)
|
||||
})
|
||||
}
|
||||
|
||||
func assertCorrectMsg(t testing.TB, got, exp string) {
|
||||
t.Helper()
|
||||
if got != exp {
|
||||
t.Errorf("got %q expected %q", got, exp)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user