Skip to content

Commit c7e449b

Browse files
Merge pull request #5085 from thomasspriggs/tas/invariant_matcher
Improve catch support for checking of thrown instances of `invariant_failedt`
2 parents 20164f5 + 91d1626 commit c7e449b

File tree

6 files changed

+89
-49
lines changed

6 files changed

+89
-49
lines changed

jbmc/unit/java_bytecode/java_string_literals.cpp

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Author: Diffblue Limited
1111
#include <java_bytecode/java_types.h>
1212
#include <java_bytecode/java_utils.h>
1313
#include <linking/static_lifetime_init.h>
14+
#include <testing-utils/invariant.h>
1415
#include <testing-utils/use_catch.h>
1516
#include <util/symbol_table.h>
1617

@@ -29,21 +30,10 @@ TEST_CASE(
2930

3031
create_java_initialize(symbol_table);
3132
symbol_table.get_writeable_ref(INITIALIZE_FUNCTION).value = code_blockt{};
32-
try
33-
{
34-
const cbmc_invariants_should_throwt invariants_throw;
35-
get_or_create_string_literal_symbol("bar", symbol_table, false);
36-
FAIL("Expected invariant failure.");
37-
}
38-
catch(const invariant_failedt &exception)
39-
{
40-
REQUIRE_THAT(
41-
exception.what(),
42-
Catch::Matchers::Contains("Cannot create more string literals after "
43-
"making " INITIALIZE_FUNCTION));
44-
}
45-
catch(...)
46-
{
47-
FAIL("Incorrect exception type thrown.");
48-
}
33+
const cbmc_invariants_should_throwt invariants_throw;
34+
REQUIRE_THROWS_MATCHES(
35+
get_or_create_string_literal_symbol("bar", symbol_table, false),
36+
invariant_failedt,
37+
invariant_failure_containing("Cannot create more string literals after "
38+
"making " INITIALIZE_FUNCTION));
4939
}

unit/goto-programs/remove_returns.cpp

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Author: Diffblue Ltd.
66
77
\*******************************************************************/
88

9+
#include <testing-utils/invariant.h>
910
#include <testing-utils/use_catch.h>
1011

1112
#include <goto-programs/goto_functions.h>
@@ -52,21 +53,10 @@ TEST_CASE(
5253
symbol_exprt{"local_variable", signedbv_typet{32}},
5354
symbol_exprt{"bar_function", code_typet{{}, signedbv_typet{32}}},
5455
{}});
55-
try
56-
{
57-
const cbmc_invariants_should_throwt invariants_throw;
58-
remove_returns(symbol_table, goto_functions);
59-
FAIL("Expected invariant failure.");
60-
}
61-
catch(const invariant_failedt &exception)
62-
{
63-
REQUIRE_THAT(
64-
exception.what(),
65-
Catch::Matchers::Contains("called function `bar_function' should have an "
66-
"entry in the function map"));
67-
}
68-
catch(...)
69-
{
70-
FAIL("Incorrect exception type thrown.");
71-
}
56+
const cbmc_invariants_should_throwt invariants_throw;
57+
REQUIRE_THROWS_MATCHES(
58+
remove_returns(symbol_table, goto_functions),
59+
invariant_failedt,
60+
invariant_failure_containing("called function `bar_function' should have "
61+
"an entry in the function map"));
7262
}

unit/testing-utils/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
SRC = \
22
call_graph_test_utils.cpp \
33
free_form_cmdline.cpp \
4+
invariant.cpp \
45
message.cpp \
56
require_expr.cpp \
67
require_symbol.cpp \

unit/testing-utils/invariant.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/// Author: Diffblue Ltd.
2+
3+
#include "invariant.h"
4+
5+
#include <utility>
6+
7+
invariant_failure_containingt invariant_failure_containing(std::string expected)
8+
{
9+
return invariant_failure_containingt{std::move(expected)};
10+
}
11+
12+
invariant_failure_containingt::invariant_failure_containingt(
13+
std::string expected)
14+
: expected{std::move(expected)}
15+
{
16+
}
17+
18+
bool invariant_failure_containingt::match(
19+
const invariant_failedt &exception) const
20+
{
21+
const std::string what = exception.what();
22+
return what.find(expected) != std::string::npos;
23+
}
24+
25+
std::string invariant_failure_containingt::describe() const
26+
{
27+
return std::string{"invariant_failedt with `.what' containing - \""} +
28+
expected + "\"";
29+
}
30+
31+
std::ostream &
32+
operator<<(std::ostream &out, const invariant_failedt &invariant_failed)
33+
{
34+
out << "invariant_failedt where `.what()' is \"" << invariant_failed.what()
35+
<< "\"";
36+
return out;
37+
}

unit/testing-utils/invariant.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/// Author: Diffblue Ltd.
2+
3+
#ifndef CPROVER_TESTING_UTILS_INVARIANT_H
4+
#define CPROVER_TESTING_UTILS_INVARIANT_H
5+
6+
#include <testing-utils/use_catch.h>
7+
#include <util/invariant.h>
8+
9+
#include <string>
10+
11+
class invariant_failure_containingt
12+
: public Catch::MatcherBase<invariant_failedt>
13+
{
14+
public:
15+
explicit invariant_failure_containingt(std::string expected);
16+
bool match(const invariant_failedt &exception) const override;
17+
std::string describe() const override;
18+
19+
private:
20+
std::string expected;
21+
};
22+
23+
/// Returns a matcher which matches an invariant_failedt exception, where the
24+
/// `.what()` returns a string containing \p expected.
25+
invariant_failure_containingt
26+
invariant_failure_containing(std::string expected);
27+
28+
/// Printing of `invariant_failedt` for test failure messages.
29+
std::ostream &
30+
operator<<(std::ostream &out, const invariant_failedt &invariant_failed);
31+
32+
#endif // CPROVER_TESTING_UTILS_INVARIANT_H

unit/util/symbol_table.cpp

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
/// \file Tests for symbol_tablet
44

5+
#include <testing-utils/invariant.h>
56
#include <testing-utils/use_catch.h>
67
#include <util/exception_utils.h>
78
#include <util/journalling_symbol_table.h>
@@ -359,19 +360,8 @@ TEST_CASE("symbol_tablet::lookup_ref invariant", "[core][utils][symbol_tablet]")
359360
symbol_table.insert(foo_symbol);
360361
const cbmc_invariants_should_throwt invariants_throw;
361362
REQUIRE_NOTHROW(symbol_table.lookup_ref("foo"));
362-
try
363-
{
364-
symbol_table.lookup_ref("bar");
365-
FAIL("Expected invariant failure.");
366-
}
367-
catch(const invariant_failedt &exception)
368-
{
369-
REQUIRE_THAT(
370-
exception.what(),
371-
Catch::Matchers::Contains("`bar' must exist in the symbol table."));
372-
}
373-
catch(...)
374-
{
375-
FAIL("Incorrect exception type thrown.");
376-
}
363+
REQUIRE_THROWS_MATCHES(
364+
symbol_table.lookup_ref("bar"),
365+
invariant_failedt,
366+
invariant_failure_containing("`bar' must exist in the symbol table."));
377367
}

0 commit comments

Comments
 (0)