gbk-test.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. var vows = require('vows'),
  2. fs = require('fs'),
  3. assert = require('assert'),
  4. iconv = require(__dirname+'/../');
  5. var testString = "中国abc",//unicode contains GBK-code and ascii
  6. testStringGBKBuffer = new Buffer([0xd6,0xd0,0xb9,0xfa,0x61,0x62,0x63]);
  7. vows.describe("GBK tests").addBatch({
  8. "Vows is working": function() {},
  9. "Return values are of correct types": function() {
  10. assert.ok(iconv.toEncoding(testString, "utf8") instanceof Buffer);
  11. var s = iconv.fromEncoding(new Buffer(testString), "utf8");
  12. assert.strictEqual(Object.prototype.toString.call(s), "[object String]");
  13. },
  14. "GBK correctly encoded/decoded": function() {
  15. assert.strictEqual(iconv.toEncoding(testString, "GBK").toString('binary'), testStringGBKBuffer.toString('binary'));
  16. assert.strictEqual(iconv.fromEncoding(testStringGBKBuffer, "GBK"), testString);
  17. },
  18. "GB2312 correctly encoded/decoded": function() {
  19. assert.strictEqual(iconv.toEncoding(testString, "GB2312").toString('binary'), testStringGBKBuffer.toString('binary'));
  20. assert.strictEqual(iconv.fromEncoding(testStringGBKBuffer, "GB2312"), testString);
  21. },
  22. "GBK file read decoded,compare with iconv result": function() {
  23. var contentBuffer = fs.readFileSync(__dirname+"/gbkFile.txt");
  24. var str = iconv.fromEncoding(contentBuffer, "GBK");
  25. var iconvc = new (require('iconv').Iconv)('GBK','utf8');
  26. assert.strictEqual(iconvc.convert(contentBuffer).toString(), str);
  27. },
  28. "GBK correctly decodes and encodes characters · and ×": function() {
  29. // https://github.com/ashtuchkin/iconv-lite/issues/13
  30. // Reference: http://www.unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP936.TXT
  31. var chars = "·×";
  32. var gbkChars = new Buffer([0xA1, 0xA4, 0xA1, 0xC1]);
  33. assert.strictEqual(iconv.toEncoding(chars, "GBK").toString('binary'), gbkChars.toString('binary'));
  34. assert.strictEqual(iconv.fromEncoding(gbkChars, "GBK"), chars)
  35. },
  36. }).export(module)