1import std/encodings
2
3var fromGBK = open("utf-8", "gbk")
4var toGBK = open("gbk", "utf-8")
5
6var fromGB2312 = open("utf-8", "gb2312")
7var toGB2312 = open("gb2312", "utf-8")
8
9
10block:
11  let data = "\215\237\186\243\178\187\214\170\204\236\212\218\203\174\163\172\194\250\180\178\208\199\195\206\209\185\208\199\186\211"
12  doAssert fromGBK.convert(data) == "醉后不知天在水,满床星梦压星河"
13
14block:
15  let data = "万两黄金容易得,知心一个也难求"
16  doAssert toGBK.convert(data) == "\205\242\193\189\187\198\189\240\200\221\210\215\181\195\163\172\214\170\208\196\210\187\184\246\210\178\196\209\199\243"
17
18
19block:
20  let data = "\215\212\208\197\200\203\201\250\182\254\176\217\196\234\163\172\187\225\181\177\203\174\187\247\200\253\199\167\192\239"
21  doAssert fromGB2312.convert(data) == "自信人生二百年,会当水击三千里"
22
23block:
24  let data = "谁怕?一蓑烟雨任平生"
25  doAssert toGB2312.convert(data) == "\203\173\197\194\163\191\210\187\203\242\209\204\211\234\200\206\198\189\201\250"
26
27
28when defined(windows):
29  block should_throw_on_unsupported_conversions:
30    let original = "some string"
31
32    doAssertRaises(EncodingError):
33      discard convert(original, "utf-8", "utf-32")
34
35    doAssertRaises(EncodingError):
36      discard convert(original, "utf-8", "unicodeFFFE")
37
38    doAssertRaises(EncodingError):
39      discard convert(original, "utf-8", "utf-32BE")
40
41    doAssertRaises(EncodingError):
42      discard convert(original, "unicodeFFFE", "utf-8")
43
44    doAssertRaises(EncodingError):
45      discard convert(original, "utf-32", "utf-8")
46
47    doAssertRaises(EncodingError):
48      discard convert(original, "utf-32BE", "utf-8")
49
50  block should_convert_from_utf16_to_utf8:
51    let original = "\x42\x04\x35\x04\x41\x04\x42\x04" # utf-16 little endian test string "тест"
52    let result = convert(original, "utf-8", "utf-16")
53    doAssert(result == "\xd1\x82\xd0\xb5\xd1\x81\xd1\x82")
54
55  block should_convert_from_utf16_to_win1251:
56    let original = "\x42\x04\x35\x04\x41\x04\x42\x04" # utf-16 little endian test string "тест"
57    let result = convert(original, "windows-1251", "utf-16")
58    doAssert(result == "\xf2\xe5\xf1\xf2")
59
60  block should_convert_from_win1251_to_koi8r:
61    let original = "\xf2\xe5\xf1\xf2" # win1251 test string "тест"
62    let result = convert(original, "koi8-r", "windows-1251")
63    doAssert(result == "\xd4\xc5\xd3\xd4")
64
65  block should_convert_from_koi8r_to_win1251:
66    let original = "\xd4\xc5\xd3\xd4" # koi8r test string "тест"
67    let result = convert(original, "windows-1251", "koi8-r")
68    doAssert(result == "\xf2\xe5\xf1\xf2")
69
70  block should_convert_from_utf8_to_win1251:
71    let original = "\xd1\x82\xd0\xb5\xd1\x81\xd1\x82" # utf-8 test string "тест"
72    let result = convert(original, "windows-1251", "utf-8")
73    doAssert(result == "\xf2\xe5\xf1\xf2")
74
75  block should_convert_from_utf8_to_utf16:
76    let original = "\xd1\x82\xd0\xb5\xd1\x81\xd1\x82" # utf-8 test string "тест"
77    let result = convert(original, "utf-16", "utf-8")
78    doAssert(result == "\x42\x04\x35\x04\x41\x04\x42\x04")
79
80  block should_handle_empty_string_for_any_conversion:
81    let original = ""
82    var result = convert(original, "utf-16", "utf-8")
83    doAssert(result == "")
84    result = convert(original, "utf-8", "utf-16")
85    doAssert(result == "")
86    result = convert(original, "windows-1251", "koi8-r")
87    doAssert(result == "")
88
89
90block:
91  let
92    orig = "öäüß"
93    cp1252 = convert(orig, "CP1252", "UTF-8")
94    ibm850 = convert(cp1252, "ibm850", "CP1252")
95    current = getCurrentEncoding()
96  doAssert orig == "\195\182\195\164\195\188\195\159"
97  doAssert ibm850 == "\148\132\129\225"
98  doAssert convert(ibm850, current, "ibm850") == orig
99