1/* Copyright JS Foundation and other contributors, http://js.foundation 2 * 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16function assertThrows(str, error) { 17 try { 18 eval(str); 19 assert(false); 20 } catch (e) { 21 if (typeof error === "function") { 22 assert(e instanceof error); 23 } else { 24 assert(e === error) 25 } 26 } 27} 28 29// https://www.ecma-international.org/ecma-262/6.0/#sec-string.raw 21.1.2.4 30// Note: the string error messages represents the nth step of the routine 31var abruptTests = [ 32 ["undefined", TypeError], // 3. 33 ["{ get raw() { throw '5'; } }", '5'], 34 ["{ raw : undefined }", TypeError], // 5.toObject 35 ["{ raw : { get length() { throw '7.1'; } } }", '7.1'], 36 ["{ raw : { length : { toString() { throw '7.2'; } } } }", '7.2'], 37 ["{ raw : { length: 2, get '0'() { throw '12.b.1'} } }", '12.b.1'], 38 ["{ raw : { length: 2, '0' : { toString() { throw '12.b.2';} } } }", '12.b.2'], 39 ["{ raw : { length: 2, '0' : 1 } }, { toString() { throw '12.h';} }", '12.h'], 40]; 41 42abruptTests.forEach(e => { 43 assertThrows("String.raw(" + e[0] + ")", e[1]); 44}); 45 46// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/raw 47assert(String.raw`Hi\n${2+3}!` === 'Hi\\n5!'); 48assert(String.raw`Hi\u000A!` === 'Hi\\u000A!'); 49 50let name = 'Bob'; 51assert(String.raw`Hi\n${name}!` === "Hi\\nBob!"); 52 53let str = String.raw({ 54 raw: ['foo', 'bar', 'baz'] 55}, 2 + 3, 'Java' + 'Script'); 56assert(str === "foo5barJavaScriptbaz"); 57 58assert(String.raw({ raw: 'test' }, 0, 1, 2) === "t0e1s2t"); 59 60var get = []; 61var raw = new Proxy({length: 2, 0: '', 1: ''}, { get: function(o, k) { get.push(k); return o[k]; }}); 62var p = new Proxy({raw: raw}, { get: function(o, k) { get.push(k); return o[k]; }}); 63String.raw(p); 64assert(get + '' === "raw,length,0,1"); 65 66assert(String.raw`\\` == "\\\\") 67assert(String.raw`\`` == "\\`") 68assert(String.raw`\ 69\ 70` == "\\\n\\\n") 71assert(String.raw`\ ` == "\\\u2029") 72