@@ -549,6 +549,92 @@ func TestDiagnoseByteString(t *testing.T) {
549549 })
550550}
551551
552+ func TestDiagnoseTextString (t * testing.T ) {
553+ testCases := []struct {
554+ title string
555+ cbor []byte
556+ diag string
557+ opts * DiagOptions
558+ }{
559+ {
560+ "valid UTF-8 text in byte string" ,
561+ hexDecode ("4d68656c6c6f2c20e4bda0e5a5bd" ),
562+ `'hello, \u4f60\u597d'` ,
563+ & DiagOptions {
564+ ByteStringText : true ,
565+ },
566+ },
567+ {
568+ "valid UTF-8 text in text string" ,
569+ hexDecode ("6d68656c6c6f2c20e4bda0e5a5bd" ),
570+ `"hello, \u4f60\u597d"` , // "hello, 你好"
571+ & DiagOptions {
572+ ByteStringText : true ,
573+ },
574+ },
575+ {
576+ "invalid UTF-8 text in byte string" ,
577+ hexDecode ("4d68656c6c6fffeee4bda0e5a5bd" ),
578+ `h'68656c6c6fffeee4bda0e5a5bd'` ,
579+ & DiagOptions {
580+ ByteStringText : true ,
581+ },
582+ },
583+ {
584+ "invalid UTF-8 text in text string" ,
585+ hexDecode ("6d68656c6c6fffeee4bda0e5a5bd" ),
586+ `"hello\u00ff\u00ee\u4f60\u597d"` ,
587+ & DiagOptions {
588+ ByteStringText : true ,
589+ },
590+ },
591+ {
592+ "valid grapheme cluster text in byte string" ,
593+ hexDecode ("583448656c6c6f2c2027e29da4efb88fe2808df09f94a5270ae4bda0e5a5bdefbc8c22f09fa791e2808df09fa49de2808df09fa79122" ),
594+ `'Hello, \'\u2764\ufe0f\u200d\ud83d\udd25\'\n\u4f60\u597d\uff0c"\ud83e\uddd1\u200d\ud83e\udd1d\u200d\ud83e\uddd1"'` ,
595+ & DiagOptions {
596+ ByteStringText : true ,
597+ },
598+ },
599+ {
600+ "valid grapheme cluster text in text string" ,
601+ hexDecode ("783448656c6c6f2c2027e29da4efb88fe2808df09f94a5270ae4bda0e5a5bdefbc8c22f09fa791e2808df09fa49de2808df09fa79122" ),
602+ `"Hello, '\u2764\ufe0f\u200d\ud83d\udd25'\n\u4f60\u597d\uff0c\"\ud83e\uddd1\u200d\ud83e\udd1d\u200d\ud83e\uddd1\""` , // "Hello, '❤️🔥'\n你好,\"🧑🤝🧑\""
603+ & DiagOptions {
604+ ByteStringText : true ,
605+ },
606+ },
607+ {
608+ "invalid grapheme cluster text in byte string" ,
609+ hexDecode ("583448656c6c6feeff27e29da4efb88fe2808df09f94a5270de4bda0e5a5bdefbc8c22f09fa791e2808df09fa49de2808df09fa79122" ),
610+ `h'48656c6c6feeff27e29da4efb88fe2808df09f94a5270de4bda0e5a5bdefbc8c22f09fa791e2808df09fa49de2808df09fa79122'` ,
611+ & DiagOptions {
612+ ByteStringText : true ,
613+ },
614+ },
615+ {
616+ "invalid grapheme cluster text in text string" ,
617+ hexDecode ("783448656c6c6feeff27e29da4efb88fe2808df09f94a5270de4bda0e5a5bdefbc8c22f09fa791e2808df09fa49de2808df09fa79122" ),
618+ `"Hello\u00ee\u00ff'\u2764\ufe0f\u200d\ud83d\udd25'\r\u4f60\u597d\uff0c\"\ud83e\uddd1\u200d\ud83e\udd1d\u200d\ud83e\uddd1\""` ,
619+ & DiagOptions {
620+ ByteStringText : true ,
621+ },
622+ },
623+ }
624+
625+ for _ , tc := range testCases {
626+ t .Run (tc .title , func (t * testing.T ) {
627+
628+ data , err := Diag (tc .cbor , tc .opts )
629+ if err != nil {
630+ t .Errorf ("Diag(0x%x) returned error %q" , tc .cbor , err )
631+ } else if string (data ) != tc .diag {
632+ t .Errorf ("Diag(0x%x) returned `%s`, want %s" , tc .cbor , string (data ), tc .diag )
633+ }
634+ })
635+ }
636+ }
637+
552638func TestDiagnoseFloatingPointNumber (t * testing.T ) {
553639 testCases := []struct {
554640 title string
0 commit comments