|
| 1 | +"""Regression tests for issue #2136: FLOAT values must not use an exponent.""" |
| 2 | + |
| 3 | +import re |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from icalendar import Event |
| 8 | +from icalendar.prop import vFloat, vGeo |
| 9 | + |
| 10 | +RFC_5545_FLOAT = re.compile(r"[+-]?\d+(\.\d+)?") |
| 11 | +"""The FLOAT grammar of RFC 5545, section 3.3.7.""" |
| 12 | + |
| 13 | +VALUES = [0.00001, -0.00002, 1.5e-07, 1e16, -1e22, 5e-324, 1.7976931348623157e308] |
| 14 | +"""Floats that Python writes with an exponent.""" |
| 15 | + |
| 16 | + |
| 17 | +@pytest.mark.parametrize("value", VALUES) |
| 18 | +def test_vfloat_follows_the_float_grammar(value): |
| 19 | + """vFloat writes no exponent.""" |
| 20 | + assert RFC_5545_FLOAT.fullmatch(vFloat(value).to_ical().decode()) |
| 21 | + |
| 22 | + |
| 23 | +@pytest.mark.parametrize("value", VALUES) |
| 24 | +def test_vfloat_round_trip(value): |
| 25 | + """The value is the same after writing and parsing it.""" |
| 26 | + assert vFloat.from_ical(vFloat(value).to_ical().decode()) == value |
| 27 | + |
| 28 | + |
| 29 | +def test_vfloat_examples(): |
| 30 | + """Small and large values are written with all their digits.""" |
| 31 | + assert vFloat(0.00001).to_ical() == b"0.00001" |
| 32 | + assert vFloat(1e16).to_ical() == b"10000000000000000" |
| 33 | + |
| 34 | + |
| 35 | +def test_vgeo_writes_no_exponent(): |
| 36 | + """Coordinates close to zero are written with all their digits.""" |
| 37 | + assert vGeo((0.00001, -0.00002)).to_ical() == "0.00001;-0.00002" |
| 38 | + |
| 39 | + |
| 40 | +def test_geo_of_an_event(): |
| 41 | + """The GEO property of an event has no exponent.""" |
| 42 | + event = Event() |
| 43 | + event.add("GEO", (0.00001, -0.00002)) |
| 44 | + assert b"GEO:0.00001;-0.00002" in event.to_ical() |
| 45 | + |
| 46 | + |
| 47 | +def test_other_values_stay_the_same(): |
| 48 | + """Values that had no exponent are written as before.""" |
| 49 | + assert vFloat(3.1415).to_ical() == b"3.1415" |
| 50 | + assert vGeo((37.386013, -122.082932)).to_ical() == "37.386013;-122.082932" |
0 commit comments