File: load_method_by_regex.cpp

package info (click to toggle)
cbmc 6.6.0-4
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 153,852 kB
  • sloc: cpp: 386,459; ansic: 114,466; java: 28,405; python: 6,003; yacc: 4,552; makefile: 4,041; lex: 2,487; xml: 2,388; sh: 2,050; perl: 557; pascal: 184; javascript: 163; ada: 36
file content (179 lines) | stat: -rw-r--r-- 6,086 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*******************************************************************\

Module: Unit tests for parsing generic classes

Author: Diffblue Limited

\*******************************************************************/

#include <java_bytecode/load_method_by_regex.h>
#include <testing-utils/require_vectors_equal_unordered.h>
#include <testing-utils/use_catch.h>

#include <util/symbol_table.h>

#include <java_bytecode/java_types.h>

SCENARIO(
  "load_method_by_regex::does_pattern_miss_descriptor",
  "[core][java_bytecode][load_method_by_regex]")
{
  GIVEN("A string with a java prefix and no descriptor")
  {
    const std::string pattern = "java::com.diffblue.ClassName.methodName";
    WHEN("When calling does_pattern_miss_descriptor")
    {
      const bool result = does_pattern_miss_descriptor(pattern);
      THEN("It should miss discriptor")
      {
        REQUIRE(result);
      }
    }
  }
  GIVEN("A string with a java prefix and a descriptor")
  {
    const std::string pattern = "java::com.diffblue.ClassName.methodName:()V";
    WHEN("When calling does_pattern_miss_descriptor")
    {
      const bool result = does_pattern_miss_descriptor(pattern);
      THEN("It should have the discriptor")
      {
        REQUIRE_FALSE(result);
      }
    }
  }
  GIVEN("A string without a java prefix and without a descriptor")
  {
    const std::string pattern = "com.diffblue.ClassName.methodName";
    WHEN("When calling does_pattern_miss_descriptor")
    {
      const bool result = does_pattern_miss_descriptor(pattern);
      THEN("It should miss discriptor")
      {
        REQUIRE(result);
      }
    }
  }
  GIVEN("A string without a java prefix and with a descriptor")
  {
    const std::string pattern = "com.diffblue.ClassName.methodName:()V";
    WHEN("When calling does_pattern_miss_descriptor")
    {
      const bool result = does_pattern_miss_descriptor(pattern);
      THEN("It should have the discriptor")
      {
        REQUIRE_FALSE(result);
      }
    }
  }
  GIVEN("A string with an almost java prefix and no descriptor")
  {
    const std::string pattern = "java:com.diffblue.ClassName.methodName";
    WHEN("When calling does_pattern_miss_descriptor")
    {
      const bool result = does_pattern_miss_descriptor(pattern);
      THEN("It should classify the last : as a descriptor")
      {
        REQUIRE_FALSE(result);
      }
    }
  }
  GIVEN("A string with an almost java prefix and with a descriptor")
  {
    const std::string pattern = "java:com.diffblue.ClassName.methodName:()V";
    WHEN("When calling does_pattern_miss_descriptor")
    {
      const bool result = does_pattern_miss_descriptor(pattern);
      THEN("It should have the discriptor")
      {
        REQUIRE_FALSE(result);
      }
    }
  }
}

static symbolt create_method_symbol(const std::string &method_name)
{
  return symbolt{method_name, java_method_typet{{}, typet{}}, ID_java};
}

static void require_result_for_pattern(
  const std::string &pattern,
  const std::vector<irep_idt> &expected,
  const symbol_tablet &symbol_table)
{
  WHEN("Constructing a load_method_by_regex")
  {
    const auto matcher = build_load_method_by_regex(pattern);
    const auto &results = matcher(symbol_table);
    if(expected.size() == 1)
    {
      THEN("Expect " + id2string(expected[0]))
      {
        require_vectors_equal_unordered(results, expected);
      }
    }
    else
    {
      THEN("Expect " + std::to_string(expected.size()) + " symbols")
      {
        require_vectors_equal_unordered(results, expected);
      }
    }
  }
}

SCENARIO("load_method_by_regex", "[core][java_bytecode][load_method_by_regex]")
{
  symbol_tablet symbol_table;
  symbol_table.add(create_method_symbol("java::pack.Class.methodName:()V"));
  symbol_table.add(create_method_symbol("java::pack.Class.anotherMethod:()V"));
  symbol_table.add(create_method_symbol("java::pack.Different.methodName:()V"));
  symbol_table.add(create_method_symbol("java::another.Class.methodName:()V"));

  GIVEN("A pattern without java prefix, without descriptor, no regex")
  {
    const std::string pattern = "pack.Class.methodName";
    const std::vector<irep_idt> expected = {"java::pack.Class.methodName:()V"};
    require_result_for_pattern(pattern, expected, symbol_table);
  }
  GIVEN("A pattern with java prefix, without descriptor, no regex")
  {
    const std::string pattern = "java::pack.Class.methodName";
    const std::vector<irep_idt> expected = {"java::pack.Class.methodName:()V"};
    require_result_for_pattern(pattern, expected, symbol_table);
  }
  GIVEN("A pattern with java prefix, with descriptor, no regex")
  {
    const std::string pattern = R"(java::pack.Class.methodName:\(\)V)";
    const std::vector<irep_idt> expected = {"java::pack.Class.methodName:()V"};
    require_result_for_pattern(pattern, expected, symbol_table);
  }
  GIVEN("A pattern with java prefix, with wrong descriptor, no regex")
  {
    const std::string pattern = R"(java::pack.Class.methodName:\(I\)V)";
    const std::vector<irep_idt> expected = {};
    require_result_for_pattern(pattern, expected, symbol_table);
  }
  GIVEN("A pattern with java prefix, without descriptor, with regex")
  {
    const std::string pattern = "java::pack.Class..*";
    const std::vector<irep_idt> expected = {
      "java::pack.Class.methodName:()V", "java::pack.Class.anotherMethod:()V"};
    require_result_for_pattern(pattern, expected, symbol_table);
  }
  GIVEN("A pattern without java prefix, without descriptor, with regex")
  {
    const std::string pattern = "pack.Class..*";
    const std::vector<irep_idt> expected = {
      "java::pack.Class.methodName:()V", "java::pack.Class.anotherMethod:()V"};
    require_result_for_pattern(pattern, expected, symbol_table);
  }
  GIVEN("A pattern without java prefix, with descriptor, with regex in package")
  {
    const std::string pattern = R"(\w+.Class.methodName:\(\)V)";
    const std::vector<irep_idt> expected = {
      "java::pack.Class.methodName:()V", "java::another.Class.methodName:()V"};
    require_result_for_pattern(pattern, expected, symbol_table);
  }
}