Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using CppUTest unit testing framework #82

Merged
merged 7 commits into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions .github/workflows/c-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,17 @@ jobs:
sh conanbuild.sh
cmake .. -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake -DCMAKE_BUILD_TYPE=Release

- name: Build and run build script
- name: Build all targets
run: |
cd c/build
make build
cd ..
./build/build
make all

- name: Build and run tests
- name: Run tests
run: |
cd c/build
make test
./test
cd c
ctest

- name: Build and run example
- name: Run example
run: |
cd c/build
make example
./example
cd c
./build/example
1 change: 1 addition & 0 deletions c/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ build/
CMakeUserPresets.json
docs/
data.h
Testing/
16 changes: 11 additions & 5 deletions c/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,36 @@ add_library(kuliya SHARED kuliya.h)
set_target_properties(kuliya PROPERTIES LINKER_LANGUAGE C)

# Create the test executable
add_executable(test test/test.c)
add_executable(test test/test.cpp)

# Create the example executable
add_executable(example example/main.c)
add_executable(example2 example/main.cpp)


# Link the test and example executables with the "kuliya" library
target_link_libraries(test kuliya)
# Link the example executable with the "kuliya" library
target_link_libraries(example kuliya)
target_link_libraries(example2 kuliya)

# Link the build and test executables with the "helpers" library
target_link_libraries(build helpers)
target_link_libraries(test helpers)

# Find and link the jsmn library
find_package(jsmn REQUIRED)
target_link_libraries(build jsmn::jsmn)

# Link the "helpers" shared library with libunistring
find_package(libunistring REQUIRED)
target_link_libraries(test libunistring::libunistring)
target_link_libraries(build libunistring::libunistring)

# Link test executable with required libraries
find_package(CppUTest REQUIRED)
target_link_libraries(test PRIVATE kuliya helpers cpputest::cpputest libunistring::libunistring)

# Make all targets depend on the 'run_build' target, which in itself depends on the 'build' target
add_dependencies(kuliya run_build)
add_dependencies(test run_build)
add_dependencies(example run_build)
add_dependencies(example2 run_build)
# Print a success message
message("CMakeLists.txt updated successfully.")
1 change: 1 addition & 0 deletions c/CTestTestfile.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_test(KuliyaTests /build/test)
1 change: 1 addition & 0 deletions c/conanfile.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[requires]
jsmn/1.1.0
libunistring/1.1
cpputest/4.0

[generators]
CMakeDeps
Expand Down
32 changes: 0 additions & 32 deletions c/test/test.c

This file was deleted.

33 changes: 33 additions & 0 deletions c/test/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "test.h"

TEST_GROUP(KuliyaSchemaTests){
void setup(){
kuliya_init();
}
void teardown()
{
kuliya_deinit();
}
}
;

TEST(KuliyaSchemaTests, GetExistingSchema)
{
kuliya_schema *res = get_node_by_path("umkb");
CHECK(res != NULL);
CHECK_EQUAL_C_STRING("جامعة محمد خيضر بسكرة", res->name.ar);
CHECK_EQUAL_C_STRING("University of Mohamed Khider Biskra", res->name.en);
CHECK_EQUAL_C_STRING("Université Mohamed Khider Biskra", res->name.fr);
CHECK_EQUAL(UNIVERSITY, res->type);
}

TEST(KuliyaSchemaTests, GetNullForNonExistingSchema)
{
kuliya_schema *res = get_node_by_path("does/not/exist");
CHECK(res == NULL);
}

int main(int argc, char **argv)
{
return RUN_ALL_TESTS(argc, argv);
}
20 changes: 2 additions & 18 deletions c/test/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,9 @@
#include <stdio.h>
#include <assert.h>
#include <kuliya.h>
#include <CppUTest/TestHarness_c.h>
#include <CppUTest/CommandLineTestRunner.h>

#include "helpers/my_string.h"

static int tests_passed = 0;
static int tests_failed = 0;

/* Test runner */
static void test(int (*func)(void), const char *name)
{
int res = func();
if (res == 0)
{
tests_passed++;
}
else
{
tests_failed++;
printf("FAILED: %s\n", name);
}
}

#endif /* __TEST_H__ */