Closed client9 closed 8 years ago
create directory junk and enter it
junk
mkdir junk cd junk
create this file pairs.go
pairs.go
package junk type Pairs struct { Name string KeyValues [][2]string }
run this
codecgen -o pairs_ugoriji.go pairs.go
or
codecgen -u -o pairs_ugoriji.go pairs.go
Write a test
package junk import ( "reflect" "testing" "github.com/ugorji/go/codec" ) func TestJunk(t *testing.T) { obj := Pairs{ Name: "whatev", KeyValues: [][2]string{ {"key1", "value1"}, {"key2", "value2"}, }, } raw := []byte{} mh := codec.MsgpackHandle{} enc := codec.NewEncoderBytes(&raw, &mh) err := enc.Encode(obj) if err != nil { t.Fatalf("Failed to Marshal via Ugorji: %s", err) } // test 3 Ugorji Ugorji mh = codec.MsgpackHandle{} dec := codec.NewDecoderBytes(raw, &mh) obj2 := Pairs{} err = dec.Decode(&obj2) if err != nil { t.Fatalf("Failed to unmarshal Ugorji via Ugorji: %s", err) } if !reflect.DeepEqual(obj, obj2) { t.Errorf("got\n %#v\n vs\n%#v", obj, obj2) } }
Run tests
go test -v ./...
I get this
$ go test -v ./... === RUN TestJunk --- FAIL: TestJunk (0.00s) pairs_test.go:36: got junk.Pairs{Name:"whatev", KeyValues:[][2]string{[2]string{"key1", "value1"}, [2]string{"key2", "value2"}}} vs junk.Pairs{Name:"whatev", KeyValues:[][2]string{[2]string{"", ""}, [2]string{"", ""}}} FAIL
create directory
junk
and enter itcreate this file
pairs.go
run this
or
Write a test
Run tests
I get this