summaryrefslogtreecommitdiffstats
path: root/intl/uconv/tests/unit/head_charsetConversionTests.js
blob: 4fbf6e9af7250089fc85f352acc4536ebdfba273 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
var CC = Components.Constructor;

function CreateScriptableConverter() {
  var ScriptableUnicodeConverter = CC(
    "@mozilla.org/intl/scriptableunicodeconverter",
    "nsIScriptableUnicodeConverter"
  );

  return new ScriptableUnicodeConverter();
}

function checkDecode(converter, charset, inText, expectedText) {
  try {
    converter.charset = charset;
  } catch (e) {
    converter.charset = "iso-8859-1";
  }

  dump("testing decoding from " + charset + " to Unicode.\n");
  try {
    var outText = converter.ConvertToUnicode(inText);
  } catch (e) {
    outText = "\ufffd";
  }

  if (outText != expectedText) {
    for (var i = 0; i < inText.length; ++i) {
      var inn = inText[i];
      var out = outText[i];
      var expected = expectedText[i];
      if (out != expected) {
        dump(
          "Decoding error at position " +
            i +
            ": for input " +
            escape(inn) +
            " expected " +
            escape(expected) +
            " but got " +
            escape(out) +
            "\n"
        );
      }
    }
  }
  Assert.equal(outText, expectedText);
}

function checkEncode(converter, charset, inText, expectedText) {
  try {
    converter.charset = charset;
  } catch (e) {
    converter.charset = "iso-8859-1";
  }

  dump("testing encoding from Unicode to " + charset + "\n");
  var outText = converter.ConvertFromUnicode(inText) + converter.Finish();

  if (outText != expectedText) {
    for (var i = 0; i < inText.length; ++i) {
      var inn = inText[i];
      var out = outText[i];
      var expected = expectedText[i];
      if (out != expected) {
        dump(
          "Encoding error at position " +
            i +
            ": for input " +
            escape(inn) +
            " expected " +
            escape(expected) +
            " but got " +
            escape(out) +
            "\n"
        );
      }
    }
  }
  Assert.equal(outText, expectedText);
}

function testDecodeAliases(aliases, inString, expectedString) {
  var converter = CreateScriptableConverter();
  for (var i = 0; i < aliases.length; ++i) {
    checkDecode(converter, aliases[i], inString, expectedString);
  }
}

function testEncodeAliases(aliases, inString, expectedString) {
  var converter = CreateScriptableConverter();
  for (var i = 0; i < aliases.length; ++i) {
    checkEncode(converter, aliases[i], inString, expectedString);
  }
}

function testDecodeAliasesInternal(aliases, inString, expectedString) {
  var converter = CreateScriptableConverter();
  converter.isInternal = true;
  for (var i = 0; i < aliases.length; ++i) {
    checkDecode(converter, aliases[i], inString, expectedString);
  }
}

function testEncodeAliasesInternal(aliases, inString, expectedString) {
  var converter = CreateScriptableConverter();
  converter.isInternal = true;
  for (var i = 0; i < aliases.length; ++i) {
    checkEncode(converter, aliases[i], inString, expectedString);
  }
}