File: test-bs-theme-preset.R

package info (click to toggle)
r-cran-bslib 0.9.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 12,412 kB
  • sloc: javascript: 13,349; makefile: 33; sh: 23
file content (180 lines) | stat: -rw-r--r-- 5,522 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
describe("resolve_bs_preset()", {
  it("returns NULL if both `name` and `bootswatch` are missing", {
    expect_null(resolve_bs_preset(preset = NULL, bootswatch = NULL))
  })

  it("throws an error if both `name` and `bootswatch` are provided", {
    expect_snapshot(
      error = TRUE,
      resolve_bs_preset(preset = "name", bootswatch = "bootswatch")
    )
  })

  it("throws an error if `name` or `bootswatch` are not scalar strings", {
    expect_snapshot(
      error = TRUE,
      resolve_bs_preset(preset = c("a", "b"))
    )
    expect_snapshot(
      error = TRUE,
      resolve_bs_preset(bootswatch = c("a", "b"))
    )

    expect_error(resolve_bs_preset(preset = 1))
    expect_error(resolve_bs_preset(bootswatch = 1))

    expect_error(resolve_bs_preset(preset = TRUE))
    expect_error(resolve_bs_preset(bootswatch = TRUE))

    expect_error(resolve_bs_preset(preset = NA_character_))
    expect_error(resolve_bs_preset(bootswatch = NA_character_))
  })

  it("throws an error if `name` or `bootswatch` don't match existing presets", {
    expect_snapshot(
      error = TRUE,
      resolve_bs_preset(preset = "not_a_preset", version = 4)
    )
    expect_snapshot(
      error = TRUE,
      resolve_bs_preset(bootswatch = "not_a_preset", version = 4)
    )
  })

  it("throws an error for unknown bootstrap version", {
    expect_error(resolve_bs_preset(preset = "cerulean", version = "2"))
    expect_error(resolve_bs_preset(bootswatch = "cerulean", version = "99"))

    expect_warning(resolve_bs_preset(bootswatch = "cerulean", version = "4-3"))
  })

  it("returns a 'default' preset if name or bootswatch is 'default' or 'bootstrap'", {
    expect_equal(
      resolve_bs_preset(preset = "default"),
      resolve_bs_preset(bootswatch = "default")
    )

    expect_equal(
      resolve_bs_preset(preset = "bootstrap"),
      resolve_bs_preset(bootswatch = "bootstrap")
    )

    expect_equal(
      resolve_bs_preset(preset = "default"),
      resolve_bs_preset(preset = "bootstrap")
    )

    expect_equal(
      unclass(resolve_bs_preset(preset = "default")),
      list(version = version_default(), name = "bootstrap")
    )

    expect_identical(class(resolve_bs_preset(preset = "default")), "bs_preset")
  })

  it("returns a BS5 Bootswatch theme preset", {
    bsw_darkly <- resolve_bs_preset(bootswatch = "darkly", version = 5)

    expect_s3_class(bsw_darkly, "bs_preset")
    expect_equal(bsw_darkly$name, "darkly")
    expect_equal(bsw_darkly$version, "5")
  })

  it("returns a BS4 Bootswatch theme preset", {
    bsw_cosmo <- resolve_bs_preset(bootswatch = "cosmo", version = 4)

    expect_s3_class(bsw_cosmo, "bs_preset")
    expect_equal(bsw_cosmo$name, "cosmo")
    expect_equal(bsw_cosmo$version, "4")
  })

  it("returns a BS3 Bootswatch theme preset", {
    bsw_readable <- resolve_bs_preset(bootswatch = "readable", version = 3)

    expect_s3_class(bsw_readable, "bs_preset")
    expect_equal(bsw_readable$name, "readable")
    expect_equal(bsw_readable$version, "3")
  })

  it("returns a bootswatch theme preset if `name` is used instead of `bootswatch`", {
    expect_equal(
      resolve_bs_preset(preset = "darkly", version = 5),
      resolve_bs_preset(bootswatch = "darkly", version = 5)
    )

    expect_equal(
      resolve_bs_preset(preset = "cosmo", version = 4),
      resolve_bs_preset(bootswatch = "cosmo", version = 4)
    )

    expect_equal(
      resolve_bs_preset(preset = "readable", version = 3),
      resolve_bs_preset(bootswatch = "readable", version = 3)
    )
  })

  it("returns the builtin shiny theme preset", {
    shiny <- resolve_bs_preset(preset = "shiny", version = 5)

    expect_s3_class(shiny, "bs_preset")
    expect_equal(shiny$name, "shiny")
    expect_equal(shiny$version, "5")
  })
})

test_that("bs_preset_bundle() returns `NULL` for default or empty preset", {
  expect_null(bs_preset_bundle(resolve_bs_preset(preset = "default")))
  expect_null(bs_preset_bundle(resolve_bs_preset(bootswatch = "default")))
  expect_null(bs_preset_bundle(NULL))
})

test_that("bs_preset_bundle() throws for unknown preset$type", {
  expect_error(bs_preset_bundle(new_bs_preset("invalid", 5, type = "unknown")))
})

describe("theme_preset_info()", {
  it("returns bootswatch theme information", {
    expect_equal(
      theme_preset_info(bs_theme(version = 5, bootswatch = "flatly")),
      new_bs_preset("flatly", version = "5", type = "bootswatch")
    )

    expect_equal(
      theme_preset_info(bs_theme(version = 4, bootswatch = "superhero")),
      new_bs_preset("superhero", version = "4", type = "bootswatch")
    )

    expect_equal(
      theme_preset_info(bs_theme(version = 3, bootswatch = "yeti")),
      new_bs_preset("yeti", version = "3", type = "bootswatch")
    )
  })

  it("returns builtin preset theme information", {
    expect_equal(
      theme_preset_info(bs_theme(version = 5, preset = "shiny")),
      new_bs_preset("shiny", version = "5", type = "builtin")
    )
  })

  it("returns vanilla bootstrap theme information", {
    expect_equal(
      theme_preset_info(bs_theme(preset = "bootstrap", version = 5)),
      new_bs_preset("bootstrap", version = "5")
    )

    expect_equal(
      theme_preset_info(bs_theme(version = 4)),
      new_bs_preset("bootstrap", version = "4")
    )

    expect_equal(
      theme_preset_info(bs_theme(version = 3)),
      new_bs_preset("bootstrap", version = "3")
    )
  })

  it("returns NULL if not given a bs_theme object", {
    expect_null(theme_preset_info(list()))
  })
})