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 <string.h>
17
18#include "jerryscript.h"
19#include "test-common.h"
20#include "jerryscript-ext/module.h"
21
22int
23main (int argc, char **argv)
24{
25  (void) argc;
26  (void) argv;
27  jerry_char_t buffer[256];
28  jerry_size_t bytes_copied;
29  const jerryx_module_resolver_t *resolver = &jerryx_module_native_resolver;
30  jerry_value_t module_name;
31
32  jerry_init (JERRY_INIT_EMPTY);
33
34  /* Attempt to load a non-existing module. */
35  module_name = jerry_create_string ((jerry_char_t *) "some-unknown-module-name");
36  jerry_value_t module = jerryx_module_resolve (module_name, &resolver, 1);
37  jerry_release_value (module_name);
38
39  TEST_ASSERT (jerry_value_is_error (module));
40
41  /* Retrieve the error message. */
42  module = jerry_get_value_from_error (module, true);
43  jerry_value_t prop_name = jerry_create_string ((const jerry_char_t *) "message");
44  jerry_value_t prop = jerry_get_property (module, prop_name);
45
46  /* Assert that the error message is a string with specific contents. */
47  TEST_ASSERT (jerry_value_is_string (prop));
48
49  bytes_copied = jerry_substring_to_utf8_char_buffer (prop, 0, 254, buffer, 256);
50  buffer[bytes_copied] = 0;
51  TEST_ASSERT (!strcmp ((const char *) buffer, "Module not found"));
52
53  /* Release the error message property name and value. */
54  jerry_release_value (prop);
55  jerry_release_value (prop_name);
56
57  /* Retrieve the moduleName property. */
58  prop_name = jerry_create_string ((const jerry_char_t *) "moduleName");
59  prop = jerry_get_property (module, prop_name);
60
61  /* Assert that the moduleName property is a string containing the requested module name. */
62  TEST_ASSERT (jerry_value_is_string (prop));
63
64  bytes_copied = jerry_substring_to_utf8_char_buffer (prop, 0, 254, buffer, 256);
65  buffer[bytes_copied] = 0;
66  TEST_ASSERT (!strcmp ((const char *) buffer, "some-unknown-module-name"));
67
68  /* Release everything. */
69  jerry_release_value (prop);
70  jerry_release_value (prop_name);
71  jerry_release_value (module);
72
73  return 0;
74} /* main */
75