Skip to content

Commit 17fb284

Browse files
committed
Beginnings of c unit testing with base module (with necessary changes to base, i.e. header)
1 parent c6e81a0 commit 17fb284

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+10124
-5
lines changed

.github/workflows/build-debian-multiarch.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ jobs:
144144
install: ${{ env.INSTALL_CMD }}
145145
run: |
146146
echo "\nInstalling wheel\n"
147-
pip3 install --no-index --pre --break-system-packages --find-links /artifacts_new pygame-ce
147+
pip3 install --no-index --pre --break-system-packages --find-links /artifacts_new pygame-ce -Csetup_args=-Dctest=true
148148
echo "\nRunning tests\n"
149149
export SDL_VIDEODRIVER=dummy
150150
export SDL_AUDIODRIVER=disk

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ dist
4848
__pycache__
4949
_headers/*
5050
buildconfig/win_dll_dirs.json
51+
*.log
5152

5253
# cython generated files
5354
src_c/_sdl2/*.c

.pre-commit-config.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ repos:
1515
| ^.*\.svg$
1616
| ^.*\.sfd$
1717
| docs/LGPL.txt
18+
| subprojects/.*
1819
)$
1920
- id: trailing-whitespace
2021
exclude: |
@@ -23,6 +24,7 @@ repos:
2324
| ^.*\.svg$
2425
| ^.*\.sfd$
2526
| docs/LGPL.txt
27+
| subprojects/.*
2628
)$
2729
2830
- repo: https://github.com/astral-sh/ruff-pre-commit
@@ -47,4 +49,5 @@ repos:
4749
| src_c/include/sse2neon.h
4850
| src_c/include/pythoncapi_compat.h
4951
| src_c/pypm.c
52+
| subprojects/.*
5053
)$

ctest/meson.build

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
unity_subproject = subproject('unity')
2+
unity_dependency = unity_subproject.get_variable('unity_dep')
3+
4+
test_sources = files(
5+
['../src_c/base.c',
6+
'test_base.c']
7+
)
8+
9+
inc = include_directories('../src_c')
10+
11+
test = executable('run_ctests',
12+
sources: [
13+
test_sources,
14+
],
15+
include_directories: [inc],
16+
dependencies: [unity_dependency, py_dep, sdl_dep],
17+
install: true,
18+
install_dir: pg_dir
19+
)
20+
21+
# test('test', test,
22+
# should_fail: false)

ctest/test_base.c

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include "base.h"
2+
#include "unity.h"
3+
4+
PyObject *base_module;
5+
6+
void setUp(void) {}
7+
8+
void tearDown(void) {}
9+
10+
/**
11+
* @brief Tests _pg_is_int_tuple when passed a tuple of ints
12+
*/
13+
void test__pg_is_int_tuple_nominal(void) {
14+
PyObject *arg1 = Py_BuildValue("(iii)", 1, 2, 3);
15+
PyObject *arg2 = Py_BuildValue("(iii)", -1, -2, -3);
16+
PyObject *arg3 = Py_BuildValue("(iii)", 1, -2, -3);
17+
18+
TEST_ASSERT_EQUAL(1, _pg_is_int_tuple(arg1));
19+
TEST_ASSERT_EQUAL(1, _pg_is_int_tuple(arg2));
20+
TEST_ASSERT_EQUAL(1, _pg_is_int_tuple(arg3));
21+
}
22+
23+
/**
24+
* @brief Tests _pg_is_int_tuple when passed a tuple of non-numeric values
25+
*/
26+
void test__pg_is_int_tuple_failureModes(void) {
27+
PyObject *arg1 =
28+
Py_BuildValue("(sss)", (char *)"Larry", (char *)"Moe", (char *)"Curly");
29+
PyObject *arg2 = Py_BuildValue("(sss)", (char *)NULL, (char *)NULL,
30+
(char *)NULL); // tuple of None's
31+
PyObject *arg3 = Py_BuildValue("(OOO)", arg1, arg2, arg1);
32+
33+
TEST_ASSERT_EQUAL(0, _pg_is_int_tuple(arg1));
34+
TEST_ASSERT_EQUAL(0, _pg_is_int_tuple(arg2));
35+
TEST_ASSERT_EQUAL(0, _pg_is_int_tuple(arg3));
36+
}
37+
38+
/**
39+
* @brief Tests _pg_is_int_tuple when passed a tuple of floats
40+
*/
41+
void test__pg_is_int_tuple_floats(void) {
42+
PyObject *arg1 = Py_BuildValue("(ddd)", 1.0, 2.0, 3.0);
43+
PyObject *arg2 = Py_BuildValue("(ddd)", -1.1, -2.2, -3.3);
44+
PyObject *arg3 = Py_BuildValue("(ddd)", 1.0, -2.0, -3.1);
45+
46+
TEST_ASSERT_EQUAL(0, _pg_is_int_tuple(arg1));
47+
TEST_ASSERT_EQUAL(0, _pg_is_int_tuple(arg2));
48+
TEST_ASSERT_EQUAL(0, _pg_is_int_tuple(arg3));
49+
}
50+
51+
#define RUN_TEST(TestFunc, TestLineNum) \
52+
{ \
53+
Unity.CurrentTestName = #TestFunc; \
54+
Unity.CurrentTestLineNumber = TestLineNum; \
55+
Unity.NumberOfTests++; \
56+
if (TEST_PROTECT()) { \
57+
setUp(); \
58+
TestFunc(); \
59+
} \
60+
if (TEST_PROTECT()) { \
61+
tearDown(); \
62+
} \
63+
UnityConcludeTest(); \
64+
}
65+
66+
/*=======Test Reset Option=====*/
67+
void resetTest(void) {
68+
tearDown();
69+
setUp();
70+
}
71+
72+
#undef main
73+
/*=======MAIN=====*/
74+
int main(void) {
75+
Py_Initialize();
76+
UnityBegin("test_base.c");
77+
RUN_TEST(test__pg_is_int_tuple_nominal, 17);
78+
RUN_TEST(test__pg_is_int_tuple_failureModes, 31);
79+
RUN_TEST(test__pg_is_int_tuple_floats, 45);
80+
81+
return (UnityEnd());
82+
}

