|
| 1 | +# Copyright 2025 Tom Most <twm@freecog.net> |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This file is a part of feedparser. |
| 5 | +# |
| 6 | +# Redistribution and use in source and binary forms, with or without |
| 7 | +# modification, are permitted provided that the following conditions are met: |
| 8 | +# |
| 9 | +# * Redistributions of source code must retain the above copyright notice, |
| 10 | +# this list of conditions and the following disclaimer. |
| 11 | +# * Redistributions in binary form must reproduce the above copyright notice, |
| 12 | +# this list of conditions and the following disclaimer in the documentation |
| 13 | +# and/or other materials provided with the distribution. |
| 14 | +# |
| 15 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' |
| 16 | +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 18 | +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 19 | +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 20 | +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 21 | +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 22 | +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 23 | +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 24 | +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 25 | +# POSSIBILITY OF SUCH DAMAGE. |
| 26 | + |
| 27 | +import pytest |
| 28 | + |
| 29 | +from feedparser.urls import srcset_candidates |
| 30 | + |
| 31 | + |
| 32 | +def test_empty(): |
| 33 | + assert srcset_candidates("") == [] |
| 34 | + assert srcset_candidates(" \n") == [] |
| 35 | + |
| 36 | + |
| 37 | +def test_default(): |
| 38 | + assert srcset_candidates("/1x.jpg") == [("/1x.jpg", "")] |
| 39 | + |
| 40 | + |
| 41 | +def test_pixel_density_descriptor_one(): |
| 42 | + assert srcset_candidates("/1x.jpg 1x") == [("/1x.jpg", "1x")] |
| 43 | + |
| 44 | + |
| 45 | +def test_pixel_density_descriptor_two(): |
| 46 | + assert srcset_candidates("/1x.jpg 1x,/2x.jpg\t2.0x") == [ |
| 47 | + ("/1x.jpg", "1x"), |
| 48 | + ("/2x.jpg", "2.0x"), |
| 49 | + ] |
| 50 | + |
| 51 | + |
| 52 | +def test_pixel_density_descriptor_three(): |
| 53 | + assert srcset_candidates("/1x.jpg, /2x.jpg 2x , /3x.jpg 3x ") == [ |
| 54 | + ("/1x.jpg", ""), |
| 55 | + ("/2x.jpg", "2x"), |
| 56 | + ("/3x.jpg", "3x"), |
| 57 | + ] |
| 58 | + |
| 59 | + |
| 60 | +@pytest.mark.parametrize( |
| 61 | + "pd", ["1x", "1.0x", "9.5x", "36x", "39.95x", "100x", "1e1x", "2E2x"] |
| 62 | +) |
| 63 | +def test_pixel_density_descriptor_floats(pd): |
| 64 | + """A pixel density descriptor allows all the valid float formats.""" |
| 65 | + assert [("/foo.jpg", pd)] == srcset_candidates("/foo.jpg " + pd) |
| 66 | + |
| 67 | + |
| 68 | +def test_url_comma(): |
| 69 | + """A URL containing a comma is not broken.""" |
| 70 | + assert srcset_candidates(" /,.jpg 6x,\n /,,,,.webp \t1e100x") == [ |
| 71 | + ("/,.jpg", "6x"), |
| 72 | + ("/,,,,.webp", "1e100x"), |
| 73 | + ] |
| 74 | + |
| 75 | + |
| 76 | +def test_width_one(): |
| 77 | + assert srcset_candidates("/a.png 600w") == [("/a.png", "600w")] |
| 78 | + |
| 79 | + |
| 80 | +def test_width_two(): |
| 81 | + assert srcset_candidates("a.jpg 123w, b.jpg 1234w") == [ |
| 82 | + ("a.jpg", "123w"), |
| 83 | + ("b.jpg", "1234w"), |
| 84 | + ] |
| 85 | + |
| 86 | + |
| 87 | +@pytest.mark.parametrize("pd", ["1.5w", "9000X", "-23w", "-60x"]) |
| 88 | +def test_invalid(pd): |
| 89 | + assert srcset_candidates("/x.gif " + pd) == [] |
0 commit comments