Skip to content

Commit

Permalink
test(text) add additional test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
tombell committed Feb 24, 2023
1 parent 32448a9 commit 84f189b
Showing 1 changed file with 87 additions and 12 deletions.
99 changes: 87 additions & 12 deletions text_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,13 @@ func TestTextValueStyles(t *testing.T) {
kvs: nil,
f: logger.Info,
},
{
name: "ignored message",
expected: "",
msg: "this is a debug message",
kvs: nil,
f: logger.Debug,
},
{
name: "message with keyvals",
expected: fmt.Sprintf(
Expand All @@ -261,18 +268,6 @@ func TestTextValueStyles(t *testing.T) {
kvs: []interface{}{"key1", "val1", "key2", "val2"},
f: logger.Info,
},
{
name: "message with keyvals",
expected: fmt.Sprintf(
"%s info %s%s%s %s%s%s\n",
InfoLevelStyle.Render("INFO"),
KeyStyle.Render("key1"), SeparatorStyle.Render("="), ValueStyle.Render("true"),
KeyStyle.Render("key2"), SeparatorStyle.Render("="), ValueStyle.Render("false"),
),
msg: "info",
kvs: []interface{}{"key1", true, "key2", false},
f: logger.Info,
},
{
name: "error message with multiline",
expected: fmt.Sprintf(
Expand All @@ -286,6 +281,42 @@ func TestTextValueStyles(t *testing.T) {
kvs: []interface{}{"key1", "val1\nval2"},
f: logger.Error,
},
{
name: "error message with keyvals",
expected: fmt.Sprintf(
"%s info %s%s%s %s%s%s\n",
ErrorLevelStyle.Render("ERRO"),
KeyStyle.Render("key1"), SeparatorStyle.Render("="), ValueStyle.Render("val1"),
KeyStyle.Render("key2"), SeparatorStyle.Render("="), ValueStyle.Render("val2"),
),
msg: "info",
kvs: []interface{}{"key1", "val1", "key2", "val2"},
f: logger.Error,
},
{
name: "odd number of keyvals",
expected: fmt.Sprintf(
"%s info %s%s%s %s%s%s %s%s\"%s\"\n",
ErrorLevelStyle.Render("ERRO"),
KeyStyle.Render("key1"), SeparatorStyle.Render("="), ValueStyle.Render("val1"),
KeyStyle.Render("key2"), SeparatorStyle.Render("="), ValueStyle.Render("val2"),
KeyStyle.Render("key3"), SeparatorStyle.Render("="), ValueStyle.Render("missing value"),
),
msg: "info",
kvs: []interface{}{"key1", "val1", "key2", "val2", "key3"},
f: logger.Error,
},
{
name: "error field",
expected: fmt.Sprintf(
"%s info %s%s\"%s\"\n",
ErrorLevelStyle.Render("ERRO"),
KeyStyle.Render("key1"), SeparatorStyle.Render("="), ValueStyle.Render("error value"),
),
msg: "info",
kvs: []interface{}{"key1", errors.New("error value")},
f: logger.Error,
},
{
name: "struct field",
expected: fmt.Sprintf(
Expand All @@ -308,6 +339,50 @@ func TestTextValueStyles(t *testing.T) {
kvs: []interface{}{"key1", struct{ foo string }{foo: "bar baz"}},
f: logger.Info,
},
{
name: "slice of strings",
expected: fmt.Sprintf(
"%s info %s%s\"%s\"\n",
ErrorLevelStyle.Render("ERRO"),
KeyStyle.Render("key1"), SeparatorStyle.Render("="), ValueStyle.Render("[foo bar]"),
),
msg: "info",
kvs: []interface{}{"key1", []string{"foo", "bar"}},
f: logger.Error,
},
{
name: "slice of structs",
expected: fmt.Sprintf(
"%s info %s%s\"%s\"\n",
ErrorLevelStyle.Render("ERRO"),
KeyStyle.Render("key1"), SeparatorStyle.Render("="), ValueStyle.Render("[{foo:bar} {foo:baz}]"),
),
msg: "info",
kvs: []interface{}{"key1", []struct{ foo string }{{foo: "bar"}, {foo: "baz"}}},
f: logger.Error,
},
{
name: "slice of errors",
expected: fmt.Sprintf(
"%s info %s%s\"%s\"\n",
ErrorLevelStyle.Render("ERRO"),
KeyStyle.Render("key1"), SeparatorStyle.Render("="), ValueStyle.Render("[error value1 error value2]"),
),
msg: "info",
kvs: []interface{}{"key1", []error{errors.New("error value1"), errors.New("error value2")}},
f: logger.Error,
},
{
name: "map of strings",
expected: fmt.Sprintf(
"%s info %s%s\"%s\"\n",
ErrorLevelStyle.Render("ERRO"),
KeyStyle.Render("key1"), SeparatorStyle.Render("="), ValueStyle.Render("map[baz:qux foo:bar]"),
),
msg: "info",
kvs: []interface{}{"key1", map[string]string{"foo": "bar", "baz": "qux"}},
f: logger.Error,
},
}
for _, c := range cases {
buf.Reset()
Expand Down

0 comments on commit 84f189b

Please sign in to comment.