From d730f1838b622abccb27c772d04a3fc79d6afcd3 Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Tue, 26 Dec 2017 23:27:58 +0100 Subject: [PATCH] support rplumber syntax --- R/rules-spacing.R | 2 +- tests/testthat/parse_comments/rplumber-in.R | 12 ++++ .../testthat/parse_comments/rplumber-in_tree | 67 +++++++++++++++++++ tests/testthat/parse_comments/rplumber-out.R | 12 ++++ tests/testthat/test-parse_comments.R | 7 +- 5 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 tests/testthat/parse_comments/rplumber-in.R create mode 100644 tests/testthat/parse_comments/rplumber-in_tree create mode 100644 tests/testthat/parse_comments/rplumber-out.R diff --git a/R/rules-spacing.R b/R/rules-spacing.R index 577380653..3f3aad10a 100644 --- a/R/rules-spacing.R +++ b/R/rules-spacing.R @@ -186,7 +186,7 @@ start_comments_with_space <- function(pd, force_one = FALSE) { comments <- rematch2::re_match( pd$text[comment_pos], - "^(?#+'*)(? *)(?.*)$" + "^(?#+['\\*]*)(? *)(?.*)$" ) comments$space_after_prefix <- nchar( diff --git a/tests/testthat/parse_comments/rplumber-in.R b/tests/testthat/parse_comments/rplumber-in.R new file mode 100644 index 000000000..186413cb6 --- /dev/null +++ b/tests/testthat/parse_comments/rplumber-in.R @@ -0,0 +1,12 @@ +# myfile.R + +#* @get /mean +normalMean <- function(samples=10) { + data <- rnorm(samples) + mean(data) +} + +#* @post /sum +addTwo <- function(a, b) { + as.numeric(a) + as.numeric(b) +} diff --git a/tests/testthat/parse_comments/rplumber-in_tree b/tests/testthat/parse_comments/rplumber-in_tree new file mode 100644 index 000000000..e0405d205 --- /dev/null +++ b/tests/testthat/parse_comments/rplumber-in_tree @@ -0,0 +1,67 @@ +ROOT (token: short_text [lag_newlines/spaces] {pos_id}) + ¦--COMMENT: # myf [0/0] {1} + ¦--COMMENT: #* @g [2/0] {2} + ¦--expr: [1/0] {3} + ¦ ¦--expr: [0/1] {5} + ¦ ¦ °--SYMBOL: norma [0/0] {4} + ¦ ¦--LEFT_ASSIGN: <- [0/1] {6} + ¦ °--expr: [0/0] {7} + ¦ ¦--FUNCTION: funct [0/0] {8} + ¦ ¦--'(': ( [0/0] {9} + ¦ ¦--SYMBOL_FORMALS: sampl [0/0] {10} + ¦ ¦--EQ_FORMALS: = [0/0] {11} + ¦ ¦--expr: [0/0] {13} + ¦ ¦ °--NUM_CONST: 10 [0/0] {12} + ¦ ¦--')': ) [0/1] {14} + ¦ °--expr: [0/0] {15} + ¦ ¦--'{': { [0/2] {16} + ¦ ¦--expr: [1/2] {17} + ¦ ¦ ¦--expr: [0/1] {19} + ¦ ¦ ¦ °--SYMBOL: data [0/0] {18} + ¦ ¦ ¦--LEFT_ASSIGN: <- [0/1] {20} + ¦ ¦ °--expr: [0/0] {21} + ¦ ¦ ¦--expr: [0/0] {23} + ¦ ¦ ¦ °--SYMBOL_FUNCTION_CALL: rnorm [0/0] {22} + ¦ ¦ ¦--'(': ( [0/0] {24} + ¦ ¦ ¦--expr: [0/0] {26} + ¦ ¦ ¦ °--SYMBOL: sampl [0/0] {25} + ¦ ¦ °--')': ) [0/0] {27} + ¦ ¦--expr: [1/0] {28} + ¦ ¦ ¦--expr: [0/0] {30} + ¦ ¦ ¦ °--SYMBOL_FUNCTION_CALL: mean [0/0] {29} + ¦ ¦ ¦--'(': ( [0/0] {31} + ¦ ¦ ¦--expr: [0/0] {33} + ¦ ¦ ¦ °--SYMBOL: data [0/0] {32} + ¦ ¦ °--')': ) [0/0] {34} + ¦ °--'}': } [1/0] {35} + ¦--COMMENT: #* @p [2/0] {36} + °--expr: [1/0] {37} + ¦--expr: [0/1] {39} + ¦ °--SYMBOL: addTw [0/0] {38} + ¦--LEFT_ASSIGN: <- [0/1] {40} + °--expr: [0/0] {41} + ¦--FUNCTION: funct [0/0] {42} + ¦--'(': ( [0/0] {43} + ¦--SYMBOL_FORMALS: a [0/0] {44} + ¦--',': , [0/1] {45} + ¦--SYMBOL_FORMALS: b [0/0] {46} + ¦--')': ) [0/1] {47} + °--expr: [0/0] {48} + ¦--'{': { [0/2] {49} + ¦--expr: [1/0] {50} + ¦ ¦--expr: [0/1] {51} + ¦ ¦ ¦--expr: [0/0] {53} + ¦ ¦ ¦ °--SYMBOL_FUNCTION_CALL: as.nu [0/0] {52} + ¦ ¦ ¦--'(': ( [0/0] {54} + ¦ ¦ ¦--expr: [0/0] {56} + ¦ ¦ ¦ °--SYMBOL: a [0/0] {55} + ¦ ¦ °--')': ) [0/0] {57} + ¦ ¦--'+': + [0/1] {58} + ¦ °--expr: [0/0] {59} + ¦ ¦--expr: [0/0] {61} + ¦ ¦ °--SYMBOL_FUNCTION_CALL: as.nu [0/0] {60} + ¦ ¦--'(': ( [0/0] {62} + ¦ ¦--expr: [0/0] {64} + ¦ ¦ °--SYMBOL: b [0/0] {63} + ¦ °--')': ) [0/0] {65} + °--'}': } [1/0] {66} diff --git a/tests/testthat/parse_comments/rplumber-out.R b/tests/testthat/parse_comments/rplumber-out.R new file mode 100644 index 000000000..186413cb6 --- /dev/null +++ b/tests/testthat/parse_comments/rplumber-out.R @@ -0,0 +1,12 @@ +# myfile.R + +#* @get /mean +normalMean <- function(samples=10) { + data <- rnorm(samples) + mean(data) +} + +#* @post /sum +addTwo <- function(a, b) { + as.numeric(a) + as.numeric(b) +} diff --git a/tests/testthat/test-parse_comments.R b/tests/testthat/test-parse_comments.R index dff80a242..61d177d8e 100644 --- a/tests/testthat/test-parse_comments.R +++ b/tests/testthat/test-parse_comments.R @@ -34,5 +34,10 @@ test_that("comments are treated corectly", { transformer = style_text, write_back = TRUE), NA) - # top-level test with indention +}) + +test_that("rplumber tags / syntax is handled properly", { + expect_warning(test_collection("parse_comments", + "rplumber", + transformer = style_text), NA) })