-
Notifications
You must be signed in to change notification settings - Fork 96
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
7 changed files
with
187 additions
and
4 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
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,60 @@ | ||
package json | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type object struct { | ||
subsetObject | ||
Name string `codec:"name"` | ||
} | ||
|
||
type subsetObject struct { | ||
Data string `codec:"data"` | ||
} | ||
|
||
func TestDecode(t *testing.T) { | ||
obj := object{ | ||
subsetObject: subsetObject{Data: "data"}, | ||
Name: "name", | ||
} | ||
encodedOb := Encode(obj) | ||
|
||
t.Run("basic encode/decode test", func(t *testing.T) { | ||
// basic encode/decode test. | ||
var decoded object | ||
err := Decode(encodedOb, &decoded) | ||
assert.NoError(t, err) | ||
assert.Equal(t, obj, decoded) | ||
}) | ||
|
||
t.Run("strict decode, pass", func(t *testing.T) { | ||
// strict decode test | ||
decoder := NewDecoder(bytes.NewReader(encodedOb)) | ||
var decoded object | ||
err := decoder.Decode(&decoded) | ||
assert.NoError(t, err) | ||
assert.Equal(t, obj, decoded) | ||
}) | ||
|
||
t.Run("strict decode subset, fail", func(t *testing.T) { | ||
// strict decode test | ||
decoder := NewDecoder(bytes.NewReader(encodedOb)) | ||
var decoded subsetObject | ||
err := decoder.Decode(&decoded) | ||
assert.Error(t, err) | ||
assert.Contains(t, err.Error(), "no matching struct field found when decoding stream map with key name") | ||
}) | ||
|
||
t.Run("lenient decode subset, pass", func(t *testing.T) { | ||
// strict decode test | ||
decoder := NewLenientDecoder(bytes.NewReader(encodedOb)) | ||
var decoded subsetObject | ||
err := decoder.Decode(&decoded) | ||
assert.NoError(t, err) | ||
assert.Equal(t, obj.subsetObject, decoded) | ||
}) | ||
} |
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,60 @@ | ||
package msgpack | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type object struct { | ||
subsetObject | ||
Name string `codec:"name"` | ||
} | ||
|
||
type subsetObject struct { | ||
Data string `codec:"data"` | ||
} | ||
|
||
func TestDecode(t *testing.T) { | ||
obj := object{ | ||
subsetObject: subsetObject{Data: "data"}, | ||
Name: "name", | ||
} | ||
encodedOb := Encode(obj) | ||
|
||
t.Run("basic encode/decode test", func(t *testing.T) { | ||
// basic encode/decode test. | ||
var decoded object | ||
err := Decode(encodedOb, &decoded) | ||
assert.NoError(t, err) | ||
assert.Equal(t, obj, decoded) | ||
}) | ||
|
||
t.Run("strict decode, pass", func(t *testing.T) { | ||
// strict decode test | ||
decoder := NewDecoder(bytes.NewReader(encodedOb)) | ||
var decoded object | ||
err := decoder.Decode(&decoded) | ||
assert.NoError(t, err) | ||
assert.Equal(t, obj, decoded) | ||
}) | ||
|
||
t.Run("strict decode subset, fail", func(t *testing.T) { | ||
// strict decode test | ||
decoder := NewDecoder(bytes.NewReader(encodedOb)) | ||
var decoded subsetObject | ||
err := decoder.Decode(&decoded) | ||
assert.Error(t, err) | ||
assert.Contains(t, err.Error(), "no matching struct field found when decoding stream map with key name") | ||
}) | ||
|
||
t.Run("lenient decode subset, pass", func(t *testing.T) { | ||
// strict decode test | ||
decoder := NewLenientDecoder(bytes.NewReader(encodedOb)) | ||
var decoded subsetObject | ||
err := decoder.Decode(&decoded) | ||
assert.NoError(t, err) | ||
assert.Equal(t, obj.subsetObject, decoded) | ||
}) | ||
} |
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