• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "plugins/ecmascript/assembler/extension/ecmascript_meta.h"
19 
20 namespace panda::test {
21 
22 HWTEST(EcmaScriptMetaTest, ValidateTest, testing::ext::TestSize.Level0)
23 {
24     pandasm::extensions::ecmascript::RecordMetadata rmd;
25     pandasm::Metadata::Error err("Attribute 'ecmascript.extends' must have a value",
26                                  pandasm::Metadata::Error::Type::MISSING_VALUE);
27 
28     std::optional<pandasm::Metadata::Error> result1 = rmd.SetAttribute("ecmascript.extends");
29     ASSERT_TRUE(result1.has_value());
30     ASSERT_EQ(err.GetMessage(), result1->GetMessage());
31     ASSERT_EQ(err.GetType(), result1->GetType());
32 
33     std::optional<pandasm::Metadata::Error> result2 = rmd.SetAttribute("ecmascript.annotation");
34     ASSERT_FALSE(result2.has_value());
35 
36     std::optional<pandasm::Metadata::Error> result3 = rmd.SetAttributeValue("ecmascript.extends", "value");
37     ASSERT_FALSE(result3.has_value());
38 
39     std::optional<pandasm::Metadata::Error> result4 = rmd.SetAttributeValue("ecmascript.annotation", "value");
40     ASSERT_TRUE(result4.has_value());
41     ASSERT_EQ(result4->GetMessage(), "Attribute 'ecmascript.annotation' must not have a value");
42     ASSERT_EQ(result4->GetType(), pandasm::Metadata::Error::Type::UNEXPECTED_VALUE);
43 
44     std::optional<pandasm::Metadata::Error> result5 = rmd.SetAttributeValue("attribute", "bool");
45     ASSERT_TRUE(result5.has_value());
46     ASSERT_EQ(result5->GetMessage(), "Unknown attribute 'attribute'");
47 
48     std::optional<pandasm::Metadata::Error> result6 = rmd.SetAttribute("ecmascript.annotation");
49     ASSERT_TRUE(result6.has_value());
50     ASSERT_EQ(result6->GetMessage(), "Attribute 'ecmascript.annotation' already defined");
51 }
52 
53 HWTEST(EcmaScriptMetaTest, SetAndRemoveFlagsTest, testing::ext::TestSize.Level0)
54 {
55     pandasm::extensions::ecmascript::RecordMetadata rmd;
56     rmd.SetAttribute("attribute");
57     rmd.SetAttribute("external");
58     rmd.SetAttribute("ecmascript.annotation");
59     ASSERT_FALSE(rmd.GetAttribute("attribute"));
60     ASSERT_TRUE(rmd.GetAttribute("external"));
61     ASSERT_TRUE(rmd.GetAttribute("ecmascript.annotation"));
62 
63     rmd.RemoveAttribute("attribute");
64     ASSERT_FALSE(rmd.GetAttribute("attribute"));
65     rmd.RemoveAttribute("external");
66     ASSERT_FALSE(rmd.GetAttribute("external"));
67     rmd.RemoveAttribute("ecmascript.annotation");
68     ASSERT_FALSE(rmd.GetAttribute("ecmascript.annotation"));
69 }
70 
71 }  // namespace panda::test
72