Files
geek-calc/tests/unit/rpn-calculator.test.js
snowprint 54f427ea21
Some checks failed
Deploy to GitHub Pages / build-and-deploy (push) Has been cancelled
init geek calc
2025-10-04 10:53:41 +08:00

79 lines
2.7 KiB
JavaScript

// Contract test for RPN Calculator API
// This test should fail initially since the rpn-calculator module doesn't exist yet
// Define expected interface for RPN calculator module
const expectedRPNCalculatorInterface = [
'push',
'pop',
'operate',
'clear',
'getStack',
'evaluate'
];
// Test if RPN calculator module exists and has expected interface
function testRPNCalculatorInterface() {
try {
// This will fail since rpn-calculator.js doesn't exist yet
const rpnCalc = new RPNCalculator();
// Check if all expected methods exist
for (const method of expectedRPNCalculatorInterface) {
if (typeof rpnCalc[method] !== 'function') {
throw new Error(`Method ${method} does not exist on RPNCalculator`);
}
}
// Test basic RPN functionality
// Push should add value to stack
rpnCalc.push(3);
const stackAfterPush = rpnCalc.getStack();
if (stackAfterPush.length !== 1 || stackAfterPush[0] !== 3) {
throw new Error('RPN calculator push method does not work correctly');
}
// Pop should remove and return top value from stack
const poppedValue = rpnCalc.pop();
if (poppedValue !== 3) {
throw new Error('RPN calculator pop method does not return correct value');
}
// Stack should be empty after pop
if (rpnCalc.getStack().length !== 0) {
throw new Error('RPN calculator stack not empty after pop');
}
// Clear should empty the stack
rpnCalc.push(1);
rpnCalc.push(2);
rpnCalc.clear();
if (rpnCalc.getStack().length !== 0) {
throw new Error('RPN calculator clear method does not empty the stack');
}
// Operate should perform operation on stack values
rpnCalc.push(2);
rpnCalc.push(3);
const result = rpnCalc.operate('+');
if (result !== 5) {
throw new Error('RPN calculator operate method does not return correct result for addition');
}
// Evaluate should handle RPN expressions
const evalResult = rpnCalc.evaluate('2 3 +');
if (evalResult !== 5) {
throw new Error('RPN calculator evaluate method does not return correct result');
}
return true; // All tests passed
} catch (error) {
return { error: error.message };
}
}
// Add test to global test collection
if (typeof addTest !== 'undefined') {
addTest('RPN Calculator Interface Contract', testRPNCalculatorInterface);
} else {
console.log('Test harness not loaded. Run with test runner.');
}