Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | /* |
michael@0 | 7 | * Date: 07 February 2001 |
michael@0 | 8 | * |
michael@0 | 9 | * Functionality common to RegExp testing - |
michael@0 | 10 | */ |
michael@0 | 11 | //----------------------------------------------------------------------------- |
michael@0 | 12 | |
michael@0 | 13 | |
michael@0 | 14 | var MSG_PATTERN = '\nregexp = '; |
michael@0 | 15 | var MSG_STRING = '\nstring = '; |
michael@0 | 16 | var MSG_EXPECT = '\nExpect: '; |
michael@0 | 17 | var MSG_ACTUAL = '\nActual: '; |
michael@0 | 18 | var ERR_LENGTH = '\nERROR !!! match arrays have different lengths:'; |
michael@0 | 19 | var ERR_MATCH = '\nERROR !!! regexp failed to give expected match array:'; |
michael@0 | 20 | var ERR_NO_MATCH = '\nERROR !!! regexp FAILED to match anything !!!'; |
michael@0 | 21 | var ERR_UNEXP_MATCH = '\nERROR !!! regexp MATCHED when we expected it to fail !!!'; |
michael@0 | 22 | var CHAR_LBRACKET = '['; |
michael@0 | 23 | var CHAR_RBRACKET = ']'; |
michael@0 | 24 | var CHAR_QT_DBL = '"'; |
michael@0 | 25 | var CHAR_QT = "'"; |
michael@0 | 26 | var CHAR_NL = '\n'; |
michael@0 | 27 | var CHAR_COMMA = ','; |
michael@0 | 28 | var CHAR_SPACE = ' '; |
michael@0 | 29 | var TYPE_STRING = typeof 'abc'; |
michael@0 | 30 | |
michael@0 | 31 | |
michael@0 | 32 | |
michael@0 | 33 | function testRegExp(statuses, patterns, strings, actualmatches, expectedmatches) |
michael@0 | 34 | { |
michael@0 | 35 | var status = ''; |
michael@0 | 36 | var pattern = new RegExp(); |
michael@0 | 37 | var string = ''; |
michael@0 | 38 | var actualmatch = new Array(); |
michael@0 | 39 | var expectedmatch = new Array(); |
michael@0 | 40 | var state = ''; |
michael@0 | 41 | var lActual = -1; |
michael@0 | 42 | var lExpect = -1; |
michael@0 | 43 | |
michael@0 | 44 | |
michael@0 | 45 | for (var i=0; i != patterns.length; i++) |
michael@0 | 46 | { |
michael@0 | 47 | status = statuses[i]; |
michael@0 | 48 | pattern = patterns[i]; |
michael@0 | 49 | string = strings[i]; |
michael@0 | 50 | actualmatch=actualmatches[i]; |
michael@0 | 51 | expectedmatch=expectedmatches[i]; |
michael@0 | 52 | state = getState(status, pattern, string); |
michael@0 | 53 | |
michael@0 | 54 | description = status; |
michael@0 | 55 | |
michael@0 | 56 | if(actualmatch) |
michael@0 | 57 | { |
michael@0 | 58 | actual = formatArray(actualmatch); |
michael@0 | 59 | if(expectedmatch) |
michael@0 | 60 | { |
michael@0 | 61 | // expectedmatch and actualmatch are arrays - |
michael@0 | 62 | lExpect = expectedmatch.length; |
michael@0 | 63 | lActual = actualmatch.length; |
michael@0 | 64 | |
michael@0 | 65 | var expected = formatArray(expectedmatch); |
michael@0 | 66 | |
michael@0 | 67 | if (lActual != lExpect) |
michael@0 | 68 | { |
michael@0 | 69 | reportCompare(lExpect, lActual, |
michael@0 | 70 | state + ERR_LENGTH + |
michael@0 | 71 | MSG_EXPECT + expected + |
michael@0 | 72 | MSG_ACTUAL + actual + |
michael@0 | 73 | CHAR_NL |
michael@0 | 74 | ); |
michael@0 | 75 | continue; |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | // OK, the arrays have same length - |
michael@0 | 79 | if (expected != actual) |
michael@0 | 80 | { |
michael@0 | 81 | reportCompare(expected, actual, |
michael@0 | 82 | state + ERR_MATCH + |
michael@0 | 83 | MSG_EXPECT + expected + |
michael@0 | 84 | MSG_ACTUAL + actual + |
michael@0 | 85 | CHAR_NL |
michael@0 | 86 | ); |
michael@0 | 87 | } |
michael@0 | 88 | else |
michael@0 | 89 | { |
michael@0 | 90 | reportCompare(expected, actual, state) |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | } |
michael@0 | 94 | else //expectedmatch is null - that is, we did not expect a match - |
michael@0 | 95 | { |
michael@0 | 96 | expected = expectedmatch; |
michael@0 | 97 | reportCompare(expected, actual, |
michael@0 | 98 | state + ERR_UNEXP_MATCH + |
michael@0 | 99 | MSG_EXPECT + expectedmatch + |
michael@0 | 100 | MSG_ACTUAL + actual + |
michael@0 | 101 | CHAR_NL |
michael@0 | 102 | ); |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | } |
michael@0 | 106 | else // actualmatch is null |
michael@0 | 107 | { |
michael@0 | 108 | if (expectedmatch) |
michael@0 | 109 | { |
michael@0 | 110 | actual = actualmatch; |
michael@0 | 111 | reportCompare(expected, actual, |
michael@0 | 112 | state + ERR_NO_MATCH + |
michael@0 | 113 | MSG_EXPECT + expectedmatch + |
michael@0 | 114 | MSG_ACTUAL + actualmatch + |
michael@0 | 115 | CHAR_NL |
michael@0 | 116 | ); |
michael@0 | 117 | } |
michael@0 | 118 | else // we did not expect a match |
michael@0 | 119 | { |
michael@0 | 120 | // Being ultra-cautious. Presumably expectedmatch===actualmatch===null |
michael@0 | 121 | expected = expectedmatch; |
michael@0 | 122 | actual = actualmatch; |
michael@0 | 123 | reportCompare (expectedmatch, actualmatch, state); |
michael@0 | 124 | } |
michael@0 | 125 | } |
michael@0 | 126 | } |
michael@0 | 127 | } |
michael@0 | 128 | |
michael@0 | 129 | |
michael@0 | 130 | function getState(status, pattern, string) |
michael@0 | 131 | { |
michael@0 | 132 | /* |
michael@0 | 133 | * Escape \n's, etc. to make them LITERAL in the presentation string. |
michael@0 | 134 | * We don't have to worry about this in |pattern|; such escaping is |
michael@0 | 135 | * done automatically by pattern.toString(), invoked implicitly below. |
michael@0 | 136 | * |
michael@0 | 137 | * One would like to simply do: string = string.replace(/(\s)/g, '\$1'). |
michael@0 | 138 | * However, the backreference $1 is not a literal string value, |
michael@0 | 139 | * so this method doesn't work. |
michael@0 | 140 | * |
michael@0 | 141 | * Also tried string = string.replace(/(\s)/g, escape('$1')); |
michael@0 | 142 | * but this just inserts the escape of the literal '$1', i.e. '%241'. |
michael@0 | 143 | */ |
michael@0 | 144 | string = string.replace(/\n/g, '\\n'); |
michael@0 | 145 | string = string.replace(/\r/g, '\\r'); |
michael@0 | 146 | string = string.replace(/\t/g, '\\t'); |
michael@0 | 147 | string = string.replace(/\v/g, '\\v'); |
michael@0 | 148 | string = string.replace(/\f/g, '\\f'); |
michael@0 | 149 | |
michael@0 | 150 | return (status + MSG_PATTERN + pattern + MSG_STRING + singleQuote(string)); |
michael@0 | 151 | } |
michael@0 | 152 | |
michael@0 | 153 | |
michael@0 | 154 | /* |
michael@0 | 155 | * If available, arr.toSource() gives more detail than arr.toString() |
michael@0 | 156 | * |
michael@0 | 157 | * var arr = Array(1,2,'3'); |
michael@0 | 158 | * |
michael@0 | 159 | * arr.toSource() |
michael@0 | 160 | * [1, 2, "3"] |
michael@0 | 161 | * |
michael@0 | 162 | * arr.toString() |
michael@0 | 163 | * 1,2,3 |
michael@0 | 164 | * |
michael@0 | 165 | * But toSource() doesn't exist in Rhino, so use our own imitation, below - |
michael@0 | 166 | * |
michael@0 | 167 | */ |
michael@0 | 168 | function formatArray(arr) |
michael@0 | 169 | { |
michael@0 | 170 | try |
michael@0 | 171 | { |
michael@0 | 172 | return arr.toSource(); |
michael@0 | 173 | } |
michael@0 | 174 | catch(e) |
michael@0 | 175 | { |
michael@0 | 176 | return toSource(arr); |
michael@0 | 177 | } |
michael@0 | 178 | } |
michael@0 | 179 | |
michael@0 | 180 | |
michael@0 | 181 | /* |
michael@0 | 182 | * Imitate SpiderMonkey's arr.toSource() method: |
michael@0 | 183 | * |
michael@0 | 184 | * a) Double-quote each array element that is of string type |
michael@0 | 185 | * b) Represent |undefined| and |null| by empty strings |
michael@0 | 186 | * c) Delimit elements by a comma + single space |
michael@0 | 187 | * d) Do not add delimiter at the end UNLESS the last element is |undefined| |
michael@0 | 188 | * e) Add square brackets to the beginning and end of the string |
michael@0 | 189 | */ |
michael@0 | 190 | function toSource(arr) |
michael@0 | 191 | { |
michael@0 | 192 | var delim = CHAR_COMMA + CHAR_SPACE; |
michael@0 | 193 | var elt = ''; |
michael@0 | 194 | var ret = ''; |
michael@0 | 195 | var len = arr.length; |
michael@0 | 196 | |
michael@0 | 197 | for (i=0; i<len; i++) |
michael@0 | 198 | { |
michael@0 | 199 | elt = arr[i]; |
michael@0 | 200 | |
michael@0 | 201 | switch(true) |
michael@0 | 202 | { |
michael@0 | 203 | case (typeof elt === TYPE_STRING) : |
michael@0 | 204 | ret += doubleQuote(elt); |
michael@0 | 205 | break; |
michael@0 | 206 | |
michael@0 | 207 | case (elt === undefined || elt === null) : |
michael@0 | 208 | break; // add nothing but the delimiter, below - |
michael@0 | 209 | |
michael@0 | 210 | default: |
michael@0 | 211 | ret += elt.toString(); |
michael@0 | 212 | } |
michael@0 | 213 | |
michael@0 | 214 | if ((i < len-1) || (elt === undefined)) |
michael@0 | 215 | ret += delim; |
michael@0 | 216 | } |
michael@0 | 217 | |
michael@0 | 218 | return CHAR_LBRACKET + ret + CHAR_RBRACKET; |
michael@0 | 219 | } |
michael@0 | 220 | |
michael@0 | 221 | |
michael@0 | 222 | function doubleQuote(text) |
michael@0 | 223 | { |
michael@0 | 224 | return CHAR_QT_DBL + text + CHAR_QT_DBL; |
michael@0 | 225 | } |
michael@0 | 226 | |
michael@0 | 227 | |
michael@0 | 228 | function singleQuote(text) |
michael@0 | 229 | { |
michael@0 | 230 | return CHAR_QT + text + CHAR_QT; |
michael@0 | 231 | } |
michael@0 | 232 |