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

add R version of TDD exercise #173

Merged
merged 2 commits into from
Apr 27, 2021
Merged
Changes from 1 commit
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
41 changes: 40 additions & 1 deletion content/code/tdd_sol.R
Original file line number Diff line number Diff line change
@@ -1 +1,40 @@
WRITEME
# define the function
fizz_buzz <- function(number){
if(!number%%1==0 | number < 0) {
stop("non-integer or negative input not allowed!")
}
wikfeldt marked this conversation as resolved.
Show resolved Hide resolved
if(number%%3 == 0 & number%%5 == 0) {
return('FizzBuzz')
}
else if(number%%3 == 0) {
return('Fizz')
}
else if (number%%5 == 0){
return('Buzz')
}
else {
return(number)
}

}

# apply it to the numbers 1 to 50
for (number in 1:50) {
print(fizz_buzz(number))
}


library(testthat)

test_that("Test FizzBuzz", {
expect_equal(fizz_buzz(1), 1)
expect_equal(fizz_buzz(2), 2)
expect_equal(fizz_buzz(3), 'Fizz')
expect_equal(fizz_buzz(4), 4)
expect_equal(fizz_buzz(5), 'Buzz')
expect_equal(fizz_buzz(15), 'FizzBuzz')

expect_error(fizz_buzz(-1))
expect_error(fizz_buzz(1.5))
expect_error(fizz_buzz('rabbit'))
})