Skip to content

Commit 7c56258

Browse files
committed
Improved tests, separate https test from metacall python port test.
1 parent 70d0ca7 commit 7c56258

File tree

6 files changed

+258
-35
lines changed

6 files changed

+258
-35
lines changed

source/tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ add_subdirectory(metacall_python_pointer_test)
153153
add_subdirectory(metacall_python_reentrant_test)
154154
add_subdirectory(metacall_python_varargs_test)
155155
add_subdirectory(metacall_python_port_test)
156+
add_subdirectory(metacall_python_port_https_test)
156157
add_subdirectory(metacall_map_test)
157158
add_subdirectory(metacall_map_await_test)
158159
add_subdirectory(metacall_initialize_test)
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
# Check if loaders are enabled
2+
if(NOT OPTION_BUILD_LOADERS OR NOT OPTION_BUILD_LOADERS_PY OR NOT OPTION_BUILD_PORTS OR NOT OPTION_BUILD_PORTS_PY)
3+
return()
4+
endif()
5+
6+
#
7+
# Executable name and options
8+
#
9+
10+
# Target name
11+
set(target metacall-python-port-https-test)
12+
message(STATUS "Test ${target}")
13+
14+
#
15+
# Compiler warnings
16+
#
17+
18+
include(Warnings)
19+
20+
#
21+
# Compiler security
22+
#
23+
24+
include(SecurityFlags)
25+
26+
#
27+
# Sources
28+
#
29+
30+
set(include_path "${CMAKE_CURRENT_SOURCE_DIR}/include/${target}")
31+
set(source_path "${CMAKE_CURRENT_SOURCE_DIR}/source")
32+
33+
set(sources
34+
${source_path}/main.cpp
35+
${source_path}/metacall_python_port_https_test.cpp
36+
)
37+
38+
# Group source files
39+
set(header_group "Header Files (API)")
40+
set(source_group "Source Files")
41+
source_group_by_path(${include_path} "\\\\.h$|\\\\.hpp$"
42+
${header_group} ${headers})
43+
source_group_by_path(${source_path} "\\\\.cpp$|\\\\.c$|\\\\.h$|\\\\.hpp$"
44+
${source_group} ${sources})
45+
46+
#
47+
# Create executable
48+
#
49+
50+
# Build executable
51+
add_executable(${target}
52+
${sources}
53+
)
54+
55+
# Create namespaced alias
56+
add_executable(${META_PROJECT_NAME}::${target} ALIAS ${target})
57+
58+
#
59+
# Project options
60+
#
61+
62+
set_target_properties(${target}
63+
PROPERTIES
64+
${DEFAULT_PROJECT_OPTIONS}
65+
FOLDER "${IDE_FOLDER}"
66+
)
67+
68+
#
69+
# Include directories
70+
#
71+
72+
target_include_directories(${target}
73+
PRIVATE
74+
${DEFAULT_INCLUDE_DIRECTORIES}
75+
${PROJECT_BINARY_DIR}/source/include
76+
)
77+
78+
#
79+
# Libraries
80+
#
81+
82+
target_link_libraries(${target}
83+
PRIVATE
84+
${DEFAULT_LIBRARIES}
85+
86+
GTest
87+
88+
${META_PROJECT_NAME}::metacall_distributable
89+
)
90+
91+
#
92+
# Compile definitions
93+
#
94+
95+
target_compile_definitions(${target}
96+
PRIVATE
97+
${DEFAULT_COMPILE_DEFINITIONS}
98+
99+
# Python Port path
100+
METACALL_PYTHON_PORT_PATH="${CMAKE_SOURCE_DIR}/source/ports/py_port"
101+
)
102+
103+
#
104+
# Compile options
105+
#
106+
107+
target_compile_options(${target}
108+
PRIVATE
109+
${DEFAULT_COMPILE_OPTIONS}
110+
)
111+
112+
#
113+
# Linker options
114+
#
115+
116+
target_link_libraries(${target}
117+
PRIVATE
118+
${DEFAULT_LINKER_OPTIONS}
119+
)
120+
121+
#
122+
# Define test
123+
#
124+
125+
add_test(NAME ${target}
126+
COMMAND $<TARGET_FILE:${target}>
127+
)
128+
129+
#
130+
# Define dependencies
131+
#
132+
133+
add_dependencies(${target}
134+
py_loader
135+
rb_loader
136+
node_loader
137+
)
138+
139+
#
140+
# Define test properties
141+
#
142+
143+
set_property(TEST ${target}
144+
PROPERTY LABELS ${target}
145+
)
146+
147+
include(TestEnvironmentVariables)
148+
149+
test_environment_variables(${target}
150+
""
151+
${TESTS_ENVIRONMENT_VARIABLES}
152+
)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* MetaCall Library by Parra Studios
3+
* A library for providing a foreign function interface calls.
4+
*
5+
* Copyright (C) 2016 - 2020 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 <gmock/gmock.h>
22+
23+
int main(int argc, char * argv[])
24+
{
25+
::testing::InitGoogleMock(&argc, argv);
26+
27+
return RUN_ALL_TESTS();
28+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* MetaCall Library by Parra Studios
3+
* A library for providing a foreign function interface calls.
4+
*
5+
* Copyright (C) 2016 - 2020 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 <gmock/gmock.h>
22+
23+
#include <metacall/metacall.h>
24+
#include <metacall/metacall_value.h>
25+
#include <metacall/metacall_loaders.h>
26+
27+
class metacall_python_port_https_test : public testing::Test
28+
{
29+
public:
30+
};
31+
32+
TEST_F(metacall_python_port_https_test, DefaultConstructor)
33+
{
34+
metacall_print_info();
35+
36+
ASSERT_EQ((int) 0, (int) metacall_initialize());
37+
38+
/* Python */
39+
#if defined(OPTION_BUILD_LOADERS_PY)
40+
{
41+
// Test import bug (__metacall_import__() missing 1 required positional argument: 'name')
42+
static const char buffer[] =
43+
"import sys\n"
44+
"sys.path.insert(0, '" METACALL_PYTHON_PORT_PATH "')\n"
45+
"import metacall\n"
46+
"from http import client\n"
47+
"def fetch_http_py(url: str):\n"
48+
" try:\n"
49+
" conn = client.HTTPSConnection(url, 443)\n"
50+
" conn.request('GET', '/')\n"
51+
" response = conn.getresponse()\n"
52+
" data = response.read()\n"
53+
" conn.close()\n"
54+
" return data\n"
55+
" except Exception as e:\n"
56+
" print(e)\n"
57+
" sys.stdout.flush()\n"
58+
" return b'<!doctype invalid>'\n";
59+
60+
ASSERT_EQ((int) 0, (int) metacall_load_from_memory("py", buffer, sizeof(buffer), NULL));
61+
62+
void * ret = metacall("fetch_http_py", "www.google.com");
63+
64+
static const char prefix[] = "<!doctype html>";
65+
66+
ASSERT_EQ((enum metacall_value_id) METACALL_BUFFER, (enum metacall_value_id) metacall_value_id(ret));
67+
68+
EXPECT_EQ((int) 0, (int) strncmp((const char *)metacall_value_to_buffer(ret), prefix, sizeof(prefix) - 1));
69+
70+
metacall_value_destroy(ret);
71+
}
72+
#endif /* OPTION_BUILD_LOADERS_PY */
73+
74+
EXPECT_EQ((int) 0, (int) metacall_destroy());
75+
}

source/tests/metacall_python_port_test/CMakeLists.txt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Check if loaders are enabled
2-
if(NOT OPTION_BUILD_LOADERS OR NOT OPTION_BUILD_LOADERS_PY OR NOT OPTION_BUILD_LOADERS_RB OR NOT OPTION_BUILD_LOADERS_NODE OR NOT OPTION_BUILD_SCRIPTS OR NOT OPTION_BUILD_SCRIPTS_PY OR NOT OPTION_BUILD_SCRIPTS_RB OR NOT OPTION_BUILD_SCRIPTS_NODE)
2+
if(NOT OPTION_BUILD_LOADERS OR NOT OPTION_BUILD_LOADERS_PY OR NOT OPTION_BUILD_LOADERS_RB OR NOT OPTION_BUILD_LOADERS_NODE OR NOT OPTION_BUILD_PORTS OR NOT OPTION_BUILD_PORTS_PY OR NOT OPTION_BUILD_SCRIPTS OR NOT OPTION_BUILD_SCRIPTS_PY OR NOT OPTION_BUILD_SCRIPTS_RB OR NOT OPTION_BUILD_SCRIPTS_NODE)
33
return()
44
endif()
55

@@ -96,10 +96,7 @@ target_compile_definitions(${target}
9696
PRIVATE
9797
${DEFAULT_COMPILE_DEFINITIONS}
9898

99-
# NodeJS Port path
100-
METACALL_PYTHON_PORT_PATH="${CMAKE_SOURCE_DIR}/source/ports/py_port"
101-
102-
# NodeJS Port Test path
99+
# Python Port Test path
103100
METACALL_PYTHON_PORT_TEST_PATH="${CMAKE_SOURCE_DIR}/source/ports/py_port/run_tests.py"
104101
)
105102

source/tests/metacall_python_port_test/source/metacall_python_port_test.cpp

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ TEST_F(metacall_python_port_test, DefaultConstructor)
4040
/* Python */
4141
#if defined(OPTION_BUILD_LOADERS_PY)
4242
{
43-
// Run port test
4443
static const char * py_scripts[] =
4544
{
4645
METACALL_PYTHON_PORT_TEST_PATH
@@ -53,35 +52,6 @@ TEST_F(metacall_python_port_test, DefaultConstructor)
5352
EXPECT_EQ((int) 0, (int) strcmp(metacall_value_to_string(ret), "Tests passed without errors"));
5453

5554
metacall_value_destroy(ret);
56-
57-
// Test import bug (__metacall_import__() missing 1 required positional argument: 'name')
58-
static const char buffer[] =
59-
"import sys\n"
60-
"from http import client\n"
61-
"def fetch_http_py(url: str):\n"
62-
" try:\n"
63-
" conn = client.HTTPSConnection(url, 443)\n"
64-
" conn.request('GET', '/')\n"
65-
" response = conn.getresponse()\n"
66-
" data = response.read()\n"
67-
" conn.close()\n"
68-
" return data\n"
69-
" except Exception as e:\n"
70-
" print(e)\n"
71-
" sys.stdout.flush()\n"
72-
" return b'<!doctype invalid>'\n";
73-
74-
ASSERT_EQ((int) 0, (int) metacall_load_from_memory("py", buffer, sizeof(buffer), NULL));
75-
76-
ret = metacall("fetch_http_py", "www.google.com");
77-
78-
static const char prefix[] = "<!doctype html>";
79-
80-
ASSERT_EQ((enum metacall_value_id) METACALL_BUFFER, (enum metacall_value_id) metacall_value_id(ret));
81-
82-
EXPECT_EQ((int) 0, (int) strncmp((const char *)metacall_value_to_buffer(ret), prefix, sizeof(prefix) - 1));
83-
84-
metacall_value_destroy(ret);
8555
}
8656
#endif /* OPTION_BUILD_LOADERS_PY */
8757

0 commit comments

Comments
 (0)