MCPcopy Index your code
hub / github.com/matplotlib/matplotlib / test_metrics_cache

Function test_metrics_cache

lib/matplotlib/tests/test_text.py:842–876  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

840
841@needs_usetex
842def test_metrics_cache():
843 # dig into the signature to get the mutable default used as a cache
844 renderer_cache = inspect.signature(
845 mpl.text._get_text_metrics_function
846 ).parameters['_cache'].default
847
848 renderer_cache.clear()
849
850 fig = plt.figure()
851 fig.text(.3, .5, "foo\nbar")
852 fig.text(.3, .5, "foo\nbar", usetex=True)
853 fig.text(.5, .5, "foo\nbar", usetex=True)
854 fig.canvas.draw()
855 renderer = fig._get_renderer()
856 assert renderer in renderer_cache
857
858 ys = {} # mapping of strings to where they were drawn in y with draw_tex.
859
860 def call(*args, **kwargs):
861 renderer, x, y, s, *_ = args
862 ys.setdefault(s, set()).add(y)
863
864 renderer.draw_tex = call
865 fig.canvas.draw()
866 assert [*ys] == ["foo", "bar"]
867 # Check that both TeX strings were drawn with the same y-position for both
868 # single-line substrings. Previously, there used to be an incorrect cache
869 # collision with the non-TeX string (drawn first here) whose metrics would
870 # get incorrectly reused by the first TeX string.
871 assert len(ys["foo"]) == len(ys["bar"]) == 1
872
873 info = renderer_cache[renderer].cache_info()
874 # Every string gets a miss for the first layouting (extents), then a hit
875 # when drawing, but "foo\nbar" gets two hits as it's drawn twice.
876 assert info.hits > info.misses
877
878
879def test_metrics_cache2():

Callers

nothing calls this directly

Calls 5

figureMethod · 0.80
clearMethod · 0.45
textMethod · 0.45
drawMethod · 0.45
_get_rendererMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…