|
| 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 <testing-utils/catch.hpp> |
| 11 | +#include <java_bytecode/java_types.h> |
| 12 | +#include <java_bytecode/java_bytecode_parse_tree.h> |
| 13 | +#include <java_bytecode/java_bytecode_convert_class.h> |
| 14 | + |
| 15 | +// See |
| 16 | +// https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7.16.1 |
| 17 | +SCENARIO( |
| 18 | + "java_bytecode_parse_annotations", |
| 19 | + "[core][java_bytecode][java_bytecode_parser]") |
| 20 | +{ |
| 21 | + GIVEN("Some class files in the class path") |
| 22 | + { |
| 23 | + WHEN("Parsing an annotation with Class value specified to non-primitive") |
| 24 | + { |
| 25 | + const symbol_tablet &new_symbol_table = load_java_class( |
| 26 | + "ClassWithClassTypeAnnotation", |
| 27 | + "./java_bytecode/java_bytecode_parser"); |
| 28 | + |
| 29 | + THEN("The annotation should store the correct type") |
| 30 | + { |
| 31 | + const symbolt &class_symbol = |
| 32 | + *new_symbol_table.lookup("java::ClassWithClassTypeAnnotation"); |
| 33 | + const std::vector<java_annotationt> &java_annotations = |
| 34 | + to_annotated_type(class_symbol.type).get_annotations(); |
| 35 | + java_bytecode_parse_treet::annotationst annotations; |
| 36 | + convert_java_annotations(java_annotations, annotations); |
| 37 | + REQUIRE(annotations.size() == 1); |
| 38 | + const auto &annotation = annotations.front(); |
| 39 | + const auto &element_value_pair = annotation.element_value_pairs.front(); |
| 40 | + const auto &id = to_symbol_expr(element_value_pair.value) |
| 41 | + .get_identifier(); |
| 42 | + REQUIRE(id2string(id) == "Ljava/lang/String;"); |
| 43 | + } |
| 44 | + } |
| 45 | + WHEN("Parsing an annotation with Class value specified to primitive") |
| 46 | + { |
| 47 | + const symbol_tablet &new_symbol_table = load_java_class( |
| 48 | + "ClassWithPrimitiveTypeAnnotation", |
| 49 | + "./java_bytecode/java_bytecode_parser"); |
| 50 | + |
| 51 | + THEN("The annotation should store the correct type") |
| 52 | + { |
| 53 | + const symbolt &class_symbol = |
| 54 | + *new_symbol_table.lookup("java::ClassWithPrimitiveTypeAnnotation"); |
| 55 | + const std::vector<java_annotationt> &java_annotations = |
| 56 | + to_annotated_type(class_symbol.type).get_annotations(); |
| 57 | + java_bytecode_parse_treet::annotationst annotations; |
| 58 | + convert_java_annotations(java_annotations, annotations); |
| 59 | + REQUIRE(annotations.size() == 1); |
| 60 | + const auto &annotation = annotations.front(); |
| 61 | + const auto &element_value_pair = annotation.element_value_pairs.front(); |
| 62 | + const auto &id = to_symbol_expr(element_value_pair.value) |
| 63 | + .get_identifier(); |
| 64 | + REQUIRE(id2string(id) == "B"); |
| 65 | + } |
| 66 | + } |
| 67 | + WHEN("Parsing an annotation with Class value specified to void") |
| 68 | + { |
| 69 | + const symbol_tablet &new_symbol_table = load_java_class( |
| 70 | + "ClassWithVoidTypeAnnotation", |
| 71 | + "./java_bytecode/java_bytecode_parser"); |
| 72 | + |
| 73 | + THEN("The annotation should store the correct type") |
| 74 | + { |
| 75 | + const symbolt &class_symbol = |
| 76 | + *new_symbol_table.lookup("java::ClassWithVoidTypeAnnotation"); |
| 77 | + const std::vector<java_annotationt> &java_annotations = |
| 78 | + to_annotated_type(class_symbol.type).get_annotations(); |
| 79 | + java_bytecode_parse_treet::annotationst annotations; |
| 80 | + convert_java_annotations(java_annotations, annotations); |
| 81 | + REQUIRE(annotations.size() == 1); |
| 82 | + const auto &annotation = annotations.front(); |
| 83 | + const auto &element_value_pair = annotation.element_value_pairs.front(); |
| 84 | + const auto &id = to_symbol_expr(element_value_pair.value) |
| 85 | + .get_identifier(); |
| 86 | + REQUIRE(id2string(id) == "V"); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments