From a78d885726c63069c8a4935d85c71d3b564bb6b4 Mon Sep 17 00:00:00 2001 From: Xercoy Date: Fri, 5 Oct 2018 01:49:10 -0700 Subject: [PATCH 1/2] main.go, main_test.go: refactored getBrowserNameByOS, added tests --- main.go | 43 ++++++++++++++++++++++--------------------- main_test.go | 23 +++++++++++++++++++++++ 2 files changed, 45 insertions(+), 21 deletions(-) diff --git a/main.go b/main.go index 756cbbe..be2b264 100644 --- a/main.go +++ b/main.go @@ -32,9 +32,11 @@ const ( ) // Colors for console output -var blue = color.New(color.FgBlue, color.Bold).SprintFunc() -var yellow = color.New(color.FgYellow, color.Bold).SprintFunc() -var red = color.New(color.FgRed, color.Bold).SprintFunc() +var ( + blue = color.New(color.FgBlue, color.Bold).SprintFunc() + yellow = color.New(color.FgYellow, color.Bold).SprintFunc() + red = color.New(color.FgRed, color.Bold).SprintFunc() +) type logWriter struct{} @@ -223,27 +225,26 @@ func findBrowser(target string) string { } // getBrowserNameByOS normilizes browser name -func getBrowserNameByOS(k string) string { - browser := "" - switch k { - case "google", "chrome": - switch runtime.GOOS { - case "darwin": - browser = "Google Chrome" - } - case "mozilla", "firefox": - switch runtime.GOOS { - case "darwin": - browser = "Firefox" - } - case "brave": - switch runtime.GOOS { - case "darwin": - browser = "Brave" +func getBrowserNameByOS(denormalizedStr string) string { + os := runtime.GOOS + + // key = denormalized value, value = normalized value + browserNameMap := map[string]string{ + "google": "Google Chrome", + "chrome": "Google Chrome", + "mozilla": "Firefox", + "firefox": "Firefox", + "brave": "Brave", + } + + if os == "darwin" { + normalizedStr, ok := browserNameMap[denormalizedStr] + if ok { + return normalizedStr } } - return browser + return "" } // checkGoPath checks for GOPATH diff --git a/main_test.go b/main_test.go index 778ad70..0e2158b 100644 --- a/main_test.go +++ b/main_test.go @@ -3,6 +3,7 @@ package main import ( "log" "reflect" + "runtime" "testing" "github.com/stretchr/testify/assert" @@ -47,3 +48,25 @@ func TestGetLobstersStories(t *testing.T) { assert.NotNil(t, news) assert.Equal(t, 10, len(news), "They should be equal") } + +func TestGetBrowserNameByOS(t *testing.T) { + // For now, getBrowserName only assumes the logic below + // applies to the darwin OS; add subtests as more os values + // are added. + if runtime.GOOS == "darwin" { + t.Run("Validate Darwin OS", func(t *testing.T) { + browserNameMap := map[string][]string{ + "Google Chrome": []string{"google", "chrome"}, + "Firefox": []string{"mozilla", "firefox"}, + "Brave": []string{"brave"}, + } + + // test every possible browser string value + for normalizedStr, browserStrSlice := range browserNameMap { + for _, browserStr := range browserStrSlice { + assert.Equal(t, getBrowserNameByOS(browserStr), normalizedStr) + } + } + }) + } +} From 3c96f9328476c6b8ad983455c25121b554c91a93 Mon Sep 17 00:00:00 2001 From: Xercoy Date: Fri, 5 Oct 2018 01:54:09 -0700 Subject: [PATCH 2/2] README: updated credits --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index f1f73f5..23d82c7 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,7 @@ Stay up to update with new technology and waste all your morning reading everyth - [Fatih Arslan](https://github.com/fatih/color) - [Martin Angers](https://github.com/PuerkitoBio/goquery) - [skratchdot](https://github.com/skratchdot/open-golang) +- [Corey Prak](https://www.instagram.com/prakattak/) #### License