|
| 1 | +/*******************************************************************\ |
| 2 | +
|
| 3 | + Module: Unit tests for converting annotations |
| 4 | +
|
| 5 | + Author: Diffblue Ltd. |
| 6 | +
|
| 7 | +\*******************************************************************/ |
| 8 | + |
| 9 | +#include <java-testing-utils/load_java_class.h> |
| 10 | +#include <java_bytecode/java_bytecode_convert_class.h> |
| 11 | +#include <java_bytecode/java_bytecode_parse_tree.h> |
| 12 | +#include <java_bytecode/java_types.h> |
| 13 | +#include <testing-utils/catch.hpp> |
| 14 | + |
| 15 | +SCENARIO( |
| 16 | + "java_bytecode_convert_annotations", |
| 17 | + "[core][java_bytecode][java_bytecode_convert_class]") |
| 18 | +{ |
| 19 | + GIVEN("Some class files in the class path") |
| 20 | + { |
| 21 | + WHEN("Parsing a class with class-level annotation MyClassAnnotation(6)") |
| 22 | + { |
| 23 | + const symbol_tablet &new_symbol_table = load_java_class( |
| 24 | + "ClassWithClassAnnotation", |
| 25 | + "./java_bytecode/java_bytecode_convert_class"); |
| 26 | + |
| 27 | + THEN("The annotation should have the correct structure") |
| 28 | + { |
| 29 | + const symbolt &class_symbol = |
| 30 | + *new_symbol_table.lookup("java::ClassWithClassAnnotation"); |
| 31 | + const std::vector<java_annotationt> &java_annotations = |
| 32 | + to_annotated_type(class_symbol.type).get_annotations(); |
| 33 | + java_bytecode_parse_treet::annotationst annotations; |
| 34 | + convert_java_annotations(java_annotations, annotations); |
| 35 | + REQUIRE(annotations.size() == 1); |
| 36 | + const auto &annotation = annotations.front(); |
| 37 | + const auto &identifier = |
| 38 | + to_symbol_type(annotation.type.subtype()).get_identifier(); |
| 39 | + REQUIRE(id2string(identifier) == "java::MyClassAnnotation"); |
| 40 | + const auto &element_value_pair = annotation.element_value_pairs.front(); |
| 41 | + const auto &element_name = element_value_pair.element_name; |
| 42 | + REQUIRE(id2string(element_name) == "value"); |
| 43 | + const auto &expr = element_value_pair.value; |
| 44 | + const auto comp_expr = from_integer(6, java_int_type()); |
| 45 | + REQUIRE(expr == comp_expr); |
| 46 | + } |
| 47 | + } |
| 48 | + WHEN("Parsing a class with method-level annotation MyMethodAnnotation(11)") |
| 49 | + { |
| 50 | + const symbol_tablet &new_symbol_table = load_java_class( |
| 51 | + "ClassWithMethodAnnotation", |
| 52 | + "./java_bytecode/java_bytecode_convert_class"); |
| 53 | + |
| 54 | + THEN("The annotation should have the correct structure") |
| 55 | + { |
| 56 | + const symbolt &method_symbol = *new_symbol_table.lookup( |
| 57 | + "java::ClassWithMethodAnnotation.myMethod:()V"); |
| 58 | + const std::vector<java_annotationt> &java_annotations = |
| 59 | + to_annotated_type(method_symbol.type).get_annotations(); |
| 60 | + java_bytecode_parse_treet::annotationst annotations; |
| 61 | + convert_java_annotations(java_annotations, annotations); |
| 62 | + REQUIRE(annotations.size() == 1); |
| 63 | + const auto &annotation = annotations.front(); |
| 64 | + const auto &identifier = |
| 65 | + to_symbol_type(annotation.type.subtype()).get_identifier(); |
| 66 | + REQUIRE(id2string(identifier) == "java::MyMethodAnnotation"); |
| 67 | + const auto &element_value_pair = annotation.element_value_pairs.front(); |
| 68 | + const auto &element_name = element_value_pair.element_name; |
| 69 | + REQUIRE(id2string(element_name) == "methodValue"); |
| 70 | + const auto &expr = element_value_pair.value; |
| 71 | + const auto &comp_expr = from_integer(11, java_int_type()); |
| 72 | + REQUIRE(expr == comp_expr); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments