Skip to content

Commit 1d9a346

Browse files
committed
Add unit tests for preserving meta-information of excluded methods
These unit tests complement the regression tests in jbmc/regression/jbmc/context-include-exclude that were added in a previous commit to check for properties of excluded methods. Some properties, like information about accessibility and final and static status, is easier to check in unit-style tests.
1 parent c54f9d3 commit 1d9a346

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-0
lines changed

jbmc/unit/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ SRC += java_bytecode/ci_lazy_methods/lazy_load_lambdas.cpp \
2222
java_bytecode/java_bytecode_convert_method/convert_method.cpp \
2323
java_bytecode/java_bytecode_instrument/virtual_call_null_checks.cpp \
2424
java_bytecode/java_bytecode_language/language.cpp \
25+
java_bytecode/java_bytecode_language/context_excluded.cpp \
2526
java_bytecode/java_bytecode_parse_generics/parse_bounded_generic_inner_classes.cpp \
2627
java_bytecode/java_bytecode_parse_generics/parse_derived_generic_class.cpp \
2728
java_bytecode/java_bytecode_parse_generics/parse_functions_with_generics.cpp \
Binary file not shown.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
public class ClassWithDifferentModifiers {
2+
3+
public static void main(String[] args) {
4+
ClassWithDifferentModifiers guineaPig = new ClassWithDifferentModifiers();
5+
assert guineaPig.testPublicInstance() == 1;
6+
assert guineaPig.testPrivateStatic() == 2;
7+
assert guineaPig.testProtectedFinalInstance() == 3;
8+
assert guineaPig.testDefaultFinalStatic() == 4;
9+
}
10+
11+
public int testPublicInstance() { return 1; }
12+
13+
private static int testPrivateStatic() { return 2; }
14+
15+
protected final int testProtectedFinalInstance() { return 3; }
16+
17+
final static int testDefaultFinalStatic() { return 4; }
18+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*******************************************************************\
2+
3+
Module: Unit tests for java_bytecode_language.
4+
5+
Author: Diffblue Limited.
6+
7+
\*******************************************************************/
8+
9+
#include <testing-utils/use_catch.h>
10+
11+
#include <util/symbol_table.h>
12+
13+
#include <java-testing-utils/load_java_class.h>
14+
#include <java-testing-utils/require_type.h>
15+
16+
SCENARIO(
17+
"Exclude a method using context-include/exclude",
18+
"[core][java_bytecode][java_bytecode_language]")
19+
{
20+
GIVEN("A class with all methods excluded except for main")
21+
{
22+
const symbol_tablet symbol_table =
23+
load_goto_model_from_java_class(
24+
"ClassWithDifferentModifiers",
25+
"./java_bytecode/java_bytecode_language",
26+
{},
27+
{{"context-include", "ClassWithDifferentModifiers.main"}})
28+
.get_symbol_table();
29+
30+
WHEN("Converting the methods of this class")
31+
{
32+
THEN(
33+
"Meta-information of excluded methods is preserved. Symbol values are "
34+
"nil as the lazy_goto_modelt for unit tests does not generate "
35+
"stub/exclude bodies.")
36+
{
37+
const auto &public_instance_symbol = symbol_table.lookup_ref(
38+
"java::ClassWithDifferentModifiers.testPublicInstance:()I");
39+
REQUIRE(public_instance_symbol.value.is_nil());
40+
const auto public_instance_type =
41+
type_try_dynamic_cast<java_method_typet>(public_instance_symbol.type);
42+
REQUIRE(public_instance_type);
43+
REQUIRE(id2string(public_instance_type->get(ID_access)) == "public");
44+
REQUIRE_FALSE(public_instance_type->get_bool(ID_is_static));
45+
REQUIRE_FALSE(public_instance_type->get_is_final());
46+
47+
const auto &private_static_symbol = symbol_table.lookup_ref(
48+
"java::ClassWithDifferentModifiers.testPrivateStatic:()I");
49+
REQUIRE(private_static_symbol.value.is_nil());
50+
const auto private_static_type =
51+
type_try_dynamic_cast<java_method_typet>(private_static_symbol.type);
52+
REQUIRE(private_static_type);
53+
REQUIRE(id2string(private_static_type->get(ID_access)) == "private");
54+
REQUIRE(private_static_type->get_bool(ID_is_static));
55+
REQUIRE_FALSE(private_static_type->get_is_final());
56+
57+
const auto &protected_final_instance_symbol = symbol_table.lookup_ref(
58+
"java::ClassWithDifferentModifiers.testProtectedFinalInstance:()I");
59+
REQUIRE(protected_final_instance_symbol.value.is_nil());
60+
const auto protected_final_instance_type =
61+
type_try_dynamic_cast<java_method_typet>(
62+
protected_final_instance_symbol.type);
63+
REQUIRE(protected_final_instance_type);
64+
REQUIRE(
65+
id2string(protected_final_instance_type->get(ID_access)) ==
66+
"protected");
67+
REQUIRE_FALSE(protected_final_instance_type->get_bool(ID_is_static));
68+
REQUIRE(protected_final_instance_type->get_is_final());
69+
70+
const auto &default_final_static_symbol = symbol_table.lookup_ref(
71+
"java::ClassWithDifferentModifiers.testDefaultFinalStatic:()I");
72+
REQUIRE(default_final_static_symbol.value.is_nil());
73+
const auto default_final_static_type =
74+
type_try_dynamic_cast<java_method_typet>(
75+
default_final_static_symbol.type);
76+
REQUIRE(default_final_static_type);
77+
REQUIRE(
78+
id2string(default_final_static_type->get(ID_access)) == "default");
79+
REQUIRE(default_final_static_type->get_bool(ID_is_static));
80+
REQUIRE(default_final_static_type->get_is_final());
81+
}
82+
}
83+
}
84+
}

0 commit comments

Comments
 (0)