regress-305064.js (4587B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 //----------------------------------------------------------------------------- 7 var BUGNUMBER = 305064; 8 var summary = 'Tests the trim, trimRight and trimLeft methods'; 9 var actual = ''; 10 var expect = ''; 11 12 printBugNumber(BUGNUMBER); 13 printStatus (summary); 14 15 var trimMethods = ['trim', 'trimLeft', 'trimRight']; 16 17 /** List from ES 3.1 Recommendation for String.trim (bug 305064) **/ 18 var whitespace = [ 19 {s : '\u0009', t : 'HORIZONTAL TAB'}, 20 {s : '\u000B', t : 'VERTICAL TAB'}, 21 {s : '\u000C', t : 'FORMFEED'}, 22 {s : '\u0020', t : 'SPACE'}, 23 {s : '\u00A0', t : 'NO-BREAK SPACE'}, 24 {s : '\u1680', t : 'OGHAM SPACE MARK'}, 25 {s : '\u2000', t : 'EN QUAD'}, 26 {s : '\u2001', t : 'EM QUAD'}, 27 {s : '\u2002', t : 'EN SPACE'}, 28 {s : '\u2003', t : 'EM SPACE'}, 29 {s : '\u2004', t : 'THREE-PER-EM SPACE'}, 30 {s : '\u2005', t : 'FOUR-PER-EM SPACE'}, 31 {s : '\u2006', t : 'SIX-PER-EM SPACE'}, 32 {s : '\u2007', t : 'FIGURE SPACE'}, 33 {s : '\u2008', t : 'PUNCTUATION SPACE'}, 34 {s : '\u2009', t : 'THIN SPACE'}, 35 {s : '\u200A', t : 'HAIR SPACE'}, 36 {s : '\u202F', t : 'NARROW NO-BREAK SPACE'}, 37 {s : '\u205F', t : 'MEDIUM MATHEMATICAL SPACE'}, 38 {s : '\u3000', t : 'IDEOGRAPHIC SPACE'}, 39 {s : '\u000A', t : 'LINE FEED OR NEW LINE'}, 40 {s : '\u000D', t : 'CARRIAGE RETURN'}, 41 {s : '\u2028', t : 'LINE SEPARATOR'}, 42 {s : '\u2029', t : 'PARAGRAPH SEPARATOR'}, 43 ]; 44 45 for (var j = 0; j < trimMethods.length; ++j) 46 { 47 var str; 48 49 var method = trimMethods[j]; 50 51 if (typeof String.prototype[method] == 'undefined') 52 { 53 reportCompare(true, true, 'Test skipped. String.prototype.' + method + ' is not supported'); 54 continue; 55 } 56 57 print('Test empty string.'); 58 str = ''; 59 expected = ''; 60 actual = str[method](); 61 reportCompare(expected, actual, '"' + toPrinted(str) + '".' + method + '()'); 62 63 print('Test string with no whitespace.'); 64 str = 'a'; 65 expected = 'a'; 66 actual = str[method](); 67 reportCompare(expected, actual, '"' + toPrinted(str) + '".' + method + '()'); 68 69 for (var i = 0; i < whitespace.length; ++i) 70 { 71 var v = whitespace[i].s; 72 var t = whitespace[i].t; 73 v = v + v + v; 74 75 print('======================================='); 76 print('Test ' + method + ' with with only whitespace. : ' + t); 77 str = v; 78 expected = ''; 79 actual = str[method](); 80 reportCompare(expected, actual, t + ':' + '"' + toPrinted(str) + '".' + method + '()'); 81 82 print('Test ' + method + ' with with no leading or trailing whitespace. : ' + t); 83 str = 'a' + v + 'b'; 84 expected = str; 85 actual = str[method](); 86 reportCompare(expected, actual, t + ':' + '"' + toPrinted(str) + '".' + method + '()'); 87 88 print('Test ' + method + ' with with leading whitespace. : ' + t); 89 str = v + 'a'; 90 switch(method) 91 { 92 case 'trim': 93 expected = 'a'; 94 break; 95 case 'trimLeft': 96 expected = 'a'; 97 break; 98 case 'trimRight': 99 expected = str; 100 break; 101 } 102 actual = str[method](); 103 reportCompare(expected, actual, t + ':' + '"' + toPrinted(str) + '".' + method + '()'); 104 105 print('Test ' + method + ' with with trailing whitespace. : ' + t); 106 str = 'a' + v; 107 switch(method) 108 { 109 case 'trim': 110 expected = 'a'; 111 break; 112 case 'trimLeft': 113 expected = str; 114 break; 115 case 'trimRight': 116 expected = 'a'; 117 break; 118 } 119 actual = str[method](); 120 reportCompare(expected, actual, t + ':' + '"' + toPrinted(str) + '".' + method + '()'); 121 122 print('Test ' + method + ' with with leading and trailing whitepace.'); 123 str = v + 'a' + v; 124 switch(method) 125 { 126 case 'trim': 127 expected = 'a'; 128 break; 129 case 'trimLeft': 130 expected = 'a' + v; 131 break; 132 case 'trimRight': 133 expected = v + 'a'; 134 break; 135 } 136 actual = str[method](); 137 reportCompare(expected, actual, t + ':' + '"' + toPrinted(str) + '".' + method + '()'); 138 139 } 140 } 141 142 function toPrinted(value) { 143 return value.replace(/[^\u0020-\u007E]/g, function(c) { 144 if (c === "\n") 145 return "NL"; 146 147 var ch = c.charCodeAt(0); 148 var hex = ch.toString(16).toUpperCase(); 149 if (ch > 0xff) { 150 return "\\u" + "0000".slice(hex.length) + hex; 151 } 152 return "\\x" + "00".slice(hex.length) + hex; 153 }); 154 }