Scripting: Refactor out some testing code

This commit is contained in:
Vicki Pfau 2022-12-31 01:19:58 -08:00
parent 0a4cafcd57
commit 1c370f6ebe
4 changed files with 35 additions and 47 deletions

View File

@ -12,6 +12,8 @@
#include <mgba/script/context.h> #include <mgba/script/context.h>
#include <mgba/script/types.h> #include <mgba/script/types.h>
#include "script/test.h"
#ifdef M_CORE_GBA #ifdef M_CORE_GBA
#include <mgba/internal/gba/memory.h> #include <mgba/internal/gba/memory.h>
#define TEST_PLATFORM mPLATFORM_GBA #define TEST_PLATFORM mPLATFORM_GBA
@ -66,22 +68,6 @@ static const uint8_t _fakeGBROM[0x4000] = {
mCoreConfigDeinit(&core->config); \ mCoreConfigDeinit(&core->config); \
core->deinit(core) core->deinit(core)
#define LOAD_PROGRAM(PROG) \
do { \
struct VFile* vf = VFileFromConstMemory(PROG, strlen(PROG)); \
assert_true(lua->load(lua, NULL, vf)); \
vf->close(vf); \
} while(0)
#define TEST_VALUE(TYPE, NAME, VALUE) \
do { \
struct mScriptValue val = mSCRIPT_MAKE(TYPE, VALUE); \
struct mScriptValue* global = lua->getGlobal(lua, NAME); \
assert_non_null(global); \
assert_true(global->type->equal(global, &val)); \
mScriptValueDeref(global); \
} while(0)
static void _mScriptTestLog(struct mLogger* log, int category, enum mLogLevel level, const char* format, va_list args) { static void _mScriptTestLog(struct mLogger* log, int category, enum mLogLevel level, const char* format, va_list args) {
UNUSED(category); UNUSED(category);
struct mScriptTestLogger* logger = (struct mScriptTestLogger*) log; struct mScriptTestLogger* logger = (struct mScriptTestLogger*) log;

29
src/script/test.h Normal file
View File

@ -0,0 +1,29 @@
/* Copyright (c) 2013-2022 Jeffrey Pfau
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef M_SCRIPT_TEST_H
#define M_SCRIPT_TEST_H
#define LOAD_PROGRAM(PROG) \
do { \
struct VFile* vf = VFileFromConstMemory(PROG, strlen(PROG)); \
assert_true(lua->load(lua, NULL, vf)); \
vf->close(vf); \
} while(0)
#define TEST_VALUE(TYPE, NAME, VALUE) \
do { \
struct mScriptValue val = mSCRIPT_MAKE(TYPE, VALUE); \
struct mScriptValue* global = lua->getGlobal(lua, NAME); \
assert_non_null(global); \
assert_true(global->type->equal(global, &val)); \
mScriptValueDeref(global); \
} while(0)
#define TEST_PROGRAM(PROG) \
LOAD_PROGRAM(PROG); \
assert_true(lua->run(lua)); \
#endif

View File

@ -8,22 +8,13 @@
#include <mgba/internal/script/lua.h> #include <mgba/internal/script/lua.h>
#include <mgba/script/macros.h> #include <mgba/script/macros.h>
#include "script/test.h"
#define SETUP_LUA \ #define SETUP_LUA \
struct mScriptContext context; \ struct mScriptContext context; \
mScriptContextInit(&context); \ mScriptContextInit(&context); \
struct mScriptEngineContext* lua = mScriptContextRegisterEngine(&context, mSCRIPT_ENGINE_LUA) struct mScriptEngineContext* lua = mScriptContextRegisterEngine(&context, mSCRIPT_ENGINE_LUA)
#define LOAD_PROGRAM(PROG) \
do { \
struct VFile* vf = VFileFromConstMemory(PROG, strlen(PROG)); \
assert_true(lua->load(lua, NULL, vf)); \
vf->close(vf); \
} while(0)
#define TEST_PROGRAM(PROG) \
LOAD_PROGRAM(PROG); \
assert_true(lua->run(lua)); \
struct Test { struct Test {
int32_t i; int32_t i;
int32_t (*ifn0)(struct Test*); int32_t (*ifn0)(struct Test*);

View File

@ -10,32 +10,14 @@
#include <mgba/script/macros.h> #include <mgba/script/macros.h>
#include <mgba/script/types.h> #include <mgba/script/types.h>
#include "script/test.h"
#define SETUP_LUA \ #define SETUP_LUA \
struct mScriptContext context; \ struct mScriptContext context; \
mScriptContextInit(&context); \ mScriptContextInit(&context); \
struct mScriptEngineContext* lua = mScriptContextRegisterEngine(&context, mSCRIPT_ENGINE_LUA); \ struct mScriptEngineContext* lua = mScriptContextRegisterEngine(&context, mSCRIPT_ENGINE_LUA); \
mScriptContextAttachStdlib(&context) mScriptContextAttachStdlib(&context)
#define LOAD_PROGRAM(PROG) \
do { \
struct VFile* vf = VFileFromConstMemory(PROG, strlen(PROG)); \
assert_true(lua->load(lua, NULL, vf)); \
vf->close(vf); \
} while(0)
#define TEST_PROGRAM(PROG) \
LOAD_PROGRAM(PROG); \
assert_true(lua->run(lua)); \
#define TEST_VALUE(TYPE, NAME, VALUE) \
do { \
struct mScriptValue val = mSCRIPT_MAKE(TYPE, VALUE); \
struct mScriptValue* global = lua->getGlobal(lua, NAME); \
assert_non_null(global); \
assert_true(global->type->equal(global, &val)); \
mScriptValueDeref(global); \
} while(0)
M_TEST_SUITE_SETUP(mScriptStdlib) { M_TEST_SUITE_SETUP(mScriptStdlib) {
if (mSCRIPT_ENGINE_LUA->init) { if (mSCRIPT_ENGINE_LUA->init) {
mSCRIPT_ENGINE_LUA->init(mSCRIPT_ENGINE_LUA); mSCRIPT_ENGINE_LUA->init(mSCRIPT_ENGINE_LUA);