add implementation and tests

This commit is contained in:
Lynn Cyrin
2019-06-05 00:10:46 -07:00
parent 23045ec6d1
commit fa0d2a82ff
2 changed files with 96 additions and 1 deletions
+88
View File
@@ -172,6 +172,94 @@ func TestDisableLevelTruncation(t *testing.T) {
checkDisableTruncation(false, InfoLevel)
}
func TestPadLevelText(t *testing.T) {
// A note for future maintainers / committers:
//
// This test denormalizes the level text as a part of its assertions.
// Because of that, its not really a "unit test" of the PadLevelText functionality.
// So! Many apologies to the potential future person who has to rewrite this test
// when they are changing some completely unrelated functionality.
params := []struct {
name string
level Level
paddedLevelText string
}{
{
name: "PanicLevel",
level: PanicLevel,
paddedLevelText: "PANIC ", // 2 extra spaces
},
{
name: "FatalLevel",
level: FatalLevel,
paddedLevelText: "FATAL ", // 2 extra spaces
},
{
name: "ErrorLevel",
level: ErrorLevel,
paddedLevelText: "ERROR ", // 2 extra spaces
},
{
name: "WarnLevel",
level: WarnLevel,
// WARNING is already the max length, so we don't need to assert a paddedLevelText
},
{
name: "DebugLevel",
level: DebugLevel,
paddedLevelText: "DEBUG ", // 2 extra spaces
},
{
name: "TraceLevel",
level: TraceLevel,
paddedLevelText: "TRACE ", // 2 extra spaces
},
{
name: "InfoLevel",
level: InfoLevel,
paddedLevelText: "INFO ", // 3 extra spaces
},
}
// We create a "default" TextFormatter to do a control case test
// and a TextFormatter with PadLevelText, which is the flag we are testing here
tfDefault := TextFormatter{}
tfWithPadding := TextFormatter{PadLevelText: true}
for _, val := range params {
t.Run(val.name, func(t *testing.T) {
// TextFormatter writes into these bytes.Buffers, and we make assertions about their contents later
var bytesDefault bytes.Buffer
var bytesWithPadding bytes.Buffer
// The TextFormatter instance and the bytes.Buffer instance are different here
// all the other arguments are the same
tfDefault.printColored(&bytesDefault, &Entry{Level: val.level}, []string{}, nil, "")
tfWithPadding.printColored(&bytesWithPadding, &Entry{Level: val.level}, []string{}, nil, "")
// turn the bytes back into a string so that we can actually work with the data
logLineDefault := (&bytesDefault).String()
logLineWithPadding := (&bytesWithPadding).String()
// Control: the level text should not be padded by default
if val.paddedLevelText != "" && strings.Contains(logLineDefault, val.paddedLevelText) {
t.Errorf("log line \"%s\" should not contain the padded level text \"%s\" by default", logLineDefault, val.paddedLevelText)
}
// Assertion: the level text should still contain the string representation of the level
if !strings.Contains(strings.ToLower(logLineWithPadding), val.level.String()) {
t.Errorf("log line \"%s\" should contain the level text \"%s\" when padding is enabled", logLineWithPadding, val.level.String())
}
// Assertion: the level text should be in its padded form now
if val.paddedLevelText != "" && !strings.Contains(logLineWithPadding, val.paddedLevelText) {
t.Errorf("log line \"%s\" should contain the padded level text \"%s\" when padding is enabled", logLineWithPadding, val.paddedLevelText)
}
})
}
}
func TestDisableTimestampWithColoredOutput(t *testing.T) {
tf := &TextFormatter{DisableTimestamp: true, ForceColors: true}