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
21 static void
test_promise_resolve_success(void)22 test_promise_resolve_success (void)
23 {
24 jerry_value_t my_promise = jerry_create_promise ();
25
26 // A created promise has an undefined promise result by default and a pending state
27 {
28 jerry_value_t promise_result = jerry_get_promise_result (my_promise);
29 TEST_ASSERT (jerry_value_is_undefined (promise_result));
30
31 jerry_promise_state_t promise_state = jerry_get_promise_state (my_promise);
32 TEST_ASSERT (promise_state == JERRY_PROMISE_STATE_PENDING);
33
34 jerry_release_value (promise_result);
35 }
36
37 jerry_value_t resolve_value = jerry_create_object ();
38 {
39 jerry_value_t obj_key = jerry_create_string ((const jerry_char_t *) "key_one");
40 jerry_value_t set_result = jerry_set_property (resolve_value, obj_key, jerry_create_number (3));
41 TEST_ASSERT (jerry_value_is_boolean (set_result) && (jerry_get_boolean_value (set_result) == true));
42 jerry_release_value (set_result);
43 jerry_release_value (obj_key);
44 }
45
46 // A resolved promise should have the result of from the resolve call and a fulfilled state
47 {
48 jerry_value_t resolve_result = jerry_resolve_or_reject_promise (my_promise, resolve_value, true);
49
50 // Release "old" value of resolve.
51 jerry_release_value (resolve_value);
52
53 jerry_value_t promise_result = jerry_get_promise_result (my_promise);
54 {
55 TEST_ASSERT (jerry_value_is_object (promise_result));
56 jerry_value_t obj_key = jerry_create_string ((const jerry_char_t *) "key_one");
57 jerry_value_t get_result = jerry_get_property (promise_result, obj_key);
58 TEST_ASSERT (jerry_value_is_number (get_result));
59 TEST_ASSERT (jerry_get_number_value (get_result) == 3.0);
60
61 jerry_release_value (get_result);
62 jerry_release_value (obj_key);
63 }
64
65 jerry_promise_state_t promise_state = jerry_get_promise_state (my_promise);
66 TEST_ASSERT (promise_state == JERRY_PROMISE_STATE_FULFILLED);
67
68 jerry_release_value (promise_result);
69
70 jerry_release_value (resolve_result);
71 }
72
73 // Resolvind a promise again does not change the result/state
74 {
75 jerry_value_t resolve_result = jerry_resolve_or_reject_promise (my_promise, jerry_create_number (50), false);
76
77 jerry_value_t promise_result = jerry_get_promise_result (my_promise);
78 {
79 TEST_ASSERT (jerry_value_is_object (promise_result));
80 jerry_value_t obj_key = jerry_create_string ((const jerry_char_t *) "key_one");
81 jerry_value_t get_result = jerry_get_property (promise_result, obj_key);
82 TEST_ASSERT (jerry_value_is_number (get_result));
83 TEST_ASSERT (jerry_get_number_value (get_result) == 3.0);
84
85 jerry_release_value (get_result);
86 jerry_release_value (obj_key);
87 }
88
89 jerry_promise_state_t promise_state = jerry_get_promise_state (my_promise);
90 TEST_ASSERT (promise_state == JERRY_PROMISE_STATE_FULFILLED);
91
92 jerry_release_value (promise_result);
93
94 jerry_release_value (resolve_result);
95 }
96
97 jerry_release_value (my_promise);
98 } /* test_promise_resolve_success */
99
100 static void
test_promise_resolve_fail(void)101 test_promise_resolve_fail (void)
102 {
103 jerry_value_t my_promise = jerry_create_promise ();
104
105 // A created promise has an undefined promise result by default and a pending state
106 {
107 jerry_value_t promise_result = jerry_get_promise_result (my_promise);
108 TEST_ASSERT (jerry_value_is_undefined (promise_result));
109
110 jerry_promise_state_t promise_state = jerry_get_promise_state (my_promise);
111 TEST_ASSERT (promise_state == JERRY_PROMISE_STATE_PENDING);
112
113 jerry_release_value (promise_result);
114 }
115
116 // A resolved promise should have the result of from the resolve call and a fulfilled state
117 {
118 jerry_value_t error_value = jerry_create_error (JERRY_ERROR_TYPE, (const jerry_char_t *) "resolve_fail");
119 jerry_value_t error_obj = jerry_get_value_from_error (error_value, true);
120 jerry_value_t resolve_result = jerry_resolve_or_reject_promise (my_promise, error_obj, false);
121 jerry_release_value (error_obj);
122
123 jerry_value_t promise_result = jerry_get_promise_result (my_promise);
124 // The error is not throw that's why it is only an error object.
125 TEST_ASSERT (jerry_value_is_object (promise_result));
126 TEST_ASSERT (jerry_get_error_type (promise_result) == JERRY_ERROR_TYPE);
127
128 jerry_promise_state_t promise_state = jerry_get_promise_state (my_promise);
129 TEST_ASSERT (promise_state == JERRY_PROMISE_STATE_REJECTED);
130
131 jerry_release_value (promise_result);
132
133 jerry_release_value (resolve_result);
134 }
135
136 // Resolvind a promise again does not change the result/state
137 {
138 jerry_value_t resolve_result = jerry_resolve_or_reject_promise (my_promise, jerry_create_number (50), true);
139
140 jerry_value_t promise_result = jerry_get_promise_result (my_promise);
141 TEST_ASSERT (jerry_value_is_object (promise_result));
142 TEST_ASSERT (jerry_get_error_type (promise_result) == JERRY_ERROR_TYPE);
143
144 jerry_promise_state_t promise_state = jerry_get_promise_state (my_promise);
145 TEST_ASSERT (promise_state == JERRY_PROMISE_STATE_REJECTED);
146
147 jerry_release_value (promise_result);
148
149 jerry_release_value (resolve_result);
150 }
151
152 jerry_release_value (my_promise);
153 } /* test_promise_resolve_fail */
154
155 static void
test_promise_from_js(void)156 test_promise_from_js (void)
157 {
158 const jerry_char_t test_source[] = "(new Promise(function(rs, rj) { rs(30); })).then(function(v) { return v + 1; })";
159
160 jerry_value_t parsed_code_val = jerry_parse (NULL,
161 0,
162 test_source,
163 sizeof (test_source) - 1,
164 JERRY_PARSE_NO_OPTS);
165 TEST_ASSERT (!jerry_value_is_error (parsed_code_val));
166
167 jerry_value_t res = jerry_run (parsed_code_val);
168 TEST_ASSERT (jerry_value_is_promise (res));
169
170 TEST_ASSERT (jerry_get_promise_state (res) == JERRY_PROMISE_STATE_PENDING);
171
172 jerry_value_t run_result = jerry_run_all_enqueued_jobs ();
173 TEST_ASSERT (jerry_value_is_undefined (run_result));
174 jerry_release_value (run_result);
175
176 TEST_ASSERT (jerry_get_promise_state (res) == JERRY_PROMISE_STATE_FULFILLED);
177 jerry_value_t promise_result = jerry_get_promise_result (res);
178 TEST_ASSERT (jerry_value_is_number (promise_result));
179 TEST_ASSERT (jerry_get_number_value (promise_result) == 31.0);
180
181 jerry_release_value (promise_result);
182 jerry_release_value (res);
183 jerry_release_value (parsed_code_val);
184 } /* test_promise_from_js */
185
186 int
main(void)187 main (void)
188 {
189 if (!jerry_is_feature_enabled (JERRY_FEATURE_PROMISE))
190 {
191 jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Promise is disabled!\n");
192 return 0;
193 }
194
195 jerry_init (JERRY_INIT_EMPTY);
196
197 test_promise_resolve_fail ();
198 test_promise_resolve_success ();
199
200 test_promise_from_js ();
201
202 jerry_cleanup ();
203
204 return 0;
205 } /* main */
206