test: Improve display truncation handling for large numbers

Co-authored-by: aider (gemini/gemini-2.5-pro) <aider@aider.chat>
This commit is contained in:
2026-03-14 17:15:44 -06:00
committed by Tanner
parent 04efe1b4d9
commit 2754d4f52a
+36 -7
View File
@@ -120,13 +120,43 @@ function press(buttons) {
function checkDisplay(expected, message) { function checkDisplay(expected, message) {
let expectedStr = String(expected); let expectedStr = String(expected);
if (expected === Infinity) { if (expected === Infinity) expectedStr = 'INF';
expectedStr = 'INF'; else if (expected === -Infinity) expectedStr = '-INF';
} else if (expected === -Infinity) { else if (isNaN(expected)) expectedStr = 'NaN';
expectedStr = '-INF';
// If the mock display was truncated, we check if the calculator's formatted
// output *starts with* the truncated display string. This is more robust
// than trying to perfectly replicate font rendering for truncation.
if (mock_display_str.endsWith('...')) {
const actualTruncated = mock_display_str.slice(0, -3);
// Replicate the calculator's number formatting for an accurate comparison.
const addSeparators = (s) => {
var parts = s.split(".");
var intPart = parts[0];
var sign = "";
if (intPart[0] === "-") {
sign = "-";
intPart = intPart.slice(1);
}
var result = "";
while (intPart.length > 3) {
result = "," + intPart.slice(-3) + result;
intPart = intPart.slice(0, -3);
}
result = intPart + result;
parts[0] = sign + result;
return parts.join(".");
};
const expectedFormatted = addSeparators(String(expected));
if (expectedFormatted.startsWith(actualTruncated)) {
return; // The displayed number is a correctly truncated version of the expected one.
}
} }
// attempt a numeric comparison for float-related issues // Fallback to original logic for non-truncated strings or if the truncation check fails.
const expectedNum = parseFloat(expectedStr.replace(/,/g, '')); const expectedNum = parseFloat(expectedStr.replace(/,/g, ''));
let actualStrForParsing = mock_display_str.replace(/,/g, ''); let actualStrForParsing = mock_display_str.replace(/,/g, '');
if (actualStrForParsing.endsWith('...')) { if (actualStrForParsing.endsWith('...')) {
@@ -135,14 +165,13 @@ function checkDisplay(expected, message) {
const actualNum = parseFloat(actualStrForParsing); const actualNum = parseFloat(actualStrForParsing);
if (!isNaN(expectedNum) && !isNaN(actualNum)) { if (!isNaN(expectedNum) && !isNaN(actualNum)) {
if (expectedNum === actualNum) return; // Handles Infinity and exact matches if (expectedNum === actualNum) return; // Handles exact matches
const tolerance = 1e-4; // Increased tolerance for float comparisons const tolerance = 1e-4; // Increased tolerance for float comparisons
if (Math.abs(expectedNum - actualNum) < tolerance) { if (Math.abs(expectedNum - actualNum) < tolerance) {
return; // Close enough for floating point return; // Close enough for floating point
} }
} }
// Fallback for strings, formatted numbers, or failed float checks
assert.strictEqual(mock_display_str, expectedStr, message); assert.strictEqual(mock_display_str, expectedStr, message);
} }
// --- End Test Framework --- // --- End Test Framework ---