Improve encoding tests

This commit is contained in:
andig 2023-02-25 13:09:37 +01:00
parent 4642a135db
commit db98a34e23
2 changed files with 16 additions and 16 deletions

View file

@ -2,14 +2,18 @@ package server
import (
"math"
"reflect"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestEncode(t *testing.T) {
now := time.Now()
tc := []struct {
in, out interface{}
in interface{}
out string
}{
{int64(1), "1"},
{math.NaN(), "null"},
@ -17,19 +21,13 @@ func TestEncode(t *testing.T) {
{"1.2345", "\"1.2345\""},
{time.Hour, "3600"},
{"minpv", "\"minpv\""},
{time.Time{}, "null"},
{now, "\"" + now.Format(time.RFC3339) + "\""},
}
for _, tc := range tc {
t.Logf("%+v", tc)
out, err := encode(tc.in)
if err != nil {
t.Error(err)
}
if out != tc.out {
t.Errorf("expected %v (string), got %v (%s)",
tc.out, out, reflect.TypeOf(out).Kind(),
)
}
assert.NoError(t, err)
assert.Equal(t, tc.out, out)
}
}