Skip to content
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

feat: Add development chunk in fusen templates #267

Merged
merged 2 commits into from
Sep 27, 2024
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
16 changes: 8 additions & 8 deletions dev/flat_create_flat.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -665,9 +665,9 @@ test_that("Other flat_name works", {
)
expect_true(file.exists(file.path(dummypackage, "dev/flat_hello.Rmd")))
hello_flat <- readLines(dev_file_path)
# 9 times hello for function name
# 10 times hello for function name
# 2 times hello in flat_hello.Rmd
expect_equal(length(grep("hello", hello_flat)), 9 + 2)
expect_equal(length(grep("hello", hello_flat)), 10 + 2)
expect_equal(length(grep("flat_hello[.]Rmd", hello_flat)), 2)

# Template minimal used with "minpkg", same name, flat changes name
Expand All @@ -683,9 +683,9 @@ test_that("Other flat_name works", {
flat_file <- file.path(dummypackage, "dev/flat_hello_2.Rmd")
expect_true(file.exists(flat_file))
hello_flat <- readLines(flat_file)
# 9 times hello for function name
# 10 times hello for function name
# 2 times hello in flat_hello_2.Rmd
expect_equal(length(grep("hello", hello_flat)), 9 + 2)
expect_equal(length(grep("hello", hello_flat)), 10 + 2)
expect_equal(length(grep("flat_hello_2[.]Rmd", hello_flat)), 2)

# Try inflate to see if files get hello name
Expand Down Expand Up @@ -735,11 +735,11 @@ test_that("Other dev_dir works", {
expect_true(file.exists(file.path(dummypackage, "devdir/flat_hello.Rmd")))
hello_flat <- readLines(dev_file_path)

# 9 times hello for function name
# 10 times hello for function name
# 2 times hello in flat_hello.Rmd
# 1 time devdir/flat_hello.Rmd
# O time dev/flat_hello.Rmd
expect_equal(length(grep("hello", hello_flat)), 9 + 2)
expect_equal(length(grep("hello", hello_flat)), 10 + 2)
expect_equal(length(grep("flat_hello[.]Rmd", hello_flat)), 2)
expect_equal(length(grep("devdir/flat_hello[.]Rmd", hello_flat)), 1)
expect_equal(length(grep("dev/flat_hello[.]Rmd", hello_flat)), 0)
Expand Down Expand Up @@ -833,8 +833,8 @@ test_that("add_flat_template allows bad flat_name for function name with add", {
)))

dev_lines <- readLines(flat_file)
# title x 1, function x 3, example x 2, tests x 3
expect_equal(length(grep("bad_for_function_but_ok2", dev_lines)), 9)
# title x 1, development x 1, function x 3, example x 2, tests x 3
expect_equal(length(grep("bad_for_function_but_ok2", dev_lines)), 10)
})
unlink(dummypackage, recursive = TRUE)
```
Expand Down
4 changes: 4 additions & 0 deletions inst/flat-template-additional.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ pkgload::load_all(export_all = FALSE)

# my_fun

```{r development-my_fun}
# Prepare the code of your function here
```

```{r function-my_fun}
#' my_fun Title
#'
Expand Down
18 changes: 13 additions & 5 deletions inst/flat-template-full.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ After inflating the template
- This function code will automatically be added in a new file in the "R/" directory
-->

```{r development-function}
# Prepare the code of your function here
```

```{r function}
#' My median
#'
Expand All @@ -75,7 +79,9 @@ After inflating the template
#'
#' @examples
my_median <- function(x, na.rm = TRUE) {
if (!is.numeric(x)) {stop("x should be numeric")}
if (!is.numeric(x)) {
stop("x should be numeric")
}
stats::median(x, na.rm = na.rm)
}
```
Expand All @@ -101,7 +107,7 @@ my_median(1:12)
datafile <- system.file("nyc_squirrels_sample.csv", package = "<my_package_name>")
nyc_squirrels <- read.csv(datafile, encoding = "UTF-8")
# Apply my function
my_median(nyc_squirrels[,"hectare_squirrel_number"])
my_median(nyc_squirrels[, "hectare_squirrel_number"])
```

<!--
Expand All @@ -128,7 +134,7 @@ datafile <- system.file("nyc_squirrels_sample.csv", package = "<my_package_name>
nyc_squirrels <- read.csv(datafile, encoding = "UTF-8")
# Apply test on my function
test_that("my_median works properly with internal dataset", {
expect_equal(my_median(nyc_squirrels[,"hectare_squirrel_number"]), 3)
expect_equal(my_median(nyc_squirrels[, "hectare_squirrel_number"]), 3)
})
```

Expand All @@ -153,8 +159,10 @@ Each of them will be inflated in a different file, provided that there is a leve
#'
#' @examples
my_other_median <- function(x, na.rm = TRUE) {
if (!is.numeric(x)) {stop("x should be numeric")}
sub_median(x, na.rm =na.rm)
if (!is.numeric(x)) {
stop("x should be numeric")
}
sub_median(x, na.rm = na.rm)
}

#' Core of the median not exported
Expand Down
4 changes: 4 additions & 0 deletions inst/flat-template-minimal_package.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ pkgload::load_all(export_all = FALSE)

# my_fun

```{r development-my_fun}
# Prepare the code of your function here
```

```{r function-my_fun}
#' my_fun Title
#'
Expand Down
7 changes: 6 additions & 1 deletion inst/flat-template-teaching.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,16 @@ You can add `1` to any `value` using function `add_one()`.
<!--
This first section shows:

- a development part to prepare the code of the function
- the three parts necessary for a package: 'function', 'examples' and 'tests'.
+ Note that the three following chunks have names accordingly.
+ Note that the four following chunks have names accordingly.

-->

```{r development-add_one}
# Prepare the code of your function here
```

```{r function-add_one}
#' Add one to any value
#'
Expand Down
16 changes: 8 additions & 8 deletions tests/testthat/test-add_flat_template.R
Original file line number Diff line number Diff line change
Expand Up @@ -370,9 +370,9 @@ test_that("Other flat_name works", {
)
expect_true(file.exists(file.path(dummypackage, "dev/flat_hello.Rmd")))
hello_flat <- readLines(dev_file_path)
# 9 times hello for function name
# 10 times hello for function name
# 2 times hello in flat_hello.Rmd
expect_equal(length(grep("hello", hello_flat)), 9 + 2)
expect_equal(length(grep("hello", hello_flat)), 10 + 2)
expect_equal(length(grep("flat_hello[.]Rmd", hello_flat)), 2)

# Template minimal used with "minpkg", same name, flat changes name
Expand All @@ -388,9 +388,9 @@ test_that("Other flat_name works", {
flat_file <- file.path(dummypackage, "dev/flat_hello_2.Rmd")
expect_true(file.exists(flat_file))
hello_flat <- readLines(flat_file)
# 9 times hello for function name
# 10 times hello for function name
# 2 times hello in flat_hello_2.Rmd
expect_equal(length(grep("hello", hello_flat)), 9 + 2)
expect_equal(length(grep("hello", hello_flat)), 10 + 2)
expect_equal(length(grep("flat_hello_2[.]Rmd", hello_flat)), 2)

# Try inflate to see if files get hello name
Expand Down Expand Up @@ -440,11 +440,11 @@ test_that("Other dev_dir works", {
expect_true(file.exists(file.path(dummypackage, "devdir/flat_hello.Rmd")))
hello_flat <- readLines(dev_file_path)

# 9 times hello for function name
# 10 times hello for function name
# 2 times hello in flat_hello.Rmd
# 1 time devdir/flat_hello.Rmd
# O time dev/flat_hello.Rmd
expect_equal(length(grep("hello", hello_flat)), 9 + 2)
expect_equal(length(grep("hello", hello_flat)), 10 + 2)
expect_equal(length(grep("flat_hello[.]Rmd", hello_flat)), 2)
expect_equal(length(grep("devdir/flat_hello[.]Rmd", hello_flat)), 1)
expect_equal(length(grep("dev/flat_hello[.]Rmd", hello_flat)), 0)
Expand Down Expand Up @@ -538,8 +538,8 @@ test_that("add_flat_template allows bad flat_name for function name with add", {
)))

dev_lines <- readLines(flat_file)
# title x 1, function x 3, example x 2, tests x 3
expect_equal(length(grep("bad_for_function_but_ok2", dev_lines)), 9)
# title x 1, development x 1, function x 3, example x 2, tests x 3
expect_equal(length(grep("bad_for_function_but_ok2", dev_lines)), 10)
})
unlink(dummypackage, recursive = TRUE)

Expand Down
Loading