Skip to content

Commit

Permalink
Use more specific test functions (#374)
Browse files Browse the repository at this point in the history
* Use expect_false and expect_true

* Use expect_null

* Use expect_match
  • Loading branch information
ErikSchierboom authored Oct 17, 2024
1 parent b088f89 commit 7a1eed9
Show file tree
Hide file tree
Showing 13 changed files with 144 additions and 143 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ library(testthat)

test_that("Cannot execute fast attack if knight is awake", {
knight_is_awake <- TRUE
expect_equal(can_fast_attack(knight_is_awake), FALSE)
expect_false(can_fast_attack(knight_is_awake))
})

test_that("Can execute fast attack if knight is sleeping", {
knight_is_awake <- FALSE
expect_equal(can_fast_attack(knight_is_awake), TRUE)
expect_true(can_fast_attack(knight_is_awake))
})

# 2) can_spy
Expand Down
22 changes: 11 additions & 11 deletions exercises/concept/elyses-enchantments/test_elyses-enchantments.R
Original file line number Diff line number Diff line change
Expand Up @@ -145,65 +145,65 @@ test_that("remove the card from the bottom", {
test_that("an empty stack of cards", {
stack <- c()
stack_size <- 0
expect_equal(check_size_of_stack(stack, stack_size), TRUE)
expect_true(check_size_of_stack(stack, stack_size))
})

test_that("an empty stack of cards", {
stack <- c()
stack_size <- 1
expect_equal(check_size_of_stack(stack, stack_size), FALSE)
expect_false(check_size_of_stack(stack, stack_size))
})

test_that("has exactly 1 card", {
stack <- c(7)
stack_size <- 0
expect_equal(check_size_of_stack(stack, stack_size), FALSE)
expect_false(check_size_of_stack(stack, stack_size))
})

test_that("has exactly 1 card", {
stack <- c(7)
stack_size <- 1
expect_equal(check_size_of_stack(stack, stack_size), TRUE)
expect_true(check_size_of_stack(stack, stack_size))
})

test_that("has exactly 1 card", {
stack <- c(7)
stack_size <- 2
expect_equal(check_size_of_stack(stack, stack_size), FALSE)
expect_false(check_size_of_stack(stack, stack_size))
})

test_that("has exactly 4 cards", {
stack <- c(2, 4, 6, 8)
stack_size <- 3
expect_equal(check_size_of_stack(stack, stack_size), FALSE)
expect_false(check_size_of_stack(stack, stack_size))
})

test_that("has exactly 4 cards", {
stack <- c(2, 4, 6, 8)
stack_size <- 4
expect_equal(check_size_of_stack(stack, stack_size), TRUE)
expect_true(check_size_of_stack(stack, stack_size))
})

test_that("has exactly 4 cards", {
stack <- c(2, 4, 6, 8)
stack_size <- 15
expect_equal(check_size_of_stack(stack, stack_size), FALSE)
expect_false(check_size_of_stack(stack, stack_size))
})

test_that("has exactly 5 cards", {
stack <- c(1, 3, 5, 7, 9)
stack_size <- 3
expect_equal(check_size_of_stack(stack, stack_size), FALSE)
expect_false(check_size_of_stack(stack, stack_size))
})

test_that("has exactly 5 cards", {
stack <- c(1, 3, 5, 7, 9)
stack_size <- 4
expect_equal(check_size_of_stack(stack, stack_size), FALSE)
expect_false(check_size_of_stack(stack, stack_size))
})

test_that("has exactly 5 cards", {
stack <- c(1, 3, 5, 7, 9)
stack_size <- 5
expect_equal(check_size_of_stack(stack, stack_size), TRUE)
expect_true(check_size_of_stack(stack, stack_size))
})
18 changes: 9 additions & 9 deletions exercises/practice/armstrong-numbers/test_armstrong-numbers.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,37 @@ source("./armstrong-numbers.R")
library(testthat)

test_that("Zero is an Armstrong number", {
expect_equal(is_armstrong_number(0), TRUE)
expect_true(is_armstrong_number(0))
})

test_that("Single-digit numbers are Armstrong numbers", {
expect_equal(is_armstrong_number(5), TRUE)
expect_true(is_armstrong_number(5))
})

test_that("There are no two-digit Armstrong numbers", {
expect_equal(is_armstrong_number(10), FALSE)
expect_false(is_armstrong_number(10))
})

test_that("Three-digit number that is an Armstrong number", {
expect_equal(is_armstrong_number(153), TRUE)
expect_true(is_armstrong_number(153))
})

test_that("Three-digit number that is not an Armstrong number", {
expect_equal(is_armstrong_number(100), FALSE)
expect_false(is_armstrong_number(100))
})

test_that("Four-digit number that is an Armstrong number", {
expect_equal(is_armstrong_number(9474), TRUE)
expect_true(is_armstrong_number(9474))
})

test_that("Four-digit number that is not an Armstrong number", {
expect_equal(is_armstrong_number(9475), FALSE)
expect_false(is_armstrong_number(9475))
})

test_that("Seven-digit number that is an Armstrong number", {
expect_equal(is_armstrong_number(9926315), TRUE)
expect_true(is_armstrong_number(9926315))
})

test_that("Seven-digit number that is not an Armstrong number", {
expect_equal(is_armstrong_number(9926314), FALSE)
expect_false(is_armstrong_number(9926314))
})
39 changes: 19 additions & 20 deletions exercises/practice/isbn-verifier/test_isbn-verifier.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,78 +2,77 @@ source("./isbn-verifier.R")
library(testthat)

test_that("Valid isbn", {
expect_equal(is_valid("3-598-21508-8"), TRUE)
expect_true(is_valid("3-598-21508-8"))
})

test_that("Invalid isbn check digit", {
expect_equal(is_valid("3-598-21508-9"), FALSE)
expect_false(is_valid("3-598-21508-9"))
})

test_that("Valid isbn with a check digit of 10", {
expect_equal(is_valid("3-598-21507-X"), TRUE)
expect_true(is_valid("3-598-21507-X"))
})

test_that("Check digit is a character other than X", {
expect_equal(is_valid("3-598-21507-A"), FALSE)
expect_false(is_valid("3-598-21507-A"))
})

test_that("Invalid check digit in isbn is not treated as zero", {
expect_equal(is_valid("4-598-21507-B"), FALSE)
expect_false(is_valid("4-598-21507-B"))
})

test_that("Invalid character in isbn is not treated as zero", {
expect_equal(is_valid("3-598-P1581-X"), FALSE)
expect_false(is_valid("3-598-P1581-X"))
})

test_that("X is only valid as a check digit", {
expect_equal(is_valid("3-598-2X507-9"), FALSE)
expect_false(is_valid("3-598-2X507-9"))
})

test_that("Valid isbn without separating dashes", {
expect_equal(is_valid("3598215088"), TRUE)
expect_true(is_valid("3598215088"))
})

test_that("Isbn without separating dashes and X as check digit", {
expect_equal(is_valid("359821507X"), TRUE)
expect_true(is_valid("359821507X"))
})

test_that("Isbn without check digit and dashes", {
expect_equal(is_valid("359821507"), FALSE)
expect_false(is_valid("359821507"))
})

test_that("Too long isbn and no dashes", {
expect_equal(is_valid("3598215078X"), FALSE)
expect_false(is_valid("3598215078X"))
})

test_that("Too short isbn", {
expect_equal(is_valid("00"), FALSE)
expect_false(is_valid("00"))
})

test_that("Isbn without check digit", {
expect_equal(is_valid("3-598-21507"), FALSE)
expect_false(is_valid("3-598-21507"))
})

test_that("Check digit of X should not be used for 0", {
expect_equal(is_valid("3-598-21515-X"), FALSE)
expect_false(is_valid("3-598-21515-X"))
})

test_that("Empty isbn", {
expect_equal(is_valid(""), FALSE)
expect_false(is_valid(""))
})

test_that("Input is 9 characters", {
expect_equal(is_valid("134456729"), FALSE)
expect_false(is_valid("134456729"))
})

test_that("Invalid characters are not ignored after checking length", {
expect_equal(is_valid("3132P34035"), FALSE)
expect_false(is_valid("3132P34035"))
})

test_that("Invalid characters are not ignored before checking length", {
expect_equal(is_valid("3598P215088"), FALSE)
expect_false(is_valid("3598P215088"))
})

test_that("Input is too long but contains a valid isbn", {
expect_equal(is_valid("98245726788"), FALSE)
expect_false(is_valid("98245726788"))
})

16 changes: 8 additions & 8 deletions exercises/practice/isogram/test_isogram.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,40 @@ library(testthat)

test_that("empty string", {
word <- ""
expect_equal(is_isogram(word), TRUE)
expect_true(is_isogram(word))
})

test_that("isogram with only lower case characters", {
word <- "isogram"
expect_equal(is_isogram(word), TRUE)
expect_true(is_isogram(word))
})

test_that("word with one duplicated character", {
word <- "eleven"
expect_equal(is_isogram(word), FALSE)
expect_false(is_isogram(word))
})

test_that("longest reported english isogram", {
word <- "subdermatoglyphic"
expect_equal(is_isogram(word), TRUE)
expect_true(is_isogram(word))
})

test_that("word with duplicated character in mixed case", {
word <- "Alphabet"
expect_equal(is_isogram(word), FALSE)
expect_false(is_isogram(word))
})

test_that("hypothetical isogrammic word with hyphen", {
word <- "thumbscrew-japingly"
expect_equal(is_isogram(word), TRUE)
expect_true(is_isogram(word))
})

test_that("isogram with duplicated non letter character", {
word <- "Hjelmqvist-Gryb-Zock-Pfund-Wax"
expect_equal(is_isogram(word), TRUE)
expect_true(is_isogram(word))
})

test_that("made-up name that is an isogram", {
word <- "Emily Jung Schwartzkopf"
expect_equal(is_isogram(word), TRUE)
expect_true(is_isogram(word))
})
8 changes: 4 additions & 4 deletions exercises/practice/leap/test_leap.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ library(testthat)

test_that("year not divisible by 4: common year", {
year <- 2015
expect_equal(leap(year), FALSE)
expect_false(leap(year))
})

test_that("year divisible by 4, not divisible by 100: leap year", {
year <- 2016
expect_equal(leap(year), TRUE)
expect_true(leap(year))
})

test_that("year divisible by 100, not divisible by 400: common year", {
year <- 2100
expect_equal(leap(year), FALSE)
expect_false(leap(year))
})

test_that("year divisible by 400: leap year", {
year <- 2000
expect_equal(leap(year), TRUE)
expect_true(leap(year))
})
28 changes: 14 additions & 14 deletions exercises/practice/luhn/test_luhn.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,70 +3,70 @@ library(testthat)

test_that("single digit strings can not be valid", {
input <- "1"
expect_equal(is_valid(input), FALSE)
expect_false(is_valid(input))
})

test_that("A single zero is invalid", {
input <- "0"
expect_equal(is_valid(input), FALSE)
expect_false(is_valid(input))
})

test_that("a simple valid SIN that remains valid if reversed", {
input <- "059"
expect_equal(is_valid(input), TRUE)
expect_true(is_valid(input))
})

test_that("a simple valid SIN that becomes invalid if reversed", {
input <- "59"
expect_equal(is_valid(input), TRUE)
expect_true(is_valid(input))
})

test_that("valid Canadian SIN", {
input <- "046 454 286"
expect_equal(is_valid(input), TRUE)
expect_true(is_valid(input))
})

test_that("invalid Canadian SIN", {
input <- "046 454 287"
expect_equal(is_valid(input), FALSE)
expect_false(is_valid(input))
})

test_that("invalid credit card", {
input <- "8273 1232 7352 0569"
expect_equal(is_valid(input), FALSE)
expect_false(is_valid(input))
})

test_that("valid strings with a non-digit added become invalid", {
input <- "055a 444 285"
expect_equal(is_valid(input), FALSE)
expect_false(is_valid(input))
})

test_that("punctuation is not allowed", {
input <- "055-444-285"
expect_equal(is_valid(input), FALSE)
expect_false(is_valid(input))
})

test_that("symbols are not allowed", {
input <- "055£ 444$ 285"
expect_equal(is_valid(input), FALSE)
expect_false(is_valid(input))
})

test_that("single zero with space is invalid", {
input <- " 0"
expect_equal(is_valid(input), FALSE)
expect_false(is_valid(input))
})

test_that("more than a single zero is valid", {
input <- "0000 0"
expect_equal(is_valid(input), TRUE)
expect_true(is_valid(input))
})

test_that("another valid sin", {
input <- "055 444 285"
expect_equal(is_valid(input), TRUE)
expect_true(is_valid(input))
})

test_that("nine doubled is nine", {
input <- "091"
expect_equal(is_valid(input), TRUE)
expect_true(is_valid(input))
})
Loading

0 comments on commit 7a1eed9

Please sign in to comment.