diff options
| author | Dmitry Vyukov <dvyukov@google.com> | 2018-05-07 13:16:29 +0200 |
|---|---|---|
| committer | Dmitry Vyukov <dvyukov@google.com> | 2018-05-07 13:16:29 +0200 |
| commit | 5f9dcfdadbca65b92a9cfd193cf280bc4968bb1a (patch) | |
| tree | 4f9afc6b2ac208c939c3853b9342cdf163827e39 | |
| parent | b9fea20df7dd0bd1279ae80c99f9bc9969e1fe5a (diff) | |
pkg/git: remove duplicated code in tests
Update #538
| -rw-r--r-- | .gometalinter.json | 2 | ||||
| -rw-r--r-- | pkg/git/git_test.go | 112 |
2 files changed, 48 insertions, 66 deletions
diff --git a/.gometalinter.json b/.gometalinter.json index 0c71cba97..83ea5f7fc 100644 --- a/.gometalinter.json +++ b/.gometalinter.json @@ -6,7 +6,7 @@ "minconstlength": 7, "linelength": 120, "cyclo": 50, - "duplthreshold": 100, + "duplthreshold": 85, "skip": [ "pkg/kd", "sys/akaros/gen", diff --git a/pkg/git/git_test.go b/pkg/git/git_test.go index 7ad42e3dc..18ba03d6e 100644 --- a/pkg/git/git_test.go +++ b/pkg/git/git_test.go @@ -25,77 +25,59 @@ func TestCanonicalizeCommit(t *testing.T) { } func TestCheckRepoAddress(t *testing.T) { - var tests = []struct { - repo string - result bool - }{ - {"git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git", true}, - {"https://github.com/torvalds/linux.git", true}, - {"git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git", true}, - {"git://git.cmpxchg.org/linux-mmots.git", true}, - {"https://anonscm.debian.org/git/kernel/linux.git", true}, - {"git://kernel.ubuntu.com/ubuntu/ubuntu-zesty.git", true}, - {"http://host.xz:123/path/to/repo.git/", true}, - {"", false}, - {"foobar", false}, - {"linux-next", false}, - {"foo://kernel.ubuntu.com/ubuntu/ubuntu-zesty.git", false}, - {"git://kernel/ubuntu.git", false}, - {"git://kernel.com/ubuntu", false}, - {"gitgit://kernel.ubuntu.com/ubuntu/ubuntu-zesty.git", false}, - } - for _, test := range tests { - res := CheckRepoAddress(test.repo) - if res != test.result { - t.Errorf("%v: got %v, want %v", test.repo, res, test.result) - } - } + testPredicate(t, CheckRepoAddress, map[string]bool{ + "git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git": true, + "https://github.com/torvalds/linux.git": true, + "git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git": true, + "git://git.cmpxchg.org/linux-mmots.git": true, + "https://anonscm.debian.org/git/kernel/linux.git": true, + "git://kernel.ubuntu.com/ubuntu/ubuntu-zesty.git": true, + "http://host.xz:123/path/to/repo.git/": true, + "": false, + "foobar": false, + "linux-next": false, + "foo://kernel.ubuntu.com/ubuntu/ubuntu-zesty.git": false, + "git://kernel/ubuntu.git": false, + "git://kernel.com/ubuntu": false, + "gitgit://kernel.ubuntu.com/ubuntu/ubuntu-zesty.git": false, + }) } func TestCheckBranch(t *testing.T) { - var tests = []struct { - branch string - result bool - }{ - {"master", true}, - {"core/core", true}, - {"irq-irqdomain-for-linus", true}, - {"timers/2038", true}, - {"ubuntu-zesty/v4.9.4", true}, - {"WIP.locking/atomics", true}, - {"linux-4.9.y", true}, - {"abi_spec", true}, - {"@", false}, - {"", false}, - } - for _, test := range tests { - res := CheckBranch(test.branch) - if res != test.result { - t.Errorf("%v: got %v, want %v", test.branch, res, test.result) - } - } + testPredicate(t, CheckBranch, map[string]bool{ + "master": true, + "core/core": true, + "irq-irqdomain-for-linus": true, + "timers/2038": true, + "ubuntu-zesty/v4.9.4": true, + "WIP.locking/atomics": true, + "linux-4.9.y": true, + "abi_spec": true, + "@": false, + "": false, + }) } func TestCheckCommitHash(t *testing.T) { - var tests = []struct { - hash string - result bool - }{ - {"ff12bea91c22bba93d3ffc3034d813d686bc7eeb", true}, // 40 - {"eae05cb0aaeae05cb0aa", true}, // 20 - {"449dd6984d0eaabb", true}, // 16 - {"449dd6984d0e", true}, // 12 - {"eae05cb0aa", true}, // 10 - {"eae05cb0", true}, // 8 - {"", false}, - {"aa", false}, - {"eae05cb0aab", false}, - {"xxxxxxxx", false}, - } - for _, test := range tests { - res := CheckCommitHash(test.hash) - if res != test.result { - t.Errorf("%v: got %v, want %v", test.hash, res, test.result) + testPredicate(t, CheckCommitHash, map[string]bool{ + "ff12bea91c22bba93d3ffc3034d813d686bc7eeb": true, // 40 + "eae05cb0aaeae05cb0aa": true, // 20 + "449dd6984d0eaabb": true, // 16 + "449dd6984d0e": true, // 12 + "eae05cb0aa": true, // 10 + "eae05cb0": true, // 8 + "": false, + "aa": false, + "eae05cb0aab": false, + "xxxxxxxx": false, + }) +} + +func testPredicate(t *testing.T, fn func(string) bool, tests map[string]bool) { + for input, want := range tests { + res := fn(input) + if res != want { + t.Errorf("%v: got %v, want %v", input, res, want) } } } |
