- Replace all testify/assert and testify/require with be library - Update go.mod to use be v0.3.0 instead of testify - Simplify test assertions using be.Equal, be.Err, and be.True - Refactor append_test, entry_test, meta_test, log_test, and formatter tests
59 lines
1.7 KiB
Go
59 lines
1.7 KiB
Go
package tools
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/nalgeon/be"
|
|
)
|
|
|
|
func TestWriteBuffer(t *testing.T) {
|
|
when := time.Now()
|
|
tests := []struct {
|
|
name string
|
|
value any
|
|
out string
|
|
err error
|
|
}{
|
|
{"nil", nil, "", nil},
|
|
{"string", "hi", "hi", nil},
|
|
{"bytes", []byte{104, 105}, "hi", nil},
|
|
{"byte", byte(104), "h", nil},
|
|
{"rune", 'h', "h", nil},
|
|
{"int", 42, "42", nil},
|
|
{"int64", int64(42), "42", nil},
|
|
{"float", 42.13, "42.13", nil},
|
|
{"bool", false, "false", nil},
|
|
{"json.Number", json.Number("42.13"), "42.13", nil},
|
|
{"json.RawMessage", json.RawMessage("{}"), "{}", nil},
|
|
{"time", when, when.Format(time.RFC3339), nil},
|
|
{"slice", []any{1, 2, "foo"}, `[1,2,"foo"]`, nil},
|
|
{"map", map[string]any{"baz": 42, "foo": "bar"}, `{"baz":42,"foo":"bar"}`, nil},
|
|
{"struct", struct{}{}, "", errors.New("Unsupported type struct {}")},
|
|
{"skip-bang-num", "!42", "42", nil},
|
|
{"skip-bang-bool", "!false", "false", nil},
|
|
{"skip-bang-time", "!" + when.Format(time.RFC3339), when.Format(time.RFC3339), nil},
|
|
{"skip-bang-duration", "!15 mins", "15 mins", nil},
|
|
{"skip-bang-bytes-num", []byte("!42"), "42", nil},
|
|
{"skip-bang-bytes-bool", []byte("!false"), "false", nil},
|
|
{"skip-bang-bytes-time", []byte("!" + when.Format(time.RFC3339)), when.Format(time.RFC3339), nil},
|
|
{"skip-bang-bytes-duration", []byte("!15 mins"), "15 mins", nil},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, getWriteTestRunner(tt.value, tt.out, tt.err))
|
|
}
|
|
}
|
|
|
|
func getWriteTestRunner(value any, out string, err error) func(*testing.T) {
|
|
return func(t *testing.T) {
|
|
buff := &bytes.Buffer{}
|
|
n, er := WriteValue(buff, value)
|
|
be.Equal(t, n, len(out))
|
|
be.Equal(t, er, err)
|
|
be.Equal(t, buff.String(), out)
|
|
}
|
|
}
|