1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include <gtest/gtest.h> 17 18 #include "extensions/ecmascript_meta.h" 19 #include "meta.h" 20 21 using namespace testing::ext; 22 23 namespace panda::pandasm { 24 class EcmascriptMetaTest : public testing::Test { 25 }; 26 27 /** 28 * @tc.name: ecmascript_meta_test_001 29 * @tc.desc: Verify the RecordMetadata SetAttributeValue function. 30 * @tc.type: FUNC 31 * @tc.require: issueNumber 32 */ 33 HWTEST_F(EcmascriptMetaTest, ecmascript_meta_test_001, TestSize.Level1) 34 { 35 pandasm::extensions::ecmascript::RecordMetadata rmd; 36 pandasm::Metadata::Error err("Attribute 'ecmascript.extends' must have a value", 37 pandasm::Metadata::Error::Type::MISSING_VALUE); 38 39 std::optional<pandasm::Metadata::Error> result1 = rmd.SetAttribute("ecmascript.extends"); 40 EXPECT_TRUE(result1.has_value()); 41 EXPECT_EQ(err.GetMessage(), result1->GetMessage()); 42 EXPECT_EQ(err.GetType(), result1->GetType()); 43 44 std::optional<pandasm::Metadata::Error> result2 = rmd.SetAttribute("ecmascript.annotation"); 45 EXPECT_FALSE(result2.has_value()); 46 47 std::optional<pandasm::Metadata::Error> result3 = rmd.SetAttributeValue("ecmascript.extends", "value"); 48 EXPECT_FALSE(result3.has_value()); 49 50 std::optional<pandasm::Metadata::Error> result4 = rmd.SetAttributeValue("ecmascript.annotation", "value"); 51 EXPECT_TRUE(result4.has_value()); 52 EXPECT_EQ(result4->GetMessage(), "Attribute 'ecmascript.annotation' must not have a value"); 53 EXPECT_EQ(result4->GetType(), pandasm::Metadata::Error::Type::UNEXPECTED_VALUE); 54 55 std::optional<pandasm::Metadata::Error> result5 = rmd.SetAttributeValue("attribute", "bool"); 56 EXPECT_TRUE(result5.has_value()); 57 EXPECT_EQ(result5->GetMessage(), "Unknown attribute 'attribute'"); 58 59 std::optional<pandasm::Metadata::Error> result6 = rmd.SetAttribute("ecmascript.annotation"); 60 EXPECT_TRUE(result6.has_value()); 61 EXPECT_EQ(result6->GetMessage(), "Attribute 'ecmascript.annotation' already defined"); 62 63 std::optional<pandasm::Metadata::Error> result7 = rmd.ValidateData(); 64 EXPECT_FALSE(result7.has_value()); 65 } 66 67 /** 68 * @tc.name: ecmascript_meta_test_002 69 * @tc.desc: Verify the RecordMetadata GetAttributeValue function. 70 * @tc.type: FUNC 71 * @tc.require: issueNumber 72 */ 73 HWTEST_F(EcmascriptMetaTest, ecmascript_meta_test_002, TestSize.Level1) 74 { 75 pandasm::extensions::ecmascript::RecordMetadata rmd; 76 rmd.SetAttribute("attribute"); 77 rmd.SetAttribute("external"); 78 rmd.SetAttribute("ecmascript.annotation"); 79 EXPECT_FALSE(rmd.GetAttribute("attribute")); 80 EXPECT_TRUE(rmd.GetAttribute("external")); 81 EXPECT_TRUE(rmd.GetAttribute("ecmascript.annotation")); 82 83 rmd.RemoveAttribute("attribute"); 84 EXPECT_FALSE(rmd.GetAttribute("attribute")); 85 rmd.RemoveAttribute("external"); 86 EXPECT_FALSE(rmd.GetAttribute("external")); 87 rmd.RemoveAttribute("ecmascript.annotation"); 88 EXPECT_FALSE(rmd.GetAttribute("ecmascript.annotation")); 89 90 EXPECT_EQ(rmd.GetBase(), ""); 91 EXPECT_EQ(rmd.GetInterfaces().size(), 0); 92 EXPECT_FALSE(rmd.IsAnnotation()); 93 EXPECT_FALSE(rmd.IsRuntimeAnnotation()); 94 rmd.SetAttributeValue("ecmascript.extends", "value"); 95 EXPECT_EQ(rmd.GetBase(), "value"); 96 97 rmd.SetAttribute("ecmascript.annotation"); 98 rmd.RemoveAttribute("ecmascript.annotation"); 99 EXPECT_FALSE(rmd.GetAttribute("ecmascript.annotation")); 100 auto ret = rmd.GetAttributeValue("attribute"); 101 EXPECT_FALSE(ret.has_value()); 102 } 103 104 /** 105 * @tc.name: ecmascript_meta_test_003 106 * @tc.desc: Verify the FieldMetadata ValidateData function. 107 * @tc.type: FUNC 108 * @tc.require: issueNumber 109 */ 110 HWTEST_F(EcmascriptMetaTest, ecmascript_meta_test_003, TestSize.Level1) 111 { 112 pandasm::extensions::ecmascript::FieldMetadata rmd; 113 pandasm::Metadata::Error err("Attribute 'ecmascript.extends' must have a value", 114 pandasm::Metadata::Error::Type::MISSING_VALUE); 115 116 std::optional<pandasm::Metadata::Error> result1 = rmd.SetAttribute("ecmascript.annotation"); 117 EXPECT_TRUE(result1.has_value()); 118 119 std::optional<pandasm::Metadata::Error> result2 = rmd.SetAttributeValue("ecmascript.extends", "value"); 120 EXPECT_TRUE(result2.has_value()); 121 122 std::optional<pandasm::Metadata::Error> result3 = rmd.SetAttributeValue("ecmascript.annotation", "value"); 123 EXPECT_TRUE(result3.has_value()); 124 EXPECT_EQ(result3->GetMessage(), "Unknown attribute 'ecmascript.annotation'"); 125 EXPECT_EQ(result3->GetType(), pandasm::Metadata::Error::Type::UNKNOWN_ATTRIBUTE); 126 127 std::optional<pandasm::Metadata::Error> result4 = rmd.SetAttributeValue("attribute", "bool"); 128 EXPECT_TRUE(result4.has_value()); 129 EXPECT_EQ(result4->GetMessage(), "Unknown attribute 'attribute'"); 130 131 std::optional<pandasm::Metadata::Error> result5 = rmd.SetAttribute("ecmascript.annotation"); 132 EXPECT_TRUE(result5.has_value()); 133 EXPECT_EQ(result5->GetMessage(), "Unknown attribute 'ecmascript.annotation'"); 134 135 std::optional<pandasm::Metadata::Error> result6 = rmd.ValidateData(); 136 EXPECT_FALSE(result6.has_value()); 137 } 138 139 /** 140 * @tc.name: ecmascript_meta_test_004 141 * @tc.desc: Verify the GetMessage function. 142 * @tc.type: FUNC 143 * @tc.require: issueNumber 144 */ 145 HWTEST_F(EcmascriptMetaTest, ecmascript_meta_test_004, TestSize.Level1) 146 { 147 pandasm::Metadata::Error err("test message", pandasm::Metadata::Error::Type::MISSING_ATTRIBUTE); 148 EXPECT_EQ(err.GetMessage(), "test message"); 149 EXPECT_EQ(err.GetType(), pandasm::Metadata::Error::Type::MISSING_ATTRIBUTE); 150 } 151 } // namespace panda::pandasm