-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Give (potential) crayon calls special treatment (#241)
* Give (potential) crayon calls special treatment Fixes #196, fixes #138 (for the links) * Add NEWS bullet * Refactor tests; cover the crayon-not-attached scenario
- Loading branch information
Showing
4 changed files
with
72 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,66 @@ | ||
skip_if_not_installed("crayon") | ||
library(crayon) | ||
|
||
describe("glue_col", { | ||
it("returns the string if no substations needed", { | ||
expect_identical(glue_col("foo"), as_glue("foo")) | ||
}) | ||
it("works the same as glue for parsable expressions", { | ||
expect_identical(glue_col("1 + 1 = {1 + 1}"), glue("1 + 1 = {1 + 1}")) | ||
}) | ||
it("applies crayon functions", { | ||
expect_identical(glue_col("{blue foo}"), as_glue(blue("foo"))) | ||
|
||
blue_and_white <- bgBlue $ white | ||
expect_identical(glue_col("{blue_and_white foo}"), as_glue(blue_and_white("foo"))) | ||
|
||
expect_identical(glue_col("{blue_and_white {1 + 1}}"), as_glue(blue_and_white("2"))) | ||
}) | ||
it("works on multiline strings", { | ||
expect_identical( | ||
glue_col(" | ||
{red foo | ||
test_that("glue_col() is just glue() when it should be", { | ||
skip_if_not_installed("crayon") | ||
expect_identical(glue_col("foo"), as_glue("foo")) | ||
expect_identical(glue_col("1 + 1 = {1 + 1}"), glue("1 + 1 = {1 + 1}")) | ||
}) | ||
|
||
test_that("glue_col() applies crayon functions, crayon not attached", { | ||
skip_if_not_installed("crayon") | ||
skip_if("crayon" %in% (.packages())) | ||
|
||
expect_identical(glue_col("{blue foo}"), as_glue(crayon::blue("foo"))) | ||
}) | ||
|
||
test_that("glue_col() applies crayon functions, crayon is attached", { | ||
skip_if_not_installed("crayon") | ||
if( !"crayon" %in% (.packages())) { | ||
withr::local_package("crayon") | ||
} | ||
|
||
blue_and_white <- bgBlue $ white | ||
expect_identical(glue_col("{blue_and_white foo}"), as_glue(blue_and_white("foo"))) | ||
expect_identical(glue_col("{blue_and_white {1 + 1}}"), as_glue(blue_and_white("2"))) | ||
}) | ||
|
||
test_that("glue_col() works on multiline strings", { | ||
skip_if_not_installed("crayon") | ||
expect_identical( | ||
glue_col(" | ||
{red foo | ||
bar | ||
}"), as_glue(red("foo\nbar"))) | ||
}) | ||
it("works on nested colors", { | ||
expect_identical(glue_col("{red This is a {green serious} problem}"), | ||
as_glue(red("This is a " %+% green("serious") %+% " problem"))) | ||
}) | ||
|
||
it("errors if there is invalid syntax or fun is not found", { | ||
expect_error(glue_col("{_}"), "unexpected input") | ||
}"), as_glue(crayon::red("foo\nbar"))) | ||
}) | ||
|
||
test_that("glue_col() works on nested colors", { | ||
skip_if_not_installed("crayon") | ||
if( !"crayon" %in% (.packages())) { | ||
withr::local_package("crayon") | ||
} | ||
expect_identical( | ||
glue_col("{red This is a {green serious} problem}"), | ||
as_glue(red("This is a " %+% green("serious") %+% " problem")) | ||
) | ||
}) | ||
|
||
test_that("glue_col() errors for invalid syntax or when color_fun can't be found", { | ||
expect_error(glue_col("{_}"), "unexpected input") | ||
expect_error(glue_col("{foo _}"), "object 'foo' of mode 'function' was not found") | ||
|
||
foo <- 1 | ||
expect_error(glue_col("{foo _}"), "object 'foo' of mode 'function' was not found") | ||
|
||
foo <- crayon::blue | ||
expect_identical(glue_col("{foo _}"), as_glue(foo("_"))) | ||
}) | ||
}) | ||
|
||
describe("glue_data_col", { | ||
it("works as expected", { | ||
mt <- head(mtcars) | ||
expect_identical(glue_data_col(mt, "A {blue {gear}} speed car with {bold {hp}} hp!"), | ||
as_glue("A " %+% blue(mt$gear) %+% " speed car with " %+% bold(mt$hp) %+% " hp!")) | ||
}) | ||
test_that("glue_data_col() works", { | ||
skip_if_not_installed("crayon") | ||
if( !"crayon" %in% (.packages())) { | ||
withr::local_package("crayon") | ||
} | ||
mt <- head(mtcars) | ||
expect_identical( | ||
glue_data_col(mt, "A {blue {gear}} speed car with {bold {hp}} hp!"), | ||
as_glue("A " %+% blue(mt$gear) %+% " speed car with " %+% bold(mt$hp) %+% " hp!") | ||
) | ||
}) |