|
| 1 | +import pytest |
| 2 | + |
| 3 | +from shiny.ui import Theme |
| 4 | +from shiny.ui._theme import ( |
| 5 | + ShinyThemePreset, |
| 6 | + ShinyThemePresets, |
| 7 | + ShinyThemePresetsBundled, |
| 8 | +) |
| 9 | + |
| 10 | + |
| 11 | +def test_theme_stores_values_correctly(): |
| 12 | + theme = ( |
| 13 | + Theme("shiny") |
| 14 | + .add_defaults( |
| 15 | + headings_color="red", |
| 16 | + bar_color="purple", |
| 17 | + select_color_text="green", |
| 18 | + bslib_dashboard_design=True, |
| 19 | + ) |
| 20 | + .add_functions("@function get-color($color) { @return $color; }") |
| 21 | + .add_rules( |
| 22 | + """ |
| 23 | + strong { color: $primary; } |
| 24 | + .sidebar-title { color: $danger; } |
| 25 | + """, |
| 26 | + ".special { color: $warning; }", |
| 27 | + ) |
| 28 | + .add_mixins("@mixin alert { color: $alert; }") |
| 29 | + ) |
| 30 | + |
| 31 | + check_vars = [ |
| 32 | + "_preset", |
| 33 | + "name", |
| 34 | + "_functions", |
| 35 | + "_defaults", |
| 36 | + "_mixins", |
| 37 | + "_rules", |
| 38 | + "_css", |
| 39 | + ] |
| 40 | + |
| 41 | + theme_dict = {k: v for k, v in vars(theme).items() if k in check_vars} |
| 42 | + |
| 43 | + assert theme_dict == { |
| 44 | + "_preset": "shiny", |
| 45 | + "name": None, |
| 46 | + "_functions": ["@function get-color($color) { @return $color; }"], |
| 47 | + "_defaults": [ |
| 48 | + "$headings_color: red;", |
| 49 | + "$bar_color: purple;", |
| 50 | + "$select_color_text: green;", |
| 51 | + "$bslib_dashboard_design: true;", |
| 52 | + ], |
| 53 | + "_mixins": ["@mixin alert { color: $alert; }"], |
| 54 | + "_rules": [ |
| 55 | + "\nstrong { color: $primary; }\n.sidebar-title { color: $danger; }\n", |
| 56 | + ".special { color: $warning; }", |
| 57 | + ], |
| 58 | + "_css": "", |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | +def test_theme_preset_must_be_valid(): |
| 63 | + with pytest.raises(ValueError, match="Invalid preset"): |
| 64 | + Theme("not_a_valid_preset") # type: ignore |
| 65 | + |
| 66 | + |
| 67 | +@pytest.mark.parametrize("preset", ShinyThemePresets) |
| 68 | +def test_theme_css_compiles_and_is_cached(preset: ShinyThemePreset): |
| 69 | + theme = Theme(preset) |
| 70 | + if preset in ShinyThemePresetsBundled: |
| 71 | + assert theme._css == "precompiled" |
| 72 | + else: |
| 73 | + assert theme._css == "" |
| 74 | + |
| 75 | + # Adding rules resets the theme's cached CSS |
| 76 | + theme.add_rules(".MY_RULE { color: red; }") |
| 77 | + assert theme._css == "" |
| 78 | + |
| 79 | + first_css = theme.to_css() |
| 80 | + assert first_css.find("Bootstrap") != -1 |
| 81 | + assert first_css.find(".MY_RULE") != -1 |
| 82 | + assert theme.to_css() == first_css # Cached value is returned |
| 83 | + |
| 84 | + # Adding another customization resets the theme's cached CSS |
| 85 | + theme.add_mixins(".MY_MIXIN { color: blue; }") |
| 86 | + second_css = theme.to_css() |
| 87 | + assert second_css != first_css, "First and second compiled CSS are the same" |
| 88 | + assert second_css.find("Bootstrap") != -1 |
| 89 | + assert second_css.find(".MY_MIXIN") != -1 |
| 90 | + |
| 91 | + |
| 92 | +def test_theme_update_preset(): |
| 93 | + theme = Theme("shiny") |
| 94 | + assert theme._preset == "shiny" |
| 95 | + assert theme._css == "precompiled" if "shiny" in ShinyThemePresetsBundled else "" |
| 96 | + |
| 97 | + theme.preset = "bootstrap" |
| 98 | + assert theme._preset == "bootstrap" |
| 99 | + assert theme._css == ( |
| 100 | + "precompiled" if "bootstrap" in ShinyThemePresetsBundled else "" |
| 101 | + ) |
| 102 | + |
| 103 | + theme.preset = "sketchy" |
| 104 | + assert theme._preset == "sketchy" |
| 105 | + assert theme._css == ( |
| 106 | + "precompiled" if "sketchy" in ShinyThemePresetsBundled else "" |
| 107 | + ) |
| 108 | + |
| 109 | + with pytest.raises(ValueError, match="Invalid preset"): |
| 110 | + theme.preset = "not_a_valid_preset" # type: ignore |
| 111 | + |
| 112 | + |
| 113 | +def test_theme_defaults_positional_or_keyword(): |
| 114 | + with pytest.raises(ValueError, match="Cannot provide both"): |
| 115 | + Theme("shiny").add_defaults("$color: red;", other_color="green") |
0 commit comments