Skip to content

Commit 6c312f2

Browse files
committed
Add unit test for inner class names
Added because it is good to test new features.
1 parent 7e28299 commit 6c312f2

File tree

8 files changed

+131
-0
lines changed

8 files changed

+131
-0
lines changed

jbmc/unit/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ SRC += java_bytecode/ci_lazy_methods/lazy_load_lambdas.cpp \
3838
java_bytecode/java_bytecode_parse_lambdas/java_bytecode_convert_class_lambda_method_handles.cpp \
3939
java_bytecode/java_bytecode_parse_lambdas/java_bytecode_parse_lambda_method_table.cpp \
4040
java_bytecode/java_bytecode_parser/parse_class_without_instructions.cpp \
41+
java_bytecode/java_bytecode_parser/parse_inner_class.cpp \
4142
java_bytecode/java_bytecode_parser/parse_java_annotations.cpp \
4243
java_bytecode/java_bytecode_parser/parse_java_attributes.cpp \
4344
java_bytecode/java_bytecode_parser/parse_java_class.cpp \
Binary file not shown.
Binary file not shown.
Binary file not shown.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class Outer$RedHerring {
2+
}
Binary file not shown.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Outer {
2+
class Inner {
3+
}
4+
5+
Outer anonymous = new Outer() {
6+
};
7+
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*******************************************************************\
2+
3+
Module: Unit tests for converting inner classes
4+
5+
Author: Diffblue Ltd.
6+
7+
\*******************************************************************/
8+
9+
#include <java-testing-utils/load_java_class.h>
10+
#include <java_bytecode/java_types.h>
11+
#include <testing-utils/catch.hpp>
12+
13+
SCENARIO(
14+
"Parsing inner java classes",
15+
"[core][java_bytecode][java_bytecode_convert_class]")
16+
{
17+
GIVEN("A class file of an inner class.")
18+
{
19+
WHEN("Parsing an inner class called \"Inner\".")
20+
{
21+
const symbol_tablet &symbol_table = load_java_class(
22+
"Outer$Inner", "./java_bytecode/java_bytecode_convert_class");
23+
const symbolt *class_symbol = symbol_table.lookup("java::Outer$Inner");
24+
REQUIRE(class_symbol);
25+
const java_class_typet *class_type =
26+
type_try_dynamic_cast<java_class_typet>(class_symbol->type);
27+
REQUIRE(class_type);
28+
29+
THEN("The inner class should have the inner name \"Inner\".")
30+
{
31+
REQUIRE(id2string(class_type->get_inner_name()) == "Inner");
32+
}
33+
THEN("The inner class is not considered to be an anonymous class.")
34+
{
35+
REQUIRE_FALSE(class_type->get_is_anonymous_class());
36+
}
37+
THEN("The inner class has the outer class \"Outer\".")
38+
{
39+
REQUIRE(id2string(class_type->get_outer_class()) == "Outer");
40+
}
41+
}
42+
}
43+
GIVEN("A class file of a class which is not an inner class.")
44+
{
45+
WHEN("Parsing an outer class.")
46+
{
47+
const symbol_tablet &symbol_table =
48+
load_java_class("Outer", "./java_bytecode/java_bytecode_convert_class");
49+
const symbolt *class_symbol = symbol_table.lookup("java::Outer");
50+
REQUIRE(class_symbol);
51+
const java_class_typet *class_type =
52+
type_try_dynamic_cast<java_class_typet>(class_symbol->type);
53+
REQUIRE(class_type);
54+
55+
THEN("The outer class should not have an inner name.")
56+
{
57+
REQUIRE(id2string(class_type->get_inner_name()).empty());
58+
}
59+
THEN("The outer class is not considered to be an anonymous class.")
60+
{
61+
REQUIRE_FALSE(class_type->get_is_anonymous_class());
62+
}
63+
THEN("The outer class does not have an outer class.")
64+
{
65+
REQUIRE(id2string(class_type->get_outer_class()).empty());
66+
}
67+
}
68+
}
69+
GIVEN("A class file of an anonymous class.")
70+
{
71+
WHEN("Parsing an anonymous class.")
72+
{
73+
const symbol_tablet &symbol_table = load_java_class(
74+
"Outer$1", "./java_bytecode/java_bytecode_convert_class");
75+
const symbolt *class_symbol = symbol_table.lookup("java::Outer$1");
76+
REQUIRE(class_symbol);
77+
const java_class_typet *class_type =
78+
type_try_dynamic_cast<java_class_typet>(class_symbol->type);
79+
REQUIRE(class_type);
80+
81+
THEN("The anonymous class should not have an inner name.")
82+
{
83+
REQUIRE(id2string(class_type->get_inner_name()).empty());
84+
}
85+
THEN("The anonymous class is considered to be an anonymous class.")
86+
{
87+
REQUIRE(class_type->get_is_anonymous_class());
88+
}
89+
THEN("The anonymous class does not have an outer class.")
90+
{
91+
REQUIRE(id2string(class_type->get_outer_class()).empty());
92+
}
93+
}
94+
}
95+
GIVEN("A class file of an outer class which is named with a '$'.")
96+
{
97+
WHEN("Parsing an outer class named with a '$'.")
98+
{
99+
const symbol_tablet &symbol_table = load_java_class(
100+
"Outer$RedHerring", "./java_bytecode/java_bytecode_convert_class");
101+
const auto *class_symbol = symbol_table.lookup("java::Outer$RedHerring");
102+
REQUIRE(class_symbol);
103+
const java_class_typet *class_type =
104+
type_try_dynamic_cast<java_class_typet>(class_symbol->type);
105+
REQUIRE(class_type);
106+
107+
THEN("The outer class should not have an inner name.")
108+
{
109+
REQUIRE(id2string(class_type->get_inner_name()).empty());
110+
}
111+
THEN("The inner class is not considered to be an anonymous class.")
112+
{
113+
REQUIRE_FALSE(class_type->get_is_anonymous_class());
114+
}
115+
THEN("The outer class does not have an outer class.")
116+
{
117+
REQUIRE(id2string(class_type->get_outer_class()).empty());
118+
}
119+
}
120+
}
121+
}

0 commit comments

Comments
 (0)