Skip to content

Commit 7bf92cb

Browse files
committed
Replace try catch blocks with REQUIRE_THROWS_MATCHES
`REQUIRE_THROWS_MATCHES` is the functionality built into the catch framework for this purpose. Using it allows more concise specification of the requirement.
1 parent fafafbd commit 7bf92cb

File tree

3 files changed

+19
-49
lines changed

3 files changed

+19
-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/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)