From a42b6eb4496ff5686702a8170c636de641383e18 Mon Sep 17 00:00:00 2001 From: 45Tatami Date: Tue, 23 May 2023 18:23:17 +0200 Subject: [PATCH] feat: add mock application for testing --- README.md | 9 +++++++ meson.build | 10 +++++-- meson_options.txt | 2 ++ mock/meson.build | 6 +++++ mock/mock.cpp | 67 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 meson_options.txt create mode 100644 mock/meson.build create mode 100644 mock/mock.cpp diff --git a/README.md b/README.md index 14bd458..fe9a774 100644 --- a/README.md +++ b/README.md @@ -29,3 +29,12 @@ meson setup --cross-file=cross/i686-w64-mingw32.txt build # x86_64/64bit 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 diff --git a/meson.build b/meson.build index b4719dc..58ded40 100644 --- a/meson.build +++ b/meson.build @@ -1,5 +1,6 @@ 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') @@ -16,5 +17,10 @@ src += windows.compile_resources('TCPSender/resource.rc') deps = [] deps += compiler.find_library('ws2_32') -library('tcpsender', src, +tcpsender = library('TCPSender', src, + name_prefix : '', name_suffix : 'xdll', dependencies : deps) + +if get_option('build_mock').enabled() + subdir('mock') +endif diff --git a/meson_options.txt b/meson_options.txt new file mode 100644 index 0000000..c72698e --- /dev/null +++ b/meson_options.txt @@ -0,0 +1,2 @@ +option('build_mock', type : 'feature', value : 'disabled', + description : 'build a mock application using this Textractor extension') diff --git a/mock/meson.build b/mock/meson.build new file mode 100644 index 0000000..d31bf40 --- /dev/null +++ b/mock/meson.build @@ -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) diff --git a/mock/mock.cpp b/mock/mock.cpp new file mode 100644 index 0000000..19b2604 --- /dev/null +++ b/mock/mock.cpp @@ -0,0 +1,67 @@ +#include "Extension.h" + +#include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include +#include + +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] << " " << 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( + reinterpret_cast( + 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; +}