-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f88869c
commit 2c8c122
Showing
2 changed files
with
222 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
|
||
re_match <- function(pattern, x) { | ||
stopifnot(length(x) == 1) | ||
|
||
mat <- regexpr(pattern, x, perl = TRUE) | ||
|
||
if (mat != -1L) { | ||
res <- substring(x, mat, mat + attr(mat, "match.length") - 1L) | ||
if (length(attr(mat, "capture.start"))) { | ||
res <- c( | ||
res, | ||
substring( | ||
x, | ||
attr(mat, "capture.start"), | ||
attr(mat, "capture.start") + attr(mat, "capture.length") - 1 | ||
) | ||
) | ||
} | ||
|
||
if (any(attr(mat, "capture.names") != "")) { | ||
names(res) <- c("", attr(mat, "capture.names")) | ||
} | ||
res | ||
|
||
} else { | ||
NULL | ||
} | ||
|
||
} | ||
|
||
praise_check <- function(template, regexp, num = 10) { | ||
|
||
for (i in 1:num) { | ||
pra <- praise(template) | ||
match <- re_match(regexp, pra) | ||
expect_true( !is.null(match), info = template ) | ||
|
||
parts <- sub("[0-9]*$", "", names(match[-1])) | ||
for (p in seq_along(parts)) { | ||
expect_true(tolower(match[p + 1]) %in% | ||
tolower(praise_parts[[ parts[p] ]])) | ||
} | ||
} | ||
} | ||
|
||
is_capitalized <- function(x) { | ||
toupper(substring(x, 1, 1)) == substring(x, 1, 1) & | ||
tolower(substring(x, 2)) == substring(x, 2) | ||
} | ||
|
||
is_all_uppercase <- function(x) { | ||
toupper(x) == x | ||
} | ||
|
||
is_all_lowercase <- function(x) { | ||
tolower(x) == x | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,170 @@ | ||
|
||
context("praise") | ||
context("parts") | ||
|
||
test_that("praise works", { | ||
test_that("all parts are lowercase (except some)", { | ||
for (p in names(praise_parts)) { | ||
pp <- setdiff(praise_parts[[p]], "R package") | ||
expect_true( all(is_all_lowercase(pp)), info = p ) | ||
} | ||
}) | ||
|
||
context("templating") | ||
|
||
test_that("template without praise word", { | ||
|
||
str <- "This is just a random string" | ||
expect_equal(praise(str), str) | ||
|
||
expect_equal(praise(""), "") | ||
|
||
}) | ||
|
||
test_that("corner cases are OK", { | ||
praise_check("", "") | ||
praise_check("x", "x") | ||
praise_check("${adjective}", "^(?<adjective>.*)$") | ||
}) | ||
|
||
test_that("template with a single part", { | ||
|
||
praise_check( | ||
"This is ${adjective}.", | ||
"^This is (?<adjective1>.*)\\.$" | ||
) | ||
|
||
praise_check( | ||
"This is ${adverb}.", | ||
"^This is (?<adverb1>.*)\\.$" | ||
) | ||
|
||
praise_check( | ||
"This is ${adverb_manner}.", | ||
"This is (?<adverb_manner1>.*)\\.$" | ||
) | ||
|
||
praise_check( | ||
"This was ${created}.", | ||
"^This was (?<created1>.*)\\.$" | ||
) | ||
|
||
praise_check( | ||
"This was ${creating}.", | ||
"^This was (?<creating1>.*)\\.$" | ||
) | ||
|
||
praise_check( | ||
"This was ${exclamation}.", | ||
"^This was (?<exclamation1>.*)\\.$" | ||
) | ||
|
||
praise_check( | ||
"This is a nice ${rpackage}!", | ||
"^This is a nice (?<rpackage1>.*)!$" | ||
) | ||
|
||
}) | ||
|
||
test_that("templates with multiple parts of the same type", { | ||
|
||
praise_check("This is ${adjective} and ${adjective}.", | ||
"^This is (?<adjective1>.*) and (?<adjective2>.*)\\.$") | ||
|
||
expect_true(TRUE) | ||
}) | ||
|
||
test_that("different part types in the same template", { | ||
|
||
praise_check( | ||
"You did this ${adverb_manner} and it got ${adjective}!", | ||
"^You did this (?<adverb_manner>.*) and it got (?<adjective>.*)!$" | ||
) | ||
|
||
}) | ||
|
||
context("Capilatization") | ||
|
||
test_that("templates are case insensitive", { | ||
|
||
praise_check("${AdjeCtiVe}", "^(?<adjective>.*)$") | ||
|
||
praise_check( | ||
"This is ${adjeCtive}.", | ||
"^This is (?<adjective1>.*)\\.$" | ||
) | ||
|
||
praise_check( | ||
"This is ${Adverb}.", | ||
"^This is (?<adverb1>.*)\\.$" | ||
) | ||
|
||
praise_check( | ||
"This is ${Adverb_Manner}.", | ||
"This is (?<adverb_manner1>.*)\\.$" | ||
) | ||
|
||
praise_check( | ||
"This was ${CREATED}.", | ||
"^This was (?<created1>.*)\\.$" | ||
) | ||
|
||
praise_check( | ||
"This was ${creatING}.", | ||
"^This was (?<creating1>.*)\\.$" | ||
) | ||
|
||
praise_check( | ||
"This was ${EXClamation}.", | ||
"^This was (?<exclamation1>.*)\\.$" | ||
) | ||
|
||
praise_check( | ||
"This is a nice ${rpACKage}!", | ||
"^This is a nice (?<rpackage1>.*)!$" | ||
) | ||
|
||
praise_check("This is ${Adjective} and ${Adjective}.", | ||
"^This is (?<adjective1>.*) and (?<adjective2>.*)\\.$") | ||
|
||
praise_check( | ||
"You did this ${adVERB_manner} and it got ${Adjective}!", | ||
"^You did this (?<adverb_manner>.*) and it got (?<adjective>.*)!$" | ||
) | ||
|
||
}) | ||
|
||
test_that("first letter is capitalized", { | ||
for (i in 1:10) { | ||
pra <- praise("${Adjective}") | ||
expect_true(is_capitalized(pra)) | ||
pra <- praise("${Adverb}") | ||
expect_true(is_capitalized(pra)) | ||
pra <- praise("${Adverb_manner}") | ||
expect_true(is_capitalized(pra)) | ||
pra <- praise("${Created}") | ||
expect_true(is_capitalized(pra)) | ||
pra <- praise("${Creating}") | ||
expect_true(is_capitalized(pra)) | ||
pra <- praise("${Exclamation}") | ||
expect_true(is_capitalized(pra)) | ||
pra <- praise("${Rpackage}") | ||
expect_true(is_capitalized(pra)) | ||
} | ||
}) | ||
|
||
test_that("whole word is upper case", { | ||
for (i in 1:10) { | ||
pra <- praise("${ADJECTIVE}") | ||
expect_true(is_all_uppercase(pra), info = pra) | ||
pra <- praise("${ADVERB}") | ||
expect_true(is_all_uppercase(pra)) | ||
pra <- praise("${ADVERB_MANNER}") | ||
expect_true(is_all_uppercase(pra)) | ||
pra <- praise("${CREATED}") | ||
expect_true(is_all_uppercase(pra)) | ||
pra <- praise("${CREATING}") | ||
expect_true(is_all_uppercase(pra)) | ||
pra <- praise("${EXCLAMATION}") | ||
expect_true(is_all_uppercase(pra)) | ||
pra <- praise("${RPACKAGE}") | ||
expect_true(is_all_uppercase(pra)) | ||
} | ||
}) |