add js_arguments_test.cpp

Signed-off-by: hjzhangcm <845154910@qq.com>
This commit is contained in:
hjzhangcm
2021-10-19 11:23:00 +08:00
parent 9c05d45a12
commit 29ed0182f4
2 changed files with 194 additions and 0 deletions
+29
View File
@@ -583,6 +583,33 @@ host_unittest_action("HugeObjectTest") {
}
}
host_unittest_action("JsArgumentsTest") {
module_out_path = module_output_path
sources = [
# test file
"js_arguments_test.cpp",
]
configs = [
"//ark/js_runtime:ecma_test_config",
"//ark/js_runtime:ark_jsruntime_public_config", # should add before
# arkruntime_public_config
"//ark/js_runtime:ark_jsruntime_config",
"$ark_root/runtime:arkruntime_public_config",
]
deps = [
"$ark_root/libpandabase:libarkbase",
"//ark/js_runtime:libark_jsruntime_test",
sdk_libc_secshared_dep,
]
if (!is_standard_system) {
deps += [ "$ark_root/runtime:libarkruntime" ]
}
}
host_unittest_action("LexicalEnvTest") {
module_out_path = module_output_path
@@ -866,6 +893,7 @@ group("unittest") {
":GcTest",
":GlueRegsTest",
":HugeObjectTest",
":JsArgumentsTest",
":JsArrayTest",
":JsDateTest",
":JsForinIteratorTest",
@@ -905,6 +933,7 @@ group("host_unittest") {
":GcTestAction",
":GlueRegsTestAction",
":HugeObjectTestAction",
":JsArgumentsTestAction",
":JsArrayTestAction",
":JsDateTestAction",
":JsForinIteratorTestAction",
+165
View File
@@ -0,0 +1,165 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ecmascript/js_arguments.h"
#include "ecmascript/ecma_vm.h"
#include "ecmascript/global_env.h"
#include "ecmascript/js_handle.h"
#include "ecmascript/object_factory.h"
#include "ecmascript/tests/test_helper.h"
using namespace panda::ecmascript;
namespace panda::test {
class JsArgumentsTest : public testing::Test {
public:
static void SetUpTestCase()
{
GTEST_LOG_(INFO) << "SetUpTestCase";
}
static void TearDownTestCase()
{
GTEST_LOG_(INFO) << "TearDownCase";
}
void SetUp() override
{
TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
}
void TearDown() override
{
TestHelper::DestroyEcmaVMWithScope(instance, scope);
}
ecmascript::EcmaHandleScope *scope {nullptr};
PandaVM *instance {nullptr};
JSThread *thread {nullptr};
};
static JSFunction *JSObjectTestCreate(JSThread *thread)
{
EcmaVM *ecmaVM = thread->GetEcmaVM();
JSHandle<GlobalEnv> globalEnv = ecmaVM->GetGlobalEnv();
return globalEnv->GetObjectFunction().GetObject<JSFunction>();
}
HWTEST_F_L0(JsArgumentsTest, SetProperty)
{
JSHandle<JSTaggedValue> argFunc(thread, JSObjectTestCreate(thread));
JSHandle<JSObject> jsarg =
thread->GetEcmaVM()->GetFactory()->NewJSObjectByConstructor(JSHandle<JSFunction>(argFunc), argFunc);
JSHandle<JSArguments> arg = thread->GetEcmaVM()->GetFactory()->NewJSArguments();
char array[] = "x";
JSHandle<JSTaggedValue> key(thread->GetEcmaVM()->GetFactory()->NewFromCanBeCompressString(array));
JSHandle<JSTaggedValue> value(thread, JSTaggedValue(1));
// receive must be jsarg's conversion
JSHandle<JSTaggedValue> receiver = JSHandle<JSTaggedValue>::Cast(jsarg);
EXPECT_TRUE(JSArguments::SetProperty(thread, arg, key, value, receiver));
EXPECT_EQ(JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(jsarg), key).GetValue()->GetInt(), 1);
EXPECT_EQ(JSArguments::GetProperty(thread, jsarg, key).GetValue()->GetInt(), 1);
JSHandle<JSTaggedValue> value2(thread, JSTaggedValue(2));
EXPECT_TRUE(JSArguments::SetProperty(thread, arg, key, value2, receiver));
EXPECT_EQ(JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(jsarg), key).GetValue()->GetInt(), 2);
EXPECT_EQ(JSArguments::GetProperty(thread, jsarg, key).GetValue()->GetInt(), 2);
}
HWTEST_F_L0(JsArgumentsTest, GetProperty)
{
JSHandle<JSTaggedValue> argFunc(thread, JSObjectTestCreate(thread));
JSHandle<JSObject> jsarg =
thread->GetEcmaVM()->GetFactory()->NewJSObjectByConstructor(JSHandle<JSFunction>(argFunc), argFunc);
JSHandle<JSArguments> arg = thread->GetEcmaVM()->GetFactory()->NewJSArguments();
char array[] = "x";
JSHandle<JSTaggedValue> key(thread->GetEcmaVM()->GetFactory()->NewFromCanBeCompressString(array));
JSHandle<JSTaggedValue> value(thread, JSTaggedValue(1));
JSHandle<JSTaggedValue> receiver = JSHandle<JSTaggedValue>::Cast(jsarg);
JSArguments::SetProperty(thread, arg, key, value, receiver);
EXPECT_EQ(JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(jsarg), key).GetValue()->GetInt(), 1);
EXPECT_EQ(JSArguments::GetProperty(thread, JSHandle<JSArguments>(jsarg), key, receiver).GetValue()->GetInt(), 1);
JSHandle<JSTaggedValue> value2(thread, JSTaggedValue(2));
JSArguments::SetProperty(thread, arg, key, value2, receiver);
EXPECT_EQ(JSArguments::GetProperty(thread, jsarg, key).GetValue()->GetInt(), 2);
EXPECT_EQ(JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(jsarg), key).GetValue()->GetInt(), 2);
}
HWTEST_F_L0(JsArgumentsTest, DeleteProperty)
{
JSHandle<JSTaggedValue> argFunc(thread, JSObjectTestCreate(thread));
JSHandle<JSObject> jsarg =
thread->GetEcmaVM()->GetFactory()->NewJSObjectByConstructor(JSHandle<JSFunction>(argFunc), argFunc);
JSHandle<JSArguments> arg = thread->GetEcmaVM()->GetFactory()->NewJSArguments();
char array[] = "delete";
JSHandle<JSTaggedValue> key(thread->GetEcmaVM()->GetFactory()->NewFromCanBeCompressString(array));
JSHandle<JSTaggedValue> value(thread, JSTaggedValue(1));
JSHandle<JSTaggedValue> receiver = JSHandle<JSTaggedValue>::Cast(jsarg);
JSArguments::SetProperty(thread, arg, key, value, receiver);
EXPECT_EQ(JSArguments::GetProperty(thread, jsarg, key).GetValue()->GetInt(), 1);
// test delete
bool result = JSArguments::DeleteProperty(thread, JSHandle<JSArguments>(jsarg), key);
EXPECT_TRUE(result);
EXPECT_TRUE(JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(jsarg), key).GetValue()->IsUndefined());
}
HWTEST_F_L0(JsArgumentsTest, DefineOwnProperty)
{
JSHandle<JSTaggedValue> argFunc(thread, JSObjectTestCreate(thread));
JSHandle<JSObject> jsarg =
thread->GetEcmaVM()->GetFactory()->NewJSObjectByConstructor(JSHandle<JSFunction>(argFunc), argFunc);
JSHandle<JSArguments> arg = thread->GetEcmaVM()->GetFactory()->NewJSArguments();
JSHandle<JSTaggedValue> key(thread->GetEcmaVM()->GetFactory()->NewFromCanBeCompressString("x"));
JSHandle<JSTaggedValue> value1(thread, JSTaggedValue(1));
JSHandle<JSTaggedValue> value2(thread, JSTaggedValue(2));
JSHandle<JSTaggedValue> receiver = JSHandle<JSTaggedValue>::Cast(jsarg);
JSArguments::SetProperty(thread, arg, key, value2, receiver);
EXPECT_EQ(JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(jsarg), key).GetValue()->GetInt(), 2);
PropertyDescriptor Desc(thread);
// set value1
Desc.SetValue(value1);
Desc.SetWritable(false);
EXPECT_TRUE(JSArguments::DefineOwnProperty(thread, JSHandle<JSArguments>(jsarg), key, Desc));
EXPECT_EQ(JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(jsarg), key).GetValue()->GetInt(), 1);
}
HWTEST_F_L0(JsArgumentsTest, GetOwnProperty)
{
JSHandle<JSTaggedValue> argFunc(thread, JSObjectTestCreate(thread));
JSHandle<JSObject> jsarg =
thread->GetEcmaVM()->GetFactory()->NewJSObjectByConstructor(JSHandle<JSFunction>(argFunc), argFunc);
JSHandle<JSArguments> arg = thread->GetEcmaVM()->GetFactory()->NewJSArguments();
JSHandle<JSTaggedValue> key(thread->GetEcmaVM()->GetFactory()->NewFromCanBeCompressString("x"));
JSHandle<JSTaggedValue> value(thread, JSTaggedValue(1));
JSHandle<JSTaggedValue> receiver = JSHandle<JSTaggedValue>::Cast(jsarg);
JSArguments::SetProperty(thread, arg, key, value, receiver);
PropertyDescriptor Desc(thread);
JSHandle<EcmaString> caller = thread->GetEcmaVM()->GetFactory()->NewFromCanBeCompressString("caller");
// key is not caller
EXPECT_FALSE(JSTaggedValue::SameValue(key.GetTaggedValue(), caller.GetTaggedValue()));
EXPECT_TRUE(JSArguments::GetOwnProperty(thread, JSHandle<JSArguments>(jsarg), key, Desc));
EXPECT_EQ(Desc.GetValue()->GetInt(), 1);
}
} // namespace panda::test