diff --git a/pkg/prettyerr/text_formatter_test.go b/pkg/prettyerr/text_formatter_test.go index 068a5927..f877ef8b 100644 --- a/pkg/prettyerr/text_formatter_test.go +++ b/pkg/prettyerr/text_formatter_test.go @@ -17,52 +17,51 @@ func TestTextFormatter(t *testing.T) { Code: "GenericError", } - want := - "File config is missing.\n" + - "\tfilename: /etc/foo/config.yml\n" + - "Something happened, I'm sorry.\n" - stack := Errors{missingConfig, genericError} - format := "{{ .Message }}\n{{ range $key, $value := .Details }}\t{{ $key }}: {{ $value }}\n{{ end }}" - - got, err := TextFormatter(stack, format) - if err != nil { - t.Errorf("TextFormatter() returned error: %v", err) - } - - if got != want { - t.Errorf("TextFormatter(). Got:\n%v\nWant:\n%v", got, want) - } - - invalidFormat := "{{ .Messag }}\n{{ range $key, $value := .Details }}\t{{ $key }}: {{ $value }}\n{{ end }}" - want = "failed to execute template for 0: template: node:1:3: executing \"node\" at <.Messag>: can't evaluate field Messag in type prettyerr.Error\n" + - "File config is missing.\n" + - "\tfilename: \"/etc/foo/config.yml\"\n" + - "failed to execute template for 1: template: node:1:3: executing \"node\" at <.Messag>: can't evaluate field Messag in type prettyerr.Error\n" + - "Something happened, I'm sorry.\n" - got, err = TextFormatter(stack, invalidFormat) - if err != nil { - t.Errorf("TextFormatter() returned error: %v", err) - } - - if got != want { - t.Errorf("TextFormatter(). \nGot:\n%v\nWant:\n%v", got, want) + tests := []struct { + name string + format string + want string + }{ + { + name: "No TextFormatter error", + format: "{{ .Message }}\n{{ range $key, $value := .Details }}\t{{ $key }}: {{ $value }}\n{{ end }}", + want: "File config is missing.\n" + + "\tfilename: /etc/foo/config.yml\n" + + "Something happened, I'm sorry.\n", + }, + { + name: "Fields error", + format: "{{ .Messag }}\n{{ range $key, $value := .Details }}\t{{ $key }}: {{ $value }}\n{{ end }}", + want: "failed to execute template for 0: template: node:1:3: executing \"node\" at <.Messag>: can't evaluate field Messag in type prettyerr.Error\n" + + "File config is missing.\n" + + "\tfilename: \"/etc/foo/config.yml\"\n" + + "failed to execute template for 1: template: node:1:3: executing \"node\" at <.Messag>: can't evaluate field Messag in type prettyerr.Error\n" + + "Something happened, I'm sorry.\n", + }, + { + name: "Template Parsing error", + format: "{{ .Message }}\n{{ range $key, $value := .Details }}\t{{ $ke }}: {{ $value }}\n{{ end }}", + want: "failed to parse template: template: node:2: undefined variable \"$ke\"\n" + + "File config is missing.\n" + + "\tfilename: \"/etc/foo/config.yml\"\n" + + "Something happened, I'm sorry.\n", + }, } - invalidFormat = "{{ .Message }}\n{{ range $key, $value := .Details }}\t{{ $ke }}: {{ $value }}\n{{ end }}" - want = "failed to parse template: template: node:2: undefined variable \"$ke\"\n" + - "File config is missing.\n" + - "\tfilename: \"/etc/foo/config.yml\"\n" + - "Something happened, I'm sorry.\n" + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := TextFormatter(stack, tt.format) - got, err = TextFormatter(stack, invalidFormat) - if err != nil { - t.Errorf("TextFormatter() returned error: %v", err) - } + if err != nil { + t.Errorf("TextFormatter() returned error: %v", err) + } - if got != want { - t.Errorf("TextFormatter(). \nGot:\n%v\nWant:\n%v", got, want) + if got != tt.want { + t.Errorf("TextFormatter(). Got:\n%v\nWant:\n%v", got, tt.want) + } + }) } }