Skip to content

Commit

Permalink
add lintr details to contributing (#58)
Browse files Browse the repository at this point in the history
* add lintr details to contributing

* fix linting issues
  • Loading branch information
cjrace authored Feb 10, 2024
1 parent 9b2cb5e commit 62fca2a
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 48 deletions.
16 changes: 14 additions & 2 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,21 @@ Once you've incremented the version number, it'll offer to perform a commit on y

### Code style

New code should follow the tidyverse [style guide](https://style.tidyverse.org).
New code should follow the tidyverse [style guide](https://style.tidyverse.org). We use [lintr](https://lintr.r-lib.org/articles/lintr.html) to scan styling on pull requests, this will automatically run and add comments for any code that is failing the standards we'd expect. Where these happen, please proactively resolve these as we are unlikely to approve pull requests that have styling issues.

You can use the [styler](https://CRAN.R-project.org/package=styler) package to apply these styles.
You can use the [styler](https://CRAN.R-project.org/package=styler) package to apply most of the styling using:

``` r
styler::style_pkg()
```

To check for any further styling issues locally, use:

``` r
lintr::lint_package()
```

[styler](https://CRAN.R-project.org/package=styler) will not fix all linting issues, so we recommend using that first, then using [lintr](https://lintr.r-lib.org/articles/lintr.html) to check for places you may need to manually fix styling issues such as line length or not using snake_case.

We use [testthat](https://cran.r-project.org/package=testthat) for unit tests, we expect all new functions to have some level of test coverage.

Expand Down
4 changes: 2 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Generated by roxygen2: do not edit by hand

export(formatAY)
export(roundFiveUp)
export(format_ay)
export(round_five_up)
importFrom(lifecycle,deprecated)
13 changes: 7 additions & 6 deletions R/formatAY.R → R/format_ay.R
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
#' Format academic year
#'
#' This function formats academic year variables for reporting purposes. It
#' will convert an academic year input from 201516 format to 2015/16 format. \cr\cr
#' will convert an academic year input from 201516 format to 2015/16 format.
#'
#' It accepts both numerical and character arguments.
#'
#' @param year Academic year
#' @return Character vector of formatted academic year
#' @export
#' @examples
#' formatAY(201617)
#' formatAY("201617")
formatAY <- function(year) {
if (!grepl("^[0-9]{6,6}$", year)) stop("year parameter must be a six digit number e.g. 201617")

#' format_ay(201617)
#' format_ay("201617")
format_ay <- function(year) {
if (!grepl("^[0-9]{6,6}$", year)) {
stop("year parameter must be a six digit number e.g. 201617")
}
sub("(.{4})(.*)", "\\1/\\2", year)
}
6 changes: 3 additions & 3 deletions R/roundFiveUp.R → R/round_five_up.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
#' @export
#'
#' @examples
#' roundFiveUp(2495, -1)
#' roundFiveUp(2495.85, 1)
roundFiveUp <- function(value, dp) {
#' round_five_up(2495, -1)
#' round_five_up(2495.85, 1)
round_five_up <- function(value, dp) {
if (!is.numeric(value) && !is.numeric(dp)) stop("both inputs must be numeric")
if (!is.numeric(value)) stop("the value to be rounded must be numeric")
if (!is.numeric(dp)) stop("the decimal places value must be numeric")
Expand Down
14 changes: 7 additions & 7 deletions man/formatAY.Rd → man/format_ay.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions man/roundFiveUp.Rd → man/round_five_up.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 0 additions & 13 deletions tests/testthat/test-formatAY.R

This file was deleted.

13 changes: 13 additions & 0 deletions tests/testthat/test-format_ay.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
test_that("Rejects non 6-digit numbers", {
expect_error(format_ay(19858))
expect_error(format_ay(1985))
expect_error(format_ay(1985867))
expect_error(format_ay("1999c"))
expect_error(format_ay("abcdef"))
expect_error(format_ay("1985-98"))
})

test_that("Converts correctly", {
expect_equal(format_ay(199920), "1999/20")
expect_equal(format_ay("199920"), "1999/20")
})
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
test_that("Rounds fives up", {
expect_equal(roundFiveUp(285, -1), 290)
expect_equal(roundFiveUp(2.85, 1), 2.9)
expect_equal(round_five_up(285, -1), 290)
expect_equal(round_five_up(2.85, 1), 2.9)
})

test_that("Rounds other numbers", {
expect_equal(roundFiveUp(283, -1), 280)
expect_equal(roundFiveUp(2.87, 1), 2.9)
expect_equal(round_five_up(283, -1), 280)
expect_equal(round_five_up(2.87, 1), 2.9)
})

test_that("Input validation", {
expect_error(
roundFiveUp("ten", "10"),
round_five_up("ten", "10"),
"both inputs must be numeric"
)
expect_error(
roundFiveUp(12, "ten"),
round_five_up(12, "ten"),
"the decimal places value must be numeric"
)
expect_error(
roundFiveUp(12, "10"),
round_five_up(12, "10"),
"the decimal places value must be numeric"
)
expect_error(
roundFiveUp("twelve", 10),
round_five_up("twelve", 10),
"the value to be rounded must be numeric"
)
expect_error(
roundFiveUp("12", 10),
round_five_up("12", 10),
"the value to be rounded must be numeric"
)
})

0 comments on commit 62fca2a

Please sign in to comment.