From 57d52f2133edb97031fa578528cfce6717108654 Mon Sep 17 00:00:00 2001 From: "Danilo M." Date: Sun, 18 Oct 2026 14:30:54 +0200 Subject: fix(weather): non-finite numbers no longer raise deg, num, duration and moon_icon now return their missing-value placeholder for NaN, inf and out-of-range input instead of raising from %d or utf8.char. sun_t clamps NaN to 0 and age_str returns ? for a NaN age. Drops the dead -0 fold in deg and fixes a mislabelled assertion. Co-Authored-By: Claude Sonnet 5.5 --- test/test_weather.lua | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'test/test_weather.lua') diff --git a/test/test_weather.lua b/test/test_weather.lua index cea4645..e7d7614 100644 --- a/test/test_weather.lua +++ b/test/test_weather.lua @@ -64,9 +64,20 @@ assert(weather.describe(nil) == 'Unknown', 'nil code words') assert(weather.deg(13.2) == '13\u{00B0}', 'rounds down') assert(weather.deg(13.5) == '14\u{00B0}', 'rounds half up') assert(weather.deg(-0.4) == '0\u{00B0}', 'just below zero reads 0') -assert(weather.deg(-2.6) == '-3\u{00B0}', 'negative rounds away from zero') +assert(weather.deg(-2.6) == '-3\u{00B0}', 'negative rounds to nearest') assert(weather.deg(nil) == '--', 'missing temperature is --, never 0') +-- Non-finite input must not raise (1e999 parses to inf from a cache line). +local NAN, INF = 0/0, math.huge +for _, bad in ipairs({ NAN, INF, -INF, 1e300, math.maxinteger }) do + assert(weather.deg(bad) == '--', 'deg non-finite ' .. tostring(bad)) + assert(weather.num(bad) == '--', 'num non-finite ' .. tostring(bad)) + assert(weather.duration(bad) == '--', 'duration non-finite ' .. tostring(bad)) + assert(weather.moon_icon(bad) == weather.ICON.unknown, 'moon_icon non-finite ' .. tostring(bad)) +end +assert(weather.sun_t(NAN, 1000, 2000) == 0, 'NaN now parks at 0') +assert(weather.age_str(NAN, 0) == '?', 'NaN age is ?') + assert(weather.num(77) == '77', 'plain number') assert(weather.num(39.6, '%') == '40%', 'rounded with suffix') assert(weather.num(0, '%') == '0%', 'zero is a real reading') -- cgit v1.2.3