feat: add mock application for testing

This commit is contained in:
45Tatami 2023-05-23 18:23:17 +02:00
parent ad5bd38f9d
commit a42b6eb449
5 changed files with 92 additions and 2 deletions

View File

@ -29,3 +29,12 @@ meson setup --cross-file=cross/i686-w64-mingw32.txt build
# x86_64/64bit # x86_64/64bit
meson setup --cross-file=cross/x86_64-w64-mingw32.txt build meson setup --cross-file=cross/x86_64-w64-mingw32.txt build
``` ```
## Mock Application
A simple application for loading the built dll and calling the usual entrypoint
with some sample text can optionally be built and started via meson.
meson configure build -Dbuild_mock=enabled
ninja -Cbuild run_mock
# build/mock/mock_textractor.exe <path to dll>

View File

@ -1,5 +1,6 @@
project('Textractor-TCPSender', 'cpp', project('Textractor-TCPSender', 'cpp',
default_options : ['cpp_std=c++17']) meson_version : '>=0.56.0',
default_options : ['cpp_std=c++17', 'warning_level=3'])
add_project_arguments('-DUNICODE', language : 'cpp') add_project_arguments('-DUNICODE', language : 'cpp')
@ -16,5 +17,10 @@ src += windows.compile_resources('TCPSender/resource.rc')
deps = [] deps = []
deps += compiler.find_library('ws2_32') deps += compiler.find_library('ws2_32')
library('tcpsender', src, tcpsender = library('TCPSender', src,
name_prefix : '', name_suffix : 'xdll',
dependencies : deps) dependencies : deps)
if get_option('build_mock').enabled()
subdir('mock')
endif

2
meson_options.txt Normal file
View File

@ -0,0 +1,2 @@
option('build_mock', type : 'feature', value : 'disabled',
description : 'build a mock application using this Textractor extension')

6
mock/meson.build Normal file
View File

@ -0,0 +1,6 @@
mock_exe = executable('mock_textractor', 'mock.cpp',
include_directories : '../TCPSender',
win_subsystem : 'windows')
run_target('run_mock', command : [mock_exe, tcpsender.full_path()],
depends : tcpsender)

67
mock/mock.cpp Normal file
View File

@ -0,0 +1,67 @@
#include "Extension.h"
#include <chrono>
#include <filesystem>
#include <iostream>
#include <string>
#include <system_error>
#include <thread>
#include <cstdlib>
#include <cwchar>
#include <errhandlingapi.h>
#include <libloaderapi.h>
#include <winuser.h>
using namespace std::chrono_literals;
using std::cerr;
using std::endl;
namespace fs = std::filesystem;
typedef wchar_t *(*entrypoint)(wchar_t *, const InfoForExtension *);
int main(int argc, char *argv[])
{
if (argc < 2) {
cerr << "err: no dll given; usage: " << argv[0] << " <dll>" << endl;
return 1;
}
auto const dll_path = fs::path(argv[1]).make_preferred();
cerr << "info: loading " << dll_path << endl;
HMODULE mod = LoadLibrary(dll_path.c_str());
if (mod == NULL) {
DWORD err = GetLastError();
auto err_msg = std::system_category().message(err);
cerr << "err: unable to load " << dll_path << ": [" << err << "] " << err_msg << endl;
return 2;
}
cerr << "info: success" << endl;
auto func = reinterpret_cast<entrypoint>(
reinterpret_cast<void *>(
GetProcAddress(mod, "OnNewSentence")
)
);
if (func == NULL) {
cerr << "err: unable to find dll entrypoint" << endl;
return 3;
}
InfoForExtension info{"current select", 1};
wchar_t word[20];
while (IDOK == MessageBox(NULL, L"Send message?", L"Mock", MB_OKCANCEL)) {
std::wmemcpy(word, L"FOOBAR", 7); // swprintf non-conforming in mingw
cerr << "info: sending msg to dll" << endl;
func(word, &info);
}
return 0;
}