-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
43 changed files
with
367 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package formatter_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/dcilke/hz/pkg/formatter" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCaller(t *testing.T) { | ||
testcases := map[string]struct { | ||
noColor bool | ||
expect string | ||
}{ | ||
"no-color": {true, "caller >"}, | ||
"color": {false, "\x1b[1mcaller\x1b[0m\x1b[36m >\x1b[0m"}, | ||
} | ||
for name, tc := range testcases { | ||
t.Run(name, func(t *testing.T) { | ||
f := formatter.NewCaller(tc.noColor) | ||
str := f.Format(map[string]any{ | ||
formatter.KeyCaller: "caller", | ||
"extra": "should be ignored", | ||
}) | ||
|
||
require.Equal(t, tc.expect, str) | ||
}) | ||
} | ||
} | ||
|
||
func TestCaller_ExcludeKeys(t *testing.T) { | ||
f := formatter.NewCaller(false) | ||
require.Equal(t, []string{formatter.KeyCaller}, f.ExcludeKeys()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package formatter_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/dcilke/hz/pkg/formatter" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestError(t *testing.T) { | ||
testcases := map[string]struct { | ||
noColor bool | ||
msg map[string]any | ||
expect string | ||
}{ | ||
"error-no-color": {true, map[string]any{"error": "err"}, "error=err"}, | ||
"err-no-color": {true, map[string]any{"err": "err"}, "error=err"}, | ||
"error-color": {false, map[string]any{"error": "err"}, "error=\x1b[31merr\x1b[0m"}, | ||
"err-color": {false, map[string]any{"err": "err"}, "error=\x1b[31merr\x1b[0m"}, | ||
"diff": {true, map[string]any{"error": "error", "err": "err"}, "error=error err=err"}, | ||
} | ||
for name, tc := range testcases { | ||
t.Run(name, func(t *testing.T) { | ||
f := formatter.NewError(tc.noColor, formatKey) | ||
str := f.Format(tc.msg) | ||
|
||
require.Equal(t, tc.expect, str) | ||
}) | ||
} | ||
} | ||
|
||
func TestError_ExcludeKeys(t *testing.T) { | ||
f := formatter.NewError(false, nil) | ||
require.Equal(t, []string{formatter.KeyError, formatter.KeyErr}, f.ExcludeKeys()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package formatter_test | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
"testing" | ||
|
||
"github.com/dcilke/hz/pkg/formatter" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func formatKey(key any) string { | ||
return fmt.Sprintf("%s=", key) | ||
} | ||
|
||
func TestKey(t *testing.T) { | ||
testcases := map[string]struct { | ||
noColor bool | ||
key any | ||
expect string | ||
}{ | ||
"no-color": {true, "key", "key="}, | ||
"color": {false, "key", "\x1b[36mkey=\x1b[0m"}, | ||
"number": {true, 42, "42="}, | ||
"byte": {true, byte(195), "195="}, | ||
"rune": {true, rune(195), "195="}, | ||
"object": {true, map[string]any{"foo": "bar"}, "map[foo:bar]="}, | ||
"array": {true, []string{"foo", "bar"}, "[foo bar]="}, | ||
} | ||
for name, tc := range testcases { | ||
t.Run(name, func(t *testing.T) { | ||
f := formatter.Key(tc.noColor) | ||
require.Equal(t, tc.expect, f(tc.key)) | ||
}) | ||
} | ||
} | ||
|
||
func TestMap(t *testing.T) { | ||
var ( | ||
mapCycle = make(map[string]any) | ||
sliceCycle = []any{nil} | ||
) | ||
mapCycle["x"] = mapCycle | ||
sliceCycle[0] = sliceCycle | ||
|
||
data := map[string]any{ | ||
"key": "value", | ||
"map": map[string]any{ | ||
"foo": "bar", | ||
}, | ||
"quotes": `this is a string`, | ||
"num": 42, | ||
"nan": math.NaN(), | ||
"pinf": math.Inf(1), | ||
"ninf": math.Inf(-1), | ||
"map-cycle": mapCycle, | ||
"slice-cycle": sliceCycle, | ||
} | ||
|
||
testcases := map[string]struct { | ||
key string | ||
expect string | ||
}{ | ||
"key": {"key", "key=value"}, | ||
"map": {"map", "map={\"foo\":\"bar\"}"}, | ||
"quotes": {"quotes", "quotes=\"this is a string\""}, | ||
"num": {"num", "num=42"}, | ||
"nan": {"nan", "nan=NaN"}, | ||
"pinf": {"pinf", "pinf=+Inf"}, | ||
"ninf": {"ninf", "ninf=-Inf"}, | ||
"map-cycle": {"map-cycle", "map-cycle=<cycle>"}, | ||
"slice-cycle": {"slice-cycle", "slice-cycle=<cycle>"}, | ||
} | ||
for name, tc := range testcases { | ||
t.Run(name, func(t *testing.T) { | ||
f := formatter.Map(formatKey) | ||
require.Equal(t, tc.expect, f(data, tc.key)) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package formatter_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/dcilke/hz/pkg/formatter" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestLevel(t *testing.T) { | ||
|
||
testcases := map[string]struct { | ||
noColor bool | ||
msg map[string]any | ||
expect string | ||
}{ | ||
"trace-no-color": {true, ml("trace"), "TRC"}, | ||
"debug-no-color": {true, ml("debug"), "DBG"}, | ||
"info-no-color": {true, ml("info"), "INF"}, | ||
"warn-no-color": {true, ml("warn"), "WRN"}, | ||
"error-no-color": {true, ml("error"), "ERR"}, | ||
"fatal-no-color": {true, ml("fatal"), "FTL"}, | ||
"panic-no-color": {true, ml("panic"), "PNC"}, | ||
"unknown-no-color": {true, ml("unknown"), "UNK"}, | ||
"10-no-color": {true, ml(10), "TRC"}, | ||
"20-no-color": {true, ml(20), "DBG"}, | ||
"30-no-color": {true, ml(30), "INF"}, | ||
"40-no-color": {true, ml(40), "WRN"}, | ||
"50-no-color": {true, ml(50), "ERR"}, | ||
"60-no-color": {true, ml(60), "FTL"}, | ||
"100-no-color": {true, ml(100), "PNC"}, | ||
"2-no-color": {true, ml(2), "2"}, | ||
"trace-color": {false, ml("trace"), "\x1b[35mTRC\x1b[0m"}, | ||
"debug-color": {false, ml("debug"), "\x1b[33mDBG\x1b[0m"}, | ||
"info-color": {false, ml("info"), "\x1b[32mINF\x1b[0m"}, | ||
"warn-color": {false, ml("warn"), "\x1b[31mWRN\x1b[0m"}, | ||
"error-color": {false, ml("error"), "\x1b[1m\x1b[31mERR\x1b[0m\x1b[0m"}, | ||
"fatal-color": {false, ml("fatal"), "\x1b[1m\x1b[31mFTL\x1b[0m\x1b[0m"}, | ||
"panic-color": {false, ml("panic"), "\x1b[1m\x1b[31mPNC\x1b[0m\x1b[0m"}, | ||
"unknown-color": {false, ml("unknown"), "\x1b[1mUNK\x1b[0m"}, | ||
"10-color": {false, ml(10), "\x1b[35mTRC\x1b[0m"}, | ||
"20-color": {false, ml(20), "\x1b[33mDBG\x1b[0m"}, | ||
"30-color": {false, ml(30), "\x1b[32mINF\x1b[0m"}, | ||
"40-color": {false, ml(40), "\x1b[31mWRN\x1b[0m"}, | ||
"50-color": {false, ml(50), "\x1b[1m\x1b[31mERR\x1b[0m\x1b[0m"}, | ||
"60-color": {false, ml(60), "\x1b[1m\x1b[31mFTL\x1b[0m\x1b[0m"}, | ||
"100-color": {false, ml(100), "\x1b[1m\x1b[31mPNC\x1b[0m\x1b[0m"}, | ||
"2-color": {false, ml(2), "\x1b[1m2\x1b[0m"}, | ||
"diff": {true, map[string]any{"level": "info", "log": map[string]any{"level": "debug"}}, "level=INF log.level=DBG"}, | ||
} | ||
for name, tc := range testcases { | ||
t.Run(name, func(t *testing.T) { | ||
f := formatter.NewLevel(tc.noColor, formatKey) | ||
str := f.Format(tc.msg) | ||
require.Equal(t, tc.expect, str) | ||
}) | ||
} | ||
} | ||
|
||
func TestLevel_ExcludeKeys(t *testing.T) { | ||
f := formatter.NewLevel(false, nil) | ||
require.Equal(t, []string{formatter.KeyLevel, formatter.KeyLog}, f.ExcludeKeys()) | ||
} | ||
|
||
func ml(level any) map[string]any { | ||
if l, ok := level.(int); ok { | ||
level = jn(l) | ||
} | ||
return map[string]any{ | ||
formatter.KeyLevel: level, | ||
formatter.KeyLog: map[string]any{ | ||
formatter.KeyLevel: level, | ||
}, | ||
} | ||
} |
Oops, something went wrong.