Skip to content

Commit

Permalink
added working unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Stateford committed Feb 11, 2023
1 parent 477a767 commit 0451e96
Show file tree
Hide file tree
Showing 19 changed files with 190 additions and 309 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ cmake_minimum_required(VERSION 3.24)
project(Display-Lock)

add_subdirectory(src)
target_include_directories(${PROJECT_NAME} PRIVATE include)

add_subdirectory(tests)
33 changes: 22 additions & 11 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"name": "windows",
"displayName": "Windows Config",
"hidden": true,
"description": "Sets Ninja generator, build and install directory",
"description": "Sets MSVC generator, build and install directory",
"generator": "Visual Studio 17 2022",
"condition": {
"type": "equals",
Expand All @@ -33,40 +33,51 @@
},
{
"name": "windows-debug",
"inherits": ["default", "windows"],
"inherits": [
"default",
"windows"
],
"displayName": "Windows Debug",
"description": "Sets Ninja generator, build and install directory",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug"
}
},
{
"name": "windows-release",
"inherits": ["default", "windows"],
"inherits": [
"default",
"windows"
],
"displayName": "Windows Release",
"description": "Sets Ninja generator, build and install directory",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release"
}
}
],
"buildPresets": [
{
"name": "windows-debug",
"configurePreset": "windows-debug"
"name": "Debug",
"configurePreset": "windows-debug",
"configuration": "Debug"
},
{
"name": "windows-release",
"configurePreset": "windows-release"
"name": "Release",
"configurePreset": "windows-release",
"configuration": "Release"
}
],
"testPresets": [
{
"name": "default",
"hidden": true,
"configurePreset": "default",
"output": {"outputOnFailure": true},
"execution": {"noTestsAction": "error", "stopOnFailure": true}
"output": {
"outputOnFailure": true
},
"execution": {
"noTestsAction": "error",
"stopOnFailure": true
}
},
{
"name": "windows-debug",
Expand Down
2 changes: 1 addition & 1 deletion include/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#pragma once

#include <Windows.h>
#include "resources/resource.h"
#include "resource.h"

#define WINDOW_VIEW 0
#define SETTINGS_VIEW 1
Expand Down
8 changes: 4 additions & 4 deletions include/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@

void initalizeSettings(HWND hDlg, SETTINGS_VIEW_CONTROLS *settingsControls);
void setSettingsDlg(HWND hDlg, SETTINGS settings);
void defaultSettings(SETTINGS *settings, wchar_t *versionStr);
BOOL checkVersion(SETTINGS *settings, wchar_t *versionStr);
void defaultSettings(SETTINGS *settings, const wchar_t *versionStr);
BOOL checkVersion(SETTINGS *settings, const wchar_t *versionStr);
BOOL findPath(wchar_t *path);
BOOL createDirectory(wchar_t *outPath);
BOOL readSettings(SETTINGS *settings, wchar_t *versionStr, wchar_t *path);
BOOL writeSettings(SETTINGS settings, wchar_t *path);
BOOL readSettings(SETTINGS *settings, const wchar_t *versionStr, const wchar_t *path);
BOOL writeSettings(SETTINGS settings, const wchar_t *path);
14 changes: 5 additions & 9 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@

enable_language("RC")

add_subdirectory(components)

add_executable(${PROJECT_NAME} WIN32
DisplayLock.c
main.c
ui.c
menu.c
notify.c
settings.c
update.c
win.c
applications.c
)
target_link_libraries(${PROJECT_NAME} PRIVATE version Shlwapi winhttp iphlpapi)
target_compile_definitions(${PROJECT_NAME} PUBLIC -DUNICODE)
target_link_libraries(${PROJECT_NAME} PUBLIC display_lock_components)
target_include_directories(display_lock_components PUBLIC resources)

add_subdirectory(resources)
13 changes: 13 additions & 0 deletions src/components/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
project(display_lock_components)

add_library(${PROJECT_NAME} STATIC
menu.c
notify.c
settings.c
update.c
win.c
applications.c
)
target_compile_definitions(${PROJECT_NAME} PUBLIC -DUNICODE)
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_SOURCE_DIR}/include)
target_link_libraries(${PROJECT_NAME} PUBLIC version Shlwapi winhttp iphlpapi)
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 4 additions & 4 deletions src/settings.c → src/components/settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void setSettingsDlg(HWND hDlg, SETTINGS settings)
return;
}

void defaultSettings(SETTINGS *settings, wchar_t *versionStr)
void defaultSettings(SETTINGS *settings, const wchar_t *versionStr)
{
strcpy(settings->header, "DLOCK");
settings->version = 0;
Expand All @@ -54,7 +54,7 @@ void defaultSettings(SETTINGS *settings, wchar_t *versionStr)
settings->checkUpdateStartup = TRUE;
}

BOOL checkVersion(SETTINGS *settings, wchar_t *versionStr)
BOOL checkVersion(SETTINGS *settings, const wchar_t *versionStr)
{
if (strcmp(settings->header, "DLOCK") != 0 || wcslen(versionStr) == 0)
return FALSE;
Expand Down Expand Up @@ -108,7 +108,7 @@ BOOL createDirectory(wchar_t *outPath)
return TRUE;
}

