[Pkg-javascript-commits] [node-iconv-lite] 74/83: Added a test to compare iconv-lite with iconv for CP949

matthew pideil mpideil-guest at moszumanska.debian.org
Tue Apr 1 19:56:53 UTC 2014


This is an automated email from the git hooks/post-receive script.

mpideil-guest pushed a commit to branch master
in repository node-iconv-lite.

commit c625476e9bd01e7d64c4f89265f397dca40d3480
Author: Dan Gibson <dan at gandale.com>
Date:   Thu Aug 8 18:29:34 2013 +1000

    Added a test to compare iconv-lite with iconv for CP949
---
 encodings/cp949.js | 32 ++++++++++++------------
 test/cp949-test.js | 72 ++++++++++++++++++++++++++++++++++++++++++++----------
 2 files changed, 76 insertions(+), 28 deletions(-)

diff --git a/encodings/cp949.js b/encodings/cp949.js
index 3ec1c5d..fcfc80a 100644
--- a/encodings/cp949.js
+++ b/encodings/cp949.js
@@ -1,26 +1,28 @@
-var cp949Table = {};
-
 var fs = require('fs');
-var f = fs.readFileSync(require.resolve('./table/cp949.txt'), 'utf8');
-var i;
-var line;
-var lines = f.split('\n');
-var col;
-for (i=0; i<lines.length; i++)
-{
-	line = lines[i];
-	if (line.charAt(0) !== '#')
+
+function loadTable() {
+	var cp949Table = {};
+	var f = fs.readFileSync(require.resolve('./table/cp949.txt'), 'utf8');
+	var i;
+	var line;
+	var lines = f.split('\n');
+	var col;
+	for (i=0; i<lines.length; i++)
 	{
-		col = line.split('\t');
-		cp949Table[parseInt(col[0],16)] = parseInt(col[1],16);
-		//cp949Table[parseInt(col[0],16)] = 51; // 3
+		line = lines[i];
+		if ((line) && (line.charAt(0) !== '#'))
+		{
+			col = line.split('\t');
+			if (col[1].trim() !== "") cp949Table[parseInt(col[0],16)] = parseInt(col[1],16);
+		}
 	}
+	return cp949Table;
 }
 
 module.exports = {
 	'ks_c_56011987': 'cp949',
 	'cp949': {
 		type: 'table',
-		table: cp949Table
+		table: loadTable()
 	}
 }
diff --git a/test/cp949-test.js b/test/cp949-test.js
index 894beb0..f17766d 100644
--- a/test/cp949-test.js
+++ b/test/cp949-test.js
@@ -1,18 +1,64 @@
-var vows    = require('vows'),
-    assert  = require('assert'),
-    iconv   = require(__dirname+'/../');
+var vows    = require("vows"),
+	assert  = require("assert"),
+	fs      = require("fs"),
+	iconv   = require(__dirname+"/../");
 
 var testStringUtf = "\u00ba\u00b0\uc96fabc",
-    testStringCP949Buffer = new Buffer([0xa8,0xac, 0xa1,0xc6, 0xa2,0xa0, 0x61, 0x62, 0x63]);
+	testStringCP949Buffer = new Buffer([0xa8, 0xac, 0xa1,0xc6, 0xa2,0xa0, 0x61, 0x62, 0x63]);
+
+// Returns an object with a buffer (other) and a string (utf8) that contain every character
+// We use this string/buffer to compare iconv-lite with iconv
+function getBigText() {
+	var utf8 = "";
+	var other = [];
+	var f = fs.readFileSync(require.resolve("../encodings/table/cp949.txt"), "utf8");
+	var i;
+	var line;
+	var lines = f.split("\n");
+	var col;
+	var x;
+	for (i=0; i<lines.length; i++)
+	{
+		line = lines[i];
+		if ((line) && (line.charAt(0) !== "#"))
+		{
+			col = line.split("\t");
+			if (col[1].trim() !== "")
+			{
+				x = parseInt(col[0],16);
+				if (x > 127) other.push((x >> 8) & 0xff);
+				other.push(x & 0xff);
+	
+				x = parseInt(col[1],16);
+				utf8 += String.fromCharCode(x);
+			}
+		}
+	}
+	var p = {};
+	p.utf8 = utf8;
+	p.other = new Buffer(other);
+	return p;
+}
 
 vows.describe("CP949 tests").addBatch({
-    "Vows is working": function() {},
-    "CP949 correctly encoded/decoded": function() {    
-        assert.strictEqual(iconv.toEncoding(testStringUtf, "cp949").toString('binary'), testStringCP949Buffer.toString('binary'));
-        assert.strictEqual(iconv.fromEncoding(testStringCP949Buffer, "cp949"), testStringUtf);
-    },
-    "ks_c_5601-1987 correctly encoded/decoded": function() {    
-        assert.strictEqual(iconv.toEncoding(testStringUtf, "ks_c_5601-1987").toString('binary'), testStringCP949Buffer.toString('binary'));
-        assert.strictEqual(iconv.fromEncoding(testStringCP949Buffer, "ks_c_5601-1987"), testStringUtf);
-    },
+	"Vows is working": function() {},
+	"CP949 correctly encoded/decoded": function() {
+		assert.strictEqual(iconv.toEncoding(testStringUtf, "cp949").toString("binary"), testStringCP949Buffer.toString("binary"));
+		assert.strictEqual(iconv.fromEncoding(testStringCP949Buffer, "cp949"), testStringUtf);
+	},
+	"ks_c_5601-1987 correctly encoded/decoded": function() {
+		assert.strictEqual(iconv.toEncoding(testStringUtf, "ks_c_5601-1987").toString("binary"), testStringCP949Buffer.toString("binary"));
+		assert.strictEqual(iconv.fromEncoding(testStringCP949Buffer, "ks_c_5601-1987"), testStringUtf);
+	},
+
+	"compare with iconv result": function() {
+		var p = getBigText();
+
+		// compare iconv-lite with utf8
+		assert.strictEqual(iconv.fromEncoding(p.other, "cp949").toString(), p.utf8);
+
+		// compare iconv with utf8
+		var iconvc = new (require("iconv").Iconv)("cp949", "utf8");
+		assert.strictEqual(iconvc.convert(p.other).toString(), p.utf8);
+	},
 }).export(module)

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-iconv-lite.git



More information about the Pkg-javascript-commits mailing list