|
1 | 1 | import pytest |
2 | 2 | from string.templatelib import Template, Interpolation |
| 3 | +from functools import lru_cache |
3 | 4 |
|
4 | 5 | from .nodes import ( |
| 6 | + TNode, |
5 | 7 | TComment, |
6 | 8 | TDocumentType, |
7 | 9 | TElement, |
|
13 | 15 | StaticAttribute, |
14 | 16 | SpreadAttribute, |
15 | 17 | ) |
16 | | -from .parser import parse_html |
| 18 | +from .parser import _parse_html, parse_html, CachedTemplate |
17 | 19 |
|
18 | 20 |
|
19 | 21 | class TestHelpers: |
@@ -437,3 +439,101 @@ def DivWrapper(attrs, embedded_t): |
437 | 439 | ] |
438 | 440 | ), |
439 | 441 | ) |
| 442 | + |
| 443 | + |
| 444 | +@pytest.fixture() |
| 445 | +def parse_html_simulation(): |
| 446 | + """ |
| 447 | + Simulate parse_html/_parse_html using the lru_cache. |
| 448 | + """ |
| 449 | + |
| 450 | + @lru_cache(maxsize=512) |
| 451 | + def _simulated_parse_html(cached_template: CachedTemplate) -> TNode: |
| 452 | + return _parse_html.__wrapped__(cached_template) |
| 453 | + |
| 454 | + def simulated_parse_html(template: Template) -> TNode: |
| 455 | + return _simulated_parse_html(CachedTemplate(template)) |
| 456 | + |
| 457 | + yield simulated_parse_html, _simulated_parse_html |
| 458 | + _simulated_parse_html.cache_clear() |
| 459 | + |
| 460 | + |
| 461 | +def hits_misses_helper(caching_func): |
| 462 | + """Shorthand for unpacking a cached func's cache info over-and-over.""" |
| 463 | + |
| 464 | + def _hits_misses(): |
| 465 | + info = caching_func.cache_info() |
| 466 | + return (info.hits, info.misses) |
| 467 | + |
| 468 | + return _hits_misses |
| 469 | + |
| 470 | + |
| 471 | +def test_parse_html_cache_hit_after_first_parse(parse_html_simulation): |
| 472 | + simulated_parse_html, _simulated_parse_html = parse_html_simulation |
| 473 | + hits_misses = hits_misses_helper(_simulated_parse_html) |
| 474 | + assert hits_misses() == (0, 0) |
| 475 | + |
| 476 | + in_template = t"<div>Parsing {'html'}!</div>" |
| 477 | + out_tnode = th.el("div", children=tuple([th.text("Parsing ", 0, "!")])) |
| 478 | + |
| 479 | + assert simulated_parse_html(in_template) == out_tnode |
| 480 | + assert hits_misses() == (0, 1) # MISS! |
| 481 | + assert simulated_parse_html(in_template) == out_tnode |
| 482 | + assert hits_misses() == (1, 1) # HIT! |
| 483 | + |
| 484 | + |
| 485 | +def test_parse_html_cache_hit_same_strings(parse_html_simulation): |
| 486 | + simulated_parse_html, _simulated_parse_html = parse_html_simulation |
| 487 | + hits_misses = hits_misses_helper(_simulated_parse_html) |
| 488 | + assert hits_misses() == (0, 0) |
| 489 | + |
| 490 | + in_template = t"<div>Parsing {'html'}!</div>" |
| 491 | + alt_template = t"<div>Parsing {100}!</div>" |
| 492 | + out_tnode = th.el("div", children=tuple([th.text("Parsing ", 0, "!")])) |
| 493 | + |
| 494 | + # Strings equal but interpolation values are different |
| 495 | + assert ( |
| 496 | + alt_template.strings == in_template.strings |
| 497 | + and alt_template.values != in_template.values |
| 498 | + ) |
| 499 | + |
| 500 | + assert simulated_parse_html(in_template) == out_tnode |
| 501 | + assert hits_misses() == (0, 1) # MISS! |
| 502 | + assert simulated_parse_html(in_template) == out_tnode |
| 503 | + assert hits_misses() == (1, 1) # HIT! |
| 504 | + assert simulated_parse_html(alt_template) == out_tnode |
| 505 | + assert hits_misses() == (2, 1) # HIT! |
| 506 | + |
| 507 | + |
| 508 | +def test_cached_template_eq(): |
| 509 | + ct1 = CachedTemplate(t"<div>Parsing {'html'}!</div>") |
| 510 | + ct2 = CachedTemplate(t"<div>Parsing {100}!</div>") |
| 511 | + assert ( |
| 512 | + ct1.template.strings == ct2.template.strings |
| 513 | + and ct1.template.values != ct2.template.values |
| 514 | + ) |
| 515 | + assert ct1 == ct2 and ct1 == ct1 |
| 516 | + |
| 517 | + ct3 = CachedTemplate(t"<div>Still parsing {'html'}!</div>") |
| 518 | + assert ( |
| 519 | + ct1.template.strings != ct3.template.strings |
| 520 | + and ct1.template.values == ct3.template.values |
| 521 | + ) |
| 522 | + assert ct1 != ct3 |
| 523 | + |
| 524 | + |
| 525 | +def test_cached_template_hash(): |
| 526 | + ct1 = CachedTemplate(t"<div>Parsing {'html'}!</div>") |
| 527 | + ct2 = CachedTemplate(t"<div>Parsing {100}!</div>") |
| 528 | + assert ( |
| 529 | + ct1.template.strings == ct2.template.strings |
| 530 | + and ct1.template.values != ct2.template.values |
| 531 | + ) |
| 532 | + assert hash(ct1) == hash(ct2) |
| 533 | + |
| 534 | + ct3 = CachedTemplate(t"<div>Still parsing {'html'}!</div>") |
| 535 | + assert ( |
| 536 | + ct1.template.strings != ct3.template.strings |
| 537 | + and ct1.template.values == ct3.template.values |
| 538 | + ) |
| 539 | + assert hash(ct1) != hash(ct3) |
0 commit comments