From a3d5d40f78bfa16b04ee5b48d08363fe5ce98270 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Fri, 5 Jan 2018 11:06:26 -0800 Subject: [PATCH] reformat TestDownloadableURL into a table test to allow adding more URLS --- common/config_test.go | 59 +++++++++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 28 deletions(-) diff --git a/common/config_test.go b/common/config_test.go index 8bd684739..23aa25dd1 100644 --- a/common/config_test.go +++ b/common/config_test.go @@ -38,36 +38,39 @@ func TestChooseString(t *testing.T) { } func TestDownloadableURL(t *testing.T) { - // Invalid URL: has hex code in host - _, err := DownloadableURL("http://what%20.com") - if err == nil { - t.Fatal("expected err") + + cases := []struct { + InputString string + OutputURL string + ErrExpected bool + }{ + // Invalid URL: has hex code in host + {"http://what%20.com", "", true}, + // Valid: http + {"HTTP://packer.io/path", "http://packer.io/path", false}, + // No path + {"HTTP://packer.io", "http://packer.io", false}, + // Invalid: unsupported scheme + {"ftp://host.com/path", "", true}, } - // Invalid: unsupported scheme - _, err = DownloadableURL("ftp://host.com/path") - if err == nil { - t.Fatal("expected err") - } - - // Valid: http - u, err := DownloadableURL("HTTP://packer.io/path") - if err != nil { - t.Fatalf("err: %s", err) - } - - if u != "http://packer.io/path" { - t.Fatalf("bad: %s", u) - } - - // No path - u, err = DownloadableURL("HTTP://packer.io") - if err != nil { - t.Fatalf("err: %s", err) - } - - if u != "http://packer.io" { - t.Fatalf("bad: %s", u) + for _, tc := range cases { + u, err := DownloadableURL(tc.InputString) + if u != tc.OutputURL { + t.Fatal(fmt.Sprintf("Error with URL %s: got %s but expected %s", + tc.InputString, tc.OutputURL, u)) + } + if (err != nil) != tc.ErrExpected { + if tc.ErrExpected == true { + t.Fatal(fmt.Sprintf("Error with URL %s: we expected "+ + "DownloadableURL to return an error but didn't get one.", + tc.InputString)) + } else { + t.Fatal(fmt.Sprintf("Error with URL %s: we did not expect an "+ + " error from DownloadableURL but we got: %s", + tc.InputString, err)) + } + } } }