From 308d7ac721535702ed6dc99ff4b9f91c3bede9fc Mon Sep 17 00:00:00 2001 From: nkorinek Date: Thu, 27 Feb 2020 14:38:46 -0700 Subject: [PATCH 1/4] Made it so that assert_string_contains accepts key words with spaces --- CHANGELOG.md | 1 + matplotcheck/base.py | 4 ++-- matplotcheck/tests/test_base_axis.py | 16 ++++++++++++++++ matplotcheck/tests/test_base_titles_captions.py | 11 +++++++++++ 4 files changed, 30 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index de1bc962..f0523526 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 correct strings with spaces in them (@nkorinek, #182) ## [0.1.2] * Created a vignette covering the testing of histograms (@ryla5068, #149) diff --git a/matplotcheck/base.py b/matplotcheck/base.py index fe828643..1f1c02a5 100644 --- a/matplotcheck/base.py +++ b/matplotcheck/base.py @@ -124,10 +124,10 @@ def assert_string_contains( string = string.lower().replace(" ", "") for check in strings_expected: if isinstance(check, str): - if not check.lower() in string: + if not check.lower().replace(" ", "") in string: raise AssertionError(message_default.format(check)) elif isinstance(check, list): - if not any([c.lower() in string for c in check]): + if not any([c.lower().replace(" ", "") in string for c in check]): if len(check) == 1: raise AssertionError(message_default.format(check[0])) else: diff --git a/matplotcheck/tests/test_base_axis.py b/matplotcheck/tests/test_base_axis.py index 83baddbe..490f07db 100644 --- a/matplotcheck/tests/test_base_axis.py +++ b/matplotcheck/tests/test_base_axis.py @@ -91,6 +91,22 @@ def test_axis_label_contains_y(pt_line_plt): pt_line_plt.assert_axis_label_contains(axis="y", strings_expected=["y"]) plt.close() +def test_axis_label_contains_x_spaces(pt_line_plt): + """Checks for assert_axis_label_contains for x axis with spaces""" + pt_line_plt.assert_axis_label_contains( + axis="x", strings_expected=["x label"] + ) + plt.close() + + +def test_axis_label_contains_y(pt_line_plt): + """Checks for assert_axis_label_contains for y axis with spaces""" + pt_line_plt.assert_axis_label_contains( + axis="y", strings_expected=["y label"] + ) + plt.close() + + def test_axis_label_contains_invalid_axis(pt_line_plt): """Check that assert_axis_label_contains fails when given unexpected axis""" diff --git a/matplotcheck/tests/test_base_titles_captions.py b/matplotcheck/tests/test_base_titles_captions.py index 303fe9c4..d38ae18f 100644 --- a/matplotcheck/tests/test_base_titles_captions.py +++ b/matplotcheck/tests/test_base_titles_captions.py @@ -46,6 +46,12 @@ def test_title_contains_axes(pt_line_plt): ) plt.close() +def test_title_contains_axes_spaces(pt_line_plt): + """Check title_contains for axes title with spaces""" + 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""" @@ -119,6 +125,11 @@ def test_assert_caption_contains(pt_line_plt): pt_line_plt.assert_caption_contains([["Figure"], ["Caption"]]) plt.close() +def test_assert_caption_contains_spaces(pt_line_plt): + """Test that caption contains passes given right text with spaces""" + pt_line_plt.assert_caption_contains([["Figure Caption"]]) + plt.close() + def test_assert_caption_contains_expect_empty(pt_line_plt): """Test that caption contains passes when expected text list is empty""" From 3363547000f48641e605ec766601ba7f5955ed1c Mon Sep 17 00:00:00 2001 From: nkorinek Date: Thu, 27 Feb 2020 14:41:15 -0700 Subject: [PATCH 2/4] black --- matplotcheck/base.py | 4 +++- matplotcheck/tests/test_base_axis.py | 2 +- matplotcheck/tests/test_base_titles_captions.py | 7 ++++--- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/matplotcheck/base.py b/matplotcheck/base.py index 1f1c02a5..ec663729 100644 --- a/matplotcheck/base.py +++ b/matplotcheck/base.py @@ -127,7 +127,9 @@ def assert_string_contains( 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 not any( + [c.lower().replace(" ", "") in string for c in check] + ): if len(check) == 1: raise AssertionError(message_default.format(check[0])) else: diff --git a/matplotcheck/tests/test_base_axis.py b/matplotcheck/tests/test_base_axis.py index 490f07db..bb1913a0 100644 --- a/matplotcheck/tests/test_base_axis.py +++ b/matplotcheck/tests/test_base_axis.py @@ -91,6 +91,7 @@ def test_axis_label_contains_y(pt_line_plt): pt_line_plt.assert_axis_label_contains(axis="y", strings_expected=["y"]) plt.close() + def test_axis_label_contains_x_spaces(pt_line_plt): """Checks for assert_axis_label_contains for x axis with spaces""" pt_line_plt.assert_axis_label_contains( @@ -107,7 +108,6 @@ def test_axis_label_contains_y(pt_line_plt): plt.close() - def test_axis_label_contains_invalid_axis(pt_line_plt): """Check that assert_axis_label_contains fails when given unexpected axis""" # Fails when given an invalid axies diff --git a/matplotcheck/tests/test_base_titles_captions.py b/matplotcheck/tests/test_base_titles_captions.py index d38ae18f..cf5eafc8 100644 --- a/matplotcheck/tests/test_base_titles_captions.py +++ b/matplotcheck/tests/test_base_titles_captions.py @@ -46,13 +46,13 @@ def test_title_contains_axes(pt_line_plt): ) plt.close() + def test_title_contains_axes_spaces(pt_line_plt): """Check title_contains for axes title with spaces""" - pt_line_plt.assert_title_contains( - ["My Plot Title"], title_type="axes" - ) + 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( @@ -125,6 +125,7 @@ def test_assert_caption_contains(pt_line_plt): pt_line_plt.assert_caption_contains([["Figure"], ["Caption"]]) plt.close() + def test_assert_caption_contains_spaces(pt_line_plt): """Test that caption contains passes given right text with spaces""" pt_line_plt.assert_caption_contains([["Figure Caption"]]) From 95830a227def710d53aa8fe6cde910d00c5ad14c Mon Sep 17 00:00:00 2001 From: nkorinek Date: Tue, 3 Mar 2020 10:08:49 -0700 Subject: [PATCH 3/4] renaming function --- matplotcheck/tests/test_base_axis.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/matplotcheck/tests/test_base_axis.py b/matplotcheck/tests/test_base_axis.py index 22bcab1c..dfd70af7 100644 --- a/matplotcheck/tests/test_base_axis.py +++ b/matplotcheck/tests/test_base_axis.py @@ -100,7 +100,7 @@ def test_axis_label_contains_x_spaces(pt_line_plt): plt.close() -def test_axis_label_contains_y(pt_line_plt): +def test_axis_label_contains_y_spaces(pt_line_plt): """Checks for assert_axis_label_contains for y axis with spaces""" pt_line_plt.assert_axis_label_contains( axis="y", strings_expected=["y label"] From f288ed2da80087a93317db5c759f226ec6d6821e Mon Sep 17 00:00:00 2001 From: nkorinek Date: Tue, 3 Mar 2020 11:59:37 -0700 Subject: [PATCH 4/4] Showed assert_title_contains working with a space in the keyword in vignette --- examples/plot_testing_basics.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/plot_testing_basics.py b/examples/plot_testing_basics.py index 839a6919..45b182a7 100644 --- a/examples/plot_testing_basics.py +++ b/examples/plot_testing_basics.py @@ -55,7 +55,7 @@ fig, ax = plt.subplots() ax.bar(months, percip, color="blue") ax.set( - title="Average Monthly Percipitation in Boulder, CO", + title="Average Monthly Precipitation in Boulder, CO", xlabel="Month", ylabel="Percipitation (in)", ) @@ -86,7 +86,7 @@ plot_tester_1.assert_plot_type("bar") # Test that the plot title contains specific words -plot_tester_1.assert_title_contains(["average", "month", "percip", "boulder"]) +plot_tester_1.assert_title_contains(["average", "monthly precip", "boulder"]) # Test that the axis labels contain specific words plot_tester_1.assert_axis_label_contains(axis="x", strings_expected=["month"])