intl/uconv/tests/unit/test_iso2022KR.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/intl/uconv/tests/unit/test_iso2022KR.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,71 @@
     1.4 +/* Test case for ISO-2022-KR
     1.5 + *
     1.6 + * Uses nsIConverterInputStream to decode ISO-2022-KR text.
     1.7 + *
     1.8 + * Sample text is: 
     1.9 + * 1: 소 잃고 외양간 고친다
    1.10 + * 2: 빈 수레가 요란하다
    1.11 + * 3: 하늘의 별 따기
    1.12 + * 4: 아는 길도 물어가라
    1.13 + *
    1.14 + * (From http://en.wikiquote.org/wiki/Korean_proverbs)
    1.15 + */
    1.16 +
    1.17 +const sample = "%1B%24%29C1%3A%20%0E%3CR%0F%20%0E%40R0m%0F%20%0E%3F%5C%3Eg0%23%0F%20%0E0mD%234Y%0A2%3A%20%0E%3As%0F%20%0E%3Cv790%21%0F%20%0E%3Fd6uGO4Y%0A3%3A%20%0EGO4C%40G%0F%20%0E%3A0%0F%20%0E5%7B1b%0A4%3A%20%0E%3EF4B%0F%20%0E1f55%0F%20%0E90%3En0%216s";
    1.18 +
    1.19 +const expected = "1: \uC18C \uC783\uACE0 \uC678\uC591\uAC04 \uACE0\uCE5C\uB2E4\n2: \uBE48 \uC218\uB808\uAC00 \uC694\uB780\uD558\uB2E4\n3: \uD558\uB298\uC758 \uBCC4 \uB530\uAE30\n4: \uC544\uB294 \uAE38\uB3C4 \uBB3C\uC5B4\uAC00\uB77C\n"; 
    1.20 +
    1.21 +const charset="ISO-2022-KR";
    1.22 +
    1.23 +function testCase(bufferLength)
    1.24 +{
    1.25 +  var dataURI = "data:text/plain;charset=" + charset + "," + sample;
    1.26 +
    1.27 +  var IOService = Components.Constructor("@mozilla.org/network/io-service;1",
    1.28 +					 "nsIIOService");
    1.29 +  var ConverterInputStream =
    1.30 +      Components.Constructor("@mozilla.org/intl/converter-input-stream;1",
    1.31 +			     "nsIConverterInputStream",
    1.32 +			     "init");
    1.33 +
    1.34 +  var ios = new IOService();
    1.35 +  var channel = ios.newChannel(dataURI, "", null);
    1.36 +  var testInputStream = channel.open();
    1.37 +  var testConverter = new ConverterInputStream(testInputStream,
    1.38 +					       charset,
    1.39 +					       bufferLength,
    1.40 +					       0xFFFD);
    1.41 +
    1.42 +  if (!(testConverter instanceof
    1.43 +	Components.interfaces.nsIUnicharLineInputStream))
    1.44 +      throw "not line input stream";
    1.45 +
    1.46 +  var outStr = "";
    1.47 +  var more;
    1.48 +  do {
    1.49 +      // read the line and check for eof
    1.50 +      var line = {};
    1.51 +      more = testConverter.readLine(line);
    1.52 +      outStr += line.value + "\n";
    1.53 +  } while (more);
    1.54 +
    1.55 +  if (outStr != expected) {
    1.56 +    dump("Failed with bufferLength = " + bufferLength + "\n");
    1.57 +    if (outStr.length == expected.length) {
    1.58 +      for (i = 0; i < outStr.length; ++i) {
    1.59 +	if (outStr.charCodeAt(i) != expected.charCodeAt(i)) {
    1.60 +	  dump(i + ": " + outStr.charCodeAt(i).toString(16) + " != " + expected.charCodeAt(i).toString(16) + "\n");
    1.61 +	}
    1.62 +      }
    1.63 +    }
    1.64 +  }
    1.65 +
    1.66 +  // escape the strings before comparing for better readability
    1.67 +  do_check_eq(escape(outStr), escape(expected));
    1.68 +}
    1.69 +
    1.70 +function run_test()
    1.71 +{
    1.72 +  testCase(34);
    1.73 +  testCase(35);
    1.74 +}

mercurial