michael@0: // |reftest| fails 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: var BUGNUMBER = 305064; michael@0: var summary = 'Tests the trim, trimRight and trimLeft methods'; michael@0: var actual = ''; michael@0: var expect = ''; michael@0: michael@0: printBugNumber(BUGNUMBER); michael@0: printStatus (summary); michael@0: michael@0: var trimMethods = ['trim', 'trimLeft', 'trimRight']; michael@0: michael@0: /** List from ES 3.1 Recommendation for String.trim (bug 305064) **/ michael@0: var whitespace = [ michael@0: {s : '\u0009', t : 'HORIZONTAL TAB'}, michael@0: {s : '\u000B', t : 'VERTICAL TAB'}, michael@0: {s : '\u000C', t : 'FORMFEED'}, michael@0: {s : '\u0020', t : 'SPACE'}, michael@0: {s : '\u00A0', t : 'NO-BREAK SPACE'}, michael@0: {s : '\u1680', t : 'OGHAM SPACE MARK'}, michael@0: {s : '\u180E', t : 'MONGOLIAN VOWEL SEPARATOR'}, michael@0: {s : '\u2000', t : 'EN QUAD'}, michael@0: {s : '\u2001', t : 'EM QUAD'}, michael@0: {s : '\u2002', t : 'EN SPACE'}, michael@0: {s : '\u2003', t : 'EM SPACE'}, michael@0: {s : '\u2004', t : 'THREE-PER-EM SPACE'}, michael@0: {s : '\u2005', t : 'FOUR-PER-EM SPACE'}, michael@0: {s : '\u2006', t : 'SIX-PER-EM SPACE'}, michael@0: {s : '\u2007', t : 'FIGURE SPACE'}, michael@0: {s : '\u2008', t : 'PUNCTUATION SPACE'}, michael@0: {s : '\u2009', t : 'THIN SPACE'}, michael@0: {s : '\u200A', t : 'HAIR SPACE'}, michael@0: {s : '\u202F', t : 'NARROW NO-BREAK SPACE'}, michael@0: {s : '\u205F', t : 'MEDIUM MATHEMATICAL SPACE'}, michael@0: {s : '\u3000', t : 'IDEOGRAPHIC SPACE'}, michael@0: {s : '\u000A', t : 'LINE FEED OR NEW LINE'}, michael@0: {s : '\u000D', t : 'CARRIAGE RETURN'}, michael@0: {s : '\u2028', t : 'LINE SEPARATOR'}, michael@0: {s : '\u2029', t : 'PARAGRAPH SEPARATOR'}, michael@0: {s : '\u200B', t : 'ZERO WIDTH SPACE (category Cf)'} michael@0: ]; michael@0: michael@0: for (var j = 0; j < trimMethods.length; ++j) michael@0: { michael@0: var str; michael@0: michael@0: var method = trimMethods[j]; michael@0: michael@0: if (typeof String.prototype[method] == 'undefined') michael@0: { michael@0: reportCompare(true, true, 'Test skipped. String.prototype.' + method + ' is not supported'); michael@0: continue; michael@0: } michael@0: michael@0: print('Test empty string.'); michael@0: str = ''; michael@0: expected = ''; michael@0: actual = str[method](); michael@0: reportCompare(expected, actual, '"' + toPrinted(str) + '".' + method + '()'); michael@0: michael@0: print('Test string with no whitespace.'); michael@0: str = 'a'; michael@0: expected = 'a'; michael@0: actual = str[method](); michael@0: reportCompare(expected, actual, '"' + toPrinted(str) + '".' + method + '()'); michael@0: michael@0: for (var i = 0; i < whitespace.length; ++i) michael@0: { michael@0: var v = whitespace[i].s; michael@0: var t = whitespace[i].t; michael@0: v = v + v + v; michael@0: michael@0: print('======================================='); michael@0: print('Test ' + method + ' with with only whitespace. : ' + t); michael@0: str = v; michael@0: expected = ''; michael@0: actual = str[method](); michael@0: reportCompare(expected, actual, t + ':' + '"' + toPrinted(str) + '".' + method + '()'); michael@0: michael@0: print('Test ' + method + ' with with no leading or trailing whitespace. : ' + t); michael@0: str = 'a' + v + 'b'; michael@0: expected = str; michael@0: actual = str[method](); michael@0: reportCompare(expected, actual, t + ':' + '"' + toPrinted(str) + '".' + method + '()'); michael@0: michael@0: print('Test ' + method + ' with with leading whitespace. : ' + t); michael@0: str = v + 'a'; michael@0: switch(method) michael@0: { michael@0: case 'trim': michael@0: expected = 'a'; michael@0: break; michael@0: case 'trimLeft': michael@0: expected = 'a'; michael@0: break; michael@0: case 'trimRight': michael@0: expected = str; michael@0: break; michael@0: } michael@0: actual = str[method](); michael@0: reportCompare(expected, actual, t + ':' + '"' + toPrinted(str) + '".' + method + '()'); michael@0: michael@0: print('Test ' + method + ' with with trailing whitespace. : ' + t); michael@0: str = 'a' + v; michael@0: switch(method) michael@0: { michael@0: case 'trim': michael@0: expected = 'a'; michael@0: break; michael@0: case 'trimLeft': michael@0: expected = str; michael@0: break; michael@0: case 'trimRight': michael@0: expected = 'a'; michael@0: break; michael@0: } michael@0: actual = str[method](); michael@0: reportCompare(expected, actual, t + ':' + '"' + toPrinted(str) + '".' + method + '()'); michael@0: michael@0: print('Test ' + method + ' with with leading and trailing whitepace.'); michael@0: str = v + 'a' + v; michael@0: switch(method) michael@0: { michael@0: case 'trim': michael@0: expected = 'a'; michael@0: break; michael@0: case 'trimLeft': michael@0: expected = 'a' + v; michael@0: break; michael@0: case 'trimRight': michael@0: expected = v + 'a'; michael@0: break; michael@0: } michael@0: actual = str[method](); michael@0: reportCompare(expected, actual, t + ':' + '"' + toPrinted(str) + '".' + method + '()'); michael@0: michael@0: } michael@0: } michael@0: