Skip to content

Commit

Permalink
Prefer map for table driven test
Browse files Browse the repository at this point in the history
  • Loading branch information
kachick committed Jun 6, 2024
1 parent e40a3d2 commit c9a9517
Showing 1 changed file with 15 additions and 25 deletions.
40 changes: 15 additions & 25 deletions internal/runner/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@ import (
const defaultPrefix string = " selfup "

func TestDryRun(t *testing.T) {
testCases := []struct {
description string
type testCase struct {
input string
prefix string
skipBy string
ok bool
want Result
}{
{
description: "Happy Path",
}
testCases := map[string]testCase{
"Happy Path": {
input: `Header
will_be_replaced: '0.39.0' # selfup { "extract": "\\d[^']+", "replacer": ["echo", "0.76.9"] }
not_be_replacedA: '0.39.0' # selfup { "extract": "\\d[^']+", "replacer": ["echo", "0.39.0"] }
Expand All @@ -46,8 +45,7 @@ not_be_replacedB: ':<' # selfup { "extract": ":[<\\)]", "replacer": ["echo", ":)
Total: 3,
},
},
{
description: "Another prefix",
"Another prefix": {
input: `Header
will_be_replaced: '0.39.0' // Update this line with { "extract": "\\d[^']+", "replacer": ["echo", "0.76.9"] }
not_be_replacedA: '0.39.0' # selfup { "extract": "\\d[^']+", "replacer": ["echo", "0.76.9"] }
Expand All @@ -67,8 +65,7 @@ not_be_replacedA: '0.39.0' # selfup { "extract": "\\d[^']+", "replacer": ["echo"
ChangedCount: 1,
Total: 1,
},
}, {
description: "SkipBy",
}, "SkipBy": {
input: `Header
will_be_replaced: '0.39.0' # selfup { "extract": "\\d[^']+", "replacer": ["echo", "0.76.9"] }
not_be_replacedA: '0.39.0' # selfup { "extract": "\\d[^']+", "replacer": ["echo", "0.76.9"] }
Expand All @@ -88,8 +85,7 @@ not_be_replacedA: '0.39.0' # selfup { "extract": "\\d[^']+", "replacer": ["echo"
ChangedCount: 1,
Total: 1,
},
}, {
description: "Handle fields",
}, "Handle fields": {
input: `will_be_replaced: '0.39.0' # selfup { "extract": "\\d[^']+", "replacer": ["echo", " supertool 0.76.9 "], "nth": 2 }
`,
prefix: defaultPrefix,
Expand All @@ -105,8 +101,7 @@ not_be_replacedA: '0.39.0' # selfup { "extract": "\\d[^']+", "replacer": ["echo"
ChangedCount: 1,
Total: 1,
},
}, {
description: "Special delimiter",
},"Special delimiter": {
input: `will_be_replaced: '0.39.0' # selfup { "extract": "\\d[^']+", "replacer": ["echo", "supertool:0.76.9"], "nth": 2, "delimiter": ":" }
`,
prefix: defaultPrefix,
Expand All @@ -122,31 +117,27 @@ not_be_replacedA: '0.39.0' # selfup { "extract": "\\d[^']+", "replacer": ["echo"
ChangedCount: 1,
Total: 1,
},
}, {
description: "Command returns unupdatable string",
},"Command returns unupdatable string": {
input: `broken_command: '0.39.0' # selfup { "extract": "\\d[^']+", "replacer": ["echo", ":)"] }
`,
prefix: defaultPrefix,
skipBy: "",
ok: false,
}, {
description: "Command is not found",
}, "Command is not found": {
input: `Header
broken: ':<' # selfup { "extract": ":[<\\)]", "replacer": ["this_command_does_not_exist_so_raise_errors_and_do_not_update_this_file"] }
`,
prefix: defaultPrefix,
skipBy: "",
ok: false,
}, {
description: "Broken JSON",
}, "Broken JSON": {
input: `Header
broken: ':<' # selfup {{ """" }
`,
prefix: defaultPrefix,
skipBy: "",
ok: false,
}, {
description: "Prefer SkipBy rather than no command error",
}, "Prefer SkipBy rather than no command error": {
input: `Header
broken: ':<' # selfup { "extract": ":[<\\)]", "replacer": ["this_command_does_not_exist_so_raise_errors_and_do_not_update_this_file"] }
`,
Expand All @@ -162,8 +153,7 @@ broken: ':<' # selfup { "extract": ":[<\\)]", "replacer": ["this_command_does_no
ChangedCount: 0,
Total: 0,
},
}, {
description: "Prefer SkipBy rather than broken JSON error",
}, "Prefer SkipBy rather than broken JSON error": {
input: `Header
broken: ':<' # selfup {{ """" }
`,
Expand All @@ -182,8 +172,8 @@ broken: ':<' # selfup {{ """" }
},
}

for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
for what, tc := range testCases {
t.Run(what, func(t *testing.T) {
result, err := DryRun(strings.NewReader(tc.input), tc.prefix, tc.skipBy)
if err != nil {
if tc.ok {
Expand Down

0 comments on commit c9a9517

Please sign in to comment.