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

WIP: Add the tests with Criterion (fix #8) #12

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
37 changes: 37 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ cmake_minimum_required (VERSION 2.6 FATAL_ERROR)
# Specify project name
project (a-vim-story C)

# Enable tests
set (tests_enabled 1)

# Set version numbers
set (PROJECT_VERSION_MAJOR 0)
set (PROJECT_VERSION_MINOR 1)
Expand Down Expand Up @@ -53,6 +56,40 @@ set (PROJECT_SOURCES
# Add the maps
add_subdirectory (maps)

# Add tests
if (tests_enabled)

# Find the criterion library for tests
find_library (CRITERION_LIB criterion)

set (TESTS
test/key.c
src/key.c
test/msg.c
src/msg.c
test/interface.c
src/interface.c
src/command.c
src/map.c
src/game.c
src/menu.c
)

add_executable (run_test ${TESTS})
target_link_libraries (run_test ${LIBRARIES} ${CRITERION_LIB})

add_custom_command (
OUTPUT run_test
COMMAND run_test
)

add_custom_target (
check run_test
DEPENDS ${PROJECT_NAME}
COMMENT "Running tests"
)
endif ()

# Add the executable
add_executable (${PROJECT_NAME} ${PROJECT_SOURCES})

Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,17 @@ Inspired by [Vim Adventures](https://vim-adventures.com/) and open sourced.
### Compiling
Assuming you have _libncurses_, _CMake_ and _Make_ installed.

Optionally, you'd require [_Criterion_](https://github.com/Snaipe/Criterion)
for testing. If you don't want that, set `enable_testing` to `0` in
`CMakeLists.txt` in the root directory.

mkdir build && cd build

cmake ..

make
make install # to install (might want to use 'sudo')
make check # to run tests

a-vim-story # to run

Expand Down
4 changes: 4 additions & 0 deletions src/msg.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h> /* for NULL */

#include "datatypes.h"
#include "msg.h"
Expand Down Expand Up @@ -127,5 +128,8 @@ char message[KEY_MAX_SIZE - START][MAX_MSG_SIZE] = {

char * msg_get_keymsg(input_key_t key)
{
if (key - START < 0 || key > KEY_MAX_SIZE) {
return NULL;
}
return message[key - START];
}
19 changes: 19 additions & 0 deletions test/interface.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <criterion/criterion.h>
#include "datatypes.h"
#include "interface.h"

void setup(void)
{
interface_init();
}

void teardown(void)
{
interface_destroy();
}

Test(interface, init, .init = setup, .fini = teardown) {
interface_display_clear();
interface_display_menu(1);
cr_expect(1, "some test");
}
15 changes: 15 additions & 0 deletions test/key.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <criterion/criterion.h>
#include "datatypes.h"
#include "key.h"

Test(key, lock_all) {
key_unlock_all();
key_lock_all();
cr_expect(!key_unlocked('a'), "Key should be locked when all locked");
}

Test(key, unlock_all) {
key_lock_all();
key_unlock_all();
cr_expect(key_unlocked('a'), "Key should be unlocked when all unlocked");
}
17 changes: 17 additions & 0 deletions test/msg.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <criterion/criterion.h>
#include "datatypes.h"
#include "msg.h"

Test(msg, msg_get_keymsg) {
cr_expect(msg_get_keymsg(' ' - 1) == NULL,
"The msg before the first key ' ' should be NULL");

cr_expect(msg_get_keymsg(' ') != NULL,
"The msg for the first key ' ' should be there");

cr_expect(msg_get_keymsg(KEY_MAX_SIZE + 1) == NULL,
"The msg after the last key should be NULL");

cr_expect(msg_get_keymsg(KEY_MAX_SIZE) != NULL,
"The msg for the last key should be there");
}