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