File indexing completed on 2024-04-28 15:28:39
0001 // normalize a piece of source code 0002 function normalize(s) 0003 { 0004 return s.replace(/[ \u000A]+/g, " ").replace(/ +$/, "").replace(/^ +/, ""); 0005 } 0006 0007 // compare toString() conversion of code without 0008 // taking newlines, indentation and other whitespace into account 0009 function tolerantSourceCompare(s, s2) 0010 { 0011 var expected = normalize(s2 ? s2 : s); 0012 var actual = normalize(eval("String(" + s + ")")); 0013 if (expected === actual) 0014 testPassed("Got the expected '" + expected + "'"); 0015 else 0016 testFailed("Expected '" + expected + "', got '" + actual + "'"); 0017 } 0018 0019 tolerantSourceCompare('function () { bar("x"); }'); 0020 tolerantSourceCompare('function () { bar(/abc/i); }'); 0021 var cccc = String.fromCharCode(0xCCCC); 0022 var ff = String.fromCharCode(0xFF); 0023 tolerantSourceCompare('function () { bar("' + cccc + '"); }', 'function () { bar("\\uCCCC"); }'); 0024 tolerantSourceCompare('function () { var o = { "a": 1 }; }'); 0025 tolerantSourceCompare('function () { var o = { "' + cccc + '": 1 }; }', 0026 'function () { var o = { "\\uCCCC": 1 }; }'); 0027 tolerantSourceCompare('function () { var a' + ff + '; }', 0028 'function () { var a\\xFF; }'); 0029 tolerantSourceCompare('function f' + ff + '() { }', 0030 'function f\\xFF() { }'); 0031 0032 tolerantSourceCompare('function () { do 1; while (false); }', 0033 'function () { do 1; while (false); }'); 0034 tolerantSourceCompare('function () { do 1; while (false) }', 0035 'function () { do 1; while (false); }'); 0036 0037 0038 var sinStr = '\nfunction sin() {\n [native code]\n}\n' 0039 shouldBe("normalize(String(Math.sin))", "normalize(sinStr)"); 0040 0041 tolerantSourceCompare('function () { [1]; }'); 0042 tolerantSourceCompare('function () { [1,2]; }'); 0043 tolerantSourceCompare('function () { [1,]; }'); 0044 tolerantSourceCompare('function () { [1,,]; }'); 0045 tolerantSourceCompare('function () { [1,,,]; }'); 0046 0047 /**** non-portable JS extension 0048 tolerantSourceCompare('function () { import a.b; }'); 0049 tolerantSourceCompare('function () { import a.b }', 0050 'function () { import a.b; }'); 0051 tolerantSourceCompare('function () { import a.b.*; }'); 0052 tolerantSourceCompare('function () { import a.b.* }', 0053 'function () { import a.b.*; }'); 0054 tolerantSourceCompare('function () { import x = a.b.c; }'); 0055 tolerantSourceCompare('function () { import x = a.b.c }', 0056 'function () { import x = a.b.c; }'); 0057 */