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_ci * Empty constructor
23425bb815Sopenharmony_ci */
24425bb815Sopenharmony_cistatic jerry_value_t
25425bb815Sopenharmony_ciconstruct_handler (const jerry_value_t func_obj_val, /**< function object */
26425bb815Sopenharmony_ci                   const jerry_value_t this_val, /**< this arg */
27425bb815Sopenharmony_ci                   const jerry_value_t args_p[], /**< function arguments */
28425bb815Sopenharmony_ci                   const jerry_length_t args_cnt) /**< number of function arguments */
29425bb815Sopenharmony_ci{
30425bb815Sopenharmony_ci  JERRY_UNUSED (func_obj_val);
31425bb815Sopenharmony_ci  JERRY_UNUSED (this_val);
32425bb815Sopenharmony_ci
33425bb815Sopenharmony_ci  TEST_ASSERT (args_cnt == 1);
34425bb815Sopenharmony_ci  TEST_ASSERT (jerry_get_number_value (args_p[0]) == 1.0);
35425bb815Sopenharmony_ci
36425bb815Sopenharmony_ci  return jerry_create_undefined ();
37425bb815Sopenharmony_ci} /* construct_handler */
38425bb815Sopenharmony_ci
39425bb815Sopenharmony_ciclass RegressionTest : public testing::Test{
40425bb815Sopenharmony_cipublic:
41425bb815Sopenharmony_ci    static void SetUpTestCase()
42425bb815Sopenharmony_ci    {
43425bb815Sopenharmony_ci        GTEST_LOG_(INFO) << "RegressionTest SetUpTestCase";
44425bb815Sopenharmony_ci    }
45425bb815Sopenharmony_ci
46425bb815Sopenharmony_ci    static void TearDownTestCase()
47425bb815Sopenharmony_ci    {
48425bb815Sopenharmony_ci        GTEST_LOG_(INFO) << "RegressionTest TearDownTestCase";
49425bb815Sopenharmony_ci    }
50425bb815Sopenharmony_ci
51425bb815Sopenharmony_ci    void SetUp() override {}
52425bb815Sopenharmony_ci    void TearDown() override {}
53425bb815Sopenharmony_ci
54425bb815Sopenharmony_ci};
55425bb815Sopenharmony_ci
56425bb815Sopenharmony_cistatic constexpr size_t JERRY_SCRIPT_MEM_SIZE = 50 * 1024 * 1024;
57425bb815Sopenharmony_cistatic void* context_alloc_fn(size_t size, void* cb_data)
58425bb815Sopenharmony_ci{
59425bb815Sopenharmony_ci    (void)cb_data;
60425bb815Sopenharmony_ci    size_t newSize = size > JERRY_SCRIPT_MEM_SIZE ? JERRY_SCRIPT_MEM_SIZE : size;
61425bb815Sopenharmony_ci    return malloc(newSize);
62425bb815Sopenharmony_ci}
63425bb815Sopenharmony_ci
64425bb815Sopenharmony_ciHWTEST_F(RegressionTest, Test001, testing::ext::TestSize.Level1)
65425bb815Sopenharmony_ci{
66425bb815Sopenharmony_ci
67425bb815Sopenharmony_ci  /* Test JERRY_FEATURE_SYMBOL feature as it is a must-have in ES2015 */
68425bb815Sopenharmony_ci  if (!jerry_is_feature_enabled (JERRY_FEATURE_SYMBOL))
69425bb815Sopenharmony_ci  {
70425bb815Sopenharmony_ci    jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Skipping test, ES2015 support is disabled.\n");
71425bb815Sopenharmony_ci  }
72425bb815Sopenharmony_ci  jerry_context_t *ctx_p = jerry_create_context (1024, context_alloc_fn, NULL);
73425bb815Sopenharmony_ci  jerry_port_default_set_current_context (ctx_p);
74425bb815Sopenharmony_ci  jerry_init (JERRY_INIT_EMPTY);
75425bb815Sopenharmony_ci
76425bb815Sopenharmony_ci  {
77425bb815Sopenharmony_ci    jerry_value_t global_obj_val = jerry_get_global_object ();
78425bb815Sopenharmony_ci
79425bb815Sopenharmony_ci    jerry_value_t function_val = jerry_create_external_function (construct_handler);
80425bb815Sopenharmony_ci    jerry_value_t function_name_val = jerry_create_string ((const jerry_char_t *) "Demo");
81425bb815Sopenharmony_ci    jerry_value_t result_val = jerry_set_property (global_obj_val, function_name_val, function_val);
82425bb815Sopenharmony_ci    TEST_ASSERT (!jerry_value_is_error (result_val));
83425bb815Sopenharmony_ci    TEST_ASSERT (jerry_get_boolean_value (result_val) == true);
84425bb815Sopenharmony_ci    jerry_release_value (result_val);
85425bb815Sopenharmony_ci    jerry_release_value (function_name_val);
86425bb815Sopenharmony_ci    jerry_release_value (global_obj_val);
87425bb815Sopenharmony_ci    jerry_release_value (function_val);
88425bb815Sopenharmony_ci  }
89425bb815Sopenharmony_ci
90425bb815Sopenharmony_ci  {
91425bb815Sopenharmony_ci    static const jerry_char_t test_source[] = TEST_STRING_LITERAL (
92425bb815Sopenharmony_ci        "class Sub1 extends Demo { constructor () { super (1); } };"
93425bb815Sopenharmony_ci        "new Sub1 ()"
94425bb815Sopenharmony_ci    );
95425bb815Sopenharmony_ci
96425bb815Sopenharmony_ci    jerry_value_t parsed_code_val = jerry_parse (NULL,
97425bb815Sopenharmony_ci                                                 0,
98425bb815Sopenharmony_ci                                                 test_source,
99425bb815Sopenharmony_ci                                                 sizeof (test_source) - 1,
100425bb815Sopenharmony_ci                                                 JERRY_PARSE_NO_OPTS);
101425bb815Sopenharmony_ci    TEST_ASSERT (!jerry_value_is_error (parsed_code_val));
102425bb815Sopenharmony_ci
103425bb815Sopenharmony_ci    jerry_value_t result = jerry_run (parsed_code_val);
104425bb815Sopenharmony_ci    TEST_ASSERT (!jerry_value_is_error (result));
105425bb815Sopenharmony_ci
106425bb815Sopenharmony_ci    jerry_release_value (result);
107425bb815Sopenharmony_ci    jerry_release_value (parsed_code_val);
108425bb815Sopenharmony_ci  }
109425bb815Sopenharmony_ci
110425bb815Sopenharmony_ci  {
111425bb815Sopenharmony_ci    static const jerry_char_t test_source[] = TEST_STRING_LITERAL (
112425bb815Sopenharmony_ci      "class Sub2 extends Demo { };"
113425bb815Sopenharmony_ci      "new Sub2 (1)"
114425bb815Sopenharmony_ci    );
115425bb815Sopenharmony_ci
116425bb815Sopenharmony_ci    jerry_value_t parsed_code_val = jerry_parse (NULL,
117425bb815Sopenharmony_ci                                                 0,
118425bb815Sopenharmony_ci                                                 test_source,
119425bb815Sopenharmony_ci                                                 sizeof (test_source) - 1,
120425bb815Sopenharmony_ci                                                 JERRY_PARSE_NO_OPTS);
121425bb815Sopenharmony_ci    TEST_ASSERT (!jerry_value_is_error (parsed_code_val));
122425bb815Sopenharmony_ci
123425bb815Sopenharmony_ci    jerry_value_t result = jerry_run (parsed_code_val);
124425bb815Sopenharmony_ci    TEST_ASSERT (!jerry_value_is_error (result));
125425bb815Sopenharmony_ci
126425bb815Sopenharmony_ci    jerry_release_value (result);
127425bb815Sopenharmony_ci    jerry_release_value (parsed_code_val);
128425bb815Sopenharmony_ci  }
129425bb815Sopenharmony_ci
130425bb815Sopenharmony_ci  jerry_cleanup ();
131425bb815Sopenharmony_ci  free (ctx_p);
132425bb815Sopenharmony_ci}
133