BOOL readSettings(SETTINGS *settings, wchar_t *versionStr, wchar_t *path)
BOOL readSettings(SETTINGS *settings, const wchar_t *versionStr, const wchar_t *path)
{
FILE *file = _wfopen(path, TEXT("rb"));

Expand All @@ -131,7 +131,7 @@ BOOL readSettings(SETTINGS *settings, wchar_t *versionStr, wchar_t *path)
return TRUE;
}

BOOL writeSettings(SETTINGS settings, wchar_t *path)
BOOL writeSettings(SETTINGS settings, const wchar_t *path)
{
// if loadstring could not be read, do not write the file
if (settings.version <= 0 || strcmp(settings.header, "DLOCK") != 0)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
42 changes: 42 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,44 @@
project(display-lock-unittests)

list(APPEND CMAKE_MODULE_PATH ${CMAKE_BINARY_DIR}/tests)
list(APPEND CMAKE_PREFIX_PATH ${CMAKE_BINARY_DIR}/tests)

message(STATUS "BINARY PATH ${CMAKE_BINARY_DIR}")

if(NOT EXISTS "${CMAKE_BINARY_DIR}/conan.cmake")
message(STATUS "Downloading conan.cmake from https://github.com/conan-io/cmake-conan")
file(DOWNLOAD "https://raw.githubusercontent.com/conan-io/cmake-conan/0.18.1/conan.cmake"
"${CMAKE_BINARY_DIR}/conan.cmake"
TLS_VERIFY ON)
endif()

include(${CMAKE_BINARY_DIR}/conan.cmake)

conan_cmake_configure(
REQUIRES gtest/1.13.0
GENERATORS cmake_find_package
)

conan_cmake_autodetect(settings)

conan_cmake_install(
PATH_OR_REFERENCE .
BUILD missing
REMOTE conancenter
SETTINGS ${settings}
)

enable_testing()

find_package(GTest REQUIRED)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_executable(${PROJECT_NAME} test_settings.cpp)
target_link_libraries(${PROJECT_NAME} PRIVATE GTest::gtest_main display_lock_components)
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/include ${CMAKE_SOURCE_DIR}/src/resources)

add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
${CMAKE_CURRENT_SOURCE_DIR}/test_resources $<TARGET_FILE_DIR:${PROJECT_NAME}>)
File renamed without changes.
File renamed without changes.
97 changes: 97 additions & 0 deletions tests/test_settings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#include "gtest/gtest.h"

extern "C" {
#include "settings.h"
}

// #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
// #include <Windows.h>
#include <iostream>
#include <filesystem>
#include <source_location>


namespace {

// settings.h
TEST(SettingsTest, DefaultSettings)
{
SETTINGS settings;
defaultSettings(&settings, L"5");

// It's impossible to test the default version, as it's determined at run time
ASSERT_STREQ(settings.header, "DLOCK");
ASSERT_EQ(settings.version, 5);
ASSERT_EQ(settings.borderless, false);
ASSERT_EQ(settings.foreground, false);
ASSERT_EQ(settings.fullScreen, false);
ASSERT_EQ(settings.minimize, true);
}

TEST(SettingsTest, CheckVersion)
{
SETTINGS settings;

defaultSettings(&settings, L"5");
ASSERT_EQ(checkVersion(&settings, L"5"), true);
ASSERT_EQ(checkVersion(&settings, L"0005"), true);
ASSERT_EQ(checkVersion(&settings, L"15"), false);
ASSERT_EQ(checkVersion(&settings, L"A"), false);
ASSERT_EQ(checkVersion(&settings, L"FOO"), false);
ASSERT_EQ(checkVersion(&settings, L"F5"), false);

strcpy_s(settings.header, 6, "5A!");
ASSERT_EQ(checkVersion(&settings, L"5"), false);
strcpy_s(settings.header, 6, "FOO");
ASSERT_EQ(checkVersion(&settings, L"5"), false);
strcpy_s(settings.header, 6, "");
ASSERT_EQ(checkVersion(&settings, L"5"), false);
}

TEST(SettingsTest, FindPath)
{
wchar_t path[MAX_PATH];
BOOL res = findPath(path);

ASSERT_EQ(res, true);
}

TEST(SettingsTest, ReadSettings)
{
SETTINGS settings = { 0 };
BOOL result;

result = readSettings(&settings, L"5", L"test1.DLOCK");
ASSERT_EQ(result, true);
ASSERT_EQ(strcmp(settings.header, "DLOCK"), 0);
ASSERT_EQ(settings.version, 5);
ASSERT_EQ(settings.minimize, false);
ASSERT_EQ(settings.foreground, false);
ASSERT_EQ(settings.borderless, false);
ASSERT_EQ(settings.fullScreen, false);
}

TEST(SettingsTest, WriteSettings)
{
BOOL result;

SETTINGS settings = {"DLOCK", 5, true, true, false, false};

result = writeSettings(settings, L"output.DLOCK");
ASSERT_EQ(result, true);

settings.version = 0;

result = writeSettings(settings, L"output.DLOCK");
ASSERT_EQ(result, false);

settings.version = 5;

strcpy_s(settings.header, 6, "FOOBA");

result = writeSettings(settings, L".\\output.DLOCK");
ASSERT_EQ(result, false);

DeleteFileW(L"output.DLOCK");
}
}
Loading

0 comments on commit 0451e96

Please sign in to comment.