44package swid
55
66import (
7+ "encoding/xml"
78 "testing"
89
910 "github.com/stretchr/testify/assert"
@@ -15,12 +16,12 @@ func TestTagID_16Bytes(t *testing.T) {
1516 0x00 , 0x01 , 0x00 , 0x01 , 0x00 , 0x01 , 0x00 , 0x01 ,
1617 }
1718
18- expected := tv
19+ expected := "00010001000100010001000100010001"
1920
20- actual , err := checkTagID (tv )
21+ actual := NewTagID (tv )
2122
22- assert .Nil (t , err )
23- assert .Equal (t , expected , actual )
23+ assert .NotNil (t , actual )
24+ assert .Equal (t , expected , actual . String () )
2425}
2526
2627func TestTagID_15Bytes (t * testing.T ) {
@@ -29,7 +30,7 @@ func TestTagID_15Bytes(t *testing.T) {
2930 0x00 , 0x01 , 0x00 , 0x01 , 0x00 , 0x01 , 0x00 ,
3031 }
3132
32- _ , err := checkTagID (tv )
33+ err := checkTagID (tv )
3334
3435 assert .EqualError (t , err , "binary tag-id MUST be 16 bytes" )
3536}
@@ -41,18 +42,18 @@ func TestTagID_17Bytes(t *testing.T) {
4142 0x00 ,
4243 }
4344
44- _ , err := checkTagID (tv )
45+ err := checkTagID (tv )
4546
4647 assert .EqualError (t , err , "binary tag-id MUST be 16 bytes" )
4748}
4849
4950func TestTagID_String (t * testing.T ) {
5051 tv := "example.acme.roadrunner-sw-v1-0-0"
5152
52- actual , err := checkTagID (tv )
53+ actual := NewTagID (tv )
5354
54- assert .Nil (t , err )
55- assert .Equal (t , tv , actual )
55+ assert .NotNil (t , actual )
56+ assert .Equal (t , tv , actual . String () )
5657}
5758
5859func TestTagID_UnhandledType (t * testing.T ) {
@@ -64,7 +65,77 @@ func TestTagID_UnhandledType(t *testing.T) {
6465 b : "one" ,
6566 }
6667
67- _ , err := checkTagID (tv )
68+ err := checkTagID (tv )
6869
6970 assert .EqualError (t , err , "tag-id MUST be []byte or string; got struct { a int; b string }" )
7071}
72+
73+ func TestTagID_UnmarshalXMLAttrString (t * testing.T ) {
74+ v := "example.acme.roadrunner-sw-v1-0-0"
75+
76+ tv := xml.Attr {
77+ Name : xml.Name {Local : "tagId" },
78+ Value : v ,
79+ }
80+
81+ expected := * NewTagID (v )
82+
83+ var actual TagID
84+
85+ err := actual .UnmarshalXMLAttr (tv )
86+
87+ assert .Nil (t , err )
88+ assert .Equal (t , expected , actual )
89+ }
90+
91+ func TestTagID_MarshalXMLAttrString (t * testing.T ) {
92+ v := "example.acme.roadrunner-sw-v1-0-0"
93+
94+ tv := * NewTagID (v )
95+
96+ expected := xml.Attr {
97+ Name : xml.Name {Local : "tagId" },
98+ Value : v ,
99+ }
100+
101+ actual , err := tv .MarshalXMLAttr (xml.Name {Local : "tagId" })
102+
103+ assert .Nil (t , err )
104+ assert .Equal (t , expected , actual )
105+ }
106+
107+ func TestTagID_MarshalXMLAttrBytes (t * testing.T ) {
108+ v := []byte {
109+ 0x00 , 0x01 , 0x00 , 0x01 , 0x00 , 0x01 , 0x00 , 0x01 ,
110+ 0x00 , 0x01 , 0x00 , 0x01 , 0x00 , 0x01 , 0x00 , 0x01 ,
111+ }
112+
113+ tv := * NewTagID (v )
114+
115+ _ , err := tv .MarshalXMLAttr (xml.Name {Local : "tagId" })
116+
117+ assert .EqualError (t , err , "only tag-id of type string can be serialized to XML" )
118+ }
119+
120+ func TestTagID_MarshalJSONBytes (t * testing.T ) {
121+ v := []byte {
122+ 0x00 , 0x01 , 0x00 , 0x01 , 0x00 , 0x01 , 0x00 , 0x01 ,
123+ 0x00 , 0x01 , 0x00 , 0x01 , 0x00 , 0x01 , 0x00 , 0x01 ,
124+ }
125+
126+ tv := * NewTagID (v )
127+
128+ _ , err := tv .MarshalJSON ()
129+
130+ assert .EqualError (t , err , "only tag-id of type string can be serialized to JSON" )
131+ }
132+
133+ func TestTagID_UnMarshalJSONUnhandled (t * testing.T ) {
134+ tv := []byte (`{ "k": "0" }` )
135+
136+ var actual TagID
137+
138+ err := actual .UnmarshalJSON (tv )
139+
140+ assert .EqualError (t , err , "expecting string, found map[string]interface {} instead" )
141+ }
0 commit comments