• 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 "ecmascript/template_string.h"
17 #include "ecmascript/builtins/builtins_string.h"
18 #include "ecmascript/global_env.h"
19 #include "ecmascript/template_map.h"
20 #include "ecmascript/tests/test_helper.h"
21 
22 using namespace panda;
23 using namespace panda::ecmascript;
24 
25 namespace panda::test {
26 using BuiltinsString = ecmascript::builtins::BuiltinsString;
27 class TemplateStringTest : public BaseTestWithScope<false> {
28 };
29 
30 /*
31  * @tc.name: GetTemplateObject
32  * @tc.desc: Call the function "arraycreate" to create a jsarray object, add key value pairs, and then call the
33  *           "NewTagedArray" function to nest the jsarray object. Call the function createarrayfromlist to create
34  *           a template literal, pass it to the "GetTemplateObject" function, check whether the templatemap object
35  *           getted from env can find the jsarray object, then call the 'Raw' function to check whether the returned
36  *           value is within expectations.
37  * @tc.type: FUNC
38  * @tc.require:
39  */
HWTEST_F_L0(TemplateStringTest,GetTemplateObject)40 HWTEST_F_L0(TemplateStringTest, GetTemplateObject)
41 {
42     uint32_t arrayLength = 3;
43     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
44     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
45     JSHandle<JSTaggedValue> templateMapTag = env->GetTemplateMap();
46     JSHandle<TemplateMap> templateMap(templateMapTag);
47     JSHandle<JSTaggedValue> rawKey(factory->NewFromASCII("raw"));
48     JSHandle<EcmaString> testString = factory->NewFromASCII("bar2bazJavaScriptbaz");
49     JSHandle<EcmaString> javaScript = factory->NewFromASCII("JavaScript");
50     JSHandle<TaggedArray> elements = factory->NewTaggedArray(arrayLength);
51     // create jsArray object
52     JSArray *arr = JSArray::ArrayCreate(thread, JSTaggedNumber(arrayLength)).GetObject<JSArray>();
53     JSHandle<JSTaggedValue> jsArrayObj(thread, arr);
54     // add key and value to the first index
55     JSHandle<JSTaggedValue> arrayObjVal(factory->NewFromASCII("bar"));
56     PropertyDescriptor propertyDesc(thread, arrayObjVal, true, true, true);
57     JSHandle<JSTaggedValue> arrayObjKey(factory->NewFromASCII("0"));
58     JSArray::DefineOwnProperty(thread, JSHandle<JSObject>(jsArrayObj), arrayObjKey, propertyDesc);
59     // add key and value to the second index
60     JSHandle<JSTaggedValue> arrayObjVal1(factory->NewFromASCII("baz"));
61     PropertyDescriptor propertyDesc1(thread, arrayObjVal1, true, true, true);
62     JSHandle<JSTaggedValue> arrayObjKey1(factory->NewFromASCII("1"));
63     JSArray::DefineOwnProperty(thread, JSHandle<JSObject>(jsArrayObj), arrayObjKey1, propertyDesc1);
64     // add key and value to the third index
65     JSHandle<JSTaggedValue> arrayObjVal2(factory->NewFromASCII("foo"));
66     PropertyDescriptor propertyDesc2(thread, arrayObjVal1, true, true, true);
67     JSHandle<JSTaggedValue> arrayObjKey2(factory->NewFromASCII("2"));
68     JSArray::DefineOwnProperty(thread, JSHandle<JSObject>(jsArrayObj), arrayObjKey2, propertyDesc2);
69     // create the list from jsArray
70     elements->Set(thread, 0, jsArrayObj);
71     elements->Set(thread, 1, jsArrayObj);
72     JSHandle<JSTaggedValue> templateLiteral(JSArray::CreateArrayFromList(thread, elements));
73     // call "GetTemplateObject" function
74     JSHandle<JSTaggedValue> templateObject = TemplateString::GetTemplateObject(thread, templateLiteral);
75     // find jsArray in templateMap
76     int resultEntry = templateMap->FindEntry(jsArrayObj.GetTaggedValue());
77     EXPECT_NE(resultEntry, -1);
78     auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 10);
79     ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
80     ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
81     ecmaRuntimeCallInfo->SetCallArg(0, templateObject.GetTaggedValue());
82     ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue(static_cast<int32_t>(2)));
83     ecmaRuntimeCallInfo->SetCallArg(2, javaScript.GetTaggedValue());
84     // call "Raw" function
85     [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
86     JSTaggedValue result = BuiltinsString::Raw(ecmaRuntimeCallInfo);
87     TestHelper::TearDownFrame(thread, prev);
88 
89     EXPECT_TRUE(result.IsString());
90     EXPECT_TRUE(EcmaStringAccessor::StringsAreEqual(reinterpret_cast<EcmaString *>(result.GetRawData()), *testString));
91 }
92 }  // namespace panda::test