github.com/wangyougui/gf/v2@v2.6.5/encoding/gcharset/gcharset_z_unit_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/wangyougui/gf. 6 7 package gcharset_test 8 9 import ( 10 "testing" 11 12 "github.com/wangyougui/gf/v2/encoding/gcharset" 13 "github.com/wangyougui/gf/v2/test/gtest" 14 ) 15 16 var testData = []struct { 17 utf8, other, otherEncoding string 18 }{ 19 {"Résumé", "Résumé", "utf-8"}, 20 //{"Résumé", "R\xe9sum\xe9", "latin-1"}, 21 {"これは漢字です。", "S0\x8c0o0\"oW[g0Y0\x020", "UTF-16LE"}, 22 {"これは漢字です。", "0S0\x8c0oo\"[W0g0Y0\x02", "UTF-16BE"}, 23 {"これは漢字です。", "\xfe\xff0S0\x8c0oo\"[W0g0Y0\x02", "UTF-16"}, 24 {"𝄢𝄞𝄪𝄫", "\xfe\xff\xd8\x34\xdd\x22\xd8\x34\xdd\x1e\xd8\x34\xdd\x2a\xd8\x34\xdd\x2b", "UTF-16"}, 25 //{"Hello, world", "Hello, world", "ASCII"}, 26 {"Gdańsk", "Gda\xf1sk", "ISO-8859-2"}, 27 {"Ââ Čč Đđ Ŋŋ Õõ Šš Žž Åå Ää", "\xc2\xe2 \xc8\xe8 \xa9\xb9 \xaf\xbf \xd5\xf5 \xaa\xba \xac\xbc \xc5\xe5 \xc4\xe4", "ISO-8859-10"}, 28 //{"สำหรับ", "\xca\xd3\xcb\xc3\u047a", "ISO-8859-11"}, 29 {"latviešu", "latvie\xf0u", "ISO-8859-13"}, 30 {"Seònaid", "Se\xf2naid", "ISO-8859-14"}, 31 {"€1 is cheap", "\xa41 is cheap", "ISO-8859-15"}, 32 {"românește", "rom\xe2ne\xbate", "ISO-8859-16"}, 33 {"nutraĵo", "nutra\xbco", "ISO-8859-3"}, 34 {"Kalâdlit", "Kal\xe2dlit", "ISO-8859-4"}, 35 {"русский", "\xe0\xe3\xe1\xe1\xda\xd8\xd9", "ISO-8859-5"}, 36 {"ελληνικά", "\xe5\xeb\xeb\xe7\xed\xe9\xea\xdc", "ISO-8859-7"}, 37 {"Kağan", "Ka\xf0an", "ISO-8859-9"}, 38 {"Résumé", "R\x8esum\x8e", "macintosh"}, 39 {"Gdańsk", "Gda\xf1sk", "windows-1250"}, 40 {"русский", "\xf0\xf3\xf1\xf1\xea\xe8\xe9", "windows-1251"}, 41 {"Résumé", "R\xe9sum\xe9", "windows-1252"}, 42 {"ελληνικά", "\xe5\xeb\xeb\xe7\xed\xe9\xea\xdc", "windows-1253"}, 43 {"Kağan", "Ka\xf0an", "windows-1254"}, 44 {"עִבְרִית", "\xf2\xc4\xe1\xc0\xf8\xc4\xe9\xfa", "windows-1255"}, 45 {"العربية", "\xc7\xe1\xda\xd1\xc8\xed\xc9", "windows-1256"}, 46 {"latviešu", "latvie\xf0u", "windows-1257"}, 47 {"Việt", "Vi\xea\xf2t", "windows-1258"}, 48 {"สำหรับ", "\xca\xd3\xcb\xc3\u047a", "windows-874"}, 49 {"русский", "\xd2\xd5\xd3\xd3\xcb\xc9\xca", "KOI8-R"}, 50 {"українська", "\xd5\xcb\xd2\xc1\xa7\xce\xd3\xd8\xcb\xc1", "KOI8-U"}, 51 {"Hello 常用國字標準字體表", "Hello \xb1`\xa5\u03b0\xea\xa6r\xbc\u0437\u01e6r\xc5\xe9\xaa\xed", "big5"}, 52 {"Hello 常用國字標準字體表", "Hello \xb3\xa3\xd3\xc3\x87\xf8\xd7\xd6\x98\xcb\x9c\xca\xd7\xd6\xf3\x77\xb1\xed", "gbk"}, 53 {"Hello 常用國字標準字體表", "Hello \xb3\xa3\xd3\xc3\x87\xf8\xd7\xd6\x98\xcb\x9c\xca\xd7\xd6\xf3\x77\xb1\xed", "gb18030"}, 54 {"花间一壶酒,独酌无相亲。", "~{;(<dR;:x>F#,6@WCN^O`GW!#", "GB2312"}, 55 {"花间一壶酒,独酌无相亲。", "~{;(<dR;:x>F#,6@WCN^O`GW!#", "HZGB2312"}, 56 {"עִבְרִית", "\x81\x30\xfb\x30\x81\x30\xf6\x34\x81\x30\xf9\x33\x81\x30\xf6\x30\x81\x30\xfb\x36\x81\x30\xf6\x34\x81\x30\xfa\x31\x81\x30\xfb\x38", "gb18030"}, 57 {"㧯", "\x82\x31\x89\x38", "gb18030"}, 58 {"㧯", "㧯", "UTF-8"}, 59 //{"これは漢字です。", "\x82\xb1\x82\xea\x82\xcd\x8a\xbf\x8e\x9a\x82\xc5\x82\xb7\x81B", "SJIS"}, 60 {"これは漢字です。", "\xa4\xb3\xa4\xec\xa4\u03f4\xc1\xbb\xfa\xa4\u01e4\xb9\xa1\xa3", "EUC-JP"}, 61 } 62 63 func TestDecode(t *testing.T) { 64 for _, data := range testData { 65 str := "" 66 str, err := gcharset.Convert("UTF-8", data.otherEncoding, data.other) 67 if err != nil { 68 t.Errorf("Could not create decoder for %v", err) 69 continue 70 } 71 72 if str != data.utf8 { 73 t.Errorf("Unexpected value: %#v (expected %#v) %v", str, data.utf8, data.otherEncoding) 74 } 75 } 76 } 77 78 func TestUTF8To(t *testing.T) { 79 for _, data := range testData { 80 str := "" 81 str, err := gcharset.UTF8To(data.otherEncoding, data.utf8) 82 if err != nil { 83 t.Errorf("Could not create decoder for %v", err) 84 continue 85 } 86 87 if str != data.other { 88 t.Errorf("Unexpected value: %#v (expected %#v) %v", str, data.other, data.otherEncoding) 89 } 90 } 91 } 92 93 func TestToUTF8(t *testing.T) { 94 for _, data := range testData { 95 str := "" 96 str, err := gcharset.ToUTF8(data.otherEncoding, data.other) 97 if err != nil { 98 t.Errorf("Could not create decoder for %v", err) 99 continue 100 } 101 102 if str != data.utf8 { 103 t.Errorf("Unexpected value: %#v (expected %#v)", str, data.utf8) 104 } 105 } 106 } 107 108 func TestEncode(t *testing.T) { 109 for _, data := range testData { 110 str := "" 111 str, err := gcharset.Convert(data.otherEncoding, "UTF-8", data.utf8) 112 if err != nil { 113 t.Errorf("Could not create decoder for %v", err) 114 continue 115 } 116 117 if str != data.other { 118 t.Errorf("Unexpected value: %#v (expected %#v)", str, data.other) 119 } 120 } 121 } 122 123 func TestConvert(t *testing.T) { 124 srcCharset := "big5" 125 src := "Hello \xb1`\xa5\u03b0\xea\xa6r\xbc\u0437\u01e6r\xc5\xe9\xaa\xed" 126 dstCharset := "gbk" 127 dst := "Hello \xb3\xa3\xd3\xc3\x87\xf8\xd7\xd6\x98\xcb\x9c\xca\xd7\xd6\xf3\x77\xb1\xed" 128 129 str, err := gcharset.Convert(dstCharset, srcCharset, src) 130 if err != nil { 131 t.Errorf("convert error. %v", err) 132 return 133 } 134 135 if str != dst { 136 t.Errorf("unexpected value:%#v (expected %#v)", str, dst) 137 } 138 } 139 140 func TestConvertErr(t *testing.T) { 141 gtest.C(t, func(t *gtest.T) { 142 srcCharset := "big5" 143 dstCharset := "gbk" 144 src := "Hello \xb1`\xa5\u03b0\xea\xa6r\xbc\u0437\u01e6r\xc5\xe9\xaa\xed" 145 146 s1, e1 := gcharset.Convert(srcCharset, srcCharset, src) 147 t.Assert(e1, nil) 148 t.Assert(s1, src) 149 150 s2, e2 := gcharset.Convert(dstCharset, "no this charset", src) 151 t.AssertNE(e2, nil) 152 t.Assert(s2, src) 153 154 s3, e3 := gcharset.Convert("no this charset", srcCharset, src) 155 t.AssertNE(e3, nil) 156 t.Assert(s3, src) 157 }) 158 } 159 160 func TestSupported(t *testing.T) { 161 gtest.C(t, func(t *gtest.T) { 162 t.Assert(gcharset.Supported("UTF-8"), true) 163 t.Assert(gcharset.Supported("UTF-80"), false) 164 }) 165 }