package coap import ( "bytes" "fmt" "testing" ) func TestMediaTypes(t *testing.T) { types := []interface{}{TextPlain, AppXML, AppOctets, AppExi, AppJSON} exp := "coap.MediaType" for _, typ := range types { if got := fmt.Sprintf("%T", typ); got != exp { t.Errorf("Error on %#v, expected %q, was %q", typ, exp, got) } } } func TestOptionToBytes(t *testing.T) { tests := []struct { in interface{} exp []byte }{ {"", []byte{}}, {[]byte{}, []byte{}}, {"x", []byte{'x'}}, {[]byte{'x'}, []byte{'x'}}, {MediaType(3), []byte{0x3}}, {3, []byte{0x3}}, {838, []byte{0x3, 0x46}}, {int32(838), []byte{0x3, 0x46}}, {uint(838), []byte{0x3, 0x46}}, {uint32(838), []byte{0x3, 0x46}}, } for _, test := range tests { op := option{Value: test.in} got := op.toBytes() if !bytes.Equal(test.exp, got) { t.Errorf("Error on %T(%v), got %#v, wanted %#v", test.in, test.in, got, test.exp) } } } func TestMessageConfirmable(t *testing.T) { tests := []struct { m Message exp bool }{ {&BaseMessage{mtype: CON}, true}, {&BaseMessage{mtype: NON}, false}, } for _, test := range tests { got := test.m.IsConfirmable() if got != test.exp { t.Errorf("Expected %v for %v", test.exp, test.m) } } }