Skip to content

Commit

Permalink
Add vectorised unit test for Pangram
Browse files Browse the repository at this point in the history
As another demonstration of issue exercism#109, I've added a vectorised unit test to the Pangram exercise:
- Add vector test to unit tests.
- Rename `is_pangram` function as `is_pangram_scalar`.
- Create new `is_pangram` function by calling `Vectorize` on `is_pangram_scalar` function.
  • Loading branch information
xarxziux committed Oct 6, 2018
1 parent 82456e5 commit b4b1cac
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
4 changes: 3 additions & 1 deletion exercises/pangram/example.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
library(magrittr)

is_pangram <- function(input) {
is_pangram_scalar <- function(input) {

input_letters <- gsub("[^A-Za-z]", "", input) %>%
tolower() %>%
Expand All @@ -12,3 +12,5 @@ is_pangram <- function(input) {
identical(input_letters, letters)

}

is_pangram <- Vectorize(is_pangram_scalar)
19 changes: 18 additions & 1 deletion exercises/pangram/test_pangram.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,26 @@ test_that("pangram with mixed case and punctuation", {
expect_true(is_pangram("\"Five quacking Zephyrs jolt my wax bed.\""))
})

test_that("upper and lower case versions of the same character should not be
test_that("upper and lower case versions of the same character should not be
counted separately", {
expect_false(is_pangram("the quick brown fox jumped over the lazy FOX"))
})

test_that("The submission can handle vector inputs", {
expect_true (all (
is_pangram(c(
"",
"the quick brown fox jumps over the lazy dog",
"a quick movement of the enemy will jeopardize five gunboats",
"the quick brown fish jumps over the lazy dog",
"the_quick_brown_fox_jumps_over_the_lazy_dog",
"the 1 quick brown fox jumps over the 2 lazy dogs",
"7h3 qu1ck brown fox jumps ov3r 7h3 lazy dog",
"\"Five quacking Zephyrs jolt my wax bed.\"",
"the quick brown fox jumped over the lazy FOX")) ==
c(FALSE, TRUE, FALSE, FALSE, TRUE, TRUE, FALSE, TRUE, FALSE)
)
)
})

message("All tests passed for exercise: pangram")

0 comments on commit b4b1cac

Please sign in to comment.