dev.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
]
3333
COVERAGE_ARGS = ["-Csetup-args=-Dcoverage=true"]
3434

35+
CTEST_ARGS = ["-Csetup-args=-Dctest=true"]
36+
3537
# We assume this script works with any pip version above this.
3638
PIP_MIN_VERSION = "23.1"
3739

@@ -213,6 +215,7 @@ def cmd_build(self):
213215
stripped = self.args.get("stripped", False)
214216
sanitize = self.args.get("sanitize")
215217
coverage = self.args.get("coverage", False)
218+
ctest = self.args.get("ctest", False)
216219
if wheel_dir and coverage:
217220
pprint("Cannot pass --wheel and --coverage together", Colors.RED)
218221
sys.exit(1)
@@ -226,6 +229,8 @@ def cmd_build(self):
226229
build_suffix += "-sdl3"
227230
if coverage:
228231
build_suffix += "-cov"
232+
if ctest:
233+
build_suffix += "-ctest"
229234
install_args = [
230235
"--no-build-isolation",
231236
f"-Cbuild-dir=.mesonpy-build{build_suffix}",
@@ -255,12 +260,16 @@ def cmd_build(self):
255260
if coverage:
256261
install_args.extend(COVERAGE_ARGS)
257262

263+
if ctest:
264+
install_args.extend(CTEST_ARGS)
265+
258266
if sanitize:
259267
install_args.append(f"-Csetup-args=-Db_sanitize={sanitize}")
260268

261269
info_str = (
262-
f"with {debug=}, {lax=}, {sdl3=}, {stripped=}, {coverage=} and {sanitize=}"
270+
f"with {debug=}, {lax=}, {sdl3=}, {stripped=}, {coverage=}, {ctest=}, and {sanitize=}"
263271
)
272+
264273
if wheel_dir:
265274
pprint(f"Building wheel at '{wheel_dir}' ({info_str})")
266275
cmd_run(
@@ -416,6 +425,9 @@ def parse_args(self):
416425
"supported if the underlying compiler supports the --coverage argument"
417426
),
418427
)
428+
build_parser.add_argument(
429+
"--ctest", action="store_true", help="Build the C-direct unit tests"
430+
)
419431

420432
# Docs command
421433
docs_parser = subparsers.add_parser("docs", help="Generate docs")

meson.build

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,4 +452,9 @@ if not get_option('stripped')
452452
subdir('buildconfig/stubs')
453453
install_subdir('examples', install_dir: pg_dir, install_tag: 'pg-tag')
454454
# TODO: install headers? not really important though
455+
456+
if get_option('ctest')
457+
subproject('unity')
458+
subdir('ctest')
459+
endif
455460
endif

meson_options.txt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,23 @@ option('midi', type: 'feature', value: 'enabled')
2424
# Controls whether to make a "stripped" pygame install. Enabling this disables
2525
# the bundling of docs/examples/tests/stubs in the wheels.
2626
# The default behaviour is to bundle all of these.
27-
option('stripped', type: 'boolean', value: 'false')
27+
option('stripped', type: 'boolean', value: false)
2828

2929
# Controls whether to compile with -Werror (or its msvc equivalent). The default
3030
# behaviour is to not do this by default
31-
option('error_on_warns', type: 'boolean', value: 'false')
31+
option('error_on_warns', type: 'boolean', value: false)
3232

3333
# Controls whether to error on build if generated docs are missing. Defaults to
3434
# false.
35-
option('error_docs_missing', type: 'boolean', value: 'false')
35+
option('error_docs_missing', type: 'boolean', value: false)
3636

3737
# Controls whether to do a coverage build.
3838
# This argument must be used together with the editable install.
3939
option('coverage', type: 'boolean', value: false)
4040

41+
# Controls whether to do to a C unit test build. Defaults to false.
42+
# If "stripped" is true, this is ignored.
43+
option('ctest', type: 'boolean', value: false)
44+
4145
# Controls whether to use SDL3 instead of SDL2. The default is to use SDL2
4246
option('sdl_api', type: 'integer', min: 2, max: 3, value: 2)

0 commit comments

Comments
 (0)