jexia / semaphore

Take control of your data, connect with anything, and expose it anywhere through protocols such as HTTP, GraphQL, and gRPC.

Home Page:https://jexia.github.io/semaphore/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

refactor TestTextFormatter to use t.Run

jeroenrinzema opened this issue · comments

TestTextFormatter has to be refactored to make use of t.Run to allow to execute isolated test cases.

missingConfig := Error{
Message: "File config is missing.",
Details: map[string]interface{}{
"filename": "/etc/foo/config.yml",
},
Code: "MissingConfig",
}
genericError := Error{
Message: "Something happened, I'm sorry.",
Details: nil,
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)
}
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"
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)
}

@jeroenrinzema is it open to pick ?

@jeroenrinzema I have just completed refactoring the test file. Shall I submit a PR, or let @HemantJoshi11 handle it?

@HemantJoshi11 did you start working on the PR already? If not do you mind if @chinvib66 resolves this issue?

Ok. Go head.

@HemantJoshi11 I have just created a new small refactor issue (#147). You could pick up this issue instead if you want.