Skip to content

Empty input for styling does not cause error #227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 27, 2017
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
4 changes: 3 additions & 1 deletion R/nested.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
8 changes: 8 additions & 0 deletions R/transform.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)) %>%
Expand Down
1 change: 1 addition & 0 deletions tests/testthat/exception_handling/empty_file.R
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

14 changes: 13 additions & 1 deletion tests/testthat/test-exception_handling.R
Original file line number Diff line number Diff line change
Expand Up @@ -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."
)
})