1425bb815Sopenharmony_ci/* Copyright JS Foundation and other contributors, http://js.foundation
2425bb815Sopenharmony_ci *
3425bb815Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4425bb815Sopenharmony_ci * you may not use this file except in compliance with the License.
5425bb815Sopenharmony_ci * You may obtain a copy of the License at
6425bb815Sopenharmony_ci *
7425bb815Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8425bb815Sopenharmony_ci *
9425bb815Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10425bb815Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS
11425bb815Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12425bb815Sopenharmony_ci * See the License for the specific language governing permissions and
13425bb815Sopenharmony_ci * limitations under the License.
14425bb815Sopenharmony_ci */
15425bb815Sopenharmony_ci
16425bb815Sopenharmony_ci#include "jerryscript.h"
17425bb815Sopenharmony_ci#include "jerryscript-port.h"
18425bb815Sopenharmony_ci#include "jerryscript-port-default.h"
19425bb815Sopenharmony_ci#include "test-common.h"
20425bb815Sopenharmony_ci#include <gtest/gtest.h>
21425bb815Sopenharmony_ci
22425bb815Sopenharmony_ciclass ApiPropertyTest : public testing::Test{
23425bb815Sopenharmony_cipublic:
24425bb815Sopenharmony_ci    static void SetUpTestCase()
25425bb815Sopenharmony_ci    {
26425bb815Sopenharmony_ci        GTEST_LOG_(INFO) << "ApiPromiseTest SetUpTestCase";
27425bb815Sopenharmony_ci    }
28425bb815Sopenharmony_ci
29425bb815Sopenharmony_ci    static void TearDownTestCase()
30425bb815Sopenharmony_ci    {
31425bb815Sopenharmony_ci        GTEST_LOG_(INFO) << "ApiPromiseTest TearDownTestCase";
32425bb815Sopenharmony_ci    }
33425bb815Sopenharmony_ci
34425bb815Sopenharmony_ci    void SetUp() override {}
35425bb815Sopenharmony_ci    void TearDown() override {}
36425bb815Sopenharmony_ci
37425bb815Sopenharmony_ci};
38425bb815Sopenharmony_ci
39425bb815Sopenharmony_cistatic constexpr size_t JERRY_SCRIPT_MEM_SIZE = 50 * 1024 * 1024;
40425bb815Sopenharmony_cistatic void* context_alloc_fn(size_t size, void* cb_data)
41425bb815Sopenharmony_ci{
42425bb815Sopenharmony_ci    (void)cb_data;
43425bb815Sopenharmony_ci    size_t newSize = size > JERRY_SCRIPT_MEM_SIZE ? JERRY_SCRIPT_MEM_SIZE : size;
44425bb815Sopenharmony_ci    return malloc(newSize);
45425bb815Sopenharmony_ci}
46425bb815Sopenharmony_ciHWTEST_F(ApiPropertyTest, Test001, testing::ext::TestSize.Level1)
47425bb815Sopenharmony_ci{
48425bb815Sopenharmony_ci  jerry_context_t* ctx_p = jerry_create_context(1024 * 1024 * 50, context_alloc_fn, NULL);
49425bb815Sopenharmony_ci  jerry_port_default_set_current_context(ctx_p);
50425bb815Sopenharmony_ci  TEST_INIT ();
51425bb815Sopenharmony_ci  jerry_init (JERRY_INIT_EMPTY);
52425bb815Sopenharmony_ci
53425bb815Sopenharmony_ci  /* Test: init property descriptor */
54425bb815Sopenharmony_ci  jerry_property_descriptor_t prop_desc;
55425bb815Sopenharmony_ci  jerry_init_property_descriptor_fields (&prop_desc);
56425bb815Sopenharmony_ci  TEST_ASSERT (prop_desc.is_value_defined == false);
57425bb815Sopenharmony_ci  TEST_ASSERT (jerry_value_is_undefined (prop_desc.value));
58425bb815Sopenharmony_ci  TEST_ASSERT (prop_desc.is_writable_defined == false);
59425bb815Sopenharmony_ci  TEST_ASSERT (prop_desc.is_writable == false);
60425bb815Sopenharmony_ci  TEST_ASSERT (prop_desc.is_enumerable_defined == false);
61425bb815Sopenharmony_ci  TEST_ASSERT (prop_desc.is_enumerable == false);
62425bb815Sopenharmony_ci  TEST_ASSERT (prop_desc.is_configurable_defined == false);
63425bb815Sopenharmony_ci  TEST_ASSERT (prop_desc.is_configurable == false);
64425bb815Sopenharmony_ci  TEST_ASSERT (prop_desc.is_get_defined == false);
65425bb815Sopenharmony_ci  TEST_ASSERT (jerry_value_is_undefined (prop_desc.getter));
66425bb815Sopenharmony_ci  TEST_ASSERT (prop_desc.is_set_defined == false);
67425bb815Sopenharmony_ci  TEST_ASSERT (jerry_value_is_undefined (prop_desc.setter));
68425bb815Sopenharmony_ci
69425bb815Sopenharmony_ci  /* Test: define own properties */
70425bb815Sopenharmony_ci  jerry_value_t global_obj_val = jerry_get_global_object ();
71425bb815Sopenharmony_ci  jerry_value_t prop_name = jerry_create_string ((const jerry_char_t *) "my_defined_property");
72425bb815Sopenharmony_ci  prop_desc.is_value_defined = true;
73425bb815Sopenharmony_ci  prop_desc.value = jerry_acquire_value (prop_name);
74425bb815Sopenharmony_ci  jerry_value_t res = jerry_define_own_property (global_obj_val, prop_name, &prop_desc);
75425bb815Sopenharmony_ci  TEST_ASSERT (!jerry_value_is_error (res));
76425bb815Sopenharmony_ci  TEST_ASSERT (jerry_value_is_boolean (res));
77425bb815Sopenharmony_ci  TEST_ASSERT (jerry_get_boolean_value (res));
78425bb815Sopenharmony_ci  jerry_release_value (res);
79425bb815Sopenharmony_ci  jerry_free_property_descriptor_fields (&prop_desc);
80425bb815Sopenharmony_ci
81425bb815Sopenharmony_ci  /* Test: get own property descriptor */
82425bb815Sopenharmony_ci  bool is_ok = jerry_get_own_property_descriptor (global_obj_val, prop_name, &prop_desc);
83425bb815Sopenharmony_ci  TEST_ASSERT (is_ok);
84425bb815Sopenharmony_ci  TEST_ASSERT (prop_desc.is_value_defined == true);
85425bb815Sopenharmony_ci  TEST_ASSERT (jerry_value_is_string (prop_desc.value));
86425bb815Sopenharmony_ci  TEST_ASSERT (prop_desc.is_writable == false);
87425bb815Sopenharmony_ci  TEST_ASSERT (prop_desc.is_enumerable == false);
88425bb815Sopenharmony_ci  TEST_ASSERT (prop_desc.is_configurable == false);
89425bb815Sopenharmony_ci  TEST_ASSERT (prop_desc.is_get_defined == false);
90425bb815Sopenharmony_ci  TEST_ASSERT (jerry_value_is_undefined (prop_desc.getter));
91425bb815Sopenharmony_ci  TEST_ASSERT (prop_desc.is_set_defined == false);
92425bb815Sopenharmony_ci  TEST_ASSERT (jerry_value_is_undefined (prop_desc.setter));
93425bb815Sopenharmony_ci  jerry_free_property_descriptor_fields (&prop_desc);
94425bb815Sopenharmony_ci
95425bb815Sopenharmony_ci  if (jerry_is_feature_enabled (JERRY_FEATURE_PROXY))
96425bb815Sopenharmony_ci  {
97425bb815Sopenharmony_ci    /* Note: update this test when the internal method is implemented */
98425bb815Sopenharmony_ci    jerry_value_t target = jerry_create_object ();
99425bb815Sopenharmony_ci    jerry_value_t handler = jerry_create_object ();
100425bb815Sopenharmony_ci    jerry_value_t proxy = jerry_create_proxy (target, handler);
101425bb815Sopenharmony_ci
102425bb815Sopenharmony_ci    jerry_release_value (target);
103425bb815Sopenharmony_ci    jerry_release_value (handler);
104425bb815Sopenharmony_ci    is_ok = jerry_get_own_property_descriptor (proxy, prop_name, &prop_desc);
105425bb815Sopenharmony_ci    TEST_ASSERT (!is_ok);
106425bb815Sopenharmony_ci    jerry_release_value (proxy);
107425bb815Sopenharmony_ci  }
108425bb815Sopenharmony_ci
109425bb815Sopenharmony_ci  jerry_release_value (prop_name);
110425bb815Sopenharmony_ci
111425bb815Sopenharmony_ci  /* Test: define and get own property descriptor */
112425bb815Sopenharmony_ci  prop_desc.is_enumerable = true;
113425bb815Sopenharmony_ci  prop_name = jerry_create_string ((const jerry_char_t *) "enumerable-property");
114425bb815Sopenharmony_ci  res = jerry_define_own_property (global_obj_val, prop_name, &prop_desc);
115425bb815Sopenharmony_ci  TEST_ASSERT (!jerry_value_is_error (res));
116425bb815Sopenharmony_ci  TEST_ASSERT (jerry_value_is_boolean (res));
117425bb815Sopenharmony_ci  TEST_ASSERT (jerry_get_boolean_value (res));
118425bb815Sopenharmony_ci  jerry_release_value (res);
119425bb815Sopenharmony_ci  jerry_free_property_descriptor_fields (&prop_desc);
120425bb815Sopenharmony_ci  is_ok = jerry_get_own_property_descriptor (global_obj_val, prop_name, &prop_desc);
121425bb815Sopenharmony_ci  TEST_ASSERT (is_ok);
122425bb815Sopenharmony_ci  TEST_ASSERT (prop_desc.is_writable == false);
123425bb815Sopenharmony_ci  TEST_ASSERT (prop_desc.is_enumerable == true);
124425bb815Sopenharmony_ci  TEST_ASSERT (prop_desc.is_configurable == false);
125425bb815Sopenharmony_ci
126425bb815Sopenharmony_ci  jerry_release_value (prop_name);
127425bb815Sopenharmony_ci  jerry_release_value (global_obj_val);
128425bb815Sopenharmony_ci  jerry_cleanup ();
129425bb815Sopenharmony_ci  free (ctx_p);
130425bb815Sopenharmony_ci}
131