From 8bd5141130bf06bff6189c52229d58e21aee8feb Mon Sep 17 00:00:00 2001 From: Steven Hebert Date: Mon, 4 May 2020 13:33:20 -0400 Subject: [PATCH] Create unit testing for sum_n_first_fibonacci_numbers. --- R/sum_first_fibonacci_numbers.R | 3 +++ tests/testthat/test-sum_n_first_fibonacci_numbers.R | 7 +++++++ 2 files changed, 10 insertions(+) create mode 100644 tests/testthat/test-sum_n_first_fibonacci_numbers.R diff --git a/R/sum_first_fibonacci_numbers.R b/R/sum_first_fibonacci_numbers.R index 8026cc3..c670d28 100644 --- a/R/sum_first_fibonacci_numbers.R +++ b/R/sum_first_fibonacci_numbers.R @@ -9,6 +9,9 @@ #' sum_n_first_fibonacci_numbers(5) #' sum_n_first_fibonacci_numbers(8) sum_n_first_fibonacci_numbers <- function(n){ + if(n < 0){ + stop("Please use a positive number") + } if(length(fibonacci) < n){ print(paste0("Fibonacci vector's length is ", length(fibonacci), ". Summing the ", length(fibonacci), " first numbers instead.")) return(sum(fibonacci)) diff --git a/tests/testthat/test-sum_n_first_fibonacci_numbers.R b/tests/testthat/test-sum_n_first_fibonacci_numbers.R new file mode 100644 index 0000000..45a2154 --- /dev/null +++ b/tests/testthat/test-sum_n_first_fibonacci_numbers.R @@ -0,0 +1,7 @@ +test_that("sum_n_first_fibonacci_numbers works", { + expect_equal(sum_n_first_fibonacci_numbers(4), 7) +}) + + test_that("sum_n_first_fibonacci_numbers handles edge cases", { + expect_error(sum_n_first_fibonacci_numbers(-3), "Please use a positive number") + })