forked from tinylib/msgp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
When `allownil` is enabled, always allocate zero length slices. This ensures roundtrips with 0-length slices are not returned as nil. Replaces tinylib#304 Adds tests. Bonus: Don't shell out to test issue 94.
- Loading branch information
Showing
6 changed files
with
99 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package _generated | ||
|
||
import ( | ||
"bytes" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/tinylib/msgp/msgp" | ||
) | ||
|
||
func TestAllownil(t *testing.T) { | ||
tt := &NamedStructAN{ | ||
A: []string{}, | ||
B: nil, | ||
} | ||
var buf bytes.Buffer | ||
|
||
err := msgp.Encode(&buf, tt) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
in := buf.Bytes() | ||
|
||
for _, tnew := range []*NamedStructAN{{}, {A: []string{}}, {B: []string{}}} { | ||
err = msgp.Decode(bytes.NewBuffer(in), tnew) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
if !reflect.DeepEqual(tt, tnew) { | ||
t.Logf("in: %#v", tt) | ||
t.Logf("out: %#v", tnew) | ||
t.Fatal("objects not equal") | ||
} | ||
} | ||
|
||
in, err = tt.MarshalMsg(nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
for _, tanother := range []*NamedStructAN{{}, {A: []string{}}, {B: []string{}}} { | ||
var left []byte | ||
left, err = tanother.UnmarshalMsg(in) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
if len(left) > 0 { | ||
t.Errorf("%d bytes left", len(left)) | ||
} | ||
|
||
if !reflect.DeepEqual(tt, tanother) { | ||
t.Logf("in: %#v", tt) | ||
t.Logf("out: %#v", tanother) | ||
t.Fatal("objects not equal") | ||
} | ||
} | ||
} |
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,25 @@ | ||
package _generated | ||
|
||
import ( | ||
"bytes" | ||
"os" | ||
"testing" | ||
) | ||
|
||
// Issue 94: shims were not propogated recursively, | ||
// which caused shims that weren't at the top level | ||
// to be silently ignored. | ||
// | ||
// The following line will generate an error after | ||
// the code is generated if the generated code doesn't | ||
// have the right identifier in it. | ||
func TestIssue94(t *testing.T) { | ||
b, err := os.ReadFile("issue94_gen.go") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
const want = "timetostr" | ||
if !bytes.Contains(b, []byte(want)) { | ||
t.Errorf("generated code did not contain %q", want) | ||
} | ||
} |
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