From fe717de34a7274fa90933c9f8ea2217f7d67cbc6 Mon Sep 17 00:00:00 2001 From: Ross Date: Mon, 27 May 2024 18:27:24 +0200 Subject: [PATCH] tests: display option edge cases --- tests/test_waybar_crypto.py | 53 +++++++++++++++++++++++++++++++++++++ waybar_crypto.py | 5 +++- 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/tests/test_waybar_crypto.py b/tests/test_waybar_crypto.py index 3274922..0d834c9 100644 --- a/tests/test_waybar_crypto.py +++ b/tests/test_waybar_crypto.py @@ -9,6 +9,7 @@ from waybar_crypto import ( API_KEY_ENV, CLASS_NAME, + DEFAULT_DISPLAY_OPTIONS, DEFAULT_DISPLAY_OPTIONS_FORMAT, DEFAULT_XDG_CONFIG_HOME_PATH, MIN_PRECISION, @@ -281,6 +282,58 @@ def test_read_config_min_precision(): assert isinstance(e, WaybarCryptoException) +def test_read_config_display_options_single(): + test_display_option = "price" + + with open(TEST_CONFIG_PATH, "r", encoding="utf-8") as f: + cfp = configparser.ConfigParser(allow_no_value=True, interpolation=None) + cfp.read_file(f) + cfp.set("general", "display", test_display_option) + + with tempfile.NamedTemporaryFile(mode="w") as tmp: + cfp.write(tmp) + tmp.flush() + tmp_config_path = tmp.file.name + + config = read_config(tmp_config_path) + display_options = config["general"]["display_options"] + assert display_options == [test_display_option] + + +def test_read_config_display_options_multiple(): + test_display_options = ["price", "percent_change_1h", "percent_change_24h"] + + with open(TEST_CONFIG_PATH, "r", encoding="utf-8") as f: + cfp = configparser.ConfigParser(allow_no_value=True, interpolation=None) + cfp.read_file(f) + cfp.set("general", "display", ",".join(test_display_options)) + + with tempfile.NamedTemporaryFile(mode="w") as tmp: + cfp.write(tmp) + tmp.flush() + tmp_config_path = tmp.file.name + + config = read_config(tmp_config_path) + display_options = config["general"]["display_options"] + assert display_options == test_display_options + + +def test_read_config_default_display_options(): + with open(TEST_CONFIG_PATH, "r", encoding="utf-8") as f: + cfp = configparser.ConfigParser(allow_no_value=True, interpolation=None) + cfp.read_file(f) + cfp.set("general", "display", None) + + with tempfile.NamedTemporaryFile(mode="w") as tmp: + cfp.write(tmp) + tmp.flush() + tmp_config_path = tmp.file.name + + config = read_config(tmp_config_path) + display_options = config["general"]["display_options"] + assert display_options == DEFAULT_DISPLAY_OPTIONS + + class TestWaybarCrypto: """Tests for the WaybarCrypto.""" diff --git a/waybar_crypto.py b/waybar_crypto.py index 0f46bbc..18ba73a 100755 --- a/waybar_crypto.py +++ b/waybar_crypto.py @@ -195,7 +195,10 @@ def read_config(config_path: str) -> Config: spacer_symbol = cfp.get("general", "spacer_symbol") # Get a list of the chosen display options - display_options: list[str] = cfp.get("general", "display").split(",") + display_options: list[str] = [] + display_options_str = cfp.get("general", "display") + if display_options_str and len(display_options_str) > 0: + display_options = display_options_str.split(",") if len(display_options) == 0: display_options = DEFAULT_DISPLAY_OPTIONS