diff --git a/tests/testthat.R b/tests/testthat.R new file mode 100644 index 0000000..b79375a --- /dev/null +++ b/tests/testthat.R @@ -0,0 +1,12 @@ +# This file is part of the standard setup for testthat. +# It is recommended that you do not modify it. +# +# Where should you do additional test configuration? +# Learn more about the roles of various files in: +# * https://r-pkgs.org/testing-design.html#sec-tests-files-overview +# * https://testthat.r-lib.org/articles/special-files.html + +library(testthat) +library(pacta.json.export) + +test_check("pacta.json.export") diff --git a/tests/testthat/test-get_scenario.R b/tests/testthat/test-get_scenario.R new file mode 100644 index 0000000..ddd5381 --- /dev/null +++ b/tests/testthat/test-get_scenario.R @@ -0,0 +1,73 @@ +test_that("get_scenario splits on underscore", { + scenario_string <- "SOURCE_SCENARIO" + results <- get_scenario(scenario_string) + expect_identical( + object = results, + expected = "SCENARIO" + ) +}) + +test_that("get_scenario respects case of original string", { + scenario_string <- "source_scenario" + results <- get_scenario(scenario_string) + expect_identical( + object = results, + expected = "scenario" + ) +}) + +test_that("get_scenario respects mixed case of original string", { + scenario_string <- "sOuRcE_ScEnArIo" + results <- get_scenario(scenario_string) + expect_identical( + object = results, + expected = "ScEnArIo" + ) +}) + +test_that("get_scenario extracts scneario name from string with 1 underscore", { + scenario_string <- "GECO2022_1.5C" + results <- get_scenario(scenario_string) + expect_identical( + object = results, + expected = "1.5C" + ) +}) + +test_that("get_scenario extracts scneario name from string with 2 underscores", { + scenario_string <- "WEO2022_NZE_2050" + results <- get_scenario(scenario_string) + expect_identical( + object = results, + expected = "NZE_2050" + ) +}) + +# test_that("get_scenario throws error if no scenario_name (nothing after underscore)", { +# scenario_string <- "SOURCE_" +# expect_error( +# get_scenario(scenario_string), +# regexp = "No scenario found in string" +# ) +# }) +# +# test_that("get_scenario throws error if no source (nothing before underscore)", { +# scenario_string <- "_SCENARIO" +# expect_error( +# get_scenario(scenario_string), +# regexp = "No source found in string" +# ) +# }) +# +# test_that("get_scenario throws warning if multiple consecutive underscores", { +# scenario_string <- "SOURCE__SCENARIO" +# expect_warning( +# {results <- get_scenario(scenario_string)}, +# regexp = "Multiple consecutive underscores in scenario string." +# ) +# expect_identical( +# object = results, +# expected = "_SCENARIO" +# ) +# }) +# diff --git a/tests/testthat/test-get_scenario_source.R b/tests/testthat/test-get_scenario_source.R new file mode 100644 index 0000000..904ef63 --- /dev/null +++ b/tests/testthat/test-get_scenario_source.R @@ -0,0 +1,72 @@ +test_that("get_scenario_source splits on underscore", { + scenario_string <- "SOURCE_SCENARIO" + results <- get_scenario_source(scenario_string) + expect_identical( + object = results, + expected = "SOURCE" + ) +}) + +test_that("get_scenario_source respects case of original string", { + scenario_string <- "source_scenario" + results <- get_scenario_source(scenario_string) + expect_identical( + object = results, + expected = "source" + ) +}) + +test_that("get_scenario_source respects mixed case of original string", { + scenario_string <- "sOuRcE_ScEnArIo" + results <- get_scenario_source(scenario_string) + expect_identical( + object = results, + expected = "sOuRcE" + ) +}) + +test_that("get_scenario_source extracts source name from string with 1 underscore", { + scenario_string <- "GECO2022_1.5C" + results <- get_scenario_source(scenario_string) + expect_identical( + object = results, + expected = "GECO2022" + ) +}) + +test_that("get_scenario_source extracts source name from string with 2 underscores", { + scenario_string <- "WEO2022_NZE_2050" + results <- get_scenario_source(scenario_string) + expect_identical( + object = results, + expected = "WEO2022" + ) +}) + +# test_that("get_scenario_source throws error if no source (nothing before underscore)", { +# scenario_string <- "_SCENARIO" +# expect_error( +# get_scenario_source(scenario_string), +# regexp = "No source found in string" +# ) +# }) +# +# test_that("get_scenario_source throws error if no scenario_name (nothing after underscore)", { +# scenario_string <- "SOURCE_" +# expect_error( +# get_scenario_source(scenario_string), +# regexp = "No scenario found in string" +# ) +# }) +# +# test_that("get_scenario_source throws warning if multiple consecutive underscores", { +# scenario_string <- "SOURCE__SCENARIO" +# expect_warning( +# {results <- get_scenario_source(scenario_string)}, +# regexp = "Multiple consecutive underscores in scenario string." +# ) +# expect_identical( +# object = results, +# expected = "SOURCE" +# ) +# })