From be1ec7ffbb088423b66f4d0491ad97b354597499 Mon Sep 17 00:00:00 2001
From: Tor Erlend Fjelde <tor.erlend95@gmail.com>
Date: Thu, 6 Jun 2024 10:39:15 +0100
Subject: [PATCH] make failrues of `two_sample_ad_test` produce more
 informative logs

---
 test/experimental/gibbs.jl         |  2 +-
 test/test_utils/numerical_tests.jl | 19 ++++++++++++++-----
 2 files changed, 15 insertions(+), 6 deletions(-)

diff --git a/test/experimental/gibbs.jl b/test/experimental/gibbs.jl
index 4e9467223..ec213a81b 100644
--- a/test/experimental/gibbs.jl
+++ b/test/experimental/gibbs.jl
@@ -132,7 +132,7 @@ has_dot_assume(::Model) = true
                 xs = Array(chain)
                 xs_true = Array(chain_true)
                 for i = 1:size(xs, 2)
-                    @test two_sample_ad_test(xs[:, i], xs_true[:, i])
+                    @test two_sample_ad_test(xs[:, i], xs_true[:, i]; warn_on_fail=true)
                     # Let's make sure that the significance level is not too low by
                     # checking that the KS test fails for some simple transformations.
                     # TODO: Replace the heuristic below with closed-form implementations
diff --git a/test/test_utils/numerical_tests.jl b/test/test_utils/numerical_tests.jl
index 6a266fe34..8711c4c6e 100644
--- a/test/test_utils/numerical_tests.jl
+++ b/test/test_utils/numerical_tests.jl
@@ -83,18 +83,27 @@ function check_MoGtest_default_z_vector(chain; atol=0.2, rtol=0.0)
 end
 
 """
-    two_sample_ad_test(xs_left, xs_right; α=1e-3)
+    two_sample_ad_test(xs_left, xs_right; α=1e-3, warn_on_fail=false)
 
 Perform a two-sample Anderson-Darling (AD) test on the two samples `xs_left` and `xs_right`.
+
+# Arguments
+- `xs_left::AbstractVector`: samples from the first distribution.
+- `xs_right::AbstractVector`: samples from the second distribution.
+
+# Keyword arguments
+- `α::Real`: significance level for the test. Default: `1e-3`.
+- `warn_on_fail::Bool`: whether to warn if the test fails. Default: `false`.
+    Makes failures a bit more informative.
 """
-function two_sample_ad_test(xs_left, xs_right; α=1e-3)
+function two_sample_ad_test(xs_left, xs_right; α=1e-3, warn_on_fail=false)
     t = HypothesisTests.KSampleADTest(xs_left, xs_right)
     # Just a way to make the logs a bit more informative in case of failure.
     if HypothesisTests.pvalue(t) > α
-        @test true
+        true
     else
-        @warn "Two-sample AD test failed with p-value $(HypothesisTests.pvalue(t))"
-        @test false
+        warn_on_fail && @warn "Two-sample AD test failed with p-value $(HypothesisTests.pvalue(t))"
+        false
     end
 end