|
| 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