Add custom sorting function in text formatter

This commit is contained in:
David Bariod
2018-09-25 13:45:23 +02:00
parent 5a88d3c21d
commit 73bc94e60c
2 changed files with 72 additions and 14 deletions
+32 -2
View File
@@ -5,11 +5,13 @@ import (
"errors"
"fmt"
"os"
"sort"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestFormatting(t *testing.T) {
@@ -446,5 +448,33 @@ func TestTextFormatterIsColored(t *testing.T) {
}
}
// TODO add tests for sorting etc., this requires a parser for the text
// formatter output.
func TestCustomSorting(t *testing.T) {
formatter := &TextFormatter{
DisableColors: true,
SortingFunc: func(keys []string) {
sort.Slice(keys, func(i, j int) bool {
if keys[j] == "prefix" {
return false
}
if keys[i] == "prefix" {
return true
}
return strings.Compare(keys[i], keys[j]) == -1
})
},
}
entry := &Entry{
Message: "Testing custom sort function",
Time: time.Now(),
Level: InfoLevel,
Data: Fields{
"test": "testvalue",
"prefix": "the application prefix",
"blablabla": "blablabla",
},
}
b, err := formatter.Format(entry)
require.NoError(t, err)
require.True(t, strings.HasPrefix(string(b), "prefix="), "format output is %q", string(b))
}