From 98a9551d2e47bb53d0069d71297924cf697f03c3 Mon Sep 17 00:00:00 2001 From: sanine Date: Thu, 16 Jun 2022 22:46:40 -0500 Subject: throw error on glfwInit fail --- src/gl/window.c | 4 +++- src/gl/window.test.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 67 insertions(+), 5 deletions(-) (limited to 'src/gl') diff --git a/src/gl/window.c b/src/gl/window.c index 80d636d..834d6ce 100644 --- a/src/gl/window.c +++ b/src/gl/window.c @@ -5,6 +5,8 @@ int gl_init(lua_State *L) { - glfwInit(); + if (glfwInit() != GLFW_TRUE) { + hs_throw_error(L, "failed to initialize GLFW"); + } return 0; } diff --git a/src/gl/window.test.c b/src/gl/window.test.c index 33b52a4..1403221 100644 --- a/src/gl/window.test.c +++ b/src/gl/window.test.c @@ -1,11 +1,16 @@ +#include #include #include +#include #include "test/honey-test.h" int mock_glfwInit(); +int mock_hs_throw_error(lua_State *L, const char *format_string, ...); #define glfwInit mock_glfwInit +#define hs_throw_error mock_hs_throw_error #include "gl/window.c" #undef glfwInit +#undef hs_throw_error lily_mock_t *mock_glfwInit_data = NULL; @@ -18,12 +23,45 @@ int mock_glfwInit() return result; } +lily_mock_t *mock_hs_throw_error_data = NULL; +int mock_hs_throw_error(lua_State *L, const char *format_string, ...) +{ + /* to avoid basically just re-implementing printf parsing here, + i am limiting this function to be able to receive strings only */ + + /* count format specifiers */ + char *ptr = strchr(format_string, '%'); + int n_args = 0; + while (ptr != NULL) { + n_args += 1; + ptr = strchr(ptr+1, '%'); + } + + /* store arguments */ + struct lily_mock_arg_t args[] = { + { sizeof(const char*), &format_string }, + { sizeof(int), &n_args }, + }; + lily_mock_call(mock_hs_throw_error_data, args); + + /* store format arguments */ + va_list vl; + va_start(vl, format_string); + for (int i=0; in_calls, 1); + lily_assert_int_equal(mock_hs_throw_error_data->n_calls, 0); +} + + +void gl_init_fail_glfwInit() +{ + USE_MOCK(mock_glfwInit); + USE_MOCK(mock_hs_throw_error); + + /* queue failure */ + mock_enqueue(mock_glfwInit, int, GLFW_FALSE); + gl_init(NULL); - lily_assert_int_equal(error, 0); lily_assert_int_equal(mock_glfwInit_data->n_calls, 1); + lily_assert_int_equal(mock_hs_throw_error_data->n_calls, 1); + + const char *fmt; int argc; + struct lily_mock_arg_t args[] = { + { sizeof(const char*), &fmt }, + { sizeof(int), &argc }, + }; + lily_get_call(mock_hs_throw_error_data, args, 0); + + lily_assert_string_equal((char*) fmt, "failed to initialize GLFW"); } -- cgit v1.2.1