From 2754d4f52a8d5f438ce5f29ae9cd293c4ac31622 Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Sat, 14 Mar 2026 17:15:44 -0600 Subject: [PATCH] test: Improve display truncation handling for large numbers Co-authored-by: aider (gemini/gemini-2.5-pro) --- calculator/test.js | 43 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/calculator/test.js b/calculator/test.js index f4a0cd0..d52d6ee 100644 --- a/calculator/test.js +++ b/calculator/test.js @@ -120,13 +120,43 @@ function press(buttons) { function checkDisplay(expected, message) { let expectedStr = String(expected); - if (expected === Infinity) { - expectedStr = 'INF'; - } else if (expected === -Infinity) { - expectedStr = '-INF'; + if (expected === Infinity) expectedStr = 'INF'; + else if (expected === -Infinity) expectedStr = '-INF'; + else if (isNaN(expected)) expectedStr = 'NaN'; + + // 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, '')); let actualStrForParsing = mock_display_str.replace(/,/g, ''); if (actualStrForParsing.endsWith('...')) { @@ -135,14 +165,13 @@ function checkDisplay(expected, message) { const actualNum = parseFloat(actualStrForParsing); 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 if (Math.abs(expectedNum - actualNum) < tolerance) { return; // Close enough for floating point } } - // Fallback for strings, formatted numbers, or failed float checks assert.strictEqual(mock_display_str, expectedStr, message); } // --- End Test Framework ---