Skip to content

Commit

Permalink
静态变量的测试
Browse files Browse the repository at this point in the history
  • Loading branch information
chexiongsheng committed Feb 25, 2025
1 parent 9974ebe commit fff141b
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion test/papi_qjs_base_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,29 @@ static void ASetterWrap(struct pesapi_ffi* apis, pesapi_callback_info info)
obj->a = apis->get_value_int32(env, p0);
}

static void CtorCountGetterWrap(struct pesapi_ffi* apis, pesapi_callback_info info)
{
auto env = apis->get_env(info);
apis->add_return(info, apis->create_int32(env, TestStruct::ctor_count));
}

static void CtorCountSetterWrap(struct pesapi_ffi* apis, pesapi_callback_info info)
{
auto env = apis->get_env(info);
auto p0 = apis->get_arg(info, 0);
TestStruct::ctor_count = apis->get_value_int32(env, p0);
}

class PApiBaseTest : public ::testing::Test {
public:
static void SetUpTestCase() {
const void* typeId = "TestStruct";
const int properties_count = 3;
const int properties_count = 4;
pesapi_property_descriptor properties = pesapi_alloc_property_descriptors(properties_count);
pesapi_set_method_info(properties, 0, "Add", true, AddWrap, NULL, NULL);
pesapi_set_method_info(properties, 1, "Calc", false, CalcWrap, NULL, NULL);
pesapi_set_property_info(properties, 2, "a", false, AGetterWrap, ASetterWrap, NULL, NULL, NULL);
pesapi_set_property_info(properties, 3, "ctor_count", true, CtorCountGetterWrap, CtorCountSetterWrap, NULL, NULL, NULL);
pesapi_define_class(typeId, NULL, "TestStruct", TestStructCtor, TestStructFinalize, properties_count, properties, NULL);
}

Expand Down Expand Up @@ -387,6 +401,30 @@ TEST_F(PApiBaseTest, PropertyAccess) {
EXPECT_STREQ("123:579", str);
}

TEST_F(PApiBaseTest, VariableAccess) {
auto env = apis->get_env_from_ref(env_ref);

auto code = R"(
(function() {
const TestStruct = loadClass('TestStruct');
const obj = new TestStruct(123);
const ret = TestStruct.ctor_count
TestStruct.ctor_count = 999;
return ret;
})();
)";
TestStruct::ctor_count = 100;
auto ret = apis->eval(env, (const uint8_t*)(code), strlen(code), "test.js");
if (apis->has_caught(scope))
{
printf("%s\n", apis->get_exception_as_string(scope, true));
}
ASSERT_FALSE(apis->has_caught(scope));
EXPECT_EQ(999, TestStruct::ctor_count);
ASSERT_TRUE(apis->is_int32(env, ret));
ASSERT_TRUE(apis->get_value_int32(env, ret) == 101);
}


} // namespace qjsimpl
} // namespace pesapi
Expand Down

0 comments on commit fff141b

Please sign in to comment.