|
| 1 | +from __future__ import (absolute_import, division, print_function, |
| 2 | + unicode_literals) |
| 3 | + |
| 4 | +import six |
| 5 | + |
| 6 | +import difflib |
| 7 | +import os |
| 8 | + |
| 9 | +from matplotlib import rcParams, rcdefaults, use |
| 10 | + |
| 11 | + |
| 12 | +_multiprocess_can_split_ = True |
| 13 | + |
| 14 | + |
| 15 | +# Check that the test directories exist |
| 16 | +if not os.path.exists(os.path.join( |
| 17 | + os.path.dirname(__file__), 'baseline_images')): |
| 18 | + raise IOError( |
| 19 | + 'The baseline image directory does not exist. ' |
| 20 | + 'This is most likely because the test data is not installed. ' |
| 21 | + 'You may need to install matplotlib from source to get the ' |
| 22 | + 'test data.') |
| 23 | + |
| 24 | + |
| 25 | +def setup(): |
| 26 | + # The baseline images are created in this locale, so we should use |
| 27 | + # it during all of the tests. |
| 28 | + import locale |
| 29 | + import warnings |
| 30 | + from matplotlib.backends import backend_agg, backend_pdf, backend_svg |
| 31 | + |
| 32 | + try: |
| 33 | + locale.setlocale(locale.LC_ALL, str('en_US.UTF-8')) |
| 34 | + except locale.Error: |
| 35 | + try: |
| 36 | + locale.setlocale(locale.LC_ALL, str('English_United States.1252')) |
| 37 | + except locale.Error: |
| 38 | + warnings.warn( |
| 39 | + "Could not set locale to English/United States. " |
| 40 | + "Some date-related tests may fail") |
| 41 | + |
| 42 | + use('Agg', warn=False) # use Agg backend for these tests |
| 43 | + |
| 44 | + # These settings *must* be hardcoded for running the comparison |
| 45 | + # tests and are not necessarily the default values as specified in |
| 46 | + # rcsetup.py |
| 47 | + rcdefaults() # Start with all defaults |
| 48 | + rcParams['font.family'] = 'Bitstream Vera Sans' |
| 49 | + rcParams['text.hinting'] = False |
| 50 | + rcParams['text.hinting_factor'] = 8 |
| 51 | + |
| 52 | + # Clear the font caches. Otherwise, the hinting mode can travel |
| 53 | + # from one test to another. |
| 54 | + backend_agg.RendererAgg._fontd.clear() |
| 55 | + backend_pdf.RendererPdf.truetype_font_cache.clear() |
| 56 | + backend_svg.RendererSVG.fontd.clear() |
| 57 | + |
| 58 | + |
| 59 | +def assert_str_equal(reference_str, test_str, |
| 60 | + format_str=('String {str1} and {str2} do not ' |
| 61 | + 'match:\n{differences}')): |
| 62 | + """ |
| 63 | + Assert the two strings are equal. If not, fail and print their |
| 64 | + diffs using difflib. |
| 65 | +
|
| 66 | + """ |
| 67 | + if reference_str != test_str: |
| 68 | + diff = difflib.unified_diff(reference_str.splitlines(1), |
| 69 | + test_str.splitlines(1), |
| 70 | + 'Reference', 'Test result', |
| 71 | + '', '', 0) |
| 72 | + raise ValueError(format_str.format(str1=reference_str, |
| 73 | + str2=test_str, |
| 74 | + differences=''.join(diff))) |
0 commit comments