|
| 1 | +/* |
| 2 | + * MetaCall Library by Parra Studios |
| 3 | + * A library for providing a foreign function interface calls. |
| 4 | + * |
| 5 | + * Copyright (C) 2016 - 2022 Vicente Eduardo Ferrer Garcia <[email protected]> |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + * |
| 19 | + */ |
| 20 | + |
| 21 | +#include <gtest/gtest.h> |
| 22 | + |
| 23 | +#include <metacall/metacall.h> |
| 24 | +#include <metacall/metacall_loaders.h> |
| 25 | + |
| 26 | +#include <filesystem> |
| 27 | + |
| 28 | +namespace fs = std::filesystem; |
| 29 | + |
| 30 | +class await_test : public testing::Test |
| 31 | +{ |
| 32 | +public: |
| 33 | +}; |
| 34 | + |
| 35 | +TEST_F(await_test, DefaultConstructor) |
| 36 | +{ |
| 37 | + metacall_print_info(); |
| 38 | + |
| 39 | + ASSERT_EQ((int)0, (int)metacall_initialize()); |
| 40 | + |
| 41 | + /* Extension */ |
| 42 | + void *handle = metacall_plugin_extension(); |
| 43 | + |
| 44 | + ASSERT_NE((void *)NULL, (void *)handle); |
| 45 | + |
| 46 | + void *args[] = { |
| 47 | + metacall_value_create_string(METACALL_PLUGIN_PATH, sizeof(METACALL_PLUGIN_PATH) - 1), |
| 48 | + metacall_value_create_ptr(&handle) |
| 49 | + }; |
| 50 | + |
| 51 | + void *result = metacallhv_s(handle, "plugin_load_from_path", args, sizeof(args) / sizeof(args[0])); |
| 52 | + |
| 53 | + ASSERT_NE((void *)NULL, (void *)result); |
| 54 | + |
| 55 | + EXPECT_EQ((enum metacall_value_id)METACALL_INT, (enum metacall_value_id)metacall_value_id(result)); |
| 56 | + |
| 57 | + EXPECT_EQ((int)0, (int)metacall_value_to_int(result)); |
| 58 | + |
| 59 | + metacall_value_destroy(args[0]); |
| 60 | + metacall_value_destroy(args[1]); |
| 61 | + metacall_value_destroy(result); |
| 62 | + |
| 63 | +/* NodeJS */ |
| 64 | +#if defined(OPTION_BUILD_LOADERS_NODE) |
| 65 | + { |
| 66 | + /* Get core plugin path and handle in order to load cli plugins */ |
| 67 | + const char *plugin_path = metacall_plugin_path(); |
| 68 | + void *plugin_extension_handle = metacall_plugin_extension(); |
| 69 | + void *cli_plugin_handle = NULL; |
| 70 | + |
| 71 | + if (plugin_path != NULL && plugin_extension_handle != NULL) |
| 72 | + { |
| 73 | + /* Define the cli plugin path as string (core plugin path plus cli) */ |
| 74 | + fs::path plugin_cli_path(plugin_path); |
| 75 | + plugin_cli_path /= "cli"; |
| 76 | + std::string plugin_cli_path_str(plugin_cli_path.string()); |
| 77 | + |
| 78 | + /* Load cli plugins into plugin cli handle */ |
| 79 | + void *args[] = { |
| 80 | + metacall_value_create_string(plugin_cli_path_str.c_str(), plugin_cli_path_str.length()), |
| 81 | + metacall_value_create_ptr(&cli_plugin_handle) |
| 82 | + }; |
| 83 | + |
| 84 | + void *ret = metacallhv_s(plugin_extension_handle, "plugin_load_from_path", args, sizeof(args) / sizeof(args[0])); |
| 85 | + |
| 86 | + if (ret == NULL || (ret != NULL && metacall_value_to_int(ret) != 0)) |
| 87 | + { |
| 88 | + std::cerr << "Failed to load CLI plugins from folder: " << plugin_cli_path_str << std::endl; |
| 89 | + } |
| 90 | + |
| 91 | + metacall_value_destroy(args[0]); |
| 92 | + metacall_value_destroy(args[1]); |
| 93 | + metacall_value_destroy(ret); |
| 94 | + } |
| 95 | + |
| 96 | + void *func = metacall_handle_function(cli_plugin_handle, "await"); |
| 97 | + if (func == NULL) |
| 98 | + std::cerr << "function not in handle\n " << METACALL_PLUGIN_PATH << '\n'; |
| 99 | + void *args[] = { |
| 100 | + metacall_value_create_function(func) |
| 101 | + }; |
| 102 | + void *ret = metacallhv_s(handle, "await__test", args, 1); |
| 103 | + |
| 104 | + EXPECT_NE((void *)NULL, (void *)ret); |
| 105 | + |
| 106 | + EXPECT_EQ((enum metacall_value_id)METACALL_DOUBLE, (enum metacall_value_id)metacall_value_id(ret)); |
| 107 | + |
| 108 | + EXPECT_EQ((double)22, (long)metacall_value_to_double(ret)); |
| 109 | + |
| 110 | + metacall_value_destroy(ret); |
| 111 | + } |
| 112 | +#endif /* OPTION_BUILD_LOADERS_NODE */ |
| 113 | + |
| 114 | + /* Print inspect information */ |
| 115 | + { |
| 116 | + size_t size = 0; |
| 117 | + |
| 118 | + struct metacall_allocator_std_type std_ctx = { &std::malloc, &std::realloc, &std::free }; |
| 119 | + |
| 120 | + void *allocator = metacall_allocator_create(METACALL_ALLOCATOR_STD, (void *)&std_ctx); |
| 121 | + |
| 122 | + char *inspect_str = metacall_inspect(&size, allocator); |
| 123 | + |
| 124 | + EXPECT_NE((char *)NULL, (char *)inspect_str); |
| 125 | + |
| 126 | + EXPECT_GT((size_t)size, (size_t)0); |
| 127 | + |
| 128 | + //std::cout << inspect_str << std::endl; |
| 129 | + |
| 130 | + metacall_allocator_free(allocator, inspect_str); |
| 131 | + |
| 132 | + metacall_allocator_destroy(allocator); |
| 133 | + } |
| 134 | + |
| 135 | + EXPECT_EQ((int)0, (int)metacall_destroy()); |
| 136 | +} |
0 commit comments