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 "config.h"
17 #include "jerryscript.h"
18 #include "jerryscript-port.h"
19 #include "jerryscript-port-default.h"
20 #include "test-common.h"
21 #include <gtest/gtest.h>
22
23 static void
assert_boolean_and_release(jerry_value_t result,bool expected)24 assert_boolean_and_release (jerry_value_t result, bool expected)
25 {
26 TEST_ASSERT (jerry_value_is_boolean (result));
27 TEST_ASSERT (jerry_get_boolean_value (result) == expected);
28 jerry_release_value (result);
29 } /* assert_boolean_and_release */
30
31 class HasPropertyTest : public testing::Test{
32 public:
SetUpTestCase()33 static void SetUpTestCase()
34 {
35 GTEST_LOG_(INFO) << "HasPropertyTest SetUpTestCase";
36 }
37
TearDownTestCase()38 static void TearDownTestCase()
39 {
40 GTEST_LOG_(INFO) << "HasPropertyTest TearDownTestCase";
41 }
42
SetUp()43 void SetUp() override {}
TearDown()44 void TearDown() override {}
45
46 };
47
48 static constexpr size_t JERRY_SCRIPT_MEM_SIZE = 50 * 1024 * 1024;
context_alloc_fn(size_t size,void * cb_data)49 static void* context_alloc_fn(size_t size, void* cb_data)
50 {
51 (void)cb_data;
52 size_t newSize = size > JERRY_SCRIPT_MEM_SIZE ? JERRY_SCRIPT_MEM_SIZE : size;
53 return malloc(newSize);
54 }
55 HWTEST_F(HasPropertyTest, Test001, testing::ext::TestSize.Level1)
56 {
57 jerry_context_t *ctx_p = jerry_create_context (1024, context_alloc_fn, NULL);
58 jerry_port_default_set_current_context (ctx_p);
59 TEST_INIT ();
60
61 jerry_init (JERRY_INIT_EMPTY);
62
63 jerry_value_t object = jerry_create_object ();
64 jerry_value_t prop_name = jerry_create_string_from_utf8 ((jerry_char_t *) "something");
65 jerry_value_t prop_value = jerry_create_boolean (true);
66 jerry_value_t proto_object = jerry_create_object ();
67
68 /* Assert that an empty object does not have the property in question */
69 assert_boolean_and_release (jerry_has_property (object, prop_name), false);
70 assert_boolean_and_release (jerry_has_own_property (object, prop_name), false);
71
72 assert_boolean_and_release (jerry_set_prototype (object, proto_object), true);
73
74 /* If the object has a prototype, that still means it doesn't have the property */
75 assert_boolean_and_release (jerry_has_property (object, prop_name), false);
76 assert_boolean_and_release (jerry_has_own_property (object, prop_name), false);
77
78 assert_boolean_and_release (jerry_set_property (proto_object, prop_name, prop_value), true);
79
80 /* After setting the property on the prototype, it must be there, but not on the object */
81 assert_boolean_and_release (jerry_has_property (object, prop_name), true);
82 assert_boolean_and_release (jerry_has_own_property (object, prop_name), false);
83
84 TEST_ASSERT (jerry_delete_property (proto_object, prop_name));
85 assert_boolean_and_release (jerry_set_property (object, prop_name, prop_value), true);
86
87 /* After relocating the property onto the object, it must be there */
88 assert_boolean_and_release (jerry_has_property (object, prop_name), true);
89 assert_boolean_and_release (jerry_has_own_property (object, prop_name), true);
90
91 jerry_release_value (object);
92 jerry_release_value (prop_name);
93 jerry_release_value (prop_value);
94 jerry_release_value (proto_object);
95
96 jerry_cleanup ();
97
98 free (ctx_p);
99 }
100