-
Notifications
You must be signed in to change notification settings - Fork 283
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
673 additions
and
628 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
"use strict"; | ||
|
||
const Iconv = require("iconv").Iconv, | ||
fs = require("fs"), | ||
path = require("path"), | ||
utils = require("../test/utils"); | ||
|
||
const fixtures = { | ||
big5: big5(), | ||
gbk: gbk(), | ||
}; | ||
const outputFile = path.resolve(__dirname, "..", "test", "fixtures", "gbk-big5.json"); | ||
fs.writeFileSync(outputFile, JSON.stringify(fixtures)); | ||
|
||
function gbk() { | ||
const inputFile = path.resolve(__dirname, "fixtures", "gbkFile.txt"); | ||
const contentBuffer = fs.readFileSync(inputFile); | ||
|
||
const codec = Iconv("GBK", "utf8"); | ||
const str = codec.convert(contentBuffer).toString(); | ||
|
||
return { | ||
bytes: utils.hex(contentBuffer, true), | ||
string: str, | ||
}; | ||
} | ||
|
||
function big5() { | ||
const contentBuffer = Buffer.from( | ||
"PEhUTUw+DQo8SEVBRD4gICAgDQoJPFRJVExFPiBtZXRhILzQxdKquqjPpc6hR6SkpOW69K22IDwvVElUTEU+DQoJPG1ldGEgSFRUUC1FUVVJVj0iQ29udGVudC1UeXBlIiBDT05URU5UPSJ0ZXh0L2h0bWw7IGNoYXJzZXQ9YmlnNSI+DQo8L0hFQUQ+DQo8Qk9EWT4NCg0Ks2+sT6RArdPBY8XppKSk5br0rbahSTxicj4NCihUaGlzIHBhZ2UgdXNlcyBiaWc1IGNoYXJhY3RlciBzZXQuKTxicj4NCmNoYXJzZXQ9YmlnNQ0KDQo8L0JPRFk+DQo8L0hUTUw+", | ||
"base64" | ||
); | ||
|
||
const codec = Iconv("big5", "utf8"); | ||
const str = codec.convert(contentBuffer).toString(); | ||
|
||
return { | ||
bytes: utils.hex(contentBuffer, true), | ||
string: str, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,68 @@ | ||
"use strict"; | ||
|
||
var assert = require("assert"), | ||
Buffer = require("safer-buffer").Buffer, | ||
iconv = require("../"); | ||
const assert = require("assert"), | ||
utils = require("./utils"), | ||
fixtures = require("./fixtures/gbk-big5.json"), | ||
iconv = utils.requireIconv(); | ||
|
||
var testString = "中文abc", //unicode contains Big5-code and ascii | ||
testStringBig5Buffer = Buffer.from([0xa4, 0xa4, 0xa4, 0xe5, 0x61, 0x62, 0x63]), | ||
const testString = "中文abc", //unicode contains Big5-code and ascii | ||
testStringBig5Buffer = utils.bytes("a4 a4 a4 e5 61 62 63"), | ||
testString2 = "測試", | ||
testStringBig5Buffer2 = Buffer.from([0xb4, 0xfa, 0xb8, 0xd5]); | ||
testStringBig5Buffer2 = utils.bytes("b4 fa b8 d5"); | ||
|
||
describe("Big5 tests", function () { | ||
describe("Big5 tests #node-web", function () { | ||
it("Big5 correctly encoded/decoded", function () { | ||
assert.strictEqual( | ||
iconv.encode(testString, "big5").toString("hex"), | ||
testStringBig5Buffer.toString("hex") | ||
utils.hex(iconv.encode(testString, "big5")), | ||
utils.hex(testStringBig5Buffer) | ||
); | ||
assert.strictEqual(iconv.decode(testStringBig5Buffer, "big5"), testString); | ||
assert.strictEqual( | ||
iconv.encode(testString2, "big5").toString("hex"), | ||
testStringBig5Buffer2.toString("hex") | ||
utils.hex(iconv.encode(testString2, "big5")), | ||
utils.hex(testStringBig5Buffer2) | ||
); | ||
assert.strictEqual(iconv.decode(testStringBig5Buffer2, "big5"), testString2); | ||
}); | ||
|
||
it("cp950 correctly encoded/decoded", function () { | ||
assert.strictEqual( | ||
iconv.encode(testString, "cp950").toString("hex"), | ||
testStringBig5Buffer.toString("hex") | ||
utils.hex(iconv.encode(testString, "cp950")), | ||
utils.hex(testStringBig5Buffer) | ||
); | ||
assert.strictEqual(iconv.decode(testStringBig5Buffer, "cp950"), testString); | ||
}); | ||
|
||
it("Big5 file read decoded,compare with iconv result", function () { | ||
var contentBuffer = Buffer.from( | ||
"PEhUTUw+DQo8SEVBRD4gICAgDQoJPFRJVExFPiBtZXRhILzQxdKquqjPpc6hR6SkpOW69K22IDwvVElUTEU+DQoJPG1ldGEgSFRUUC1FUVVJVj0iQ29udGVudC1UeXBlIiBDT05URU5UPSJ0ZXh0L2h0bWw7IGNoYXJzZXQ9YmlnNSI+DQo8L0hFQUQ+DQo8Qk9EWT4NCg0Ks2+sT6RArdPBY8XppKSk5br0rbahSTxicj4NCihUaGlzIHBhZ2UgdXNlcyBiaWc1IGNoYXJhY3RlciBzZXQuKTxicj4NCmNoYXJzZXQ9YmlnNQ0KDQo8L0JPRFk+DQo8L0hUTUw+", | ||
"base64" | ||
); | ||
var str = iconv.decode(contentBuffer, "big5"); | ||
var iconvc = new (require("iconv").Iconv)("big5", "utf8"); | ||
assert.strictEqual(iconvc.convert(contentBuffer).toString(), str); | ||
const contentBuffer = utils.bytes(fixtures.big5.bytes); | ||
const str = iconv.decode(contentBuffer, "big5"); | ||
assert.strictEqual(fixtures.big5.string, str); | ||
}); | ||
|
||
it("Big5 correctly decodes and encodes characters · and ×", function () { | ||
// https://github.com/ashtuchkin/iconv-lite/issues/13 | ||
// Reference: http://www.unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP950.TXT | ||
var chars = "·×"; | ||
var big5Chars = Buffer.from([0xa1, 0x50, 0xa1, 0xd1]); | ||
assert.strictEqual(iconv.encode(chars, "big5").toString("hex"), big5Chars.toString("hex")); | ||
const chars = "·×"; | ||
const big5Chars = utils.bytes("a1 50 a1 d1"); | ||
assert.strictEqual(utils.hex(iconv.encode(chars, "big5")), utils.hex(big5Chars)); | ||
assert.strictEqual(iconv.decode(big5Chars, "big5"), chars); | ||
}); | ||
|
||
it("Big5 correctly encodes & decodes sequences", function () { | ||
assert.strictEqual(iconv.encode("\u00CA\u0304", "big5").toString("hex"), "8862"); | ||
assert.strictEqual(iconv.encode("\u00EA\u030C", "big5").toString("hex"), "88a5"); | ||
assert.strictEqual(iconv.encode("\u00CA", "big5").toString("hex"), "8866"); | ||
assert.strictEqual(iconv.encode("\u00CA\u00CA", "big5").toString("hex"), "88668866"); | ||
assert.strictEqual(utils.hex(iconv.encode("\u00CA\u0304", "big5")), "88 62"); | ||
assert.strictEqual(utils.hex(iconv.encode("\u00EA\u030C", "big5")), "88 a5"); | ||
assert.strictEqual(utils.hex(iconv.encode("\u00CA", "big5")), "88 66"); | ||
assert.strictEqual(utils.hex(iconv.encode("\u00CA\u00CA", "big5")), "88 66 88 66"); | ||
|
||
assert.strictEqual(iconv.encode("\u00CA\uD800", "big5").toString("hex"), "88663f"); // Unfinished surrogate. | ||
assert.strictEqual(iconv.encode("\u00CA\uD841\uDD47", "big5").toString("hex"), "8866fa40"); // Finished surrogate ('𠕇'). | ||
assert.strictEqual(iconv.encode("\u00CA𠕇", "big5").toString("hex"), "8866fa40"); // Finished surrogate ('𠕇'). | ||
assert.strictEqual(utils.hex(iconv.encode("\u00CA\uD800", "big5")), "88 66 3f"); // Unfinished surrogate. | ||
assert.strictEqual(utils.hex(iconv.encode("\u00CA\uD841\uDD47", "big5")), "88 66 fa 40"); // Finished surrogate ('𠕇'). | ||
assert.strictEqual(utils.hex(iconv.encode("\u00CA𠕇", "big5")), "88 66 fa 40"); // Finished surrogate ('𠕇'). | ||
|
||
assert.strictEqual(iconv.decode(Buffer.from("8862", "hex"), "big5"), "\u00CA\u0304"); | ||
assert.strictEqual(iconv.decode(Buffer.from("8866", "hex"), "big5"), "\u00CA"); | ||
assert.strictEqual(iconv.decode(Buffer.from("8866fa40", "hex"), "big5"), "\u00CA𠕇"); | ||
assert.strictEqual(iconv.decode(utils.bytes("88 62"), "big5"), "\u00CA\u0304"); | ||
assert.strictEqual(iconv.decode(utils.bytes("88 66"), "big5"), "\u00CA"); | ||
assert.strictEqual(iconv.decode(utils.bytes("88 66 fa 40"), "big5"), "\u00CA𠕇"); | ||
}); | ||
|
||
it("Big5 correctly encodes 十", function () { | ||
assert.strictEqual(iconv.encode("十", "big5").toString("hex"), "a451"); | ||
assert.strictEqual(utils.hex(iconv.encode("十", "big5")), "a4 51"); | ||
}); | ||
}); |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.