1
0
Fork 0
mirror of https://github.com/tailix/libkernaux.git synced 2024-10-30 11:54:01 -04:00

mruby: partially implement assertions

This commit is contained in:
Alex Kotov 2022-01-30 09:33:09 +05:00
parent bfaf176670
commit 24a5e24171
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
3 changed files with 60 additions and 5 deletions

50
pkgs/mruby/src/assert.c Normal file
View file

@ -0,0 +1,50 @@
#include <kernaux.h>
#include <mruby.h>
#include <mruby/variable.h>
static void assert_cb(const char *file, int line, const char *msg);
static mrb_value rb_KernAux_assert_cb(mrb_state *mrb, mrb_value self);
static mrb_value rb_KernAux_assert_cb_EQ(mrb_state *mrb, mrb_value self);
static mrb_value rb_KernAux_assert_do(mrb_state *mrb, mrb_value self);
void init_assert(mrb_state *const mrb)
{
kernaux_assert_cb = assert_cb;
struct RClass *const rb_KernAux = mrb_define_module(mrb, "KernAux");
mrb_define_class_method(mrb, rb_KernAux, "assert_cb",
rb_KernAux_assert_cb, MRB_ARGS_NONE());
mrb_define_class_method(mrb, rb_KernAux, "assert_cb=",
rb_KernAux_assert_cb_EQ, MRB_ARGS_REQ(1));
mrb_define_class_method(mrb, rb_KernAux, "assert_do",
rb_KernAux_assert_do, MRB_ARGS_REQ(3));
}
void assert_cb(const char *const file, const int line, const char *const msg)
{
}
mrb_value rb_KernAux_assert_cb(mrb_state *const mrb, const mrb_value self_rb)
{
return mrb_iv_get(mrb, self_rb, mrb_intern_lit(mrb, "@assert_cb"));
}
mrb_value rb_KernAux_assert_cb_EQ(mrb_state *const mrb, const mrb_value self_rb)
{
mrb_value assert_cb_rb = mrb_nil_value();
mrb_get_args(mrb, "o", &assert_cb_rb);
mrb_iv_set(mrb, self_rb, mrb_intern_lit(mrb, "@assert_cb"), assert_cb_rb);
return assert_cb_rb;
}
mrb_value rb_KernAux_assert_do(mrb_state *const mrb, const mrb_value self_rb)
{
const char *file = NULL;
mrb_int line_rb = 0;
const char *msg = NULL;
mrb_get_args(mrb, "ziz", &file, &line_rb, &msg);
kernaux_assert_do(file, 0, msg);
return mrb_nil_value();
}

View file

@ -1,8 +1,10 @@
#include <mruby.h>
void mrb_mruby_kernaux_gem_final(mrb_state *mrb) {}
void init_assert(mrb_state *mrb);
void mrb_mruby_kernaux_gem_init(mrb_state *mrb)
void mrb_mruby_kernaux_gem_final(mrb_state *const mrb) {}
void mrb_mruby_kernaux_gem_init(mrb_state *const mrb)
{
/* struct RClass *const rb_KernAux = */ mrb_define_module(mrb, "KernAux");
init_assert(mrb);
}

View file

@ -1,3 +1,6 @@
assert 'Main module exists' do
assert_equal KernAux, ::KernAux
assert 'Dummy' do
assert_equal nil, KernAux.assert_cb
KernAux.assert_cb = 123
assert_equal 123, KernAux.assert_cb
assert_equal nil, KernAux.assert_do(__FILE__, __LINE__, 'hello')
end