Skip to content

Commit

Permalink
fix helper and add scriptlet tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislav-atr committed Jan 9, 2023
1 parent bcef888 commit 105ea07
Show file tree
Hide file tree
Showing 6 changed files with 400 additions and 48 deletions.
15 changes: 10 additions & 5 deletions src/helpers/string-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,11 +355,11 @@ export function generateRandomResponse(customResponseText) {

/**
* Infers value from string argument
* Inferring goes from more specific to more ambiguous options
* @param {string} value
* @returns {any}
*/
export function inferValue(value) {
// describe waterfall here
if (value === 'undefined') {
return undefined;
} if (value === 'false') {
Expand All @@ -372,18 +372,23 @@ export function inferValue(value) {
return NaN;
}

const numVal = parseFloat(value, 10)
const numVal = parseFloat(value, 10);
if (!nativeIsNaN(numVal)) {
if (Math.abs(numVal) > 0x7FFF) {
throw new Error('number values bigger than 32767 are not allowed');
}
return numVal;
}

let errorMessage = `'${value}' value type can't be inferred`;
try {
const parsableVal = JSON.parse(value);
if (parsableVal instanceof Object) {
return parsableVal;
}
} catch { /* no */ }
} catch (e) {
errorMessage += `: ${e}`;
}

const errorMessage = `'${value}' argument's value can't be inferred`;
throw new TypeError(errorMessage);
}
}
10 changes: 8 additions & 2 deletions src/scriptlets/trusted-set-constant.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,19 @@ import {
* ```
*/
/* eslint-enable max-len */
export function trustedSetConstant(source, property, value, infer = false, stack) {
export function trustedSetConstant(source, property, value, shouldInfer, stack) {
if (!property
|| !matchStackTrace(stack, new Error().stack)) {
return;
}

const constantValue = infer ? inferValue(value, type) : value;
let constantValue;
try {
constantValue = shouldInfer === 'infer' ? inferValue(value) : value;
} catch (e) {
logMessage(source, e);
return;
}

let canceled = false;
const mustCancel = (value) => {
Expand Down
File renamed without changes.
82 changes: 41 additions & 41 deletions tests/helpers/string-utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,47 @@ const name = 'scriptlets-redirects helpers';

module(name);

// test('Test toRegExp for valid inputs', (assert) => {
// const DEFAULT_VALUE = '.?';
// const defaultRegexp = new RegExp(DEFAULT_VALUE);
// let inputStr;
// let expRegex;

// inputStr = '/abc/';
// expRegex = /abc/;
// assert.deepEqual(toRegExp(inputStr), expRegex);

// inputStr = '/[a-z]{1,9}/';
// expRegex = /[a-z]{1,9}/;
// assert.deepEqual(toRegExp(inputStr), expRegex);

// inputStr = '';
// assert.deepEqual(toRegExp(inputStr), defaultRegexp);
// });

// test('Test toRegExp for invalid inputs', (assert) => {
// let inputStr;

// assert.throws(() => {
// inputStr = '/\\/';
// toRegExp(inputStr);
// });

// assert.throws(() => {
// inputStr = '/[/';
// toRegExp(inputStr);
// });

// assert.throws(() => {
// inputStr = '/*/';
// toRegExp(inputStr);
// });

// assert.throws(() => {
// inputStr = '/[0-9]++/';
// toRegExp(inputStr);
// });
// });
test('Test toRegExp for valid inputs', (assert) => {
const DEFAULT_VALUE = '.?';
const defaultRegexp = new RegExp(DEFAULT_VALUE);
let inputStr;
let expRegex;

inputStr = '/abc/';
expRegex = /abc/;
assert.deepEqual(toRegExp(inputStr), expRegex);

inputStr = '/[a-z]{1,9}/';
expRegex = /[a-z]{1,9}/;
assert.deepEqual(toRegExp(inputStr), expRegex);

inputStr = '';
assert.deepEqual(toRegExp(inputStr), defaultRegexp);
});

test('Test toRegExp for invalid inputs', (assert) => {
let inputStr;

assert.throws(() => {
inputStr = '/\\/';
toRegExp(inputStr);
});

assert.throws(() => {
inputStr = '/[/';
toRegExp(inputStr);
});

assert.throws(() => {
inputStr = '/*/';
toRegExp(inputStr);
});

assert.throws(() => {
inputStr = '/[0-9]++/';
toRegExp(inputStr);
});
});

test('inferValue works as expected with valid args', (assert) => {
// convert to number
Expand Down
1 change: 1 addition & 0 deletions tests/scriptlets/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,4 @@ import './trusted-click-element.test';
import './trusted-set-cookie.test';
import './trusted-replace-fetch-response.test';
import './trusted-set-local-storage-item.test';
import './trusted-set-constant.test';
Loading

0 comments on commit 105ea07

Please sign in to comment.