diff --git a/R/nested.R b/R/nested.R index 11c71f38f..05d7b562a 100644 --- a/R/nested.R +++ b/R/nested.R @@ -7,7 +7,9 @@ #' @return A nested parse table. See [tokenize()] for details on the columns #' of the parse table. compute_parse_data_nested <- function(text) { - parse_data <- tokenize(text) %>% + tokenized <- tokenize(text) + if (nrow(tokenized) == 0) return(data_frame()) + parse_data <- tokenized %>% add_terminal_token_before() %>% add_terminal_token_after() diff --git a/R/transform.R b/R/transform.R index 4d1a4cdfe..e675d9dd6 100644 --- a/R/transform.R +++ b/R/transform.R @@ -84,6 +84,14 @@ make_transformer <- function(transformers) { #' @inheritParams apply_transformers parse_transform_serialize <- function(text, transformers) { pd_nested <- compute_parse_data_nested(text) + + if (nrow(pd_nested) == 0) { + warning( + "Text to style did not contain any tokens. Returning empty string.", + call. = FALSE + ) + return("") + } transformed_pd <- apply_transformers(pd_nested, transformers) # TODO verify_roundtrip flattened_pd <- post_visit(transformed_pd, list(extract_terminals)) %>% diff --git a/tests/testthat/exception_handling/empty_file.R b/tests/testthat/exception_handling/empty_file.R new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/tests/testthat/exception_handling/empty_file.R @@ -0,0 +1 @@ + diff --git a/tests/testthat/test-exception_handling.R b/tests/testthat/test-exception_handling.R index 1371a70c3..4e6b327e3 100644 --- a/tests/testthat/test-exception_handling.R +++ b/tests/testthat/test-exception_handling.R @@ -6,7 +6,19 @@ test_that("style_text returns custom error", { test_that("style_file returns custom error", { expect_warning( - style_file(testthat_file("exception_handling", "/parser-error.R")), + style_file(testthat_file("exception_handling", "parser-error.R")), "When processing" ) }) + + +test_that("style_text with no tokens returns empty string and warning", { + expect_warning(style_text("\n\n"), "not contain any tokens.") +}) + +test_that("style_file with no tokens returns empty string and warning", { + expect_warning( + style_file(testthat_file("exception_handling", "empty_file.R")), + "not contain any tokens." + ) +})