From 3ecfeb500fc47fea77e2ac5cf4dec02c6b37a722 Mon Sep 17 00:00:00 2001 From: Jackson Hoffart Date: Wed, 17 Jan 2024 12:09:45 -0600 Subject: [PATCH] add failing test --- tests/testthat/test-plot_emission_intensity.R | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/tests/testthat/test-plot_emission_intensity.R b/tests/testthat/test-plot_emission_intensity.R index ec0d069e..d5e97d19 100644 --- a/tests/testthat/test-plot_emission_intensity.R +++ b/tests/testthat/test-plot_emission_intensity.R @@ -87,3 +87,72 @@ test_that("throws expected warning about API change", { class = "warning" ) }) + +test_that("works well with `scale_colour_r2dii`", { + #TODO: Check if this belongs here or in `test-scale_colour_r2dii.R` + data <- sda %>% + filter(region == "global") + + input_levels <- c( + "projected", + "corporate_economy", + "target_demo", + "adjusted_scenario_demo" + ) + + input_color_scale <- + c( + "dark_blue", + "green", + "grey", + "ruby_red" + ) + + input_color_scale_hex <- data.frame(label = input_color_scale) %>% + left_join(palette_colours, by = "label") %>% + pull(hex) + + expected_output <- data.frame( + levels = input_levels, + hex = input_color_scale_hex + ) + + p <- data %>% + dplyr::mutate( + emission_factor_metric = factor( + .data$emission_factor_metric, + levels = input_levels + ) + ) %>% + plot_emission_intensity() + + scale_colour_r2dii( + labels = input_color_scale + ) + + # print the levels that colours are applied to + ordered_output_levels <- levels(match_lines_order(p$data)) + + # print the actual colour scales of the plot + ordered_output_colour_scale <- p$scales$get_scales("colour")$palette( + length(ordered_output_levels) + ) + + plot_output <- data.frame( + levels = ordered_output_levels, + hex = ordered_output_colour_scale + ) + + out <- left_join( + plot_output, + expected_output, + by = "levels", + suffix = c("_out", "_expected") + ) %>% + split(.$levels) + + expect_equal(out$projected$hex_out, out$projected$hex_expected) + expect_equal(out$corporate_economy$hex_out, out$corporate_economy$hex_expected) + expect_equal(out$target_demo$hex_out, out$target_demo$hex_expected) + expect_equal(out$adjusted_scenario_demo$hex_out, out$adjusted_scenario_demo$hex_expected) + +})