Skip to content

Commit cd98429

Browse files
author
thk123
committed
Adding tests for the overall function
1 parent b5050e4 commit cd98429

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

jbmc/unit/java_bytecode/load_method_by_regex.cpp

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <java_bytecode/load_method_by_regex.h>
1010
#include <testing-utils/catch.hpp>
11+
#include <testing-utils/require_vectors_equal_unordered.h>
1112

1213
class load_method_by_regex_testt
1314
{
@@ -101,3 +102,92 @@ SCENARIO(
101102
}
102103
}
103104
}
105+
106+
static symbolt create_method_symbol(const std::string &method_name)
107+
{
108+
symbolt new_symbol;
109+
new_symbol.name = method_name;
110+
new_symbol.type = code_typet{{}, nil_typet{}};
111+
return new_symbol;
112+
}
113+
114+
static void require_result_for_pattern(
115+
const std::string &pattern,
116+
const std::vector<irep_idt> &expected,
117+
const symbol_tablet &symbol_table)
118+
{
119+
WHEN("Constructing a load_method_by_regex")
120+
{
121+
load_method_by_regext matcher{pattern};
122+
const auto &results = matcher.extra_methods(symbol_table);
123+
if(expected.size() == 1)
124+
{
125+
THEN("Expect " + id2string(expected[0]))
126+
{
127+
require_vectors_equal_unordered(results, expected);
128+
}
129+
}
130+
else
131+
{
132+
THEN("Expect " + std::to_string(expected.size()) + " symbols")
133+
{
134+
require_vectors_equal_unordered(results, expected);
135+
}
136+
}
137+
}
138+
}
139+
140+
SCENARIO("load_method_by_regex", "[core][java_bytecode][load_method_by_regex]")
141+
{
142+
symbol_tablet symbol_table;
143+
symbol_table.add(create_method_symbol("java::pack.Class.methodName:()V"));
144+
symbol_table.add(create_method_symbol("java::pack.Class.anotherMethod:()V"));
145+
symbol_table.add(create_method_symbol("java::pack.Different.methodName:()V"));
146+
symbol_table.add(create_method_symbol("java::another.Class.methodName:()V"));
147+
148+
GIVEN("A pattern without java prefix, without descriptor, no regex")
149+
{
150+
const std::string pattern = "pack.Class.methodName";
151+
const std::vector<irep_idt> expected = {"java::pack.Class.methodName:()V"};
152+
require_result_for_pattern(pattern, expected, symbol_table);
153+
}
154+
GIVEN("A pattern with java prefix, without descriptor, no regex")
155+
{
156+
const std::string pattern = "java::pack.Class.methodName";
157+
const std::vector<irep_idt> expected = {"java::pack.Class.methodName:()V"};
158+
require_result_for_pattern(pattern, expected, symbol_table);
159+
}
160+
GIVEN("A pattern with java prefix, with descriptor, no regex")
161+
{
162+
const std::string pattern = R"(java::pack.Class.methodName:\(\)V)";
163+
const std::vector<irep_idt> expected = {"java::pack.Class.methodName:()V"};
164+
require_result_for_pattern(pattern, expected, symbol_table);
165+
}
166+
GIVEN("A pattern with java prefix, with wrong descriptor, no regex")
167+
{
168+
const std::string pattern = R"(java::pack.Class.methodName:\(I\)V)";
169+
const std::vector<irep_idt> expected = {};
170+
require_result_for_pattern(pattern, expected, symbol_table);
171+
}
172+
GIVEN("A pattern with java prefix, without descriptor, with regex")
173+
{
174+
const std::string pattern = "java::pack.Class..*";
175+
const std::vector<irep_idt> expected = {
176+
"java::pack.Class.methodName:()V", "java::pack.Class.anotherMethod:()V"};
177+
require_result_for_pattern(pattern, expected, symbol_table);
178+
}
179+
GIVEN("A pattern without java prefix, without descriptor, with regex")
180+
{
181+
const std::string pattern = "pack.Class..*";
182+
const std::vector<irep_idt> expected = {
183+
"java::pack.Class.methodName:()V", "java::pack.Class.anotherMethod:()V"};
184+
require_result_for_pattern(pattern, expected, symbol_table);
185+
}
186+
GIVEN("A pattern without java prefix, with descriptor, with regex in package")
187+
{
188+
const std::string pattern = R"(\w+.Class.methodName:\(\)V)";
189+
const std::vector<irep_idt> expected = {
190+
"java::pack.Class.methodName:()V", "java::another.Class.methodName:()V"};
191+
require_result_for_pattern(pattern, expected, symbol_table);
192+
}
193+
}

0 commit comments

Comments
 (0)