Skip to content

Commit

Permalink
Merge pull request #25 from WSUCEG-7140/9-basic-testing-suite
Browse files Browse the repository at this point in the history
Adds Gtest as our unit testing framework.
  • Loading branch information
TonePoems committed Jul 5, 2023
2 parents d0080c2 + 93e1b39 commit 582d420
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ log.txt
*.dll
*.pyc

deps/*
29 changes: 29 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,32 @@ if(MINGW)
target_link_libraries(craft ws2_32.lib glfw
${GLFW_LIBRARIES} ${CURL_LIBRARIES})
endif()

# GTest setup
# do not want main included so it is a valid executable
#MESSAGE(STATUS "${SOURCE_FILES}")
FOREACH(item ${SOURCE_FILES})
IF(${item} MATCHES "src/main.c")
LIST(REMOVE_ITEM SOURCE_FILES ${item})
ENDIF(${item} MATCHES "src/main.c")
ENDFOREACH(item)
#MESSAGE(STATUS "${SOURCE_FILES}")

enable_testing()
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIR})
add_executable(
runTests
tests/tests.cpp
${SOURCE_FILES}
deps/glew/src/glew.c
deps/lodepng/lodepng.c
deps/noise/noise.c
deps/sqlite/sqlite3.c
deps/tinycthread/tinycthread.c)
target_link_libraries(runTests
${GTEST_BOTH_LIBRARIES}
pthread
dl glfw
${GLFW_LIBRARIES}
${CURL_LIBRARIES} )
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ the installation:

#### Linux (Ubuntu)

GTest install and build from dev package:
```bash
sudo apt-get install libgtest-dev # install gtest dev package
cd /usr/src/gtest
sudo cmake CMakeLists.txt
sudo make
# copy or symlink libgtest.a and libgtest_main.a to your /usr/lib folder
sudo cp *.a /usr/lib
```
The rest of the dependencies can be found below.

sudo apt-get install cmake libglew-dev xorg-dev libcurl4-openssl-dev
sudo apt-get build-dep glfw

Expand Down Expand Up @@ -63,6 +74,12 @@ terminal.
make
./craft

### Tests

Follow the TEST structure within `tests/tests.cpp` to add unit tests to your code. It is structured to allow multiple tests under one test suite, so each feature can have multiple tests under it.

Once your tests are written, follow the normal build procedure above and then target the new `./runTests` executable.

### Multiplayer

After many years, craft.michaelfogleman.com has been taken down. See the [Server](#server) section for info on self-hosting.
Expand Down
22 changes: 22 additions & 0 deletions tests/tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <gtest/gtest.h>

extern "C" { // necessary to link to the c code
#include "../src/util.h"
}

// TEST(TestSuiteName, TestName) {
// ... test body ...
// }

TEST(UtilTests, stringWidth) {
ASSERT_EQ(string_width("1"), 3);
ASSERT_EQ(string_width("2"), 6);
ASSERT_EQ(string_width("3"), 6);
}



int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

0 comments on commit 582d420

Please sign in to comment.