michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * Date: 07 February 2001 michael@0: * michael@0: * Functionality common to RegExp testing - michael@0: */ michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: michael@0: var MSG_PATTERN = '\nregexp = '; michael@0: var MSG_STRING = '\nstring = '; michael@0: var MSG_EXPECT = '\nExpect: '; michael@0: var MSG_ACTUAL = '\nActual: '; michael@0: var ERR_LENGTH = '\nERROR !!! match arrays have different lengths:'; michael@0: var ERR_MATCH = '\nERROR !!! regexp failed to give expected match array:'; michael@0: var ERR_NO_MATCH = '\nERROR !!! regexp FAILED to match anything !!!'; michael@0: var ERR_UNEXP_MATCH = '\nERROR !!! regexp MATCHED when we expected it to fail !!!'; michael@0: var CHAR_LBRACKET = '['; michael@0: var CHAR_RBRACKET = ']'; michael@0: var CHAR_QT_DBL = '"'; michael@0: var CHAR_QT = "'"; michael@0: var CHAR_NL = '\n'; michael@0: var CHAR_COMMA = ','; michael@0: var CHAR_SPACE = ' '; michael@0: var TYPE_STRING = typeof 'abc'; michael@0: michael@0: michael@0: michael@0: function testRegExp(statuses, patterns, strings, actualmatches, expectedmatches) michael@0: { michael@0: var status = ''; michael@0: var pattern = new RegExp(); michael@0: var string = ''; michael@0: var actualmatch = new Array(); michael@0: var expectedmatch = new Array(); michael@0: var state = ''; michael@0: var lActual = -1; michael@0: var lExpect = -1; michael@0: michael@0: michael@0: for (var i=0; i != patterns.length; i++) michael@0: { michael@0: status = statuses[i]; michael@0: pattern = patterns[i]; michael@0: string = strings[i]; michael@0: actualmatch=actualmatches[i]; michael@0: expectedmatch=expectedmatches[i]; michael@0: state = getState(status, pattern, string); michael@0: michael@0: description = status; michael@0: michael@0: if(actualmatch) michael@0: { michael@0: actual = formatArray(actualmatch); michael@0: if(expectedmatch) michael@0: { michael@0: // expectedmatch and actualmatch are arrays - michael@0: lExpect = expectedmatch.length; michael@0: lActual = actualmatch.length; michael@0: michael@0: var expected = formatArray(expectedmatch); michael@0: michael@0: if (lActual != lExpect) michael@0: { michael@0: reportCompare(lExpect, lActual, michael@0: state + ERR_LENGTH + michael@0: MSG_EXPECT + expected + michael@0: MSG_ACTUAL + actual + michael@0: CHAR_NL michael@0: ); michael@0: continue; michael@0: } michael@0: michael@0: // OK, the arrays have same length - michael@0: if (expected != actual) michael@0: { michael@0: reportCompare(expected, actual, michael@0: state + ERR_MATCH + michael@0: MSG_EXPECT + expected + michael@0: MSG_ACTUAL + actual + michael@0: CHAR_NL michael@0: ); michael@0: } michael@0: else michael@0: { michael@0: reportCompare(expected, actual, state) michael@0: } michael@0: michael@0: } michael@0: else //expectedmatch is null - that is, we did not expect a match - michael@0: { michael@0: expected = expectedmatch; michael@0: reportCompare(expected, actual, michael@0: state + ERR_UNEXP_MATCH + michael@0: MSG_EXPECT + expectedmatch + michael@0: MSG_ACTUAL + actual + michael@0: CHAR_NL michael@0: ); michael@0: } michael@0: michael@0: } michael@0: else // actualmatch is null michael@0: { michael@0: if (expectedmatch) michael@0: { michael@0: actual = actualmatch; michael@0: reportCompare(expected, actual, michael@0: state + ERR_NO_MATCH + michael@0: MSG_EXPECT + expectedmatch + michael@0: MSG_ACTUAL + actualmatch + michael@0: CHAR_NL michael@0: ); michael@0: } michael@0: else // we did not expect a match michael@0: { michael@0: // Being ultra-cautious. Presumably expectedmatch===actualmatch===null michael@0: expected = expectedmatch; michael@0: actual = actualmatch; michael@0: reportCompare (expectedmatch, actualmatch, state); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: michael@0: function getState(status, pattern, string) michael@0: { michael@0: /* michael@0: * Escape \n's, etc. to make them LITERAL in the presentation string. michael@0: * We don't have to worry about this in |pattern|; such escaping is michael@0: * done automatically by pattern.toString(), invoked implicitly below. michael@0: * michael@0: * One would like to simply do: string = string.replace(/(\s)/g, '\$1'). michael@0: * However, the backreference $1 is not a literal string value, michael@0: * so this method doesn't work. michael@0: * michael@0: * Also tried string = string.replace(/(\s)/g, escape('$1')); michael@0: * but this just inserts the escape of the literal '$1', i.e. '%241'. michael@0: */ michael@0: string = string.replace(/\n/g, '\\n'); michael@0: string = string.replace(/\r/g, '\\r'); michael@0: string = string.replace(/\t/g, '\\t'); michael@0: string = string.replace(/\v/g, '\\v'); michael@0: string = string.replace(/\f/g, '\\f'); michael@0: michael@0: return (status + MSG_PATTERN + pattern + MSG_STRING + singleQuote(string)); michael@0: } michael@0: michael@0: michael@0: /* michael@0: * If available, arr.toSource() gives more detail than arr.toString() michael@0: * michael@0: * var arr = Array(1,2,'3'); michael@0: * michael@0: * arr.toSource() michael@0: * [1, 2, "3"] michael@0: * michael@0: * arr.toString() michael@0: * 1,2,3 michael@0: * michael@0: * But toSource() doesn't exist in Rhino, so use our own imitation, below - michael@0: * michael@0: */ michael@0: function formatArray(arr) michael@0: { michael@0: try michael@0: { michael@0: return arr.toSource(); michael@0: } michael@0: catch(e) michael@0: { michael@0: return toSource(arr); michael@0: } michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Imitate SpiderMonkey's arr.toSource() method: michael@0: * michael@0: * a) Double-quote each array element that is of string type michael@0: * b) Represent |undefined| and |null| by empty strings michael@0: * c) Delimit elements by a comma + single space michael@0: * d) Do not add delimiter at the end UNLESS the last element is |undefined| michael@0: * e) Add square brackets to the beginning and end of the string michael@0: */ michael@0: function toSource(arr) michael@0: { michael@0: var delim = CHAR_COMMA + CHAR_SPACE; michael@0: var elt = ''; michael@0: var ret = ''; michael@0: var len = arr.length; michael@0: michael@0: for (i=0; i