From 89a9b6e5d3e43db2314a73257fa45c366d19e4c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Max=20Str=C3=BCbing?= Date: Fri, 14 Jun 2019 10:56:28 +0200 Subject: [PATCH 1/2] refactor: extract flag parsing --- cmd/duration/duration.go | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/cmd/duration/duration.go b/cmd/duration/duration.go index c81aea9..f421aad 100644 --- a/cmd/duration/duration.go +++ b/cmd/duration/duration.go @@ -93,6 +93,21 @@ func printHelp() { fmt.Println("\tduration sleep 5 && sleep 4 (use ie a bash script instead)") } +func isFlag(short string, long string) func(string) bool { + return func(maybeFlag string) bool { + trimmedFlag := strings.Trim(maybeFlag, "-") + return trimmedFlag == short || trimmedFlag == long + } +} + +func isHelpFlag(input string) bool { + return isFlag("h", "help")(input) +} + +func isVersionFlag(input string) bool { + return isFlag("v", "version")(input) +} + func main() { if len(os.Args) < 2 { fmt.Fprintln(os.Stderr, "ERROR: You need to provide a command to execute.") @@ -100,21 +115,20 @@ func main() { os.Exit(1) } - program := strings.Join(os.Args[1:2], "") + programOrFlag := strings.Join(os.Args[1:2], "") args := strings.Join(os.Args[2:], " ") - maybeFlag := strings.Trim(program, "-") - if maybeFlag == "version" || maybeFlag == "v" { + if isVersionFlag(programOrFlag) { printVersion() os.Exit(0) } - if maybeFlag == "help" || maybeFlag == "h" { + if isHelpFlag(programOrFlag) { printHelp() os.Exit(0) } - cmd := exec.Command(program, args) + cmd := exec.Command(programOrFlag, args) var output bytes.Buffer From 7fefd714935c241fd94a2d03040451b02de4cfbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Max=20Str=C3=BCbing?= Date: Fri, 14 Jun 2019 10:56:40 +0200 Subject: [PATCH 2/2] refactor: use table driven tests --- cmd/duration/duration_test.go | 165 ++++++++++++++++++++++++++-------- 1 file changed, 126 insertions(+), 39 deletions(-) diff --git a/cmd/duration/duration_test.go b/cmd/duration/duration_test.go index bccb592..98cff4e 100644 --- a/cmd/duration/duration_test.go +++ b/cmd/duration/duration_test.go @@ -6,74 +6,161 @@ import ( ) func TestPadTimePart(t *testing.T) { - result := padTimePart(20) - - if result != "20" { - t.Errorf("Should not pad if value has two things, got: %v", result) + padTimePartTests := []struct { + input int + expected string + }{ + {20, "20"}, + {2, "02"}, } - result = padTimePart(2) - if result != "02" { - t.Errorf("Should pad if value has one thing, got: %v", result) + for _, tt := range padTimePartTests { + actual := padTimePart(tt.input) + + if actual != tt.expected { + t.Errorf("padTimePart(%d): expected: %v, got: %v", tt.input, tt.expected, actual) + } } } func TestGetSeconds(t *testing.T) { - result := getSeconds(time.Nanosecond) - - if result != "00" { - t.Errorf("Should correctly calculate seconds, got: %v", result) + getSecondsTest := []struct { + input time.Duration + expected string + }{ + {time.Nanosecond, "00"}, + {time.Second * 5, "05"}, + {time.Second * 65, "05"}, } - result = getSeconds(time.Second * 5) + for _, tt := range getSecondsTest { + actual := getSeconds(tt.input) - if result != "05" { - t.Errorf("Should correctly calculate seconds, got: %v", result) + if actual != tt.expected { + t.Errorf("getSeconds(%d): expected: %v, got: %v", tt.input, tt.expected, actual) + } } +} - result = getSeconds(time.Second * 65) +func TestGetMinutes(t *testing.T) { + getMinutesTest := []struct { + input time.Duration + expected string + }{ + {time.Nanosecond, "00"}, + {time.Minute * 5, "05"}, + {time.Minute * 65, "05"}, + } + + for _, tt := range getMinutesTest { + actual := getMinutes(tt.input) - if result != "05" { - t.Errorf("Should correctly calculate seconds, got: %v", result) + if actual != tt.expected { + t.Errorf("getMinutes(%d): expected: %v, got: %v", tt.input, tt.expected, actual) + } } } -func TestGetMinutes(t *testing.T) { - result := getMinutes(time.Nanosecond) - - if result != "00" { - t.Errorf("Should correctly calculate seconds, got: %v", result) +func TestGetHours(t *testing.T) { + getHoursTest := []struct { + input time.Duration + expected string + }{ + {time.Nanosecond, "00"}, + {time.Hour * 5, "05"}, + {time.Hour * 25, "25"}, } - result = getMinutes(time.Minute * 5) + for _, tt := range getHoursTest { + actual := getHours(tt.input) - if result != "05" { - t.Errorf("Should correctly calculate seconds, got: %v", result) + if actual != tt.expected { + t.Errorf("getHours(%d): expected: %v, got: %v", tt.input, tt.expected, actual) + } } +} - result = getMinutes(time.Minute * 65) +func TestIsFlag(t *testing.T) { + isMyFlag := isFlag("f", "foo") + + isFlagTest := []struct { + input string + expected bool + }{ + {"f", true}, + {"-f", true}, + {"--f", true}, + {"foo", true}, + {"-foo", true}, + {"--foo", true}, + {"fo", false}, + {"-fo", false}, + {"--fo", false}, + {"abc", false}, + {"--abc", false}, + } - if result != "05" { - t.Errorf("Should correctly calculate seconds, got: %v", result) + for _, tt := range isFlagTest { + actual := isMyFlag(tt.input) + + if actual != tt.expected { + t.Errorf("isMyFlag(%s): expected: %v, got: %v", tt.input, tt.expected, actual) + } } } -func TestGetHours(t *testing.T) { - result := getHours(time.Nanosecond) - - if result != "00" { - t.Errorf("Should correctly calculate seconds, got: %v", result) +func TestIsHelpFlag(t *testing.T) { + isHelpFlagTest := []struct { + input string + expected bool + }{ + {"h", true}, + {"help", true}, + {"-h", true}, + {"-help", true}, + {"--h", true}, + {"--help", true}, + {"f", false}, + {"foo", false}, + {"-f", false}, + {"-foo", false}, + {"--f", false}, + {"--foo", false}, } - result = getHours(time.Hour * 5) + for _, tt := range isHelpFlagTest { + actual := isHelpFlag(tt.input) + + if actual != tt.expected { + t.Errorf("isHelpFlag(%s): expected: %v, got: %v", tt.input, tt.expected, actual) + } + } +} - if result != "05" { - t.Errorf("Should correctly calculate seconds, got: %v", result) +func TestIsVersionFlag(t *testing.T) { + isVersionFlagTest := []struct { + input string + expected bool + }{ + {"v", true}, + {"version", true}, + {"-v", true}, + {"-version", true}, + {"--v", true}, + {"--version", true}, + {"f", false}, + {"foo", false}, + {"-f", false}, + {"-foo", false}, + {"--f", false}, + {"--foo", false}, } - result = getHours(time.Hour * 25) + for _, tt := range isVersionFlagTest { + actual := isVersionFlag(tt.input) - if result != "25" { - t.Errorf("Should correctly calculate seconds, got: %v", result) + if actual != tt.expected { + t.Errorf("isVersionFlag(%s): expected: %v, got: %v", tt.input, tt.expected, actual) + } } }