chore: reformat with 2 spaces

This commit is contained in:
Johannes Loher 2023-07-10 22:23:13 +02:00
parent d659e4bed9
commit 7670d7f808
No known key found for this signature in database
GPG key ID: 7CB0A9FB553DA045
1577 changed files with 70010 additions and 70042 deletions

View file

@ -7,84 +7,84 @@ import { describe, expect, it } from "vitest";
import { defaultEvaluator, Evaluator, mathEvaluator } from "../../src/expression-evaluation/evaluator";
describe("Evaluator", () => {
it("evaluates expressions that only use identifiers according to the given predicate", () => {
// given
const expression = "typeof 'foo' === 'string' ? 42 : null";
it("evaluates expressions that only use identifiers according to the given predicate", () => {
// given
const expression = "typeof 'foo' === 'string' ? 42 : null";
// when
const result = defaultEvaluator.evaluate(expression);
// when
const result = defaultEvaluator.evaluate(expression);
// then
expect(result).toEqual(42);
});
// then
expect(result).toEqual(42);
});
it("fails to evaluate expressions that contain identifiers that are not allowed by the predicate", () => {
// given
const expression = "typeof 'foo' === 'string' ? 42 : function (){}";
it("fails to evaluate expressions that contain identifiers that are not allowed by the predicate", () => {
// given
const expression = "typeof 'foo' === 'string' ? 42 : function (){}";
// when
const evaluate = () => defaultEvaluator.evaluate(expression);
// when
const evaluate = () => defaultEvaluator.evaluate(expression);
// then
expect(evaluate).toThrowError("'function' is not an allowed identifier.");
});
// then
expect(evaluate).toThrowError("'function' is not an allowed identifier.");
});
it("fails to evaluate expressions that contain invalid tokens", () => {
// given
const expression = "1;";
it("fails to evaluate expressions that contain invalid tokens", () => {
// given
const expression = "1;";
// when
const evaluate = () => defaultEvaluator.evaluate(expression);
// when
const evaluate = () => defaultEvaluator.evaluate(expression);
// then
expect(evaluate).toThrowError("Invalid or unexpected token (1)");
});
// then
expect(evaluate).toThrowError("Invalid or unexpected token (1)");
});
it("fails to evaluate expressions that contain arrow functions", () => {
// given
const expression = "(() => 1)()";
it("fails to evaluate expressions that contain arrow functions", () => {
// given
const expression = "(() => 1)()";
// when
const evaluate = () => defaultEvaluator.evaluate(expression);
// when
const evaluate = () => defaultEvaluator.evaluate(expression);
// then
expect(evaluate).toThrowError("Invalid or unexpected token (4)");
});
// then
expect(evaluate).toThrowError("Invalid or unexpected token (4)");
});
it("makes the given context available", () => {
// given
const context = { floor: Math.floor };
const evaluator = new Evaluator({ context });
const expression = "floor(0.5)";
// when
const result = evaluator.evaluate(expression);
// then
expect(result).toEqual(0);
});
describe("mathEvaluator", () => {
it("makes the given context available", () => {
// given
const context = { floor: Math.floor };
const evaluator = new Evaluator({ context });
const expression = "floor(0.5)";
// given
const expression = "sqrt(sin(PI))";
// when
const result = evaluator.evaluate(expression);
// when
const result = mathEvaluator.evaluate(expression);
// then
expect(result).toEqual(0);
// then
expect(result).toEqual(Math.sqrt(Math.sin(Math.PI)));
});
describe("mathEvaluator", () => {
it("makes the given context available", () => {
// given
const expression = "sqrt(sin(PI))";
it("does not give acces to the function constructor", () => {
// given
const expression = "sqrt.constructor";
// when
const result = mathEvaluator.evaluate(expression);
// when
const evaluate = () => mathEvaluator.evaluate(expression);
// then
expect(result).toEqual(Math.sqrt(Math.sin(Math.PI)));
});
it("does not give acces to the function constructor", () => {
// given
const expression = "sqrt.constructor";
// when
const evaluate = () => mathEvaluator.evaluate(expression);
// then
expect(evaluate).toThrowError("'constructor' is not an allowed identifier.");
});
// then
expect(evaluate).toThrowError("'constructor' is not an allowed identifier.");
});
});
});

File diff suppressed because it is too large Load diff

View file

@ -8,119 +8,118 @@ import { literals, safeOperators } from "../../src/expression-evaluation/grammar
import { Validator } from "../../src/expression-evaluation/validator";
describe("Validator", () => {
it("allows identifier according to the given predicate", () => {
// given
const predicate = (identifier: string) => identifier === "true";
const validator = new Validator(predicate);
const input = "true";
it("allows identifier according to the given predicate", () => {
// given
const predicate = (identifier: string) => identifier === "true";
const validator = new Validator(predicate);
const input = "true";
// when
const validate = () => validator.validate(input);
// when
const validate = () => validator.validate(input);
// then
expect(validate).not.toThrow();
});
// then
expect(validate).not.toThrow();
});
it("disallows identifier according to the given predicate", () => {
// given
const predicate = (identifier: string) => identifier === "false";
const validator = new Validator(predicate);
const input = "true";
it("disallows identifier according to the given predicate", () => {
// given
const predicate = (identifier: string) => identifier === "false";
const validator = new Validator(predicate);
const input = "true";
// when
const validate = () => validator.validate(input);
// when
const validate = () => validator.validate(input);
// then
expect(validate).toThrowError("'true' is not an allowed identifier");
});
// then
expect(validate).toThrowError("'true' is not an allowed identifier");
});
it("allows multiple identifiers according to the given predicate", () => {
// given
const predicate = (identifier: string) => identifier === "true" || identifier === "null";
const validator = new Validator(predicate);
const input = "true null";
it("allows multiple identifiers according to the given predicate", () => {
// given
const predicate = (identifier: string) => identifier === "true" || identifier === "null";
const validator = new Validator(predicate);
const input = "true null";
// when
const validate = () => validator.validate(input);
// when
const validate = () => validator.validate(input);
// then
expect(validate).not.toThrow();
});
// then
expect(validate).not.toThrow();
});
it("allows multiple identifiers in a more complex expression according to the given rule", () => {
// given
const predicate = (identifier: string) => identifier === "true" || identifier === "null";
const validator = new Validator(predicate);
const input = "true === null";
it("allows multiple identifiers in a more complex expression according to the given rule", () => {
// given
const predicate = (identifier: string) => identifier === "true" || identifier === "null";
const validator = new Validator(predicate);
const input = "true === null";
// when
const validate = () => validator.validate(input);
// when
const validate = () => validator.validate(input);
// then
expect(validate).not.toThrow();
});
// then
expect(validate).not.toThrow();
});
it("mentions the first not allowed identifier in the thrown errror", () => {
// given
const predicate = (identifier: string) => identifier === "true" || identifier === "null";
const validator = new Validator(predicate);
const input = "true === null && undefined === false";
it("mentions the first not allowed identifier in the thrown errror", () => {
// given
const predicate = (identifier: string) => identifier === "true" || identifier === "null";
const validator = new Validator(predicate);
const input = "true === null && undefined === false";
// when
const validate = () => validator.validate(input);
// when
const validate = () => validator.validate(input);
// then
expect(validate).toThrowError("'undefined' is not an allowed identifier.");
});
// then
expect(validate).toThrowError("'undefined' is not an allowed identifier.");
});
it("disallows invalid invalid tokens", () => {
// given
const validator = new Validator();
const input = ";";
it("disallows invalid invalid tokens", () => {
// given
const validator = new Validator();
const input = ";";
// when
const validate = () => validator.validate(input);
// when
const validate = () => validator.validate(input);
// then
expect(validate).toThrowError("Invalid or unexpected token (0)");
});
// then
expect(validate).toThrowError("Invalid or unexpected token (0)");
});
it("allows a complicated valid expression", () => {
// given
const predicate = (identifier: string) =>
[...safeOperators, ...literals, "floor", "random"].includes(identifier);
const validator = new Validator(predicate);
const input = "typeof (floor(random() * 5) / 2) === 'number' ? 42 : 'foo'";
it("allows a complicated valid expression", () => {
// given
const predicate = (identifier: string) => [...safeOperators, ...literals, "floor", "random"].includes(identifier);
const validator = new Validator(predicate);
const input = "typeof (floor(random() * 5) / 2) === 'number' ? 42 : 'foo'";
// when
const validate = () => validator.validate(input);
// when
const validate = () => validator.validate(input);
// then
expect(validate).not.toThrow();
});
// then
expect(validate).not.toThrow();
});
it("disallows a complicated expression if it contains a disallowed identifier", () => {
// given
const predicate = (identifier: string) => [...safeOperators, ...literals, "ceil"].includes(identifier);
const validator = new Validator(predicate);
const input = "ceil.constructor('alert(1); return 1;')()";
it("disallows a complicated expression if it contains a disallowed identifier", () => {
// given
const predicate = (identifier: string) => [...safeOperators, ...literals, "ceil"].includes(identifier);
const validator = new Validator(predicate);
const input = "ceil.constructor('alert(1); return 1;')()";
// when
const validate = () => validator.validate(input);
// when
const validate = () => validator.validate(input);
// then
expect(validate).toThrowError("'constructor' is not an allowed identifier.");
});
// then
expect(validate).toThrowError("'constructor' is not an allowed identifier.");
});
it("disallows arrow functions", () => {
// given
const validator = new Validator();
const input = "() => {}";
it("disallows arrow functions", () => {
// given
const validator = new Validator();
const input = "() => {}";
// when
const validate = () => validator.validate(input);
// when
const validate = () => validator.validate(input);
// then
expect(validate).toThrowError("Invalid or unexpected token (3)");
});
// then
expect(validate).toThrowError("Invalid or unexpected token (3)");
});
});