Debian Codegraph @

api/v1/lib/encoding/proto/encoding_test.go

raw symbols SCIP
1 package proto_test
2
3 import (
4 "bytes"
5 "testing"
6
7 "github.com/mesos/mesos-go/api/v1/lib"
8 "github.com/mesos/mesos-go/api/v1/lib/encoding"
9 . "github.com/mesos/mesos-go/api/v1/lib/encoding/proto"
10 )
11
12 type FakeMessage string
13
14 func (f *FakeMessage) Marshal() ([]byte, error) { return ([]byte)(*f), nil }
15 func (f *FakeMessage) MarshalJSON() ([]byte, error) { return nil, nil }
16
17 func TestEncoder(t *testing.T) {
18 // write a proto message, validate that we're actually marshaling proto
19 buf := bytes.Buffer{}
20 enc := NewEncoder(encoding.SinkWriter(&buf))
21 err := enc.Encode(&mesos.FrameworkID{Value: "hello"})
22
23 if err != nil {
24 t.Fatal(err)
25 }
26
27 data := string(buf.Bytes())
28 if data != "\n\x05hello" {
29 // \n == 00001 010 == {field-id} {tag-type}
30 // \x05 == length of the string
31 t.Fatalf("expected `hello` instead of %q", data)
32 }
33
34 // write a non-proto message, verify panic
35 caughtPanic := false
36 func() {
37 defer func() {
38 if x := recover(); x != nil {
39 caughtPanic = true
40 }
41 }()
42 m := FakeMessage("hello")
43 enc.Encode(&m)
44 t.Fatal("expected panic, but Encode completed normally")
45 }()
46 if !caughtPanic {
47 t.Fatal("Encode failed to complete normally, but we didn't see a panic? should never happen")
48 }
49 }