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:
+36
-7
@@ -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 ---
|
||||
|
||||
Reference in New Issue
Block a user