Skip to content

Commit 5b489a7

Browse files
committed
test that table uses repr and doesn't try to convert unitized data
1 parent e394940 commit 5b489a7

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

lib/matplotlib/tests/test_table.py

+32
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import datetime
2+
from unittest.mock import Mock
3+
14
import numpy as np
25
import pytest
36

@@ -7,6 +10,7 @@
710
from matplotlib.table import CustomCell, Table
811
from matplotlib.testing.decorators import image_comparison, check_figures_equal
912
from matplotlib.transforms import Bbox
13+
import matplotlib.units as munits
1014

1115

1216
def test_non_square():
@@ -229,3 +233,31 @@ def test_table_bbox(fig_test, fig_ref):
229233
loc='center',
230234
bbox=Bbox.from_extents(0.1, 0.2, 0.9, 0.8)
231235
)
236+
237+
238+
@check_figures_equal()
239+
def test_table_unit(fig_test, fig_ref):
240+
# test that table doesn't participate in unit machinery,\
241+
# instead uses repr/str
242+
243+
class FakeUnit:
244+
def __init__(self, thing):
245+
pass
246+
def __repr__(self):
247+
return "Hello"
248+
249+
fake_convertor = munits.ConversionInterface()
250+
fake_convertor.convert = Mock(side_effect=lambda v, u, a: 0)
251+
# not used, here for completion
252+
fake_convertor.default_units = Mock(side_effect=lambda v, a: None)
253+
fake_convertor.axisinfo = Mock(side_effect=lambda u, a: munits.AxisInfo())
254+
255+
munits.registry[FakeUnit] = fake_convertor
256+
257+
data = [[FakeUnit("yellow"), FakeUnit(42)],
258+
[FakeUnit(datetime.datetime(1968, 8, 1)), FakeUnit(True)]]
259+
260+
fig_test.subplots().table(data)
261+
fig_ref.subplots().table([["Hello", "Hello"], ["Hello", "Hello"]])
262+
263+
assert not fake_convertor.convert.called

0 commit comments

Comments
 (0)