Skip to content

Commit

Permalink
Merge pull request #148 from chinvib66/refactor-textformatter-tests
Browse files Browse the repository at this point in the history
refactored text-formatter-tests
  • Loading branch information
jeroenrinzema authored Oct 6, 2020
2 parents 8a70ab4 + 6b620db commit 185f7ed
Showing 1 changed file with 39 additions and 40 deletions.
79 changes: 39 additions & 40 deletions pkg/prettyerr/text_formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}

}

0 comments on commit 185f7ed

Please sign in to comment.