From ad83b5ee6cd9221fa29b1e4b670749d19e8e7a4b Mon Sep 17 00:00:00 2001 From: Joachim Zuckarelli <38797219+jsugarelli@users.noreply.github.com> Date: Fri, 20 Jun 2025 17:50:42 +0200 Subject: [PATCH] Add testthat tests and dependency --- DESCRIPTION | 2 ++ tests/testthat.R | 1 + tests/testthat/test-switchcase.R | 25 +++++++++++++++++++++++++ 3 files changed, 28 insertions(+) create mode 100644 tests/testthat.R create mode 100644 tests/testthat/test-switchcase.R diff --git a/DESCRIPTION b/DESCRIPTION index 04ed8ae..5c17992 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -3,6 +3,7 @@ Type: Package Title: A Simple and Flexible Switch-Case Construct for the 'R' Language Version: 0.1.1 Authors@R: person("Joachim", "Zuckarelli", role = c("aut", "cre"), email = "joachim@zuckarelli.de", comment = c(ORCID = "0000-0002-9280-3016")) +Author: Joachim Zuckarelli [aut, cre] Maintainer: Joachim Zuckarelli Description: Provides a switch-case construct for 'R', as it is known from other programming languages. It allows to test multiple, similar conditions in an efficient, easy-to-read manner, so nested if-else constructs can be avoided. The switch-case construct is designed as an 'R' function that allows to return values depending on which condition is met and lets the programmer flexibly decide whether or not to leave the switch-case construct after a case block has been executed. License: GPL-3 @@ -11,3 +12,4 @@ URL: https://github.com/jsugarelli/switchcase/, https://youtu.be/3ybF8u_PE7w Encoding: UTF-8 LazyData: true RoxygenNote: 7.1.0 +Suggests: testthat diff --git a/tests/testthat.R b/tests/testthat.R new file mode 100644 index 0000000..9b31049 --- /dev/null +++ b/tests/testthat.R @@ -0,0 +1 @@ +testthat::test_check("switchcase") diff --git a/tests/testthat/test-switchcase.R b/tests/testthat/test-switchcase.R new file mode 100644 index 0000000..81e4a30 --- /dev/null +++ b/tests/testthat/test-switchcase.R @@ -0,0 +1,25 @@ +library(testthat) +library(switchcase) + +test_that("numeric comparison works", { + res <- switchCase(5, + alt(..expr < 5, NULL, "less"), + alt(..expr == 5, NULL, "equal"), + alt(..expr > 5, NULL, "greater")) + expect_equal(res, "equal") +}) + +test_that("character comparison works", { + res <- switchCase("b", + alt(..expr == "a", NULL, "A"), + alt(..expr == "b", NULL, "B")) + expect_equal(res, "B") +}) + +test_that("default case works", { + res <- switchCase(3, + alt(..expr == 1, NULL, "one"), + alt(..expr == 2, NULL, "two"), + alt(NULL, NULL, "default")) + expect_equal(res, "default") +})