Improve the test code. (#733)
authorYuchen Ying <github.com@yegle.net>
Thu, 7 Jan 2021 19:39:06 +0000 (11:39 -0800)
committerGitHub <noreply@github.com>
Thu, 7 Jan 2021 19:39:06 +0000 (19:39 +0000)
Signed-off-by: Yuchen Ying <github.com@yegle.net>
config/config_test.go

index a47aa70d382055738046d2f750bf8c53ea15c66a..67bb409cda5558aee34087bd091fea67169662f4 100644 (file)
@@ -36,59 +36,57 @@ func TestLoadBadConfigs(t *testing.T) {
                C: &Config{},
        }
        tests := []struct {
-               ConfigFile    string
-               ExpectedError string
+               input string
+               want  string
        }{
                {
-                       ConfigFile:    "testdata/blackbox-bad.yml",
-                       ExpectedError: "error parsing config file: yaml: unmarshal errors:\n  line 50: field invalid_extra_field not found in type config.plain",
+                       input: "testdata/blackbox-bad.yml",
+                       want:  "error parsing config file: yaml: unmarshal errors:\n  line 50: field invalid_extra_field not found in type config.plain",
                },
                {
-                       ConfigFile:    "testdata/blackbox-bad2.yml",
-                       ExpectedError: "error parsing config file: at most one of bearer_token & bearer_token_file must be configured",
+                       input: "testdata/blackbox-bad2.yml",
+                       want:  "error parsing config file: at most one of bearer_token & bearer_token_file must be configured",
                },
                {
-                       ConfigFile:    "testdata/invalid-dns-module.yml",
-                       ExpectedError: "error parsing config file: query name must be set for DNS module",
+                       input: "testdata/invalid-dns-module.yml",
+                       want:  "error parsing config file: query name must be set for DNS module",
                },
                {
-                       ConfigFile:    "testdata/invalid-dns-class.yml",
-                       ExpectedError: "error parsing config file: query class 'X' is not valid",
+                       input: "testdata/invalid-dns-class.yml",
+                       want:  "error parsing config file: query class 'X' is not valid",
                },
                {
-                       ConfigFile:    "testdata/invalid-dns-type.yml",
-                       ExpectedError: "error parsing config file: query type 'X' is not valid",
+                       input: "testdata/invalid-dns-type.yml",
+                       want:  "error parsing config file: query type 'X' is not valid",
                },
                {
-                       ConfigFile:    "testdata/invalid-http-header-match.yml",
-                       ExpectedError: "error parsing config file: regexp must be set for HTTP header matchers",
+                       input: "testdata/invalid-http-header-match.yml",
+                       want:  "error parsing config file: regexp must be set for HTTP header matchers",
                },
                {
-                       ConfigFile:    "testdata/invalid-http-body-match-regexp.yml",
-                       ExpectedError: "error parsing config file: \"Could not compile regular expression\" regexp=\":[\"",
+                       input: "testdata/invalid-http-body-match-regexp.yml",
+                       want:  `error parsing config file: "Could not compile regular expression" regexp=":["`,
                },
                {
-                       ConfigFile:    "testdata/invalid-http-body-not-match-regexp.yml",
-                       ExpectedError: "error parsing config file: \"Could not compile regular expression\" regexp=\":[\"",
+                       input: "testdata/invalid-http-body-not-match-regexp.yml",
+                       want:  `error parsing config file: "Could not compile regular expression" regexp=":["`,
                },
                {
-                       ConfigFile:    "testdata/invalid-http-header-match-regexp.yml",
-                       ExpectedError: "error parsing config file: \"Could not compile regular expression\" regexp=\":[\"",
+                       input: "testdata/invalid-http-header-match-regexp.yml",
+                       want:  `error parsing config file: "Could not compile regular expression" regexp=":["`,
                },
                {
-                       ConfigFile:    "testdata/invalid-tcp-query-response-regexp.yml",
-                       ExpectedError: "error parsing config file: \"Could not compile regular expression\" regexp=\":[\"",
+                       input: "testdata/invalid-tcp-query-response-regexp.yml",
+                       want:  `error parsing config file: "Could not compile regular expression" regexp=":["`,
                },
        }
-       for i, test := range tests {
-               err := sc.ReloadConfig(test.ConfigFile)
-               if err == nil {
-                       t.Errorf("In case %v:\nExpected:\n%v\nGot:\nnil", i, test.ExpectedError)
-                       continue
-               }
-               if err.Error() != test.ExpectedError {
-                       t.Errorf("In case %v:\nExpected:\n%v\nGot:\n%v", i, test.ExpectedError, err.Error())
-               }
+       for _, test := range tests {
+               t.Run(test.input, func(t *testing.T) {
+                       got := sc.ReloadConfig(test.input)
+                       if got == nil || got.Error() != test.want {
+                               t.Fatalf("ReloadConfig(%q) = %v; want %q", test.input, got, test.want)
+                       }
+               })
        }
 }