Skip to content

Commit

Permalink
Merge pull request #6 from benkates/master
Browse files Browse the repository at this point in the history
  • Loading branch information
ColinFay authored Jun 18, 2020
2 parents 42f6171 + db9979a commit e979574
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
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
)
})
})

0 comments on commit e979574

Please sign in to comment.