main-test.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. var vows = require('vows'),
  2. assert = require('assert'),
  3. iconv = require(__dirname+'/../');
  4. var testString = "Hello123!";
  5. var testStringLatin1 = "Hello123!£Å÷×çþÿ¿®";
  6. var testStringBase64 = "SGVsbG8xMjMh";
  7. vows.describe("Generic UTF8-UCS2 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. "Internal encodings all correctly encoded/decoded": function() {
  15. ['utf8', "UTF-8", "UCS2", "binary", ""].forEach(function(enc) {
  16. assert.strictEqual(iconv.toEncoding(testStringLatin1, enc).toString(enc), testStringLatin1);
  17. assert.strictEqual(iconv.fromEncoding(new Buffer(testStringLatin1, enc), enc), testStringLatin1);
  18. });
  19. },
  20. "Base64 correctly encoded/decoded": function() {
  21. assert.strictEqual(iconv.toEncoding(testStringBase64, "base64").toString("binary"), testString);
  22. assert.strictEqual(iconv.fromEncoding(new Buffer(testString, "binary"), "base64"), testStringBase64);
  23. },
  24. "Latin1 correctly encoded/decoded": function() {
  25. assert.strictEqual(iconv.toEncoding(testStringLatin1, "latin1").toString("binary"), testStringLatin1);
  26. assert.strictEqual(iconv.fromEncoding(new Buffer(testStringLatin1, "binary"), "latin1"), testStringLatin1);
  27. },
  28. "Convert from string, not buffer (binary encoding used)": function() {
  29. assert.strictEqual(iconv.fromEncoding(testStringLatin1, "binary"), testStringLatin1);
  30. },
  31. "Convert to string, not buffer (utf8 used)": function() {
  32. var res = iconv.toEncoding(new Buffer(testStringLatin1, "utf8"));
  33. assert.ok(res instanceof Buffer);
  34. assert.strictEqual(res.toString("utf8"), testStringLatin1);
  35. },
  36. "Throws on unknown encodings": function() {
  37. assert.throws(function() { iconv.toEncoding("a", "xxx"); });
  38. assert.throws(function() { iconv.fromEncoding("a", "xxx"); });
  39. },
  40. "Convert non-strings and non-buffers": function() {
  41. assert.strictEqual(iconv.toEncoding({}, "utf8").toString(), "[object Object]");
  42. assert.strictEqual(iconv.toEncoding(10, "utf8").toString(), "10");
  43. assert.strictEqual(iconv.toEncoding(undefined, "utf8").toString(), "");
  44. assert.strictEqual(iconv.fromEncoding({}, "utf8"), "[object Object]");
  45. assert.strictEqual(iconv.fromEncoding(10, "utf8"), "10");
  46. assert.strictEqual(iconv.fromEncoding(undefined, "utf8"), "");
  47. },
  48. "Aliases encode and decode work the same as toEncoding and fromEncoding": function() {
  49. assert.strictEqual(iconv.toEncoding(testString, "latin1").toString("binary"), iconv.encode(testString, "latin1").toString("binary"));
  50. assert.strictEqual(iconv.fromEncoding(testStringLatin1, "latin1"), iconv.decode(testStringLatin1, "latin1"));
  51. },
  52. }).export(module)