big5-test.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. var vows = require('vows'),
  2. fs = require('fs'),
  3. assert = require('assert'),
  4. iconv = require(__dirname + '/../');
  5. var testString = "中文abc", //unicode contains Big5-code and ascii
  6. testStringBig5Buffer = new Buffer([0xa4,0xa4,0xa4,0xe5,0x61,0x62,0x63]),
  7. testString2 = '測試',
  8. testStringBig5Buffer2 = new Buffer([0xb4, 0xfa, 0xb8, 0xd5]);
  9. vows.describe("Big5 tests").addBatch({
  10. "Big5 correctly encoded/decoded": function() {
  11. assert.strictEqual(iconv.toEncoding(testString, "big5").toString('binary'), testStringBig5Buffer.toString('binary'));
  12. assert.strictEqual(iconv.fromEncoding(testStringBig5Buffer, "big5"), testString);
  13. assert.strictEqual(iconv.toEncoding(testString2, 'big5').toString('binary'), testStringBig5Buffer2.toString('binary'));
  14. assert.strictEqual(iconv.fromEncoding(testStringBig5Buffer2, 'big5'), testString2);
  15. },
  16. "cp950 correctly encoded/decoded": function() {
  17. assert.strictEqual(iconv.toEncoding(testString, "cp950").toString('binary'), testStringBig5Buffer.toString('binary'));
  18. assert.strictEqual(iconv.fromEncoding(testStringBig5Buffer, "cp950"), testString);
  19. },
  20. "Big5 file read decoded,compare with iconv result": function() {
  21. var contentBuffer = fs.readFileSync(__dirname+"/big5File.txt");
  22. var str = iconv.fromEncoding(contentBuffer, "big5");
  23. var iconvc = new (require('iconv').Iconv)('big5','utf8');
  24. assert.strictEqual(iconvc.convert(contentBuffer).toString(), str);
  25. },
  26. "Big5 correctly decodes and encodes characters · and ×": function() {
  27. // https://github.com/ashtuchkin/iconv-lite/issues/13
  28. // Reference: http://www.unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP950.TXT
  29. var chars = "·×";
  30. var big5Chars = new Buffer([0xA1, 0x50, 0xA1, 0xD1]);
  31. assert.strictEqual(iconv.toEncoding(chars, "big5").toString('binary'), big5Chars.toString('binary'));
  32. assert.strictEqual(iconv.fromEncoding(big5Chars, "big5"), chars)
  33. },
  34. }).export(module)