Skip to content

Commit

Permalink
Adding unit test for Luigi Pulcini's logical expression pull request
Browse files Browse the repository at this point in the history
  • Loading branch information
bylexus committed Aug 24, 2024
1 parent e1ae97a commit f50a02b
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 9 deletions.
2 changes: 1 addition & 1 deletion dist/fparser.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ export default class Formula {
*/
buildExpressionTree(expressions: Expression[]): Expression;
isOperator(char: string | null): false | RegExpMatchArray | null;
isOperatorExpr(expr: Expression): expr is PowerExpression | MultDivExpression | PlusMinusExpression | LogicalExpression;
isOperatorExpr(expr: Expression): boolean;
registerVariable(varName: string): void;
getVariables(): string[];
/**
Expand Down
12 changes: 7 additions & 5 deletions dist/fparser.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/fparser.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/fparser.umd.cjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/fparser.umd.cjs.map

Large diffs are not rendered by default.

32 changes: 32 additions & 0 deletions spec/specs/exprTreeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,38 @@ describe('Expression Tree tests', function () {
);
});

it('evaluates the order of logical expressions correct', () => {
// logical operators should have the lowest precedence.
// 3+2>1*4<2^3 should be evaulated as:
// ( 3+2 )>( 1*4 )<( 2^3 )
// (5 > 4) < 8
// 1 < 8
// which evaulates to 1
const formulaStr = '3+2>1*4<2^3';
const f = new Fparser();
let ret = f.parse(formulaStr);
expect(ret).toEqual(
new Fparser.LogicalExpression(
'<',
new Fparser.LogicalExpression(
'>',
new Fparser.PlusMinusExpression(
'+',
new Fparser.ValueExpression(3),
new Fparser.ValueExpression(2)
),
new Fparser.MultDivExpression(
'*',
new Fparser.ValueExpression(1),
new Fparser.ValueExpression(4)
)
),
new Fparser.PowerExpression(new Fparser.ValueExpression(2), new Fparser.ValueExpression(3))
)
);
expect(ret.evaluate()).toBe(1);
});

it('parses parentheses correctly', () => {
const formulaStr = '2*(3+4)/4x*(3-y)';
const f = new Fparser();
Expand Down

0 comments on commit f50a02b

Please sign in to comment.