|
| 1 | +test_that("two_sample_test works with persistence sets", { |
| 2 | + testthat::skip_if_not_installed("phutil") |
| 3 | + testthat::skip_if_not_installed("flipr") |
| 4 | + |
| 5 | + # Mock the parse_inputs function to avoid actual computation |
| 6 | + mockD <- matrix(1:16, 4, 4) |
| 7 | + class(mockD) <- "dist" |
| 8 | + mock_result <- list(D = mockD, sample_sizes = c(2, 2)) |
| 9 | + |
| 10 | + testthat::local_mocked_bindings( |
| 11 | + parse_inputs = function(...) mock_result, |
| 12 | + null_spec = function(y, parameters) y |
| 13 | + ) |
| 14 | + |
| 15 | + # Create a mock PlausibilityFunction class |
| 16 | + MockPF <- R6::R6Class( |
| 17 | + "PlausibilityFunction", |
| 18 | + public = list( |
| 19 | + alternative = NULL, |
| 20 | + nperms = NULL, |
| 21 | + aggregator = NULL, |
| 22 | + initialize = function(...) { |
| 23 | + }, |
| 24 | + get_value = function(...) 0.042 |
| 25 | + ) |
| 26 | + ) |
| 27 | + |
| 28 | + # Mock the flipr package |
| 29 | + testthat::local_mocked_bindings( |
| 30 | + PlausibilityFunction = MockPF, |
| 31 | + stat_t_ip = function() { |
| 32 | + }, |
| 33 | + stat_f_ip = function() { |
| 34 | + }, |
| 35 | + .package = "flipr" |
| 36 | + ) |
| 37 | + |
| 38 | + # Test with default parameters |
| 39 | + result <- two_sample_test( |
| 40 | + x = list(), |
| 41 | + y = list(), |
| 42 | + B = 100L |
| 43 | + ) |
| 44 | + expect_equal(result, 0.042) |
| 45 | +}) |
| 46 | + |
| 47 | +test_that("two_sample_test handles verbose parameter correctly", { |
| 48 | + testthat::skip_if_not_installed("phutil") |
| 49 | + testthat::skip_if_not_installed("flipr") |
| 50 | + |
| 51 | + # Mock the parse_inputs function to avoid actual computation |
| 52 | + mockD <- matrix(1:16, 4, 4) |
| 53 | + class(mockD) <- "dist" |
| 54 | + mock_result <- list(D = mockD, sample_sizes = c(2, 2)) |
| 55 | + |
| 56 | + testthat::local_mocked_bindings( |
| 57 | + parse_inputs = function(...) mock_result, |
| 58 | + null_spec = function(y, parameters) y |
| 59 | + ) |
| 60 | + |
| 61 | + # Create a mock PlausibilityFunction class |
| 62 | + MockPF <- R6::R6Class( |
| 63 | + "PlausibilityFunction", |
| 64 | + public = list( |
| 65 | + alternative = NULL, |
| 66 | + nperms = NULL, |
| 67 | + aggregator = NULL, |
| 68 | + initialize = function(...) { |
| 69 | + }, |
| 70 | + get_value = function(...) 0.042 |
| 71 | + ) |
| 72 | + ) |
| 73 | + |
| 74 | + # Mock the flipr package |
| 75 | + testthat::local_mocked_bindings( |
| 76 | + PlausibilityFunction = MockPF, |
| 77 | + stat_t_ip = function() { |
| 78 | + }, |
| 79 | + stat_f_ip = function() { |
| 80 | + }, |
| 81 | + .package = "flipr" |
| 82 | + ) |
| 83 | + |
| 84 | + # Test with verbose = TRUE |
| 85 | + expect_snapshot( |
| 86 | + two_sample_test( |
| 87 | + x = list(), |
| 88 | + y = list(), |
| 89 | + B = 100L, |
| 90 | + verbose = TRUE |
| 91 | + ) |
| 92 | + ) |
| 93 | +}) |
| 94 | + |
| 95 | +test_that("two_sample_test sets correct parameters for plausibility function", { |
| 96 | + testthat::skip_if_not_installed("phutil") |
| 97 | + testthat::skip_if_not_installed("flipr") |
| 98 | + |
| 99 | + # Mock the parse_inputs function |
| 100 | + mockD <- matrix(1:16, 4, 4) |
| 101 | + class(mockD) <- "dist" |
| 102 | + mock_result <- list(D = mockD, sample_sizes = c(2, 2)) |
| 103 | + |
| 104 | + testthat::local_mocked_bindings( |
| 105 | + parse_inputs = function(...) mock_result, |
| 106 | + null_spec = function(y, parameters) y |
| 107 | + ) |
| 108 | + |
| 109 | + # Create a testable mock PlausibilityFunction class that records parameters |
| 110 | + params_set <- list() |
| 111 | + MockPF <- R6::R6Class( |
| 112 | + "PlausibilityFunction", |
| 113 | + public = list( |
| 114 | + alternative = NULL, |
| 115 | + nperms = NULL, |
| 116 | + aggregator = NULL, |
| 117 | + initialize = function(...) { |
| 118 | + params_set <<- list(...) |
| 119 | + }, |
| 120 | + get_value = function(...) 0.042 |
| 121 | + ) |
| 122 | + ) |
| 123 | + |
| 124 | + # Mock the flipr package |
| 125 | + testthat::local_mocked_bindings( |
| 126 | + PlausibilityFunction = MockPF, |
| 127 | + stat_t_ip = function() { |
| 128 | + }, |
| 129 | + stat_f_ip = function() { |
| 130 | + }, |
| 131 | + .package = "flipr" |
| 132 | + ) |
| 133 | + |
| 134 | + # Test with custom B and npc parameters |
| 135 | + custom_B <- 500L |
| 136 | + custom_npc <- "fisher" |
| 137 | + |
| 138 | + result <- two_sample_test( |
| 139 | + x = list(), |
| 140 | + y = list(), |
| 141 | + B = custom_B, |
| 142 | + npc = custom_npc |
| 143 | + ) |
| 144 | + |
| 145 | + # Verify correct parameters were set |
| 146 | + expect_equal(params_set[[4]], mockD) # D parameter |
| 147 | + expect_equal(params_set[[5]], 2) # sample_sizes[1] |
| 148 | + |
| 149 | + # Check the PlausibilityFunction properties were set correctly |
| 150 | + expect_equal(MockPF$new()$alternative, "right_tail") |
| 151 | + expect_equal(MockPF$new()$nperms, custom_B) |
| 152 | + expect_equal(MockPF$new()$aggregator, custom_npc) |
| 153 | +}) |
0 commit comments