Skip to content

Commit

Permalink
Fix bug in squishing whitespace around comments (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian Cook committed Sep 6, 2019
1 parent 6f5d884 commit 2e0001c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
2 changes: 2 additions & 0 deletions R/squish_sql.R
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,14 @@ squish_sql <- function(x) {
in_line_comment <- TRUE
next;
}
in_ws <- FALSE
seek(rc_in, -1L, "current")
} else if (identical(char, "/")) {
if (identical(readChar(rc_in, 1L), "*")) {
in_block_comment <- TRUE
next;
}
in_ws <- FALSE
seek(rc_in, -1L, "current")
} else if (isTRUE(is_whitespace_character(char))) {
# this is a whitespace character
Expand Down
10 changes: 5 additions & 5 deletions tests/testthat/test-squish_sql.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,29 @@ test_that("squish_sql() removes tabs and newlines from an expression", {

test_that("squish_sql() removes line comments from a query", {
expect_equal(
squish_sql("\nSELECT x - y--x\n\tFROM y--y' ' SELECT\r\n\tWHERE z\n\t WHERE w --???w\n"),
" SELECT x FROM y WHERE z WHERE w "
squish_sql("\nSELECT x - y --x \n\t FROM y--y' ' SELECT\r\n\tWHERE z--q\n\t WHERE w --???w\n"),
" SELECT x - y FROM y WHERE z WHERE w "
)
})

test_that("squish_sql() removes block comments from a query", {
expect_equal(
squish_sql("\nSELECT /*SELECT ' ' */ x\n\tFROM y\r\n\tWHERE z\n\t WHERE w\n"),
squish_sql("\nSELECT /*SELECT ' ' */ x\n\tFROM/*)*/ y\r\n\tWHERE /*abc(((*/z\n\t WHERE w\n"),
" SELECT x FROM y WHERE z WHERE w "
)
})

test_that("squish_sql() removes line comments from an expression", {
expect_equal(
squish_sql("\nx / 2 BETWEEN --a\ny AND\r\n\tz\n -- b"),
" x BETWEEN y AND z "
" x / 2 BETWEEN y AND z "
)
})

test_that("squish_sql() removes block comments from an expression", {
expect_equal(
squish_sql("\nx*3 BETWEEN /*a AND x * 2 / 3 b*/ \ny AND /*' ' ! \"*/\r\n\tz\n"),
" x BETWEEN y AND z "
" x*3 BETWEEN y AND z "
)
})

Expand Down

0 comments on commit 2e0001c

Please sign in to comment.