Skip to content

Commit

Permalink
Merge pull request #10 from mstruebing/tableDrivenTest
Browse files Browse the repository at this point in the history
Table driven test
  • Loading branch information
mstruebing authored Jun 14, 2019
2 parents b145dc4 + 7fefd71 commit 41f55b0
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 44 deletions.
24 changes: 19 additions & 5 deletions cmd/duration/duration.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,28 +93,42 @@ 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.")
printHelp()
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

Expand Down
165 changes: 126 additions & 39 deletions cmd/duration/duration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

0 comments on commit 41f55b0

Please sign in to comment.