cyrillic-test.js 5.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. var vows = require('vows'),
  2. assert = require('assert'),
  3. iconv = require(__dirname+'/../');
  4. var baseStrings = {
  5. empty: "",
  6. hi: "Привет!",
  7. ascii: '\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f'+
  8. ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7f',
  9. rus: "АБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдежзийклмнопрстуфхцчшщъыьэюя",
  10. additional1: "ЂЃ‚ѓ„…†‡€‰Љ‹ЊЌЋЏђ‘’“”•–—™љ›њќћџ ЎўЈ¤Ґ¦§Ё©Є«¬\xAD®Ї°±Ііґµ¶·ё№є»јЅѕї",
  11. additional2: "─│┌┐└┘├┤┬┴┼▀▄█▌▐░▒▓⌠■∙√≈≤≥ ⌡°²·÷═║╒ё╓╔╕╖╗╘╙╚╛╜╝╞╟╠╡Ё╢╣╤╥╦╧╨╩╪╫╬©",
  12. additional3: " ЁЂЃЄЅІЇЈЉЊЋЌ­ЎЏ№ёђѓєѕіїјљњћќ§ўџ",
  13. untranslatable: "£Åçþÿ¿",
  14. };
  15. var encodings = [{
  16. name: "Win-1251",
  17. variations: ['win1251', 'Windows-1251', 'windows1251', 'CP1251', 1251],
  18. encodedStrings: {
  19. empty: new Buffer(''),
  20. hi: new Buffer('\xcf\xf0\xe8\xe2\xe5\xf2!', 'binary'),
  21. ascii: new Buffer(baseStrings.ascii, 'binary'),
  22. rus: new Buffer('\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff', 'binary'),
  23. additional1: new Buffer('\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf','binary'),
  24. }
  25. }, {
  26. name: "Koi8-R",
  27. variations: ['koi8r', 'KOI8-R', 'cp20866', 20866],
  28. encodedStrings: {
  29. empty: new Buffer(''),
  30. hi: new Buffer('\xf0\xd2\xc9\xd7\xc5\xd4!', 'binary'),
  31. ascii: new Buffer(baseStrings.ascii, 'binary'),
  32. rus: new Buffer('\xe1\xe2\xf7\xe7\xe4\xe5\xf6\xfa\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf2\xf3\xf4\xf5\xe6\xe8\xe3\xfe\xfb\xfd\xff\xf9\xf8\xfc\xe0\xf1\xc1\xc2\xd7\xc7\xc4\xc5\xd6\xda\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd2\xd3\xd4\xd5\xc6\xc8\xc3\xde\xdb\xdd\xdf\xd9\xd8\xdc\xc0\xd1', 'binary'),
  33. additional2: new Buffer('\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf', 'binary'),
  34. }
  35. }, {
  36. name: "ISO 8859-5",
  37. variations: ['iso88595', 'ISO-8859-5', 'ISO 8859-5', 'cp28595', 28595],
  38. encodedStrings: {
  39. empty: new Buffer(''),
  40. hi: new Buffer('\xbf\xe0\xd8\xd2\xd5\xe2!', 'binary'),
  41. ascii: new Buffer(baseStrings.ascii, 'binary'),
  42. rus: new Buffer('\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef', 'binary'),
  43. additional3: new Buffer('\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff', 'binary'),
  44. }
  45. }];
  46. var testsBatch = {};
  47. encodings.forEach(function(encoding) {
  48. var enc = encoding.variations[0];
  49. var key = "hi";
  50. var tests = {
  51. "Convert to empty buffer": function() {
  52. assert.strictEqual(iconv.toEncoding("", enc).toString('binary'), new Buffer('').toString('binary'));
  53. },
  54. "Convert from empty buffer": function() {
  55. assert.strictEqual(iconv.fromEncoding(new Buffer(''), enc), "");
  56. },
  57. "Convert from buffer": function() {
  58. for (var key in encoding.encodedStrings)
  59. assert.strictEqual(iconv.fromEncoding(encoding.encodedStrings[key], enc),
  60. baseStrings[key]);
  61. },
  62. "Convert to buffer": function() {
  63. for (var key in encoding.encodedStrings)
  64. assert.strictEqual(iconv.toEncoding(baseStrings[key], enc).toString('binary'),
  65. encoding.encodedStrings[key].toString('binary'));
  66. },
  67. "Try different variations of encoding": function() {
  68. encoding.variations.forEach(function(enc) {
  69. assert.strictEqual(iconv.fromEncoding(encoding.encodedStrings[key], enc), baseStrings[key]);
  70. assert.strictEqual(iconv.toEncoding(baseStrings[key], enc).toString('binary'), encoding.encodedStrings[key].toString('binary'));
  71. });
  72. },
  73. "Untranslatable chars are converted to defaultCharSingleByte": function() {
  74. var expected = baseStrings.untranslatable.split('').map(function(c) {return iconv.defaultCharSingleByte; }).join('');
  75. assert.strictEqual(iconv.toEncoding(baseStrings.untranslatable, enc).toString('binary'), expected); // Only '?' characters.
  76. }
  77. };
  78. testsBatch[encoding.name+":"] = tests;
  79. });
  80. vows.describe("Test Cyrillic encodings").addBatch(testsBatch).export(module);