-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from shamaton/add_internal_tests
Add internal tests and fix some bugs
- Loading branch information
Showing
70 changed files
with
12,268 additions
and
74 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,31 @@ | ||
package def | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
var ( | ||
// base errors | ||
|
||
ErrMsgpack = errors.New("") | ||
|
||
// decoding errors | ||
|
||
ErrNoData = fmt.Errorf("%wno data", ErrMsgpack) | ||
ErrHasLeftOver = fmt.Errorf("%wdata has left over", ErrMsgpack) | ||
ErrReceiverNotPointer = fmt.Errorf("%wreceiver not pointer", ErrMsgpack) | ||
ErrNotMatchArrayElement = fmt.Errorf("%wnot match array element", ErrMsgpack) | ||
ErrCanNotDecode = fmt.Errorf("%winvalid code", ErrMsgpack) | ||
ErrCanNotSetSliceAsMapKey = fmt.Errorf("%wcan not set slice as map key", ErrMsgpack) | ||
ErrCanNotSetMapAsMapKey = fmt.Errorf("%wcan not set map as map key", ErrMsgpack) | ||
|
||
// encoding errors | ||
|
||
ErrTooShortBytes = fmt.Errorf("%wtoo short bytes", ErrMsgpack) | ||
ErrLackDataLengthToSlice = fmt.Errorf("%wdata length lacks to create slice", ErrMsgpack) | ||
ErrLackDataLengthToMap = fmt.Errorf("%wdata length lacks to create map", ErrMsgpack) | ||
ErrUnsupportedType = fmt.Errorf("%wunsupported type", ErrMsgpack) | ||
ErrUnsupportedLength = fmt.Errorf("%wunsupported length", ErrMsgpack) | ||
ErrNotMatchLastIndex = fmt.Errorf("%wnot match last index", ErrMsgpack) | ||
) |
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,8 @@ | ||
package msgpack | ||
|
||
import ( | ||
"github.com/shamaton/msgpack/v2/def" | ||
) | ||
|
||
// Error is used in all msgpack error as the based error. | ||
var Error = def.ErrMsgpack |
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,48 @@ | ||
package common | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
tu "github.com/shamaton/msgpack/v2/internal/common/testutil" | ||
) | ||
|
||
func TestCommon_CheckField(t *testing.T) { | ||
common := Common{} | ||
|
||
t.Run("tag:-", func(t *testing.T) { | ||
field := reflect.StructField{ | ||
Name: "A", | ||
Tag: `msgpack:"-"`, | ||
} | ||
b, v := common.CheckField(field) | ||
tu.Equal(t, b, false) | ||
tu.Equal(t, v, "") | ||
}) | ||
t.Run("tag:B", func(t *testing.T) { | ||
field := reflect.StructField{ | ||
Name: "A", | ||
Tag: `msgpack:"B"`, | ||
} | ||
b, v := common.CheckField(field) | ||
tu.Equal(t, b, true) | ||
tu.Equal(t, v, "B") | ||
}) | ||
t.Run("name:A", func(t *testing.T) { | ||
field := reflect.StructField{ | ||
Name: "A", | ||
Tag: `msgpack:""`, | ||
} | ||
b, v := common.CheckField(field) | ||
tu.Equal(t, b, true) | ||
tu.Equal(t, v, "A") | ||
}) | ||
t.Run("private", func(t *testing.T) { | ||
field := reflect.StructField{ | ||
Name: "a", | ||
} | ||
b, v := common.CheckField(field) | ||
tu.Equal(t, b, false) | ||
tu.Equal(t, v, "") | ||
}) | ||
} |
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,42 @@ | ||
package testutil | ||
|
||
import ( | ||
"errors" | ||
"io" | ||
) | ||
|
||
var ErrReaderErr = errors.New("reader error") | ||
|
||
type ErrReader struct{} | ||
|
||
func NewErrReader() *ErrReader { | ||
return &ErrReader{} | ||
} | ||
|
||
func (ErrReader) Read(_ []byte) (int, error) { | ||
return 0, ErrReaderErr | ||
} | ||
|
||
type TestReader struct { | ||
s []byte | ||
i int64 // current reading index | ||
count int | ||
} | ||
|
||
func NewTestReader(b []byte) *TestReader { | ||
return &TestReader{s: b, i: 0, count: 0} | ||
} | ||
|
||
func (r *TestReader) Read(b []byte) (n int, err error) { | ||
if r.i >= int64(len(r.s)) { | ||
return 0, io.EOF | ||
} | ||
n = copy(b, r.s[r.i:]) | ||
r.i += int64(n) | ||
r.count++ | ||
return | ||
} | ||
|
||
func (r *TestReader) Count() int { | ||
return r.count | ||
} |
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,56 @@ | ||
package testutil | ||
|
||
import ( | ||
"math" | ||
"reflect" | ||
"strconv" | ||
|
||
"github.com/shamaton/msgpack/v2/def" | ||
) | ||
|
||
// CreateStruct returns a struct that is made dynamically and encoded bytes. | ||
func CreateStruct(fieldNum int) (v any, asMapBytes []byte, asArrayBytes []byte) { | ||
if fieldNum < 0 { | ||
panic("negative field number") | ||
} | ||
|
||
fields := make([]reflect.StructField, 0, fieldNum) | ||
asMapBytes = make([]byte, 0, fieldNum*2) | ||
asArrayBytes = make([]byte, 0, fieldNum) | ||
|
||
for i := 0; i < fieldNum; i++ { | ||
// create struct field | ||
name := "A" + strconv.Itoa(i) | ||
typ := reflect.TypeOf(1) | ||
field := reflect.StructField{ | ||
Name: name, | ||
Type: typ, | ||
Tag: `json:"B"`, | ||
} | ||
fields = append(fields, field) | ||
|
||
// set encoded bytes | ||
if len(name) < 32 { | ||
asMapBytes = append(asMapBytes, def.FixStr+byte(len(name))) | ||
} else if len(name) < math.MaxUint8 { | ||
asMapBytes = append(asMapBytes, def.Str8) | ||
asMapBytes = append(asMapBytes, byte(len(name))) | ||
} | ||
for _, c := range name { | ||
asMapBytes = append(asMapBytes, byte(c)) | ||
} | ||
value := byte(i % 0x7f) | ||
asMapBytes = append(asMapBytes, value) | ||
asArrayBytes = append(asArrayBytes, value) | ||
} | ||
t := reflect.StructOf(fields) | ||
|
||
// set field values | ||
v = reflect.New(t).Interface() | ||
rv := reflect.ValueOf(v) | ||
for i := 0; i < rv.Elem().NumField(); i++ { | ||
field := rv.Elem().Field(i) | ||
field.SetInt(int64(i % 0x7f)) | ||
} | ||
return v, asMapBytes, asArrayBytes | ||
} |
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,93 @@ | ||
package testutil | ||
|
||
import ( | ||
"errors" | ||
"reflect" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func NoError(t *testing.T, err error) { | ||
t.Helper() | ||
if err != nil { | ||
t.Fatalf("error is not nil: %v", err) | ||
} | ||
} | ||
|
||
func Error(t *testing.T, err error) { | ||
t.Helper() | ||
if err == nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
func IsError(t *testing.T, actual, expected error) { | ||
t.Helper() | ||
if !errors.Is(actual, expected) { | ||
t.Fatalf("not equal. actual: %v, expected: %v", actual, expected) | ||
} | ||
} | ||
|
||
func ErrorContains(t *testing.T, err error, errStr string) { | ||
t.Helper() | ||
if err == nil { | ||
t.Fatal("error should occur") | ||
} | ||
if !strings.Contains(err.Error(), errStr) { | ||
t.Fatalf("error does not contain '%s'. err: %v", errStr, err) | ||
} | ||
} | ||
|
||
func Equal[T any](t *testing.T, actual, expected T) { | ||
t.Helper() | ||
if !reflect.DeepEqual(actual, expected) { | ||
t.Fatalf("not equal. actual: %v, expected: %v", actual, expected) | ||
} | ||
} | ||
|
||
func EqualSlice[T comparable](t *testing.T, actual, expected []T) { | ||
t.Helper() | ||
if len(actual) != len(expected) { | ||
switch a := any(actual).(type) { | ||
case []byte: | ||
e := any(expected).([]byte) | ||
t.Fatalf("diffrent length. actual: [% 02x], expected: [% 02x]", a, e) | ||
default: | ||
t.Fatalf("diffrent length. actual: %v, expected: %v", actual, expected) | ||
} | ||
} | ||
for i := range actual { | ||
if !reflect.DeepEqual(actual[i], expected[i]) { | ||
switch a := any(actual).(type) { | ||
case []byte: | ||
e := any(expected).([]byte) | ||
t.Fatalf("not equal. actual: [% 02x], expected: [% 02x]", a, e) | ||
default: | ||
t.Fatalf("not equal. actual: %v, expected: %v", actual, expected) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func EqualMap[K comparable, V comparable](t *testing.T, actual, expected map[K]V) { | ||
t.Helper() | ||
if len(actual) != len(expected) { | ||
t.Fatalf("diffrent length. actual: %v, expected: %v", actual, expected) | ||
} | ||
for k, v1 := range actual { | ||
if v2, ok := expected[k]; !ok || v1 != v2 { | ||
t.Fatalf("not equal. actual: %v, expected: %v", actual, expected) | ||
} | ||
} | ||
} | ||
|
||
type Equaler[T any] interface { | ||
Equal(other T) bool | ||
} | ||
|
||
func EqualEqualer[T Equaler[T]](t *testing.T, actual, expected T) { | ||
t.Helper() | ||
if !actual.Equal(expected) { | ||
t.Fatalf("not equal. actual: %v, expected: %v", actual, 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
Oops, something went wrong.