summaryrefslogtreecommitdiff
path: root/codemirror_ui/lib/CodeMirror-2.3/test/mode_test.js
blob: d77ac143f81e8976b1240307d838f685f804acd6 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/**
 * Helper to test CodeMirror highlighting modes. It pretty prints output of the
 * highlighter and can check against expected styles.
 *
 * See test.html in the stex mode for examples.
 */
ModeTest = {};

ModeTest.modeOptions = {};
ModeTest.modeName = CodeMirror.defaults.mode;

/* keep track of results for printSummary */
ModeTest.tests = 0;
ModeTest.passes = 0;

/**
 * Run a test; prettyprints the results using document.write().
 *
 * @param string to highlight
 *
 * @param style[i] expected style of the i'th token in string
 *
 * @param token[i] expected value for the i'th token in string
 */
ModeTest.test = function() {
  ModeTest.tests += 1;

  var mode = CodeMirror.getMode(ModeTest.modeOptions, ModeTest.modeName);

  if (arguments.length < 1) {
    throw "must have text for test";
  }
  if (arguments.length % 2 != 1) {
    throw "must have text for test plus expected (style, token) pairs";
  }

  var text = arguments[0];
  var expectedOutput = [];
  for (var i = 1; i < arguments.length; i += 2) {
    expectedOutput.push([arguments[i],arguments[i + 1]]);
  }

  var observedOutput = ModeTest.highlight(text, mode)

  var pass, passStyle = "";
  if (expectedOutput.length > 0) {
    pass = ModeTest.highlightOutputsEqual(expectedOutput, observedOutput);
    passStyle = pass ? 'mt-pass' : 'mt-fail';
    ModeTest.passes += pass ? 1 : 0;
  }

  var s = '';
  s += '<div class="mt-test ' + passStyle + '">';
  s +=   '<pre>' + ModeTest.htmlEscape(text) + '</pre>';
  s +=   '<div class="cm-s-default">';
  if (pass || expectedOutput.length == 0) {
    s +=   ModeTest.prettyPrintOutputTable(observedOutput);
  } else {
    s += 'expected:';
    s +=   ModeTest.prettyPrintOutputTable(expectedOutput);
    s += 'observed:';
    s +=   ModeTest.prettyPrintOutputTable(observedOutput);
  }
  s +=   '</div>';
  s += '</div>';
  document.write(s);
}

/**
 * Emulation of CodeMirror's internal highlight routine for testing. Multi-line
 * input is supported.
 *
 * @param string to highlight
 *
 * @param mode the mode that will do the actual highlighting
 *
 * @return array of [style, token] pairs
 */
ModeTest.highlight = function(string, mode) {
  var state = mode.startState()

  var lines = string.replace(/\r\n/g,'\n').split('\n');
  var output = [];
  for (var i = 0; i < lines.length; ++i) {
    var line = lines[i];
    var stream = new CodeMirror.StringStream(line);
    if (line == "" && mode.blankLine) mode.blankLine(state);
    while (!stream.eol()) {
      var style = mode.token(stream, state);
      var substr = line.slice(stream.start, stream.pos);
      output.push([style, substr]);
      stream.start = stream.pos;
    }
  }

  return output;
}

/**
 * Compare two arrays of output from ModeTest.highlight.
 *
 * @param o1 array of [style, token] pairs
 *
 * @param o2 array of [style, token] pairs
 *
 * @return boolean; true iff outputs equal
 */
ModeTest.highlightOutputsEqual = function(o1, o2) {
  var eq = (o1.length == o2.length);
  if (eq) {
    for (var j in o1) {
      eq = eq &&
        o1[j].length == 2 && o1[j][0] == o2[j][0] && o1[j][1] == o2[j][1];
    }
  }
  return eq;
}

/**
 * Print tokens and corresponding styles in a table. Spaces in the token are
 * replaced with 'interpunct' dots (&middot;).
 *
 * @param output array of [style, token] pairs
 *
 * @return html string
 */
ModeTest.prettyPrintOutputTable = function(output) {
  var s = '<table class="mt-output">';
  s += '<tr>';
  for (var i = 0; i < output.length; ++i) {
    var token = output[i];
    s +=
      '<td class="mt-token">' +
        '<span class="cm-' + token[0] + '">' +
          ModeTest.htmlEscape(token[1]).replace(/ /g,'&middot;') +
        '</span>' +
      '</td>';
  }
  s += '</tr><tr>';
  for (var i = 0; i < output.length; ++i) {
    var token = output[i];
    s +=
      '<td class="mt-style"><span>' + token[0] + '</span></td>';
  }
  s += '</table>';
  return s;
}

/**
 * Print how many tests have run so far and how many of those passed.
 */
ModeTest.printSummary = function() {
  document.write(ModeTest.passes + ' passes for ' + ModeTest.tests + ' tests');
}

/**
 * Basic HTML escaping.
 */
ModeTest.htmlEscape = function(str) {
  str = str.toString();
  return str.replace(/[<&]/g,
      function(str) {return str == "&" ? "&amp;" : "&lt;";});
}