From 84f189b0169fe6e56e6719d0f2d15fd52009b7ef Mon Sep 17 00:00:00 2001 From: Tom Bell Date: Fri, 24 Feb 2023 20:36:31 +0000 Subject: [PATCH] test(text) add additional test cases --- text_test.go | 99 +++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 87 insertions(+), 12 deletions(-) diff --git a/text_test.go b/text_test.go index 6e740f3..2e6aea4 100644 --- a/text_test.go +++ b/text_test.go @@ -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( @@ -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( @@ -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( @@ -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()