Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/copilot-instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ you're testing with the most recent R version that users will have.
- Always wrap test conditions in `{}`: `assert('message', {})`
- Use `has_error()` instead of `tryCatch()` for error testing
- Load the package with `library(gglite)` before testing
- Use `%==%` (from testit) instead of `==` to test for strict equality
- Never use `:::` to access internal functions in tests; testit exposes
internal functions automatically, so call them directly

### Rendering Example Rmd Files

Expand Down
90 changes: 45 additions & 45 deletions tests/testit/test-gglite.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,24 @@ assert('g2() accepts data and aesthetics', {
df = data.frame(x = 1:3, y = 4:6)
chart = g2(df, x = 'x', y = 'y')
(identical(chart$data, df))
(chart$aesthetics$x == 'x')
(chart$aesthetics$y == 'y')
(chart$aesthetics$x %==% 'x')
(chart$aesthetics$y %==% 'y')
})

# encode() maps aesthetics as character strings
assert('encode() maps column names to aesthetics', {
chart = g2() |> encode(x = 'foo', y = 'bar', color = 'baz')
(chart$aesthetics$x == 'foo')
(chart$aesthetics$y == 'bar')
(chart$aesthetics$color == 'baz')
(chart$aesthetics$x %==% 'foo')
(chart$aesthetics$y %==% 'bar')
(chart$aesthetics$color %==% 'baz')
})

# mark_*() functions add layers
assert('mark_point() and mark_line() add layers', {
chart = g2() |> mark_point() |> mark_line()
(length(chart$layers) == 2)
(chart$layers[[1]]$type == 'point')
(chart$layers[[2]]$type == 'line')
(length(chart$layers) %==% 2L)
(chart$layers[[1]]$type %==% 'point')
(chart$layers[[2]]$type %==% 'line')
})

# build_config() produces correct spec with column-major data
Expand All @@ -37,20 +37,20 @@ assert('build_config() produces correct spec', {
chart = g2(df, x = 'x', y = 'y') |>
mark_interval() |>
scale_of('y', nice = TRUE)
config = gglite:::build_config(chart)
config = build_config(chart)

# spec should NOT contain constructor options
(is.null(config$width))
(is.null(config$height))
# data should be annotated as column-major
(config$data$type == 'column')
(config$data$type %==% 'column')
(is.data.frame(config$data$value))
# marks
(length(config$children) == 1)
(config$children[[1]]$type == 'interval')
(config$children[[1]]$encode$x == 'x')
(config$children[[1]]$encode$y == 'y')
(config$scale$y$nice == TRUE)
(length(config$children) %==% 1L)
(config$children[[1]]$type %==% 'interval')
(config$children[[1]]$encode$x %==% 'x')
(config$children[[1]]$encode$y %==% 'y')
(config$scale$y$nice %==% TRUE)
})

