Skip to content

Commit

Permalink
Merge pull request #587 from yjunechoe/glue-brief
Browse files Browse the repository at this point in the history
Support glue syntax in `brief`
  • Loading branch information
yjunechoe authored Jan 24, 2025
2 parents 8ba5e85 + 06cb5ea commit 4449f9a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 14 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# pointblank (development version)

* The `brief` argument of validation functions now also supports `{glue}` syntax (#587)

* Validation step `brief`s correctly recycle to match expanded steps (#564)

# pointblank 0.12.2

This release provides a few minor improvements along with many bug fixes.
Expand Down
31 changes: 17 additions & 14 deletions R/steps_and_briefs.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,18 @@ create_validation_step <- function(
f_failed = NA_real_
)

# glue-powered labels
if (!is.na(validation_step_df$label)) {
glue_mask <- rlang::new_environment(
data = list(
.step = assertion_type,
.col = column,
.seg_col = seg_col,
.seg_val = seg_val
),
parent = .call
)
label <- glue::glue_data(glue_mask, validation_step_df$label, .envir = NULL)
validation_step_df$label <- as.character(label)
}
# glue-powered labels/briefs
glue_mask <- rlang::new_environment(
data = list(
.step = assertion_type,
.col = column,
.seg_col = seg_col,
.seg_val = seg_val
),
parent = .call
)
validation_step_df$label <- glue_chr(validation_step_df$label, glue_mask)
validation_step_df$brief <- glue_chr(validation_step_df$brief, glue_mask)

# Append `validation_step` to `validation_set`
agent$validation_set <-
Expand All @@ -117,6 +115,11 @@ create_validation_step <- function(
agent
}

glue_chr <- function(x, glue_mask) {
if (is.na(x)) return(x)
as.character(glue::glue_data(glue_mask, x, .envir = NULL))
}

get_hash_version <- function() {
"v0.12"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,43 @@ test_that("glue syntax works for custom vector of labels", {
)

})

test_that("glue works for brief too", {

# Basic usage (recycled)
agent <- create_agent(~ small_table) %>%
col_vals_lt(
c(a, c), 5, segments = vars(f),
label = "{.col} {.seg_val}",
brief = "{.col} {.seg_val}"
)
expect_identical(
agent$validation_set$label,
agent$validation_set$brief
)

# NAs (recycled)
agent_NA <- create_agent(~ small_table) %>%
col_vals_lt(
c(a, c), 5, segments = vars(f),
label = NA,
brief = NA
)
expect_identical(
agent_NA$validation_set$label,
agent_NA$validation_set$brief
)

# Mix (non-recycled)
agent_mix <- create_agent(~ small_table) %>%
col_vals_lt(
c(a, c), 5, segments = vars(f),
label = replace(rep("{.col} {.seg_val}", 6), 3, NA),
brief = replace(rep("{.col} {.seg_val}", 6), 3, NA)
)
expect_identical(
agent_mix$validation_set$label,
agent_mix$validation_set$brief
)

})

0 comments on commit 4449f9a

Please sign in to comment.