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 "config.h" 17425bb815Sopenharmony_ci#include "jerryscript.h" 18425bb815Sopenharmony_ci#include "jerryscript-port.h" 19425bb815Sopenharmony_ci#include "jerryscript-port-default.h" 20425bb815Sopenharmony_ci#include "test-common.h" 21425bb815Sopenharmony_ci#include <gtest/gtest.h> 22425bb815Sopenharmony_ci 23425bb815Sopenharmony_cistatic void 24425bb815Sopenharmony_ciassert_boolean_and_release (jerry_value_t result, bool expected) 25425bb815Sopenharmony_ci{ 26425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_boolean (result)); 27425bb815Sopenharmony_ci TEST_ASSERT (jerry_get_boolean_value (result) == expected); 28425bb815Sopenharmony_ci jerry_release_value (result); 29425bb815Sopenharmony_ci} /* assert_boolean_and_release */ 30425bb815Sopenharmony_ci 31425bb815Sopenharmony_ciclass HasPropertyTest : public testing::Test{ 32425bb815Sopenharmony_cipublic: 33425bb815Sopenharmony_ci static void SetUpTestCase() 34425bb815Sopenharmony_ci { 35425bb815Sopenharmony_ci GTEST_LOG_(INFO) << "HasPropertyTest SetUpTestCase"; 36425bb815Sopenharmony_ci } 37425bb815Sopenharmony_ci 38425bb815Sopenharmony_ci static void TearDownTestCase() 39425bb815Sopenharmony_ci { 40425bb815Sopenharmony_ci GTEST_LOG_(INFO) << "HasPropertyTest TearDownTestCase"; 41425bb815Sopenharmony_ci } 42425bb815Sopenharmony_ci 43425bb815Sopenharmony_ci void SetUp() override {} 44425bb815Sopenharmony_ci void TearDown() override {} 45425bb815Sopenharmony_ci 46425bb815Sopenharmony_ci}; 47425bb815Sopenharmony_ci 48425bb815Sopenharmony_cistatic constexpr size_t JERRY_SCRIPT_MEM_SIZE = 50 * 1024 * 1024; 49425bb815Sopenharmony_cistatic void* context_alloc_fn(size_t size, void* cb_data) 50425bb815Sopenharmony_ci{ 51425bb815Sopenharmony_ci (void)cb_data; 52425bb815Sopenharmony_ci size_t newSize = size > JERRY_SCRIPT_MEM_SIZE ? JERRY_SCRIPT_MEM_SIZE : size; 53425bb815Sopenharmony_ci return malloc(newSize); 54425bb815Sopenharmony_ci} 55425bb815Sopenharmony_ciHWTEST_F(HasPropertyTest, Test001, testing::ext::TestSize.Level1) 56425bb815Sopenharmony_ci{ 57425bb815Sopenharmony_ci jerry_context_t *ctx_p = jerry_create_context (1024, context_alloc_fn, NULL); 58425bb815Sopenharmony_ci jerry_port_default_set_current_context (ctx_p); 59425bb815Sopenharmony_ci TEST_INIT (); 60425bb815Sopenharmony_ci 61425bb815Sopenharmony_ci jerry_init (JERRY_INIT_EMPTY); 62425bb815Sopenharmony_ci 63425bb815Sopenharmony_ci jerry_value_t object = jerry_create_object (); 64425bb815Sopenharmony_ci jerry_value_t prop_name = jerry_create_string_from_utf8 ((jerry_char_t *) "something"); 65425bb815Sopenharmony_ci jerry_value_t prop_value = jerry_create_boolean (true); 66425bb815Sopenharmony_ci jerry_value_t proto_object = jerry_create_object (); 67425bb815Sopenharmony_ci 68425bb815Sopenharmony_ci /* Assert that an empty object does not have the property in question */ 69425bb815Sopenharmony_ci assert_boolean_and_release (jerry_has_property (object, prop_name), false); 70425bb815Sopenharmony_ci assert_boolean_and_release (jerry_has_own_property (object, prop_name), false); 71425bb815Sopenharmony_ci 72425bb815Sopenharmony_ci assert_boolean_and_release (jerry_set_prototype (object, proto_object), true); 73425bb815Sopenharmony_ci 74425bb815Sopenharmony_ci /* If the object has a prototype, that still means it doesn't have the property */ 75425bb815Sopenharmony_ci assert_boolean_and_release (jerry_has_property (object, prop_name), false); 76425bb815Sopenharmony_ci assert_boolean_and_release (jerry_has_own_property (object, prop_name), false); 77425bb815Sopenharmony_ci 78425bb815Sopenharmony_ci assert_boolean_and_release (jerry_set_property (proto_object, prop_name, prop_value), true); 79425bb815Sopenharmony_ci 80425bb815Sopenharmony_ci /* After setting the property on the prototype, it must be there, but not on the object */ 81425bb815Sopenharmony_ci assert_boolean_and_release (jerry_has_property (object, prop_name), true); 82425bb815Sopenharmony_ci assert_boolean_and_release (jerry_has_own_property (object, prop_name), false); 83425bb815Sopenharmony_ci 84425bb815Sopenharmony_ci TEST_ASSERT (jerry_delete_property (proto_object, prop_name)); 85425bb815Sopenharmony_ci assert_boolean_and_release (jerry_set_property (object, prop_name, prop_value), true); 86425bb815Sopenharmony_ci 87425bb815Sopenharmony_ci /* After relocating the property onto the object, it must be there */ 88425bb815Sopenharmony_ci assert_boolean_and_release (jerry_has_property (object, prop_name), true); 89425bb815Sopenharmony_ci assert_boolean_and_release (jerry_has_own_property (object, prop_name), true); 90425bb815Sopenharmony_ci 91425bb815Sopenharmony_ci jerry_release_value (object); 92425bb815Sopenharmony_ci jerry_release_value (prop_name); 93425bb815Sopenharmony_ci jerry_release_value (prop_value); 94425bb815Sopenharmony_ci jerry_release_value (proto_object); 95425bb815Sopenharmony_ci 96425bb815Sopenharmony_ci jerry_cleanup (); 97425bb815Sopenharmony_ci 98425bb815Sopenharmony_ci free (ctx_p); 99425bb815Sopenharmony_ci} 100