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

Offset in random_text() #6

Merged
merged 8 commits into from
Jun 18, 2020
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
11 changes: 7 additions & 4 deletions R/Text.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
#'
#' @param nchars number of characters. One of the two params should be left NULL.
#' @param nwords number of words to return. One of the two params should be left NULL.
#' @param offset number of characters or words to offset the result by. Defaults to 0.
#'
#' @importFrom attempt stop_if_all
#'
#' @return a text
#'
#' @export

random_text <- function(nchars = NULL, nwords = NULL){
random_text <- function(nchars = NULL, nwords = NULL, offset=0){
stop_if_all(
c(nchars, nwords),
is.null,
Expand All @@ -24,12 +25,14 @@ random_text <- function(nchars = NULL, nwords = NULL){
if (!is.null(nchars)){
res <- substr(
shinipsum::lorem,
1,
nchars
1+offset,
nchars+offset
)
} else {
res <- paste(shinipsum::lorem_words[1:nwords], collapse = " ")
res <- paste(shinipsum::lorem_words[1+offset:nwords+offset], collapse = " ")
}

substr(res, 1, 1) <- toupper(substr(res, 1, 1))
res
}

4 changes: 3 additions & 1 deletion man/random_text.Rd

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

37 changes: 34 additions & 3 deletions tests/testthat/test-text.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,46 @@ test_that("nchar works", {

expect_nchar(
random_text(nwords = 12),
83
86
)
expect_nchar(
random_text(nwords = 100),
643
650
)
expect_nchar(
random_text(nwords = 800),
5239
5244
)
})
})

test_that("offset works", {
lapply(
1:100, function(x){
expect_nchar(
random_text(nchars = 10, offset = 10),
10
)
expect_nchar(
random_text(nchars = 100, offset = 20),
100
)
expect_nchar(
random_text(nchars = 42, offset = 20),
42
)

expect_nchar(
random_text(nwords = 12, offset = 20),
51
)
expect_nchar(
random_text(nwords = 100, offset = 20),
526
)
expect_nchar(
random_text(nwords = 800, offset = 20),
5105
)
})
})