From 446d7238a317715492487a89fa78d37cfe1d21c6 Mon Sep 17 00:00:00 2001 From: nkorinek Date: Mon, 23 Mar 2020 15:23:01 -0600 Subject: [PATCH 1/3] Fixed assert_title_contains to take strings as well as lists --- matplotcheck/base.py | 42 +++++++++++-------- .../tests/test_base_titles_captions.py | 6 +++ 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/matplotcheck/base.py b/matplotcheck/base.py index 29be8d9b..00e90ef6 100644 --- a/matplotcheck/base.py +++ b/matplotcheck/base.py @@ -90,7 +90,7 @@ def assert_string_contains( Parameters ---------- - strings_expected : list + strings_expected : list or string Any string in `strings_expected` must be in the title for the assertion to pass. If there is a list of strings in `strings_expected`, at least one of the strings in that list must @@ -123,22 +123,28 @@ def assert_string_contains( return string = string.lower().replace(" ", "") - for check in strings_expected: - if isinstance(check, str): - if not check.lower().replace(" ", "") in string: - raise AssertionError(message_default.format(check)) - elif isinstance(check, list): - if not any( - [c.lower().replace(" ", "") in string for c in check] - ): - if len(check) == 1: - raise AssertionError(message_default.format(check[0])) - else: - raise AssertionError(message_or.format(check)) - else: - raise ValueError( - "str_lst must be a list of: lists or strings." - ) + + if isinstance(strings_expected, str): + if not strings_expected.lower().replace(" ", "") in string: + raise AssertionError(message_default.format(strings_expected)) + elif isinstance(strings_expected, list): + for check in strings_expected: + if isinstance(check, str): + if not check.lower().replace(" ", "") in string: + raise AssertionError(message_default.format(check)) + elif isinstance(check, list): + if not any( + [c.lower().replace(" ", "") in string for c in check] + ): + if len(check) == 1: + raise AssertionError(message_default.format(check[0])) + else: + raise AssertionError(message_or.format(check)) + else: + raise ValueError( + "strings_expected must be a string or a list of: lists " + "or strings." + ) def assert_plot_type( self, plot_type=None, message="Plot is not of type {0}" @@ -204,7 +210,7 @@ def assert_title_contains( Parameters ---------- - strings_expected : list + strings_expected : list or string Any string in `strings_expected` must be in the title for the assertion to pass. If there is a list of strings in `strings_expected`, at least one of the strings in that list must diff --git a/matplotcheck/tests/test_base_titles_captions.py b/matplotcheck/tests/test_base_titles_captions.py index 8fd65581..50c2ac96 100644 --- a/matplotcheck/tests/test_base_titles_captions.py +++ b/matplotcheck/tests/test_base_titles_captions.py @@ -54,6 +54,12 @@ def test_title_contains_axes_spaces(pt_line_plt): plt.close() +def test_title_contains_axes_string(pt_line_plt): + """Check title_contains for axes title as a string, not a list""" + pt_line_plt.assert_title_contains("My Plot Title", title_type="axes") + plt.close() + + def test_title_contains_axes_badtext(pt_line_plt): """Check title_contains fails when given bad text""" with pytest.raises( From e4ca68e39c6aa20573913438081da4f3b5c01bfd Mon Sep 17 00:00:00 2001 From: nkorinek Date: Mon, 23 Mar 2020 15:29:04 -0600 Subject: [PATCH 2/3] Better implementation --- matplotcheck/base.py | 42 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/matplotcheck/base.py b/matplotcheck/base.py index 00e90ef6..9591ff8f 100644 --- a/matplotcheck/base.py +++ b/matplotcheck/base.py @@ -90,7 +90,7 @@ def assert_string_contains( Parameters ---------- - strings_expected : list or string + strings_expected : list Any string in `strings_expected` must be in the title for the assertion to pass. If there is a list of strings in `strings_expected`, at least one of the strings in that list must @@ -125,26 +125,24 @@ def assert_string_contains( string = string.lower().replace(" ", "") if isinstance(strings_expected, str): - if not strings_expected.lower().replace(" ", "") in string: - raise AssertionError(message_default.format(strings_expected)) - elif isinstance(strings_expected, list): - for check in strings_expected: - if isinstance(check, str): - if not check.lower().replace(" ", "") in string: - raise AssertionError(message_default.format(check)) - elif isinstance(check, list): - if not any( - [c.lower().replace(" ", "") in string for c in check] - ): - if len(check) == 1: - raise AssertionError(message_default.format(check[0])) - else: - raise AssertionError(message_or.format(check)) - else: - raise ValueError( - "strings_expected must be a string or a list of: lists " - "or strings." - ) + strings_expected = [strings_expected] + + for check in strings_expected: + if isinstance(check, str): + if not check.lower().replace(" ", "") in string: + raise AssertionError(message_default.format(check)) + elif isinstance(check, list): + if not any( + [c.lower().replace(" ", "") in string for c in check] + ): + if len(check) == 1: + raise AssertionError(message_default.format(check[0])) + else: + raise AssertionError(message_or.format(check)) + else: + raise ValueError( + "str_lst must be a list of: lists or strings." + ) def assert_plot_type( self, plot_type=None, message="Plot is not of type {0}" @@ -210,7 +208,7 @@ def assert_title_contains( Parameters ---------- - strings_expected : list or string + strings_expected : list Any string in `strings_expected` must be in the title for the assertion to pass. If there is a list of strings in `strings_expected`, at least one of the strings in that list must From 7ec8a930c0073a0e8c5601bf384c9daaaae38655 Mon Sep 17 00:00:00 2001 From: nkorinek Date: Mon, 23 Mar 2020 15:31:16 -0600 Subject: [PATCH 3/3] Changelog update --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index af32256c..c2f8f09a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +* Made `assert_string_contains()` accept a string instead of a list (@nkorinek, #53) * Created tests for the vector module (@nkorinek, #209) * Created functions to test point geometries in VectorTester (@nkorinek, #176) * made `assert_string_contains()` accept correct strings with spaces in them (@nkorinek, #182)