⚠️ For demonstration purposes, this code prioritizes simplicity over best practices and is not suitable for production.
Install prerequisites:
npm install -g allure-commandline
Install dependencies:
find . -maxdepth 2 -name 'package.json' ! -path './node_modules/*' -print0 | xargs -0 -n1 dirname | sort -u | xargs -I {} npm install --prefix {}
Start the app:
npm run start
Run tests using another cmd window (or a tmux pane if you're awesome):
cd __tests__
npm test
Run reports:
npm run report
Outcome: Update the application to evaluate exponents:
Create a new feature to the parser to enable exponents.
- Create a branch for the feature
git checkout -b exponent_feature
- Add the scenario to
__test__/features/expression_evaluation.feature
Scenario: Basic Exponents Given I have the expression "5^2" When I send the expression to the backend Then I should get the result 25
- Push branch for dev
git push --set-upstream origin exponent_feature
- Pull branch
git pull git checkout exponent_feature
- Start the test server
npm start
- Examine test failure (requires a new cmd window)
cd __tests__ npm test
- Close the server by pressing
<Ctrl>+C
- Modify
server/src/expressionParser/tokenizer.ts
export function tokenize(expression: string): Token[] { - const regex = /([\+\-\*\/])|(\d+(\.\d+)?)/g; + const regex = /([\+\-\*\^\/])|(\d+(\.\d+)?)/g; const tokens: Token[] = []; let match; while ((match = regex.exec(expression))) { if (match[1]) { tokens.push({ type: "Operator", value: match[1], }); } else if (match[2]) { tokens.push({ type: "Number", value: match[2], }); } } return tokens; }
- Modify
server/src/expressionParser/parser.ts
function parse(tokens: Token[]): number { if (tokens.length === 1) { return parseFloat(tokens[0].value); } const operatorIndex = tokens.findIndex((token) => token.type === "Operator"); if (operatorIndex === -1) { throw new Error("Invalid expression."); } const operator = tokens[operatorIndex]; const leftTokens = tokens.slice(0, operatorIndex); const rightTokens = tokens.slice(operatorIndex + 1); const leftValue = parse(leftTokens); const rightValue = parse(rightTokens); switch (operator.value) { case "+": return leftValue + rightValue; case "-": return leftValue - rightValue; case "*": return leftValue * rightValue; case "/": if (rightValue === 0) throw new Error("Division by zero."); return leftValue / rightValue; + case "^": + return leftValue ** rightValue; default: throw new Error("Unknown operator: " + operator.value); } }
- Restart the server
npm start
- Enjoy a nice dopamine hit (requires new cmd window)
cd __test__ npm test