diff options
author | sanine-a <sanine.not@pm.me> | 2021-05-23 16:47:59 -0500 |
---|---|---|
committer | sanine-a <sanine.not@pm.me> | 2021-05-23 16:47:59 -0500 |
commit | e06165088c13cd31819a7b1bb1a2ba9e5aaf689e (patch) | |
tree | a1772bfb552202af710bddc0ebf5231376c845e4 | |
parent | 4dc4d9f1d20a73d1e4c399a56c86bf70589b13d9 (diff) |
create initial files and write three hs_pushstring() tests
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | CMakeLists.txt | 26 | ||||
-rw-r--r-- | README.md | 4 | ||||
-rw-r--r-- | src/honeysuckle.c | 7 | ||||
-rw-r--r-- | src/honeysuckle.h | 27 | ||||
-rw-r--r-- | src/test.c | 80 |
6 files changed, 144 insertions, 1 deletions
@@ -1 +1,2 @@ *~ +build diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..4dcfccd --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,26 @@ +cmake_minimum_required(VERSION 3.2) +project(honeysuckle + VERSION 1.0.0 + DESCRIPTION "A C library to make writing lua bindings simple") + +set(CMAKE_C_FLAGS "-Wall -Wextra -Werror -Wfatal-errors -Wpedantic") + +find_package(Lua51 REQUIRED) +include_directories(${LUA_INCLUDE_DIR}) + +# build and link the library +add_library(honeysuckle src/honeysuckle.c) +target_link_libraries(honeysuckle ${LUA_LIBRARIES}) +set_target_properties(honeysuckle PROPERTIES + VERSION ${PROJECT_VERSION} + PUBLIC_HEADER src/honeysuckle.h) + +# optionally build the tests +add_executable(test EXCLUDE_FROM_ALL src/test.c) +target_link_libraries(test ${LUA_LIBRARIES} honeysuckle) + +include(GNUInstallDirs) + +install(TARGETS honeysuckle + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) @@ -273,7 +273,9 @@ Development honeysuckle is still very early in development. As the specifics of the build process and testing become clearer I'll update this section. -# License +License +------- + [(Back to top)](#table-of-contents) diff --git a/src/honeysuckle.c b/src/honeysuckle.c new file mode 100644 index 0000000..423fa1b --- /dev/null +++ b/src/honeysuckle.c @@ -0,0 +1,7 @@ +#include "honeysuckle.h" + +void hs_pushstring(lua_State *L, const char *format_string, ...) +{ + lua_pushnumber(L, 0.4); + format_string = format_string; +} diff --git a/src/honeysuckle.h b/src/honeysuckle.h new file mode 100644 index 0000000..7538791 --- /dev/null +++ b/src/honeysuckle.h @@ -0,0 +1,27 @@ +#ifndef HONEYSUCKLE_H +#define HONEYSUCKLE_H + +#include <lua.h> +#include <lualib.h> +#include <lauxlib.h> + + +#define HS_END 0 + +/* type constants */ +#define HS_BOOL 1 +#define HS_INT 2 +#define HS_NUM 3 +#define HS_STR 4 +#define HS_TBL 5 +#define HS_FUNC 6 +#define HS_CFUNC 7 +#define HS_USER 8 +#define HS_LIGHT 9 +#define HS_NIL 10 +#define HS_ANY 11 + + +void hs_pushstring(lua_State *L, const char *format_string, ...); + +#endif diff --git a/src/test.c b/src/test.c new file mode 100644 index 0000000..5275d4d --- /dev/null +++ b/src/test.c @@ -0,0 +1,80 @@ +#include <stdio.h> +#include <string.h> + +#include <lua.h> +#include <lualib.h> +#include <lauxlib.h> + +#include "honeysuckle.h" + +/* minunit testing macros modified from those at www.jera.com/techinfo/jtns/jtn002.html */ +#define mu_assert(message, test) do { if (!(test)) return message; } while (0) +#define mu_run_test(name, test) do { \ + lua_State *L = luaL_newstate(); \ + luaL_openlibs(L); \ + char *message = test(L); \ + lua_close(L); \ + tests_run++; \ + if (message) { \ + printf("test '%s' failed: %s\n", name, message); \ + tests_failed++; \ + } \ + } while (0) + +int tests_run = 0; +int tests_failed = 0; + + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * + * tests for hs_pushstring + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + */ + +static char* push_noformat(lua_State *L) +{ + hs_pushstring(L, "a string"); + mu_assert("no string at top of stack!", lua_isstring(L, -1)); + const char *string = lua_tostring(L, -1); + mu_assert("string != 'a string'", strcmp(string, "a string") == 0); + return 0; +} + +static char* push_formatint(lua_State *L) +{ + hs_pushstring(L, "%d is 5", 5); + mu_assert("no string at top of stack!", lua_isstring(L, -1)); + const char *string = lua_tostring(L, -1); + mu_assert("string != '5 is 5'", strcmp(string, "5 is 5") == 0); + return 0; +} + +static char* push_formatstring(lua_State *L) +{ + hs_pushstring(L, "%s is 'hello'", "hello"); + mu_assert("no string at top of stack!", lua_isstring(L, -1)); + const char *string = lua_tostring(L, -1); + mu_assert("string != 'hello is 'hello''", + strcmp(string, "hello is 'hello'") == 0); + return 0; +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * + * RUN TESTS + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + */ + +int main() +{ + printf("================ start tests ================\n\n"); + printf("running hs_pushstring() tests...\n"); + mu_run_test("hs_pushstring (no printf formatting)", push_noformat); + mu_run_test("hs_pushstring (integer formatting)", push_formatint); + mu_run_test("hs_pushstring (string formatting)", push_formatstring); + printf("\n=============== tests finished ===============\n\n"); + printf("ran %d tests, %d failed\n", tests_run, tests_failed); + return 0; +} |