Some checks failed
Deploy to GitHub Pages / build-and-deploy (push) Has been cancelled
94 lines
3.2 KiB
JavaScript
94 lines
3.2 KiB
JavaScript
// Contract test for Core Calculator API
|
|
// This test should fail initially since the calculator module doesn't exist yet
|
|
|
|
// Define expected interface for calculator module
|
|
const expectedCalculatorInterface = [
|
|
'evaluate',
|
|
'add',
|
|
'subtract',
|
|
'multiply',
|
|
'divide',
|
|
'percentage',
|
|
'toggleSign',
|
|
'clear',
|
|
'getCurrentDisplay'
|
|
];
|
|
|
|
// Test if calculator module exists and has expected interface
|
|
function testCalculatorInterface() {
|
|
try {
|
|
// This will fail since calculator.js doesn't exist yet
|
|
const calc = new Calculator();
|
|
|
|
// Check if all expected methods exist
|
|
for (const method of expectedCalculatorInterface) {
|
|
if (typeof calc[method] !== 'function') {
|
|
throw new Error(`Method ${method} does not exist on Calculator`);
|
|
}
|
|
}
|
|
|
|
// Test basic functionality
|
|
// Add should return sum of two numbers
|
|
if (calc.add(2, 3) !== 5) {
|
|
throw new Error('Calculator add method does not return correct result');
|
|
}
|
|
|
|
// Subtract should return difference
|
|
if (calc.subtract(5, 3) !== 2) {
|
|
throw new Error('Calculator subtract method does not return correct result');
|
|
}
|
|
|
|
// Multiply should return product
|
|
if (calc.multiply(4, 5) !== 20) {
|
|
throw new Error('Calculator multiply method does not return correct result');
|
|
}
|
|
|
|
// Divide should return quotient
|
|
if (calc.divide(10, 2) !== 5) {
|
|
throw new Error('Calculator divide method does not return correct result');
|
|
}
|
|
|
|
// Division by zero should return Infinity
|
|
if (!isFinite(calc.divide(10, 0))) {
|
|
// This is expected, division by zero returns Infinity
|
|
} else {
|
|
throw new Error('Calculator divide by zero does not return Infinity');
|
|
}
|
|
|
|
// Percentage should convert percentage to decimal
|
|
if (calc.percentage(50) !== 0.5) {
|
|
throw new Error('Calculator percentage method does not return correct result');
|
|
}
|
|
|
|
// ToggleSign should change sign
|
|
if (calc.toggleSign(5) !== -5) {
|
|
throw new Error('Calculator toggleSign method does not return correct result');
|
|
}
|
|
|
|
if (calc.toggleSign(-5) !== 5) {
|
|
throw new Error('Calculator toggleSign method does not return correct result for negative number');
|
|
}
|
|
|
|
// Clear should reset to initial state
|
|
calc.clear();
|
|
if (calc.getCurrentDisplay() !== '0') {
|
|
throw new Error('Calculator clear method does not reset to initial state');
|
|
}
|
|
|
|
// Evaluate should handle simple expressions
|
|
if (calc.evaluate('2 + 3') !== 5) {
|
|
throw new Error('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('Calculator Interface Contract', testCalculatorInterface);
|
|
} else {
|
|
console.log('Test harness not loaded. Run with test runner.');
|
|
} |