# chart_html() generates valid HTML with G2.Chart constructor args
Expand All @@ -72,32 +72,32 @@ assert('chart_html() generates correct HTML structure', {
# transform_of() adds transforms to the last mark
assert('transform_of() adds transforms', {
chart = g2() |> mark_interval() |> transform_of('stackY')
(length(chart$layers[[1]]$transform) == 1)
(chart$layers[[1]]$transform[[1]]$type == 'stackY')
(length(chart$layers[[1]]$transform) %==% 1L)
(chart$layers[[1]]$transform[[1]]$type %==% 'stackY')
})

# coord_transpose() adds transpose transform
assert('coord_transpose() adds transpose transform', {
chart = g2() |> mark_interval() |> coord_transpose()
(length(chart$coords$transform) == 1)
(chart$coords$transform[[1]]$type == 'transpose')
(length(chart$coords$transform) %==% 1L)
(chart$coords$transform[[1]]$type %==% 'transpose')
})

# facet_rect() sets facet config
assert('facet_rect() sets faceting', {
chart = g2() |> mark_point() |> facet_rect(x = 'Species')
(chart$facet$type == 'facetRect')
(chart$facet$encode$x == 'Species')
config = gglite:::build_config(chart)
(config$type == 'facetRect')
(config$encode$x == 'Species')
(chart$facet$type %==% 'facetRect')
(chart$facet$encode$x %==% 'Species')
config = build_config(chart)
(config$type %==% 'facetRect')
(config$encode$x %==% 'Species')
})

# animate() sets animation on last mark
assert('animate() sets animation options', {
chart = g2() |> mark_interval() |>
animate(enter = list(type = 'fadeIn'))
(chart$layers[[1]]$animate$enter$type == 'fadeIn')
(chart$layers[[1]]$animate$enter$type %==% 'fadeIn')
})

# theme_of(), axis_of(), legend_of(), title_of() set chart options
Expand All @@ -108,10 +108,10 @@ assert('component functions set chart options', {
axis_of('x', title = 'MPG') |>
legend_of('color', position = 'right') |>
title_of('Cars')
(chart$theme$type == 'dark')
(chart$axes$x$title == 'MPG')
(chart$legends$color$position == 'right')
(chart$chart_title == 'Cars')
(chart$theme$type %==% 'dark')
(chart$axes$x$title %==% 'MPG')
(chart$legends$color$position %==% 'right')
(chart$chart_title %==% 'Cars')
})

# End-to-end pipe chaining
Expand All @@ -122,61 +122,61 @@ assert('pipe chaining works end-to-end', {
coordinate('polar') |>
interact('tooltip')
(inherits(chart, 'g2'))
(length(chart$layers) == 1)
(chart$scales$x$type == 'linear')
(chart$coords$type == 'polar')
(length(chart$interactions) == 1)
(length(chart$layers) %==% 1L)
(chart$scales$x$type %==% 'linear')
(chart$coords$type %==% 'polar')
(length(chart$interactions) %==% 1L)
})

# g2_cdn() is customizable via option
assert('g2_cdn() respects gglite.g2_cdn option', {
old = getOption('gglite.g2_cdn')
options(gglite.g2_cdn = 'https://example.com/g2.js')
res = gglite:::g2_cdn()
res = g2_cdn()
options(gglite.g2_cdn = old)
(res == 'https://example.com/g2.js')
(res %==% 'https://example.com/g2.js')
})

# annotate_df correctly wraps data frames
assert('annotate_df() wraps data frames', {
x = list(data = data.frame(a = 1:3), children = list(
list(type = 'point', data = data.frame(b = 4:6))
))
res = gglite:::annotate_df(x)
(res$data$type == 'column')
res = annotate_df(x)
(res$data$type %==% 'column')
(is.data.frame(res$data$value))
(res$children[[1]]$data$type == 'column')
(res$children[[1]]$data$type %==% 'column')
})

# coord_*() shortcut functions
assert('coord_polar() sets polar coordinate', {
chart = g2() |> mark_interval() |> coord_polar()
(chart$coords$type == 'polar')
(chart$coords$type %==% 'polar')
})

assert('coord_theta() sets theta coordinate', {
chart = g2() |> mark_interval() |> coord_theta(innerRadius = 0.5)
(chart$coords$type == 'theta')
(chart$coords$innerRadius == 0.5)
(chart$coords$type %==% 'theta')
(chart$coords$innerRadius %==% 0.5)
})

assert('coord_radial() sets radial coordinate', {
chart = g2() |> mark_interval() |> coord_radial()
(chart$coords$type == 'radial')
(chart$coords$type %==% 'radial')
})

# theme_*() shortcut functions
assert('theme_classic() sets classic theme', {
chart = g2() |> mark_point() |> theme_classic()
(chart$theme$type == 'classic')
(chart$theme$type %==% 'classic')
})

assert('theme_dark() sets dark theme', {
chart = g2() |> mark_point() |> theme_dark()
(chart$theme$type == 'dark')
(chart$theme$type %==% 'dark')
})

assert('theme_academy() sets academy theme', {
chart = g2() |> mark_point() |> theme_academy()
(chart$theme$type == 'academy')
(chart$theme$type %==% 'academy')
})
Loading