From bb8ce9f7829090b6dfcf5904c037159b15593aae Mon Sep 17 00:00:00 2001 From: hjzhangcm <845154910@qq.com> Date: Wed, 5 Jan 2022 18:48:16 +0800 Subject: [PATCH] add test case Signed-off-by: hjzhangcm <845154910@qq.com> --- ecmascript/napi/test/jsnapi_tests.cpp | 55 ++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 6 deletions(-) diff --git a/ecmascript/napi/test/jsnapi_tests.cpp b/ecmascript/napi/test/jsnapi_tests.cpp index 920c5f7ebb..ac0d3cbbef 100644 --- a/ecmascript/napi/test/jsnapi_tests.cpp +++ b/ecmascript/napi/test/jsnapi_tests.cpp @@ -633,19 +633,20 @@ HWTEST_F_L0(JSNApiTests, SyntaxError) ASSERT_TRUE(thread_->HasPendingException()); } -HWTEST_F_L0(JSNApiTests, InheritPrototype) +HWTEST_F_L0(JSNApiTests, InheritPrototype_001) { LocalScope scope(vm_); JSHandle env = vm_->GetGlobalEnv(); // new with Builtins::Set Prototype JSHandle set = env->GetBuiltinsSetFunction(); - Local setLocal = JSNApiHelper::ToLocal(set); + Local setLocal = JSNApiHelper::ToLocal(set);//父类 // new with Builtins::Map Prototype JSHandle map = env->GetBuiltinsMapFunction(); - Local mapLocal = JSNApiHelper::ToLocal(map); + Local mapLocal = JSNApiHelper::ToLocal(map);//子类 JSHandle setPrototype(thread_, JSHandle::Cast(set)->GetFunctionPrototype()); JSHandle mapPrototype(thread_, JSHandle::Cast(map)->GetFunctionPrototype()); JSHandle mapPrototypeProto(thread_, JSHandle::Cast(mapPrototype)->GetPrototype(thread_)); + //在父类中比较设置的setPrototype和映射的mapPrototype bool same = JSTaggedValue::SameValue(setPrototype, mapPrototypeProto); // before inherit, map.Prototype.__proto__ should be different from set.Prototype ASSERT_FALSE(same); @@ -654,7 +655,7 @@ HWTEST_F_L0(JSNApiTests, InheritPrototype) bool same1 = JSTaggedValue::SameValue(set, mapProto); ASSERT_FALSE(same1); - // Set property to Set Function + // Set property to Set Function 设置属性并且判断是否设置成功 auto factory = vm_->GetFactory(); JSHandle defaultString = thread_->GlobalConstants()->GetHandledDefaultString(); PropertyDescriptor desc = PropertyDescriptor(thread_, defaultString); @@ -667,21 +668,24 @@ HWTEST_F_L0(JSNApiTests, InheritPrototype) ASSERT_TRUE(success1); mapLocal->Inherit(vm_, setLocal); + //获取子类的Prototype并与在父类中设置的比较 JSHandle sonHandle = JSNApiHelper::ToJSHandle(mapLocal); JSHandle sonPrototype(thread_, JSHandle::Cast(sonHandle)->GetFunctionPrototype()); JSHandle sonPrototypeProto(thread_, JSHandle::Cast(sonPrototype)->GetPrototype(thread_)); bool same2 = JSTaggedValue::SameValue(setPrototype, sonPrototypeProto); ASSERT_TRUE(same2); + + //获取子类的Proto并与在父类中设置的比较 JSHandle sonProto(thread_, JSHandle::Cast(map)->GetPrototype(thread_)); bool same3 = JSTaggedValue::SameValue(set, sonProto); ASSERT_TRUE(same3); // son = new Son(), Son() inherit from Parent(), Test whether son.InstanceOf(Parent) is true JSHandle sonObj = factory->NewJSObjectByConstructor(JSHandle::Cast(sonHandle), sonHandle); - bool isInstance = JSObject::InstanceOf(thread_, JSHandle::Cast(sonObj), set); + bool isInstance = JSObject::InstanceOf(thread_, JSHandle::Cast(sonObj), set);//实例化 ASSERT_TRUE(isInstance); - // Test whether son Function can access to property of parent Function + // Test whether son Function can access to property of parent Function 获取属性defaultString 和 property1String并且与父类涉及的进行比较 OperationResult res = JSObject::GetProperty(thread_, JSHandle::Cast(sonHandle), defaultString); bool same4 = JSTaggedValue::SameValue(defaultString, res.GetValue()); ASSERT_TRUE(same4); @@ -694,6 +698,45 @@ HWTEST_F_L0(JSNApiTests, InheritPrototype) son1->Inherit(vm_, mapLocal); JSHandle son1Handle = JSHandle::Cast(JSNApiHelper::ToJSHandle(son1)); ASSERT_TRUE(son1Handle->HasFunctionPrototype()); + +} + +HWTEST_F_L0(JSNApiTests, InheritPrototype_002) +{ + LocalScope scope(vm_); + JSHandle env = vm_->GetGlobalEnv(); + // new with Builtins::Set Prototype + JSHandle set = env->GetBuiltinsSetFunction(); + Local setLocal = JSNApiHelper::ToLocal(set);//父类 + // new with Builtins::Map Prototype + JSHandle map = env->GetBuiltinsMapFunction(); + Local mapLocal = JSNApiHelper::ToLocal(map);//子类 + + auto factory = vm_->GetFactory(); + JSHandle defaultString = thread_->GlobalConstants()->GetHandledDefaultString(); + PropertyDescriptor desc = PropertyDescriptor(thread_, defaultString); + bool success = JSObject::DefineOwnProperty(thread_, JSHandle::Cast(set), defaultString, desc); + ASSERT_TRUE(success); + + mapLocal->Inherit(vm_, setLocal); + + JSHandle property1String(thread_, factory->NewFromCanBeCompressString("property1").GetTaggedValue()); + JSHandle func = env->GetTypedArrayFunction(); + PropertyDescriptor desc1 = PropertyDescriptor(thread_, func); + bool success1 = JSObject::DefineOwnProperty(thread_, JSHandle::Cast(map), property1String, desc1); + ASSERT_TRUE(success1); + + + JSHandle sonHandle = JSNApiHelper::ToJSHandle(mapLocal); + JSHandle sonObj = factory->NewJSObjectByConstructor(JSHandle::Cast(sonHandle), sonHandle); + + JSHandle fatherHandle = JSNApiHelper::ToJSHandle(setLocal); + JSHandle fatherObj = factory->NewJSObjectByConstructor(JSHandle::Cast(fatherHandle), fatherHandle); + + JSHandle sonMethod = JSObject::GetMethod(thread_, JSHandle(sonObj), property1String); + JSHandle fatherMethod = JSObject::GetMethod(thread_, JSHandle(fatherObj), property1String); + bool same = JSTaggedValue::SameValue(sonMethod, fatherMethod); + ASSERT_TRUE(same); } HWTEST_F_L0(JSNApiTests, ClassFunction) -- Gitee