summaryrefslogtreecommitdiff
path: root/src/pkg/gob/gobencdec_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/pkg/gob/gobencdec_test.go')
-rw-r--r--src/pkg/gob/gobencdec_test.go61
1 files changed, 57 insertions, 4 deletions
diff --git a/src/pkg/gob/gobencdec_test.go b/src/pkg/gob/gobencdec_test.go
index 82ca68170..012b09956 100644
--- a/src/pkg/gob/gobencdec_test.go
+++ b/src/pkg/gob/gobencdec_test.go
@@ -110,8 +110,8 @@ type GobTest2 struct {
}
type GobTest3 struct {
- X int // guarantee we have something in common with GobTest*
- G *Gobber // TODO: should be able to satisfy interface without a pointer
+ X int // guarantee we have something in common with GobTest*
+ G *Gobber
}
type GobTest4 struct {
@@ -128,6 +128,16 @@ type GobTestIgnoreEncoder struct {
X int // guarantee we have something in common with GobTest*
}
+type GobTestValueEncDec struct {
+ X int // guarantee we have something in common with GobTest*
+ G StringStruct // not a pointer.
+}
+
+type GobTestIndirectEncDec struct {
+ X int // guarantee we have something in common with GobTest*
+ G ***StringStruct // indirections to the receiver.
+}
+
func TestGobEncoderField(t *testing.T) {
b := new(bytes.Buffer)
// First a field that's a structure.
@@ -162,6 +172,50 @@ func TestGobEncoderField(t *testing.T) {
}
}
+// Even though the field is a value, we can still take its address
+// and should be able to call the methods.
+func TestGobEncoderValueField(t *testing.T) {
+ b := new(bytes.Buffer)
+ // First a field that's a structure.
+ enc := NewEncoder(b)
+ err := enc.Encode(GobTestValueEncDec{17, StringStruct{"HIJKL"}})
+ if err != nil {
+ t.Fatal("encode error:", err)
+ }
+ dec := NewDecoder(b)
+ x := new(GobTestValueEncDec)
+ err = dec.Decode(x)
+ if err != nil {
+ t.Fatal("decode error:", err)
+ }
+ if x.G.s != "HIJKL" {
+ t.Errorf("expected `HIJKL` got %s", x.G.s)
+ }
+}
+
+// GobEncode/Decode should work even if the value is
+// more indirect than the receiver.
+func TestGobEncoderIndirectField(t *testing.T) {
+ b := new(bytes.Buffer)
+ // First a field that's a structure.
+ enc := NewEncoder(b)
+ s := &StringStruct{"HIJKL"}
+ sp := &s
+ err := enc.Encode(GobTestIndirectEncDec{17, &sp})
+ if err != nil {
+ t.Fatal("encode error:", err)
+ }
+ dec := NewDecoder(b)
+ x := new(GobTestIndirectEncDec)
+ err = dec.Decode(x)
+ if err != nil {
+ t.Fatal("decode error:", err)
+ }
+ if (***x.G).s != "HIJKL" {
+ t.Errorf("expected `HIJKL` got %s", (***x.G).s)
+ }
+}
+
// As long as the fields have the same name and implement the
// interface, we can cross-connect them. Not sure it's useful
// and may even be bad but it works and it's hard to prevent
@@ -275,8 +329,7 @@ func TestGobEncoderStructSingleton(t *testing.T) {
func TestGobEncoderNonStructSingleton(t *testing.T) {
b := new(bytes.Buffer)
enc := NewEncoder(b)
- g := Gobber(1234) // TODO: shouldn't need to take the address here.
- err := enc.Encode(&g)
+ err := enc.Encode(Gobber(1234))
if err != nil {
t.Fatal("encode error:", err)
}