From fce5cbee0a7353eaab52a079bdedcd639134367c Mon Sep 17 00:00:00 2001 From: Vincenzo Eduardo Padulano Date: Thu, 30 Jul 2026 15:26:50 +0200 Subject: [PATCH] [test] Add tests for error handling of interpreter APIs This commit introduces tests to document the error handling behaviour of various interpreter APIs. Fixes https://github.com/root-project/root/issues/22235 --- core/metacling/test/TClingTests.cxx | 44 ++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/core/metacling/test/TClingTests.cxx b/core/metacling/test/TClingTests.cxx index 2a54d12864dd3..0ff3a7d226cd0 100644 --- a/core/metacling/test/TClingTests.cxx +++ b/core/metacling/test/TClingTests.cxx @@ -467,4 +467,46 @@ using func0_ret_t = typename ROOT::TypeTraits::CallableTraits:: auto res = gInterpreter->Declare(expression.c_str()); EXPECT_TRUE(res); } -#endif \ No newline at end of file +#endif + +// https://github.com/root-project/root/issues/22235 +TEST_F(TClingTests, ErrorHandling) +{ + // Tests of error handling of various TInterpreter APIs. + const char *wrongCode{"rndm_stuff"}; + + auto processLineWithError = [](const char *code) { + // ProcessLine + ROOT::TestSupport::CheckDiagsRAII diags; + diags.requiredDiag(kError, "cling", "use of undeclared identifier 'rndm_stuff'", + /*matchFullMessage=*/false); + diags.requiredDiag(kError, "HandleInterpreterException", "Error evaluating expression (rndm_stuff)", + /*matchFullMessage=*/false); + TInterpreter::EErrorCode error = TInterpreter::kNoError; + gInterpreter->ProcessLine(code, &error); + EXPECT_NE(error, TInterpreter::kNoError); + }; + EXPECT_NO_THROW(processLineWithError(wrongCode)); + + auto calcWithError = [](const char *code) { + // Calc + ROOT::TestSupport::CheckDiagsRAII diags; + diags.requiredDiag(kError, "cling", "use of undeclared identifier 'rndm_stuff'", + /*matchFullMessage=*/false); + diags.requiredDiag(kError, "Calc", "Error evaluating expression (rndm_stuff)", + /*matchFullMessage=*/false); + TInterpreter::EErrorCode error = TInterpreter::kNoError; + gInterpreter->Calc(code, &error); + EXPECT_NE(error, TInterpreter::kNoError); + }; + EXPECT_NO_THROW(calcWithError(wrongCode)); + + // Evaluate + try { + std::unique_ptr v = gInterpreter->MakeInterpreterValue(); + gInterpreter->Evaluate(wrongCode, *v); + FAIL() << "expected exception thrown by Evaluate"; + } catch (const std::runtime_error &e) { + EXPECT_THAT(e.what(), testing::HasSubstr("Error evaluating expression (rndm_stuff)")); + } +}