From 02fdcf32268ed4ffb5f292792b4c55abbbfc52c7 Mon Sep 17 00:00:00 2001 From: Denis Pushkarev Date: Fri, 10 Jul 2015 01:40:41 +0600 Subject: [PATCH] add @@search tests, #83 --- tests/tests.js | 104 +++++++++++++++++++++++++++++++ tests/tests/es6.regexp.search.ls | 62 ++++++++++++++++++ 2 files changed, 166 insertions(+) create mode 100644 tests/tests/es6.regexp.search.ls diff --git a/tests/tests.js b/tests/tests.js index ebea9a19b931..5198cdea4a5f 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -4434,6 +4434,110 @@ }); }).call(this); +// Generated by LiveScript 1.3.1 +(function(){ + var eq, toString$ = {}.toString; + QUnit.module('ES6'); + eq = strictEqual; + test('String#search regression', function(){ + var instance, e, aString; + ok(toString$.call(''.search).slice(8, -1) === 'Function', 'String#search is function'); + eq(''.search.length, 1, 'String#search length is 1'); + ok(/native code/.test(''.search), 'looks like native'); + if ('name' in ''.search) { + eq(''.search.name, 'search', 'String#search is "search" (can fail if compressed)'); + } + instance = Object(true); + instance.search = String.prototype.search; + eq(instance.search(true), 0, 'S15.5.4.12_A1_T1'); + instance = Object(false); + instance.search = String.prototype.search; + eq(instance.search(false), 0, 'S15.5.4.12_A1_T2'); + eq(''.search(), 0, 'S15.5.4.12_A1_T4 #1'); + eq('--undefined--'.search(), 0, 'S15.5.4.12_A1_T4 #2'); + eq('gnulluna'.search(null), 1, 'S15.5.4.12_A1_T5'); + eq(Object('undefined').search(void 8), 0, 'S15.5.4.12_A1_T6'); + eq('undefined'.search(void 8), 0, 'S15.5.4.12_A1_T7'); + eq(String({ + toString: function(){} + }).search(void 8), 0, 'S15.5.4.12_A1_T8'); + eq('ssABB\u0041BABAB'.search({ + toString: function(){ + return '\u0041B'; + } + }), 2, 'S15.5.4.12_A1_T10'); + try { + 'ABB\u0041BABAB'.search({ + toString: function(){ + throw 'intostr'; + } + }); + ok(false, 'S15.5.4.12_A1_T11 #1 lead to throwing exception'); + } catch (e$) { + e = e$; + eq(e, 'intostr', 'S15.5.4.12_A1_T11 #2'); + } + try { + Object('ABB\u0041BABAB').search({ + toString: function(){ + return {}; + }, + valueOf: function(){ + throw 'intostr'; + } + }); + ok(false, 'S15.5.4.12_A1_T12 #1 lead to throwing exception'); + } catch (e$) { + e = e$; + eq(e, 'intostr', 'S15.5.4.12_A1_T12 #2'); + } + eq('ABB\u0041B\u0031ABAB\u0031BBAA'.search({ + toString: function(){ + return {}; + }, + valueOf: function(){ + return 1; + } + }), 5, 'S15.5.4.12_A1_T13'); + eq('ABB\u0041BABAB\u0037\u0037BBAA'.search(RegExp('77')), 9, 'S15.5.4.12_A1_T14'); + eq(Object('test string').search('string'), 5, 'S15.5.4.12_A2_T1'); + eq(Object('test string').search('String'), -1, 'S15.5.4.12_A2_T2'); + eq(Object('test string').search(/String/i), 5, 'S15.5.4.12_A2_T3'); + eq(Object('test string').search(/Four/), -1, 'S15.5.4.12_A2_T4'); + eq(Object('one two three four five').search(/four/), 14, 'S15.5.4.12_A2_T5'); + eq(Object('test string').search('notexist'), -1, 'S15.5.4.12_A2_T6'); + eq(Object('test string probe').search('string pro'), 5, 'S15.5.4.12_A2_T7'); + aString = Object('power of the power of the power of the power of the power of the power of the great sword'); + eq(aString.search(/the/), aString.search(/the/g), 'S15.5.4.12_A3_T1'); + aString = Object('power \u006F\u0066 the power of the power \u006F\u0066 the power of the power \u006F\u0066 the power of the great sword'); + eq(aString.search(/of/), aString.search(/of/g), 'S15.5.4.12_A3_T2'); + }); + test('RegExp#@@search', function(){ + ok(toString$.call(/./[Symbol.search]).slice(8, -1) === 'Function', 'RegExp#@@search is function'); + eq(/./[Symbol.search].length, 1, 'RegExp#@@search length is 1'); + eq(/four/[Symbol.search]('one two three four five'), 14); + eq(/Four/[Symbol.search]('one two three four five'), -1); + }); + test('@@search logic', function(){ + var O, ref$, re; + O = (ref$ = {}, ref$[Symbol.search] = function(it){ + return { + value: it + }; + }, ref$); + eq('qwe'.search(O).value, 'qwe'); + eq(''.search.call(123, O).value, '123'); + re = /./; + re[Symbol.search] = function(it){ + return { + value: it + }; + }; + eq('qwe'.search(re).value, 'qwe'); + eq(''.search.call(123, re).value, '123'); + }); +}).call(this); + // Generated by LiveScript 1.3.1 (function(){ var isFunction, isIterator, same, getOwnPropertyDescriptor, freeze, eq, deq, toString$ = {}.toString; diff --git a/tests/tests/es6.regexp.search.ls b/tests/tests/es6.regexp.search.ls new file mode 100644 index 000000000000..b805eca6c840 --- /dev/null +++ b/tests/tests/es6.regexp.search.ls @@ -0,0 +1,62 @@ +QUnit.module \ES6 + +eq = strictEqual + +test 'String#search regression' !-> + ok typeof! ''search is \Function, 'String#search is function' + eq ''search.length, 1, 'String#search length is 1' + ok /native code/.test(''search), 'looks like native' + if \name of ''search => eq ''search.name, \search, 'String#search is "search" (can fail if compressed)' + # based on https://github.com/tc39/test262/tree/master/test/built-ins/String/prototype/search + instance = Object on + instance.search = String::search + eq instance.search(on), 0, 'S15.5.4.12_A1_T1' + instance = Object no + instance.search = String::search + eq instance.search(no), 0, 'S15.5.4.12_A1_T2' + eq ''search!, 0, 'S15.5.4.12_A1_T4 #1' + eq '--undefined--'search!, 0, 'S15.5.4.12_A1_T4 #2' + eq 'gnulluna'search(null), 1, 'S15.5.4.12_A1_T5' + eq Object(\undefined)search(void), 0, 'S15.5.4.12_A1_T6' + eq 'undefined'search(void), 0, 'S15.5.4.12_A1_T7' + eq String({toString: ->})search(void), 0, 'S15.5.4.12_A1_T8' + eq 'ssABB\u0041BABAB'search({toString: -> '\u0041B'}), 2, 'S15.5.4.12_A1_T10' + try + 'ABB\u0041BABAB'search {toString: -> throw \intostr} + ok no, 'S15.5.4.12_A1_T11 #1 lead to throwing exception' + catch e + eq e, \intostr, 'S15.5.4.12_A1_T11 #2' + try + Object('ABB\u0041BABAB')search {toString: (-> {}), valueOf: -> throw \intostr} + ok no, 'S15.5.4.12_A1_T12 #1 lead to throwing exception' + catch e + eq e, \intostr, 'S15.5.4.12_A1_T12 #2' + eq 'ABB\u0041B\u0031ABAB\u0031BBAA'search({toString: (-> {}), valueOf: -> 1}), 5, 'S15.5.4.12_A1_T13' + eq 'ABB\u0041BABAB\u0037\u0037BBAA'search(RegExp \77), 9, 'S15.5.4.12_A1_T14' + eq Object('test string')search(\string), 5, 'S15.5.4.12_A2_T1' + eq Object('test string')search(\String), -1, 'S15.5.4.12_A2_T2' + eq Object('test string')search(/String/i), 5, 'S15.5.4.12_A2_T3' + eq Object('test string')search(/Four/), -1, 'S15.5.4.12_A2_T4' + eq Object('one two three four five')search(/four/), 14, 'S15.5.4.12_A2_T5' + eq Object('test string')search(\notexist), -1, 'S15.5.4.12_A2_T6' + eq Object('test string probe')search('string pro'), 5, 'S15.5.4.12_A2_T7' + aString = Object 'power of the power of the power of the power of the power of the power of the great sword' + eq aString.search(/the/), aString.search(/the/g), 'S15.5.4.12_A3_T1' + aString = Object 'power \u006F\u0066 the power of the power \u006F\u0066 the power of the power \u006F\u0066 the power of the great sword' + eq aString.search(/of/), aString.search(/of/g), 'S15.5.4.12_A3_T2' + +test 'RegExp#@@search' !-> + ok typeof! /./[Symbol.search] is \Function, 'RegExp#@@search is function' + eq /./[Symbol.search].length, 1, 'RegExp#@@search length is 1' + eq /four/[Symbol.search]('one two three four five'), 14 + eq /Four/[Symbol.search]('one two three four five'), -1 + +test '@@search logic' !-> + O = {(Symbol.search): -> {value: it}} + eq 'qwe'search(O)value, \qwe + eq ''search.call(123, O)value, \123 + re = /./ + re[Symbol.search] = -> {value: it} + eq 'qwe'search(re)value, \qwe + eq ''search.call(123, re)value, \123 + \ No newline at end of file