-
Notifications
You must be signed in to change notification settings - Fork 18
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
4 changed files
with
286 additions
and
2 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
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,30 @@ | ||
package encoding | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/shamaton/msgpack/v2/def" | ||
) | ||
|
||
func Test_asBool(t *testing.T) { | ||
method := func(e *encoder) func(bool) error { | ||
return e.writeBool | ||
} | ||
testcases := AsXXXTestCases[bool]{ | ||
{ | ||
Name: "True.error", | ||
Value: true, | ||
BufferSize: 1, | ||
PreWriteSize: 1, | ||
Method: method, | ||
}, | ||
{ | ||
Name: "True.ok", | ||
Value: true, | ||
Expected: []byte{def.True}, | ||
BufferSize: 1, | ||
Method: method, | ||
}, | ||
} | ||
testcases.Run(t) | ||
} |
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,111 @@ | ||
package encoding | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"io" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/shamaton/msgpack/v2/internal/common" | ||
tu "github.com/shamaton/msgpack/v2/internal/common/testutil" | ||
) | ||
|
||
const dummyByte = 0xc1 | ||
|
||
type TestWriter struct { | ||
WrittenBytes []byte | ||
} | ||
|
||
var _ io.Writer = (*TestWriter)(nil) | ||
|
||
var ErrTestWriter = errors.New("expected written error") | ||
|
||
func (w *TestWriter) Write(p []byte) (n int, err error) { | ||
w.WrittenBytes = append(w.WrittenBytes, p...) | ||
if bytes.Contains(p, []byte{dummyByte}) { | ||
return 0, ErrTestWriter | ||
} | ||
return len(p), nil | ||
} | ||
|
||
func NewTestWriter() *TestWriter { | ||
return &TestWriter{} | ||
} | ||
|
||
type AsXXXTestCase[T any] struct { | ||
Name string | ||
Value T | ||
Expected []byte | ||
Contains []byte | ||
BufferSize int | ||
PreWriteSize int | ||
Method func(*encoder) func(T) error | ||
MethodForFixed func(*encoder) func(reflect.Value) (bool, error) | ||
MethodForStruct func(*encoder) func(reflect.Value) error | ||
} | ||
|
||
type AsXXXTestCases[T any] []AsXXXTestCase[T] | ||
|
||
func (tcs AsXXXTestCases[T]) Run(t *testing.T) { | ||
for _, tc := range tcs { | ||
tc.Run(t) | ||
} | ||
} | ||
|
||
func (tc *AsXXXTestCase[T]) Run(t *testing.T) { | ||
t.Helper() | ||
|
||
if tc.Method == nil && tc.MethodForFixed == nil && tc.MethodForStruct == nil { | ||
t.Fatal("must set either Method or MethodForFixed or MethodForStruct") | ||
} | ||
|
||
method := func(e *encoder) error { | ||
if tc.Method != nil { | ||
return tc.Method(e)(tc.Value) | ||
} | ||
if tc.MethodForFixed != nil { | ||
_, err := tc.MethodForFixed(e)(reflect.ValueOf(tc.Value)) | ||
return err | ||
} | ||
if tc.MethodForStruct != nil { | ||
return tc.MethodForStruct(e)(reflect.ValueOf(tc.Value)) | ||
} | ||
panic("unreachable") | ||
} | ||
|
||
t.Run(tc.Name, func(t *testing.T) { | ||
w := NewTestWriter() | ||
e := encoder{ | ||
w: w, | ||
buf: common.GetBuffer(), | ||
Common: common.Common{}, | ||
} | ||
|
||
if tc.BufferSize < tc.PreWriteSize { | ||
t.Fatal("buffer size must be greater than pre write size") | ||
} | ||
|
||
e.buf.Data = make([]byte, tc.BufferSize) | ||
if tc.PreWriteSize > 0 { | ||
for i := 0; i < tc.PreWriteSize; i++ { | ||
_ = e.buf.Write(e.w, dummyByte) | ||
} | ||
} | ||
|
||
err := method(&e) | ||
_ = e.buf.Flush(w) | ||
common.PutBuffer(e.buf) | ||
|
||
if tc.PreWriteSize > 0 { | ||
tu.IsError(t, err, ErrTestWriter) | ||
if !bytes.Contains(w.WrittenBytes, tc.Contains) { | ||
t.Fatalf("[% 02x] does not contain in [% 02x]", tc.Contains, w.WrittenBytes) | ||
} | ||
return | ||
} | ||
|
||
tu.NoError(t, err) | ||
tu.EqualSlice(t, w.WrittenBytes, tc.Expected) | ||
}) | ||
} |
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,133 @@ | ||
package encoding | ||
|
||
import ( | ||
"math" | ||
"testing" | ||
|
||
"github.com/shamaton/msgpack/v2/def" | ||
) | ||
|
||
func Test_asInt(t *testing.T) { | ||
method := func(e *encoder) func(int64) error { | ||
return e.writeInt | ||
} | ||
testcases := AsXXXTestCases[int64]{ | ||
{ | ||
Name: "Uint.error", | ||
Value: math.MaxInt32, | ||
BufferSize: 1, | ||
PreWriteSize: 1, | ||
Method: method, | ||
}, | ||
{ | ||
Name: "Uint.ok", | ||
Value: math.MaxInt32, | ||
Expected: []byte{def.Uint32, 0x7f, 0xff, 0xff, 0xff}, | ||
BufferSize: 1, | ||
Method: method, | ||
}, | ||
{ | ||
Name: "NegativeFix.error", | ||
Value: -1, | ||
BufferSize: 1, | ||
PreWriteSize: 1, | ||
Method: method, | ||
}, | ||
{ | ||
Name: "NegativeFix.ok", | ||
Value: -1, | ||
Expected: []byte{0xff}, | ||
BufferSize: 1, | ||
Method: method, | ||
}, | ||
{ | ||
Name: "Int8.error.def", | ||
Value: math.MinInt8, | ||
BufferSize: 1, | ||
PreWriteSize: 1, | ||
Method: method, | ||
}, | ||
{ | ||
Name: "Int8.error.value", | ||
Value: math.MinInt8, | ||
BufferSize: 2, | ||
PreWriteSize: 1, | ||
Contains: []byte{def.Int8}, | ||
Method: method, | ||
}, | ||
{ | ||
Name: "Int8.ok", | ||
Value: math.MinInt8, | ||
Expected: []byte{def.Int8, 0x80}, | ||
BufferSize: 1, | ||
Method: method, | ||
}, | ||
{ | ||
Name: "Int16.error.def", | ||
Value: math.MinInt16, | ||
BufferSize: 1, | ||
PreWriteSize: 1, | ||
Method: method, | ||
}, | ||
{ | ||
Name: "Int16.error.value", | ||
Value: math.MinInt16, | ||
BufferSize: 3, | ||
PreWriteSize: 1, | ||
Contains: []byte{def.Int16}, | ||
Method: method, | ||
}, | ||
{ | ||
Name: "Int16.ok", | ||
Value: math.MinInt16, | ||
Expected: []byte{def.Int16, 0x80, 0x00}, | ||
BufferSize: 1, | ||
Method: method, | ||
}, | ||
{ | ||
Name: "Int32.error.def", | ||
Value: math.MinInt32, | ||
BufferSize: 1, | ||
PreWriteSize: 1, | ||
Method: method, | ||
}, | ||
{ | ||
Name: "Int32.error.value", | ||
Value: math.MinInt32, | ||
BufferSize: 5, | ||
PreWriteSize: 1, | ||
Contains: []byte{def.Int32}, | ||
Method: method, | ||
}, | ||
{ | ||
Name: "Int32.ok", | ||
Value: math.MinInt32, | ||
Expected: []byte{def.Int32, 0x80, 0x00, 0x00, 0x00}, | ||
BufferSize: 1, | ||
Method: method, | ||
}, | ||
{ | ||
Name: "Int64.error.def", | ||
Value: math.MinInt64, | ||
BufferSize: 1, | ||
PreWriteSize: 1, | ||
Method: method, | ||
}, | ||
{ | ||
Name: "Int64.error.value", | ||
Value: math.MinInt64, | ||
BufferSize: 9, | ||
PreWriteSize: 1, | ||
Contains: []byte{def.Int64}, | ||
Method: method, | ||
}, | ||
{ | ||
Name: "Int64.ok", | ||
Value: math.MinInt64, | ||
Expected: []byte{def.Int64, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, | ||
BufferSize: 1, | ||
Method: method, | ||
}, | ||
} | ||
testcases.Run(t) | ||
} |