1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/tests/ecma_3/extensions/shell.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,232 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +/* 1.10 + * Date: 07 February 2001 1.11 + * 1.12 + * Functionality common to RegExp testing - 1.13 + */ 1.14 +//----------------------------------------------------------------------------- 1.15 + 1.16 + 1.17 +var MSG_PATTERN = '\nregexp = '; 1.18 +var MSG_STRING = '\nstring = '; 1.19 +var MSG_EXPECT = '\nExpect: '; 1.20 +var MSG_ACTUAL = '\nActual: '; 1.21 +var ERR_LENGTH = '\nERROR !!! match arrays have different lengths:'; 1.22 +var ERR_MATCH = '\nERROR !!! regexp failed to give expected match array:'; 1.23 +var ERR_NO_MATCH = '\nERROR !!! regexp FAILED to match anything !!!'; 1.24 +var ERR_UNEXP_MATCH = '\nERROR !!! regexp MATCHED when we expected it to fail !!!'; 1.25 +var CHAR_LBRACKET = '['; 1.26 +var CHAR_RBRACKET = ']'; 1.27 +var CHAR_QT_DBL = '"'; 1.28 +var CHAR_QT = "'"; 1.29 +var CHAR_NL = '\n'; 1.30 +var CHAR_COMMA = ','; 1.31 +var CHAR_SPACE = ' '; 1.32 +var TYPE_STRING = typeof 'abc'; 1.33 + 1.34 + 1.35 + 1.36 +function testRegExp(statuses, patterns, strings, actualmatches, expectedmatches) 1.37 +{ 1.38 + var status = ''; 1.39 + var pattern = new RegExp(); 1.40 + var string = ''; 1.41 + var actualmatch = new Array(); 1.42 + var expectedmatch = new Array(); 1.43 + var state = ''; 1.44 + var lActual = -1; 1.45 + var lExpect = -1; 1.46 + 1.47 + 1.48 + for (var i=0; i != patterns.length; i++) 1.49 + { 1.50 + status = statuses[i]; 1.51 + pattern = patterns[i]; 1.52 + string = strings[i]; 1.53 + actualmatch=actualmatches[i]; 1.54 + expectedmatch=expectedmatches[i]; 1.55 + state = getState(status, pattern, string); 1.56 + 1.57 + description = status; 1.58 + 1.59 + if(actualmatch) 1.60 + { 1.61 + actual = formatArray(actualmatch); 1.62 + if(expectedmatch) 1.63 + { 1.64 + // expectedmatch and actualmatch are arrays - 1.65 + lExpect = expectedmatch.length; 1.66 + lActual = actualmatch.length; 1.67 + 1.68 + var expected = formatArray(expectedmatch); 1.69 + 1.70 + if (lActual != lExpect) 1.71 + { 1.72 + reportCompare(lExpect, lActual, 1.73 + state + ERR_LENGTH + 1.74 + MSG_EXPECT + expected + 1.75 + MSG_ACTUAL + actual + 1.76 + CHAR_NL 1.77 + ); 1.78 + continue; 1.79 + } 1.80 + 1.81 + // OK, the arrays have same length - 1.82 + if (expected != actual) 1.83 + { 1.84 + reportCompare(expected, actual, 1.85 + state + ERR_MATCH + 1.86 + MSG_EXPECT + expected + 1.87 + MSG_ACTUAL + actual + 1.88 + CHAR_NL 1.89 + ); 1.90 + } 1.91 + else 1.92 + { 1.93 + reportCompare(expected, actual, state) 1.94 + } 1.95 + 1.96 + } 1.97 + else //expectedmatch is null - that is, we did not expect a match - 1.98 + { 1.99 + expected = expectedmatch; 1.100 + reportCompare(expected, actual, 1.101 + state + ERR_UNEXP_MATCH + 1.102 + MSG_EXPECT + expectedmatch + 1.103 + MSG_ACTUAL + actual + 1.104 + CHAR_NL 1.105 + ); 1.106 + } 1.107 + 1.108 + } 1.109 + else // actualmatch is null 1.110 + { 1.111 + if (expectedmatch) 1.112 + { 1.113 + actual = actualmatch; 1.114 + reportCompare(expected, actual, 1.115 + state + ERR_NO_MATCH + 1.116 + MSG_EXPECT + expectedmatch + 1.117 + MSG_ACTUAL + actualmatch + 1.118 + CHAR_NL 1.119 + ); 1.120 + } 1.121 + else // we did not expect a match 1.122 + { 1.123 + // Being ultra-cautious. Presumably expectedmatch===actualmatch===null 1.124 + expected = expectedmatch; 1.125 + actual = actualmatch; 1.126 + reportCompare (expectedmatch, actualmatch, state); 1.127 + } 1.128 + } 1.129 + } 1.130 +} 1.131 + 1.132 + 1.133 +function getState(status, pattern, string) 1.134 +{ 1.135 + /* 1.136 + * Escape \n's, etc. to make them LITERAL in the presentation string. 1.137 + * We don't have to worry about this in |pattern|; such escaping is 1.138 + * done automatically by pattern.toString(), invoked implicitly below. 1.139 + * 1.140 + * One would like to simply do: string = string.replace(/(\s)/g, '\$1'). 1.141 + * However, the backreference $1 is not a literal string value, 1.142 + * so this method doesn't work. 1.143 + * 1.144 + * Also tried string = string.replace(/(\s)/g, escape('$1')); 1.145 + * but this just inserts the escape of the literal '$1', i.e. '%241'. 1.146 + */ 1.147 + string = string.replace(/\n/g, '\\n'); 1.148 + string = string.replace(/\r/g, '\\r'); 1.149 + string = string.replace(/\t/g, '\\t'); 1.150 + string = string.replace(/\v/g, '\\v'); 1.151 + string = string.replace(/\f/g, '\\f'); 1.152 + 1.153 + return (status + MSG_PATTERN + pattern + MSG_STRING + singleQuote(string)); 1.154 +} 1.155 + 1.156 + 1.157 +/* 1.158 + * If available, arr.toSource() gives more detail than arr.toString() 1.159 + * 1.160 + * var arr = Array(1,2,'3'); 1.161 + * 1.162 + * arr.toSource() 1.163 + * [1, 2, "3"] 1.164 + * 1.165 + * arr.toString() 1.166 + * 1,2,3 1.167 + * 1.168 + * But toSource() doesn't exist in Rhino, so use our own imitation, below - 1.169 + * 1.170 + */ 1.171 +function formatArray(arr) 1.172 +{ 1.173 + try 1.174 + { 1.175 + return arr.toSource(); 1.176 + } 1.177 + catch(e) 1.178 + { 1.179 + return toSource(arr); 1.180 + } 1.181 +} 1.182 + 1.183 + 1.184 +/* 1.185 + * Imitate SpiderMonkey's arr.toSource() method: 1.186 + * 1.187 + * a) Double-quote each array element that is of string type 1.188 + * b) Represent |undefined| and |null| by empty strings 1.189 + * c) Delimit elements by a comma + single space 1.190 + * d) Do not add delimiter at the end UNLESS the last element is |undefined| 1.191 + * e) Add square brackets to the beginning and end of the string 1.192 + */ 1.193 +function toSource(arr) 1.194 +{ 1.195 + var delim = CHAR_COMMA + CHAR_SPACE; 1.196 + var elt = ''; 1.197 + var ret = ''; 1.198 + var len = arr.length; 1.199 + 1.200 + for (i=0; i<len; i++) 1.201 + { 1.202 + elt = arr[i]; 1.203 + 1.204 + switch(true) 1.205 + { 1.206 + case (typeof elt === TYPE_STRING) : 1.207 + ret += doubleQuote(elt); 1.208 + break; 1.209 + 1.210 + case (elt === undefined || elt === null) : 1.211 + break; // add nothing but the delimiter, below - 1.212 + 1.213 + default: 1.214 + ret += elt.toString(); 1.215 + } 1.216 + 1.217 + if ((i < len-1) || (elt === undefined)) 1.218 + ret += delim; 1.219 + } 1.220 + 1.221 + return CHAR_LBRACKET + ret + CHAR_RBRACKET; 1.222 +} 1.223 + 1.224 + 1.225 +function doubleQuote(text) 1.226 +{ 1.227 + return CHAR_QT_DBL + text + CHAR_QT_DBL; 1.228 +} 1.229 + 1.230 + 1.231 +function singleQuote(text) 1.232 +{ 1.233 + return CHAR_QT + text + CHAR_QT; 1.234 +} 1.235 +