| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
"""Property-based tests for the styles module.""" |
| 17 |
|
| 18 |
from collections.abc import Iterable |
| 19 |
from typing import Optional |
| 20 |
from typing import Union |
| 21 |
|
| 22 |
from hypothesis import given |
| 23 |
from hypothesis import strategies as st |
| 24 |
from hypothesis.provisional import urls |
| 25 |
|
| 26 |
import fastkml |
| 27 |
import fastkml.enums |
| 28 |
import fastkml.links |
| 29 |
import fastkml.styles |
| 30 |
from tests.base import Lxml |
| 31 |
from tests.hypothesis.common import assert_repr_roundtrip |
| 32 |
from tests.hypothesis.common import assert_str_roundtrip |
| 33 |
from tests.hypothesis.common import assert_str_roundtrip_terse |
| 34 |
from tests.hypothesis.common import assert_str_roundtrip_verbose |
| 35 |
from tests.hypothesis.strategies import kml_colors |
| 36 |
from tests.hypothesis.strategies import nc_name |
| 37 |
from tests.hypothesis.strategies import styles |
| 38 |
from tests.hypothesis.strategies import xml_text |
| 39 |
|
| 40 |
|
| 41 |
class TestLxml(Lxml): |
| 42 |
@given( |
| 43 |
url=st.one_of(st.none(), urls()), |
| 44 |
) |
| 45 |
def test_fuzz_style_url( |
| 46 |
self, |
| 47 |
url: Optional[str], |
| 48 |
) -> None: |
| 49 |
style_url = fastkml.StyleUrl( |
| 50 |
url=url, |
| 51 |
) |
| 52 |
|
| 53 |
assert_repr_roundtrip(style_url) |
| 54 |
assert_str_roundtrip(style_url) |
| 55 |
assert_str_roundtrip_terse(style_url) |
| 56 |
assert_str_roundtrip_verbose(style_url) |
| 57 |
|
| 58 |
@given( |
| 59 |
x=st.one_of(st.none(), st.floats(allow_nan=False, allow_infinity=False)), |
| 60 |
y=st.one_of(st.none(), st.floats(allow_nan=False, allow_infinity=False)), |
| 61 |
xunits=st.one_of(st.none(), st.sampled_from(fastkml.enums.Units)), |
| 62 |
yunits=st.one_of(st.none(), st.sampled_from(fastkml.enums.Units)), |
| 63 |
) |
| 64 |
def test_fuzz_hot_spot( |
| 65 |
self, |
| 66 |
x: Optional[float], |
| 67 |
y: Optional[float], |
| 68 |
xunits: Optional[fastkml.enums.Units], |
| 69 |
yunits: Optional[fastkml.enums.Units], |
| 70 |
) -> None: |
| 71 |
hot_spot = fastkml.styles.HotSpot( |
| 72 |
x=x, |
| 73 |
y=y, |
| 74 |
xunits=xunits, |
| 75 |
yunits=yunits, |
| 76 |
) |
| 77 |
|
| 78 |
assert_repr_roundtrip(hot_spot) |
| 79 |
assert_str_roundtrip(hot_spot) |
| 80 |
assert_str_roundtrip_terse(hot_spot) |
| 81 |
assert_str_roundtrip_verbose(hot_spot) |
| 82 |
|
| 83 |
@given( |
| 84 |
id=st.one_of(st.none(), nc_name()), |
| 85 |
target_id=st.one_of(st.none(), nc_name()), |
| 86 |
color=st.one_of(st.none(), kml_colors()), |
| 87 |
color_mode=st.one_of(st.none(), st.sampled_from(fastkml.enums.ColorMode)), |
| 88 |
scale=st.one_of(st.none(), st.floats(allow_nan=False, allow_infinity=False)), |
| 89 |
heading=st.one_of( |
| 90 |
st.none(), |
| 91 |
st.floats( |
| 92 |
allow_nan=False, |
| 93 |
allow_infinity=False, |
| 94 |
min_value=0, |
| 95 |
max_value=360, |
| 96 |
), |
| 97 |
), |
| 98 |
icon=st.one_of( |
| 99 |
st.none(), |
| 100 |
st.builds( |
| 101 |
fastkml.links.Icon, |
| 102 |
href=urls(), |
| 103 |
), |
| 104 |
), |
| 105 |
hot_spot=st.one_of( |
| 106 |
st.none(), |
| 107 |
st.builds( |
| 108 |
fastkml.styles.HotSpot, |
| 109 |
x=st.floats(allow_nan=False, allow_infinity=False).filter( |
| 110 |
lambda x: x != 0, |
| 111 |
), |
| 112 |
xunits=st.one_of(st.none(), st.sampled_from(fastkml.enums.Units)), |
| 113 |
y=st.floats(allow_nan=False, allow_infinity=False).filter( |
| 114 |
lambda x: x != 0, |
| 115 |
), |
| 116 |
yunits=st.one_of(st.none(), st.sampled_from(fastkml.enums.Units)), |
| 117 |
), |
| 118 |
), |
| 119 |
) |
| 120 |
def test_fuzz_icon_style( |
| 121 |
self, |
| 122 |
id: Optional[str], |
| 123 |
target_id: Optional[str], |
| 124 |
color: Optional[str], |
| 125 |
color_mode: Optional[fastkml.enums.ColorMode], |
| 126 |
scale: Optional[float], |
| 127 |
heading: Optional[float], |
| 128 |
icon: Optional[fastkml.links.Icon], |
| 129 |
hot_spot: Optional[fastkml.styles.HotSpot], |
| 130 |
) -> None: |
| 131 |
icon_style = fastkml.IconStyle( |
| 132 |
id=id, |
| 133 |
target_id=target_id, |
| 134 |
color=color, |
| 135 |
color_mode=color_mode, |
| 136 |
scale=scale, |
| 137 |
heading=heading, |
| 138 |
icon=icon, |
| 139 |
hot_spot=hot_spot, |
| 140 |
) |
| 141 |
|
| 142 |
assert_repr_roundtrip(icon_style) |
| 143 |
assert_str_roundtrip(icon_style) |
| 144 |
assert_str_roundtrip_terse(icon_style) |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
@given( |
| 149 |
id=st.one_of(st.none(), nc_name()), |
| 150 |
target_id=st.one_of(st.none(), nc_name()), |
| 151 |
color=st.one_of(st.none(), kml_colors()), |
| 152 |
color_mode=st.one_of(st.none(), st.sampled_from(fastkml.enums.ColorMode)), |
| 153 |
width=st.one_of( |
| 154 |
st.none(), |
| 155 |
st.floats(allow_nan=False, allow_infinity=False, min_value=0), |
| 156 |
), |
| 157 |
) |
| 158 |
def test_fuzz_line_style( |
| 159 |
self, |
| 160 |
id: Optional[str], |
| 161 |
target_id: Optional[str], |
| 162 |
color: Optional[str], |
| 163 |
color_mode: Optional[fastkml.enums.ColorMode], |
| 164 |
width: Optional[float], |
| 165 |
) -> None: |
| 166 |
line_style = fastkml.LineStyle( |
| 167 |
id=id, |
| 168 |
target_id=target_id, |
| 169 |
color=color, |
| 170 |
color_mode=color_mode, |
| 171 |
width=width, |
| 172 |
) |
| 173 |
|
| 174 |
assert_repr_roundtrip(line_style) |
| 175 |
assert_str_roundtrip(line_style) |
| 176 |
assert_str_roundtrip_terse(line_style) |
| 177 |
assert_str_roundtrip_verbose(line_style) |
| 178 |
|
| 179 |
@given( |
| 180 |
id=st.one_of(st.none(), nc_name()), |
| 181 |
target_id=st.one_of(st.none(), nc_name()), |
| 182 |
color=st.one_of(st.none(), kml_colors()), |
| 183 |
color_mode=st.one_of(st.none(), st.sampled_from(fastkml.enums.ColorMode)), |
| 184 |
fill=st.one_of(st.none(), st.booleans()), |
| 185 |
outline=st.one_of(st.none(), st.booleans()), |
| 186 |
) |
| 187 |
def test_fuzz_poly_style( |
| 188 |
self, |
| 189 |
id: Optional[str], |
| 190 |
target_id: Optional[str], |
| 191 |
color: Optional[str], |
| 192 |
color_mode: Optional[fastkml.enums.ColorMode], |
| 193 |
fill: Optional[bool], |
| 194 |
outline: Optional[bool], |
| 195 |
) -> None: |
| 196 |
poly_style = fastkml.PolyStyle( |
| 197 |
id=id, |
| 198 |
target_id=target_id, |
| 199 |
color=color, |
| 200 |
color_mode=color_mode, |
| 201 |
fill=fill, |
| 202 |
outline=outline, |
| 203 |
) |
| 204 |
|
| 205 |
assert_repr_roundtrip(poly_style) |
| 206 |
assert_str_roundtrip(poly_style) |
| 207 |
assert_str_roundtrip_terse(poly_style) |
| 208 |
assert_str_roundtrip_verbose(poly_style) |
| 209 |
|
| 210 |
@given( |
| 211 |
id=st.one_of(st.none(), nc_name()), |
| 212 |
target_id=st.one_of(st.none(), nc_name()), |
| 213 |
color=st.one_of(st.none(), kml_colors()), |
| 214 |
color_mode=st.one_of(st.none(), st.sampled_from(fastkml.enums.ColorMode)), |
| 215 |
scale=st.one_of( |
| 216 |
st.none(), |
| 217 |
st.floats(allow_nan=False, allow_infinity=False, min_value=0), |
| 218 |
), |
| 219 |
) |
| 220 |
def test_fuzz_label_style( |
| 221 |
self, |
| 222 |
id: Optional[str], |
| 223 |
target_id: Optional[str], |
| 224 |
color: Optional[str], |
| 225 |
color_mode: Optional[fastkml.enums.ColorMode], |
| 226 |
scale: Optional[float], |
| 227 |
) -> None: |
| 228 |
label_style = fastkml.LabelStyle( |
| 229 |
id=id, |
| 230 |
target_id=target_id, |
| 231 |
color=color, |
| 232 |
color_mode=color_mode, |
| 233 |
scale=scale, |
| 234 |
) |
| 235 |
|
| 236 |
assert_repr_roundtrip(label_style) |
| 237 |
assert_str_roundtrip(label_style) |
| 238 |
assert_str_roundtrip_terse(label_style) |
| 239 |
assert_str_roundtrip_verbose(label_style) |
| 240 |
|
| 241 |
@given( |
| 242 |
id=st.one_of(st.none(), nc_name()), |
| 243 |
target_id=st.one_of(st.none(), nc_name()), |
| 244 |
bg_color=st.one_of(st.none(), kml_colors()), |
| 245 |
text_color=st.one_of(st.none(), kml_colors()), |
| 246 |
text=st.one_of(st.none(), xml_text()), |
| 247 |
display_mode=st.one_of(st.none(), st.sampled_from(fastkml.enums.DisplayMode)), |
| 248 |
) |
| 249 |
def test_fuzz_balloon_style( |
| 250 |
self, |
| 251 |
id: Optional[str], |
| 252 |
target_id: Optional[str], |
| 253 |
bg_color: Optional[str], |
| 254 |
text_color: Optional[str], |
| 255 |
text: Optional[str], |
| 256 |
display_mode: Optional[fastkml.enums.DisplayMode], |
| 257 |
) -> None: |
| 258 |
balloon_style = fastkml.BalloonStyle( |
| 259 |
id=id, |
| 260 |
target_id=target_id, |
| 261 |
bg_color=bg_color, |
| 262 |
text_color=text_color, |
| 263 |
text=text, |
| 264 |
display_mode=display_mode, |
| 265 |
) |
| 266 |
|
| 267 |
assert_repr_roundtrip(balloon_style) |
| 268 |
assert_str_roundtrip(balloon_style) |
| 269 |
assert_str_roundtrip_terse(balloon_style) |
| 270 |
assert_str_roundtrip_verbose(balloon_style) |
| 271 |
|
| 272 |
@given( |
| 273 |
id=st.one_of(st.none(), nc_name()), |
| 274 |
target_id=st.one_of(st.none(), nc_name()), |
| 275 |
styles=st.one_of( |
| 276 |
st.none(), |
| 277 |
st.tuples( |
| 278 |
st.builds( |
| 279 |
fastkml.styles.IconStyle, |
| 280 |
color=kml_colors(), |
| 281 |
color_mode=st.sampled_from(fastkml.enums.ColorMode), |
| 282 |
scale=st.floats(allow_nan=False, allow_infinity=False), |
| 283 |
heading=st.floats( |
| 284 |
allow_nan=False, |
| 285 |
allow_infinity=False, |
| 286 |
min_value=0, |
| 287 |
max_value=360, |
| 288 |
), |
| 289 |
icon=st.builds(fastkml.links.Icon, href=urls()), |
| 290 |
), |
| 291 |
st.builds( |
| 292 |
fastkml.styles.LabelStyle, |
| 293 |
color=kml_colors().filter(lambda x: x != "ffffffff"), |
| 294 |
color_mode=st.sampled_from(fastkml.enums.ColorMode), |
| 295 |
scale=st.floats(allow_nan=False, allow_infinity=False), |
| 296 |
), |
| 297 |
st.builds( |
| 298 |
fastkml.styles.LineStyle, |
| 299 |
color=kml_colors().filter(lambda x: x != "ffffffff"), |
| 300 |
color_mode=st.sampled_from(fastkml.enums.ColorMode), |
| 301 |
width=st.floats(allow_nan=False, allow_infinity=False, min_value=0), |
| 302 |
), |
| 303 |
st.builds( |
| 304 |
fastkml.styles.PolyStyle, |
| 305 |
color=kml_colors().filter(lambda x: x != "ffffffff"), |
| 306 |
color_mode=st.sampled_from(fastkml.enums.ColorMode), |
| 307 |
fill=st.booleans(), |
| 308 |
outline=st.booleans(), |
| 309 |
), |
| 310 |
st.builds( |
| 311 |
fastkml.styles.BalloonStyle, |
| 312 |
bg_color=kml_colors(), |
| 313 |
text_color=kml_colors(), |
| 314 |
text=xml_text(min_size=1, max_size=256).filter( |
| 315 |
lambda x: x.strip() != "", |
| 316 |
), |
| 317 |
display_mode=st.sampled_from(fastkml.enums.DisplayMode), |
| 318 |
), |
| 319 |
), |
| 320 |
), |
| 321 |
) |
| 322 |
def test_fuzz_style( |
| 323 |
self, |
| 324 |
id: Optional[str], |
| 325 |
target_id: Optional[str], |
| 326 |
styles: Optional[ |
| 327 |
Iterable[ |
| 328 |
Union[ |
| 329 |
fastkml.BalloonStyle, |
| 330 |
fastkml.IconStyle, |
| 331 |
fastkml.LabelStyle, |
| 332 |
fastkml.LineStyle, |
| 333 |
fastkml.PolyStyle, |
| 334 |
] |
| 335 |
] |
| 336 |
], |
| 337 |
) -> None: |
| 338 |
style = fastkml.Style(id=id, target_id=target_id, styles=styles) |
| 339 |
|
| 340 |
assert_repr_roundtrip(style) |
| 341 |
assert_str_roundtrip(style) |
| 342 |
assert_str_roundtrip_terse(style) |
| 343 |
|
| 344 |
|
| 345 |
@given( |
| 346 |
id=st.one_of(st.none(), nc_name()), |
| 347 |
target_id=st.one_of(st.none(), nc_name()), |
| 348 |
styles=st.one_of( |
| 349 |
st.none(), |
| 350 |
st.tuples( |
| 351 |
st.builds( |
| 352 |
fastkml.styles.LabelStyle, |
| 353 |
color=kml_colors().filter(lambda x: x != "ffffffff"), |
| 354 |
color_mode=st.sampled_from(fastkml.enums.ColorMode), |
| 355 |
scale=st.floats(allow_nan=False, allow_infinity=False), |
| 356 |
), |
| 357 |
st.builds( |
| 358 |
fastkml.styles.LineStyle, |
| 359 |
color=kml_colors().filter(lambda x: x != "ffffffff"), |
| 360 |
color_mode=st.sampled_from(fastkml.enums.ColorMode), |
| 361 |
width=st.floats(allow_nan=False, allow_infinity=False, min_value=0), |
| 362 |
), |
| 363 |
st.builds( |
| 364 |
fastkml.styles.PolyStyle, |
| 365 |
color=kml_colors().filter(lambda x: x != "ffffffff"), |
| 366 |
color_mode=st.sampled_from(fastkml.enums.ColorMode), |
| 367 |
fill=st.booleans(), |
| 368 |
outline=st.booleans(), |
| 369 |
), |
| 370 |
st.builds( |
| 371 |
fastkml.styles.BalloonStyle, |
| 372 |
bg_color=kml_colors(), |
| 373 |
text_color=kml_colors(), |
| 374 |
text=xml_text(min_size=1, max_size=256).filter( |
| 375 |
lambda x: x.strip() != "", |
| 376 |
), |
| 377 |
display_mode=st.sampled_from(fastkml.enums.DisplayMode), |
| 378 |
), |
| 379 |
), |
| 380 |
), |
| 381 |
) |
| 382 |
def test_fuzz_styles_no_icon_style( |
| 383 |
self, |
| 384 |
id: Optional[str], |
| 385 |
target_id: Optional[str], |
| 386 |
styles: Optional[ |
| 387 |
Iterable[ |
| 388 |
Union[ |
| 389 |
fastkml.BalloonStyle, |
| 390 |
fastkml.LabelStyle, |
| 391 |
fastkml.LineStyle, |
| 392 |
fastkml.PolyStyle, |
| 393 |
] |
| 394 |
] |
| 395 |
], |
| 396 |
) -> None: |
| 397 |
style = fastkml.Style(id=id, target_id=target_id, styles=styles) |
| 398 |
|
| 399 |
assert_repr_roundtrip(style) |
| 400 |
assert_str_roundtrip(style) |
| 401 |
assert_str_roundtrip_terse(style) |
| 402 |
assert_str_roundtrip_verbose(style) |
| 403 |
|
| 404 |
@given( |
| 405 |
id=st.one_of(st.none(), nc_name()), |
| 406 |
target_id=st.one_of(st.none(), nc_name()), |
| 407 |
key=st.sampled_from(fastkml.enums.PairKey), |
| 408 |
style=st.one_of( |
| 409 |
st.builds( |
| 410 |
fastkml.StyleUrl, |
| 411 |
url=urls(), |
| 412 |
), |
| 413 |
st.builds( |
| 414 |
fastkml.Style, |
| 415 |
styles=st.lists( |
| 416 |
styles(), |
| 417 |
min_size=1, |
| 418 |
max_size=1, |
| 419 |
), |
| 420 |
), |
| 421 |
), |
| 422 |
) |
| 423 |
def test_fuzz_pair( |
| 424 |
self, |
| 425 |
id: Optional[str], |
| 426 |
target_id: Optional[str], |
| 427 |
key: Optional[fastkml.enums.PairKey], |
| 428 |
style: Union[fastkml.StyleUrl, fastkml.Style, None], |
| 429 |
) -> None: |
| 430 |
pair = fastkml.styles.Pair(id=id, target_id=target_id, key=key, style=style) |
| 431 |
|
| 432 |
assert_repr_roundtrip(pair) |
| 433 |
assert_str_roundtrip(pair) |
| 434 |
assert_str_roundtrip_terse(pair) |
| 435 |
assert_str_roundtrip_verbose(pair) |
| 436 |
|
| 437 |
@given( |
| 438 |
id=st.one_of(st.none(), nc_name()), |
| 439 |
target_id=st.one_of(st.none(), nc_name()), |
| 440 |
pairs=st.one_of( |
| 441 |
st.none(), |
| 442 |
st.lists( |
| 443 |
st.builds( |
| 444 |
fastkml.styles.Pair, |
| 445 |
key=st.sampled_from(fastkml.enums.PairKey), |
| 446 |
style=st.one_of( |
| 447 |
st.builds( |
| 448 |
fastkml.StyleUrl, |
| 449 |
url=urls(), |
| 450 |
), |
| 451 |
st.builds( |
| 452 |
fastkml.Style, |
| 453 |
styles=st.lists( |
| 454 |
styles(), |
| 455 |
min_size=1, |
| 456 |
max_size=1, |
| 457 |
), |
| 458 |
), |
| 459 |
), |
| 460 |
), |
| 461 |
), |
| 462 |
), |
| 463 |
) |
| 464 |
def test_fuzz_style_map_one_pair( |
| 465 |
self, |
| 466 |
id: Optional[str], |
| 467 |
target_id: Optional[str], |
| 468 |
pairs: Optional[tuple[fastkml.styles.Pair]], |
| 469 |
) -> None: |
| 470 |
style_map = fastkml.StyleMap(id=id, target_id=target_id, pairs=pairs) |
| 471 |
|
| 472 |
assert_repr_roundtrip(style_map) |
| 473 |
assert_str_roundtrip(style_map) |
| 474 |
assert_str_roundtrip_terse(style_map) |
| 475 |
assert_str_roundtrip_verbose(style_map) |
| 476 |
|
| 477 |
@given( |
| 478 |
id=st.one_of(st.none(), nc_name()), |
| 479 |
target_id=st.one_of(st.none(), nc_name()), |
| 480 |
pairs=st.tuples( |
| 481 |
st.builds( |
| 482 |
fastkml.styles.Pair, |
| 483 |
key=st.just(fastkml.enums.PairKey.normal), |
| 484 |
style=st.one_of( |
| 485 |
st.builds( |
| 486 |
fastkml.StyleUrl, |
| 487 |
url=urls(), |
| 488 |
), |
| 489 |
st.builds( |
| 490 |
fastkml.Style, |
| 491 |
styles=st.lists( |
| 492 |
styles(), |
| 493 |
min_size=1, |
| 494 |
max_size=1, |
| 495 |
), |
| 496 |
), |
| 497 |
), |
| 498 |
), |
| 499 |
st.builds( |
| 500 |
fastkml.styles.Pair, |
| 501 |
key=st.just(fastkml.enums.PairKey.highlight), |
| 502 |
style=st.one_of( |
| 503 |
st.builds( |
| 504 |
fastkml.StyleUrl, |
| 505 |
url=urls(), |
| 506 |
), |
| 507 |
st.builds( |
| 508 |
fastkml.Style, |
| 509 |
styles=st.lists( |
| 510 |
styles(), |
| 511 |
min_size=1, |
| 512 |
max_size=1, |
| 513 |
), |
| 514 |
), |
| 515 |
), |
| 516 |
), |
| 517 |
), |
| 518 |
) |
| 519 |
def test_fuzz_style_map_pairs( |
| 520 |
self, |
| 521 |
id: Optional[str], |
| 522 |
target_id: Optional[str], |
| 523 |
pairs: Optional[tuple[fastkml.styles.Pair]], |
| 524 |
) -> None: |
| 525 |
style_map = fastkml.StyleMap(id=id, target_id=target_id, pairs=pairs) |
| 526 |
|
| 527 |
assert_repr_roundtrip(style_map) |
| 528 |
assert_str_roundtrip(style_map) |
| 529 |
assert_str_roundtrip_terse(style_map) |
| 530 |
assert_str_roundtrip_verbose(style_map) |