1 /* Copyright JS Foundation and other contributors, http://js.foundation
2 *
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 "jerryscript.h"
17 #include "jerryscript-port.h"
18 #include "jerryscript-port-default.h"
19 #include "test-common.h"
20 #include <gtest/gtest.h>
21
22 class ApiPropertyTest : public testing::Test{
23 public:
SetUpTestCase()24 static void SetUpTestCase()
25 {
26 GTEST_LOG_(INFO) << "ApiPromiseTest SetUpTestCase";
27 }
28
TearDownTestCase()29 static void TearDownTestCase()
30 {
31 GTEST_LOG_(INFO) << "ApiPromiseTest TearDownTestCase";
32 }
33
SetUp()34 void SetUp() override {}
TearDown()35 void TearDown() override {}
36
37 };
38
39 static constexpr size_t JERRY_SCRIPT_MEM_SIZE = 50 * 1024 * 1024;
context_alloc_fn(size_t size,void * cb_data)40 static void* context_alloc_fn(size_t size, void* cb_data)
41 {
42 (void)cb_data;
43 size_t newSize = size > JERRY_SCRIPT_MEM_SIZE ? JERRY_SCRIPT_MEM_SIZE : size;
44 return malloc(newSize);
45 }
46 HWTEST_F(ApiPropertyTest, Test001, testing::ext::TestSize.Level1)
47 {
48 jerry_context_t* ctx_p = jerry_create_context(1024 * 1024 * 50, context_alloc_fn, NULL);
49 jerry_port_default_set_current_context(ctx_p);
50 TEST_INIT ();
51 jerry_init (JERRY_INIT_EMPTY);
52
53 /* Test: init property descriptor */
54 jerry_property_descriptor_t prop_desc;
55 jerry_init_property_descriptor_fields (&prop_desc);
56 TEST_ASSERT (prop_desc.is_value_defined == false);
57 TEST_ASSERT (jerry_value_is_undefined (prop_desc.value));
58 TEST_ASSERT (prop_desc.is_writable_defined == false);
59 TEST_ASSERT (prop_desc.is_writable == false);
60 TEST_ASSERT (prop_desc.is_enumerable_defined == false);
61 TEST_ASSERT (prop_desc.is_enumerable == false);
62 TEST_ASSERT (prop_desc.is_configurable_defined == false);
63 TEST_ASSERT (prop_desc.is_configurable == false);
64 TEST_ASSERT (prop_desc.is_get_defined == false);
65 TEST_ASSERT (jerry_value_is_undefined (prop_desc.getter));
66 TEST_ASSERT (prop_desc.is_set_defined == false);
67 TEST_ASSERT (jerry_value_is_undefined (prop_desc.setter));
68
69 /* Test: define own properties */
70 jerry_value_t global_obj_val = jerry_get_global_object ();
71 jerry_value_t prop_name = jerry_create_string ((const jerry_char_t *) "my_defined_property");
72 prop_desc.is_value_defined = true;
73 prop_desc.value = jerry_acquire_value (prop_name);
74 jerry_value_t res = jerry_define_own_property (global_obj_val, prop_name, &prop_desc);
75 TEST_ASSERT (!jerry_value_is_error (res));
76 TEST_ASSERT (jerry_value_is_boolean (res));
77 TEST_ASSERT (jerry_get_boolean_value (res));
78 jerry_release_value (res);
79 jerry_free_property_descriptor_fields (&prop_desc);
80
81 /* Test: get own property descriptor */
82 bool is_ok = jerry_get_own_property_descriptor (global_obj_val, prop_name, &prop_desc);
83 TEST_ASSERT (is_ok);
84 TEST_ASSERT (prop_desc.is_value_defined == true);
85 TEST_ASSERT (jerry_value_is_string (prop_desc.value));
86 TEST_ASSERT (prop_desc.is_writable == false);
87 TEST_ASSERT (prop_desc.is_enumerable == false);
88 TEST_ASSERT (prop_desc.is_configurable == false);
89 TEST_ASSERT (prop_desc.is_get_defined == false);
90 TEST_ASSERT (jerry_value_is_undefined (prop_desc.getter));
91 TEST_ASSERT (prop_desc.is_set_defined == false);
92 TEST_ASSERT (jerry_value_is_undefined (prop_desc.setter));
93 jerry_free_property_descriptor_fields (&prop_desc);
94
95 if (jerry_is_feature_enabled (JERRY_FEATURE_PROXY))
96 {
97 /* Note: update this test when the internal method is implemented */
98 jerry_value_t target = jerry_create_object ();
99 jerry_value_t handler = jerry_create_object ();
100 jerry_value_t proxy = jerry_create_proxy (target, handler);
101
102 jerry_release_value (target);
103 jerry_release_value (handler);
104 is_ok = jerry_get_own_property_descriptor (proxy, prop_name, &prop_desc);
105 TEST_ASSERT (!is_ok);
106 jerry_release_value (proxy);
107 }
108
109 jerry_release_value (prop_name);
110
111 /* Test: define and get own property descriptor */
112 prop_desc.is_enumerable = true;
113 prop_name = jerry_create_string ((const jerry_char_t *) "enumerable-property");
114 res = jerry_define_own_property (global_obj_val, prop_name, &prop_desc);
115 TEST_ASSERT (!jerry_value_is_error (res));
116 TEST_ASSERT (jerry_value_is_boolean (res));
117 TEST_ASSERT (jerry_get_boolean_value (res));
118 jerry_release_value (res);
119 jerry_free_property_descriptor_fields (&prop_desc);
120 is_ok = jerry_get_own_property_descriptor (global_obj_val, prop_name, &prop_desc);
121 TEST_ASSERT (is_ok);
122 TEST_ASSERT (prop_desc.is_writable == false);
123 TEST_ASSERT (prop_desc.is_enumerable == true);
124 TEST_ASSERT (prop_desc.is_configurable == false);
125
126 jerry_release_value (prop_name);
127 jerry_release_value (global_obj_val);
128 jerry_cleanup ();
129 free (ctx_p);
130 }
131