Skip to content

Commit

Permalink
Add tests for get_scenario and get_scenario_source
Browse files Browse the repository at this point in the history
Progress towards #31
  • Loading branch information
AlexAxthelm committed Dec 16, 2024
1 parent 6f33b17 commit c821c41
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 0 deletions.
12 changes: 12 additions & 0 deletions tests/testthat.R
Original file line number Diff line number Diff line change
@@ -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")
73 changes: 73 additions & 0 deletions tests/testthat/test-get_scenario.R
Original file line number Diff line number Diff line change
@@ -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"
# )
# })
#
72 changes: 72 additions & 0 deletions tests/testthat/test-get_scenario_source.R
Original file line number Diff line number Diff line change
@@ -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"
# )
# })

0 comments on commit c821c41

Please sign in to comment.