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:
parent
bfaf176670
commit
24a5e24171
3 changed files with 60 additions and 5 deletions
50
pkgs/mruby/src/assert.c
Normal file
50
pkgs/mruby/src/assert.c
Normal 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();
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue