11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ci// Tests below are not from WPT. 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_cirequire('../common'); 61cb0ef41Sopenharmony_ciconst assert = require('assert'); 71cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures'); 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ciconst serialized = 'a=a&a=1&a=true&a=undefined&a=null&a=%EF%BF%BD' + 101cb0ef41Sopenharmony_ci '&a=%EF%BF%BD&a=%F0%9F%98%80&a=%EF%BF%BD%EF%BF%BD' + 111cb0ef41Sopenharmony_ci '&a=%5Bobject+Object%5D'; 121cb0ef41Sopenharmony_ciconst values = ['a', 1, true, undefined, null, '\uD83D', '\uDE00', 131cb0ef41Sopenharmony_ci '\uD83D\uDE00', '\uDE00\uD83D', {}]; 141cb0ef41Sopenharmony_ciconst normalizedValues = ['a', '1', 'true', 'undefined', 'null', '\uFFFD', 151cb0ef41Sopenharmony_ci '\uFFFD', '\uD83D\uDE00', '\uFFFD\uFFFD', 161cb0ef41Sopenharmony_ci '[object Object]']; 171cb0ef41Sopenharmony_ci 181cb0ef41Sopenharmony_ciconst m = new URL('http://example.org'); 191cb0ef41Sopenharmony_ciconst ownSymbolsBeforeGetterAccess = Object.getOwnPropertySymbols(m); 201cb0ef41Sopenharmony_ciconst sp = m.searchParams; 211cb0ef41Sopenharmony_ciassert.deepStrictEqual(Object.getOwnPropertySymbols(m), ownSymbolsBeforeGetterAccess); 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_ciassert(sp); 241cb0ef41Sopenharmony_ciassert.strictEqual(sp.toString(), ''); 251cb0ef41Sopenharmony_ciassert.strictEqual(m.search, ''); 261cb0ef41Sopenharmony_ci 271cb0ef41Sopenharmony_ciassert(!sp.has('a')); 281cb0ef41Sopenharmony_civalues.forEach((i) => sp.set('a', i)); 291cb0ef41Sopenharmony_ciassert(sp.has('a')); 301cb0ef41Sopenharmony_ciassert.strictEqual(sp.get('a'), '[object Object]'); 311cb0ef41Sopenharmony_cisp.delete('a'); 321cb0ef41Sopenharmony_ciassert(!sp.has('a')); 331cb0ef41Sopenharmony_ci 341cb0ef41Sopenharmony_cim.search = ''; 351cb0ef41Sopenharmony_ciassert.strictEqual(sp.toString(), ''); 361cb0ef41Sopenharmony_ci 371cb0ef41Sopenharmony_civalues.forEach((i) => sp.append('a', i)); 381cb0ef41Sopenharmony_ciassert(sp.has('a')); 391cb0ef41Sopenharmony_ciassert.strictEqual(sp.getAll('a').length, values.length); 401cb0ef41Sopenharmony_ciassert.strictEqual(sp.get('a'), 'a'); 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_ciassert.strictEqual(sp.toString(), serialized); 431cb0ef41Sopenharmony_ci 441cb0ef41Sopenharmony_ciassert.strictEqual(m.search, `?${serialized}`); 451cb0ef41Sopenharmony_ci 461cb0ef41Sopenharmony_ciassert.strictEqual(sp[Symbol.iterator], sp.entries); 471cb0ef41Sopenharmony_ci 481cb0ef41Sopenharmony_cilet key, val; 491cb0ef41Sopenharmony_cilet n = 0; 501cb0ef41Sopenharmony_cifor ([key, val] of sp) { 511cb0ef41Sopenharmony_ci assert.strictEqual(key, 'a', n); 521cb0ef41Sopenharmony_ci assert.strictEqual(val, normalizedValues[n], n); 531cb0ef41Sopenharmony_ci n++; 541cb0ef41Sopenharmony_ci} 551cb0ef41Sopenharmony_cin = 0; 561cb0ef41Sopenharmony_cifor (key of sp.keys()) { 571cb0ef41Sopenharmony_ci assert.strictEqual(key, 'a', n); 581cb0ef41Sopenharmony_ci n++; 591cb0ef41Sopenharmony_ci} 601cb0ef41Sopenharmony_cin = 0; 611cb0ef41Sopenharmony_cifor (val of sp.values()) { 621cb0ef41Sopenharmony_ci assert.strictEqual(val, normalizedValues[n], n); 631cb0ef41Sopenharmony_ci n++; 641cb0ef41Sopenharmony_ci} 651cb0ef41Sopenharmony_cin = 0; 661cb0ef41Sopenharmony_cisp.forEach(function(val, key, obj) { 671cb0ef41Sopenharmony_ci assert.strictEqual(this, undefined, n); 681cb0ef41Sopenharmony_ci assert.strictEqual(key, 'a', n); 691cb0ef41Sopenharmony_ci assert.strictEqual(val, normalizedValues[n], n); 701cb0ef41Sopenharmony_ci assert.strictEqual(obj, sp, n); 711cb0ef41Sopenharmony_ci n++; 721cb0ef41Sopenharmony_ci}); 731cb0ef41Sopenharmony_cisp.forEach(function() { 741cb0ef41Sopenharmony_ci assert.strictEqual(this, m); 751cb0ef41Sopenharmony_ci}, m); 761cb0ef41Sopenharmony_ci 771cb0ef41Sopenharmony_ci{ 781cb0ef41Sopenharmony_ci const callbackErr = { 791cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE', 801cb0ef41Sopenharmony_ci name: 'TypeError' 811cb0ef41Sopenharmony_ci }; 821cb0ef41Sopenharmony_ci assert.throws(() => sp.forEach(), callbackErr); 831cb0ef41Sopenharmony_ci assert.throws(() => sp.forEach(1), callbackErr); 841cb0ef41Sopenharmony_ci} 851cb0ef41Sopenharmony_ci 861cb0ef41Sopenharmony_cim.search = '?a=a&b=b'; 871cb0ef41Sopenharmony_ciassert.strictEqual(sp.toString(), 'a=a&b=b'); 881cb0ef41Sopenharmony_ci 891cb0ef41Sopenharmony_ciconst tests = require(fixtures.path('url-searchparams.js')); 901cb0ef41Sopenharmony_ci 911cb0ef41Sopenharmony_cifor (const [input, expected, parsed] of tests) { 921cb0ef41Sopenharmony_ci if (input[0] !== '?') { 931cb0ef41Sopenharmony_ci const sp = new URLSearchParams(input); 941cb0ef41Sopenharmony_ci assert.strictEqual(String(sp), expected); 951cb0ef41Sopenharmony_ci assert.deepStrictEqual(Array.from(sp), parsed); 961cb0ef41Sopenharmony_ci 971cb0ef41Sopenharmony_ci m.search = input; 981cb0ef41Sopenharmony_ci assert.strictEqual(String(m.searchParams), expected); 991cb0ef41Sopenharmony_ci assert.deepStrictEqual(Array.from(m.searchParams), parsed); 1001cb0ef41Sopenharmony_ci } 1011cb0ef41Sopenharmony_ci 1021cb0ef41Sopenharmony_ci { 1031cb0ef41Sopenharmony_ci const sp = new URLSearchParams(`?${input}`); 1041cb0ef41Sopenharmony_ci assert.strictEqual(String(sp), expected); 1051cb0ef41Sopenharmony_ci assert.deepStrictEqual(Array.from(sp), parsed); 1061cb0ef41Sopenharmony_ci 1071cb0ef41Sopenharmony_ci m.search = `?${input}`; 1081cb0ef41Sopenharmony_ci assert.strictEqual(String(m.searchParams), expected); 1091cb0ef41Sopenharmony_ci assert.deepStrictEqual(Array.from(m.searchParams), parsed); 1101cb0ef41Sopenharmony_ci } 1111cb0ef41Sopenharmony_ci} 112