Changes
2 changed files (+111/-1)
-
-
@@ -2,6 +2,7 @@ package activitypubimport ( "bytes" "encoding/gob" "fmt" "strconv" "strings"
-
@@ -258,6 +259,7 @@ func (l LangRefValue) Equals(other LangRefValue) bool {func (c *Content) UnmarshalJSON(data []byte) error { return c.UnmarshalText(data) } func (c *Content) UnmarshalText(data []byte) error { *c = Content{} if len(data) == 0 {
-
@@ -273,6 +275,28 @@ func (c *Content) UnmarshalText(data []byte) error {return nil } func (c Content) GobEncode() ([]byte, error) { b := new(bytes.Buffer) gg := gob.NewEncoder(b) if err := gg.Encode([]byte(c)); err != nil { return nil, err } return b.Bytes(), nil } func (c *Content) GobDecode(data []byte) error { if len(data) == 0 { // NOTE(marius): this behaviour diverges from vanilla gob package return nil } bb := make([]byte, 0) if err := gob.NewDecoder(bytes.NewReader(data)).Decode(&bb); err != nil { return err } *c = bb return nil } func (c Content) String() string { return string(c) }
-
-
-
@@ -1,10 +1,14 @@package activitypub import ( "bytes" "encoding/gob" "fmt" json "github.com/go-ap/jsonld" "reflect" "strconv" "testing" json "github.com/go-ap/jsonld" ) func TestNaturalLanguageValue_MarshalJSON(t *testing.T) {
-
@@ -539,3 +543,85 @@ func TestContent_UnmarshalJSON(t *testing.T) {func TestContent_UnmarshalText(t *testing.T) { t.Skip("TODO") } func gobValue(a interface{}) []byte { b := bytes.Buffer{} gg := gob.NewEncoder(&b) gg.Encode(a) return b.Bytes() } func TestContent_GobEncode(t *testing.T) { tests := []struct { name string c Content want []byte wantErr bool }{ { name: "empty", c: Content{}, want: gobValue([]byte{}), wantErr: false, }, { name: "empty value", c: Content{'0'}, want: gobValue([]byte{'0'}), wantErr: false, }, { name: "some text", c: Content{'a', 'n', 'a', ' ', 'a', 'r', 'e'}, want: gobValue([]byte{'a', 'n', 'a', ' ', 'a', 'r', 'e'}), wantErr: false, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got, err := tt.c.GobEncode() if (err != nil) != tt.wantErr { t.Errorf("GobEncode() error = %v, wantErr %v", err, tt.wantErr) return } if !reflect.DeepEqual(got, tt.want) { t.Errorf("GobEncode() got = %v, want %v", got, tt.want) } }) } } func TestContent_GobDecode(t *testing.T) { tests := []struct { name string c Content data []byte wantErr bool }{ { name: "empty", c: Content{}, data: []byte{}, wantErr: false, }, { name: "empty value", c: Content{'0'}, data: gobValue([]byte{'0'}), wantErr: false, }, { name: "some text", c: Content{'a', 'n', 'a', ' ', 'a', 'r', 'e'}, data: gobValue([]byte{'a', 'n', 'a', ' ', 'a', 'r', 'e'}), wantErr: false, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := tt.c.GobDecode(tt.data); (err != nil) != tt.wantErr { t.Errorf("GobDecode() error = %v, wantErr %v", err, tt.wantErr) } }) } }
-