From da8849d7f57499501768accdd6264c717c94a6c1 Mon Sep 17 00:00:00 2001 From: Grzegorz Dlugoszewski Date: Thu, 25 Jun 2020 09:50:12 +0200 Subject: [PATCH] Update dump tests to better follow table-driven pattern --- pkg/dump_test.go | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/pkg/dump_test.go b/pkg/dump_test.go index cb588d2..2d7c5b7 100644 --- a/pkg/dump_test.go +++ b/pkg/dump_test.go @@ -6,50 +6,52 @@ import ( func TestParsingRefs(t *testing.T) { var tests = []struct { + name string line string wantBranch string wantErr error }{ { + name: "url without branch", line: "https://github.com/grdl/git-get", wantBranch: "", wantErr: nil, }, { + name: "url with branch", line: "https://github.com/grdl/git-get master", wantBranch: "master", wantErr: nil, }, { - line: "https://github.com/grdl/git-get refs/tags/v1.0.0", - wantBranch: "refs/tags/v1.0.0", - wantErr: nil, - }, - { + name: "url with multiple branches", line: "https://github.com/grdl/git-get master branch", wantBranch: "", wantErr: errInvalidNumberOfElements, }, { + name: "url without path", line: "https://github.com", wantBranch: "", wantErr: errEmptyURLPath, }, } - for i, test := range tests { - got, err := parseLine(test.line) - if err != nil && test.wantErr == nil { - t.Fatalf("Test case %d should not return an error", i) - } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + got, err := parseLine(test.line) + if err != nil && test.wantErr == nil { + t.Fatalf("got error %q", err) + } - if err != nil && test.wantErr != nil { - continue - } + // TODO: this should check if we actually got the error we expected + if err != nil && test.wantErr != nil { + return + } - if got.branch != test.wantBranch { - t.Errorf("Failed test case %d, got: %s; wantBranch: %s", i, got.branch, test.wantBranch) - } + if got.branch != test.wantBranch { + t.Errorf("expected %q; got %q", test.wantBranch, got.branch) + } + }) } - }