Skip to content

Commit

Permalink
Merge pull request #2 from dragostis/move_tests_separate_file
Browse files Browse the repository at this point in the history
Better test organization and MRValue testing.
  • Loading branch information
dragostis committed Feb 8, 2016
2 parents d2943c9 + 8298e83 commit f37cb40
Show file tree
Hide file tree
Showing 4 changed files with 364 additions and 249 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "mrusty"
version = "0.1.0"
authors = ["Dragoș Tiselice <dragostiselice@gmail.com>"]
authors = ["Dragoș Tiselice <dragostiselice@gmail.com>", "Robert Krody <krody.robi@gmail.com>"]
build = "build.rs"

[build-dependencies]
Expand Down
251 changes: 3 additions & 248 deletions src/mruby_ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,252 +237,7 @@ extern "C" {
pub fn mrb_ext_get_exc(mrb: *mut MRState) -> MRValue;
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
pub fn test_open_close() {
unsafe {
let mrb = mrb_open();

mrb_close(mrb);
}
}

#[test]
pub fn test_exec_context() {
unsafe {
let mrb = mrb_open();
let context = mrbc_context_new(mrb);

mrbc_filename(mrb, context, "script.rb\0".as_ptr());

let code = "'' + 0\0".as_ptr();

mrb_load_string_cxt(mrb, code, context);

assert_eq!(mrb_ext_get_exc(mrb).to_str(mrb).unwrap(), "script.rb:1: expected String (TypeError)");

mrb_close(mrb);
}
}

#[test]
pub fn test_create_run_proc() {
unsafe {
let mrb = mrb_open();
let context = mrbc_context_new(mrb);

let code = "1 + 1\0".as_ptr();
let parser = mrb_parse_string(mrb, code, context);
let prc = mrb_generate_code(mrb, parser);

let result = mrb_run(mrb, prc, mrb_top_self(mrb));

assert_eq!(result.to_i32().unwrap(), 2);

mrb_close(mrb);
}
}

#[test]
pub fn test_class_defined() {
unsafe {
let mrb = mrb_open();

let obj_class = "Object\0".as_ptr();

assert_eq!(mrb_class_defined(mrb, obj_class), 1);

mrb_close(mrb);
}
}

#[test]
pub fn test_define_class() {
unsafe {
let mrb = mrb_open();

let obj_class = "Object\0".as_ptr();
let new_class = "Mine\0".as_ptr();

let obj_class = mrb_class_get(mrb, "Object\0".as_ptr());

mrb_define_class(mrb, new_class, obj_class);

assert_eq!(mrb_class_defined(mrb, new_class), 1);

mrb_close(mrb);
}
}

#[test]
pub fn test_define_module() {
unsafe {
let mrb = mrb_open();

let new_module = "Mine\0".as_ptr();

mrb_define_module(mrb, new_module);

mrb_module_get(mrb, new_module);

mrb_close(mrb);
}
}

#[test]
pub fn test_include_module() {
unsafe {
let mrb = mrb_open();

let kernel = "Kernel\0".as_ptr();
let new_module = "Mine\0".as_ptr();

let new_module = mrb_define_module(mrb, new_module);
let kernel = mrb_module_get(mrb, kernel);

mrb_include_module(mrb, kernel, new_module);

mrb_close(mrb);
}
}

#[test]
pub fn test_prepend_module() {
unsafe {
let mrb = mrb_open();

let kernel = "Kernel\0".as_ptr();
let new_module = "Mine\0".as_ptr();

let new_module = mrb_define_module(mrb, new_module);
let kernel = mrb_module_get(mrb, kernel);

mrb_prepend_module(mrb, kernel, new_module);

mrb_close(mrb);
}
}

#[test]
pub fn test_define_method() {
unsafe {
let mrb = mrb_open();
let context = mrbc_context_new(mrb);

let obj_class = "Object\0".as_ptr();
let new_class = "Mine\0".as_ptr();

let obj_class = mrb_class_get(mrb, "Object\0".as_ptr());
let new_class = mrb_define_class(mrb, new_class, obj_class);

extern "C" fn job(mrb: *mut MRState, slf: MRValue) -> MRValue {
unsafe {
MRValue::fixnum(2)
}
}

mrb_define_method(mrb, new_class, "job\0".as_ptr(), job, 0);

let code = "Mine.new.job\0".as_ptr();

assert_eq!(mrb_load_string_cxt(mrb, code, context).to_i32().unwrap(), 2);

mrb_close(mrb);
}
}

#[test]
pub fn test_define_class_method() {
unsafe {
let mrb = mrb_open();
let context = mrbc_context_new(mrb);

let obj_class = "Object\0".as_ptr();
let new_class = "Mine\0".as_ptr();

let obj_class = mrb_class_get(mrb, "Object\0".as_ptr());
let new_class = mrb_define_class(mrb, new_class, obj_class);

extern "C" fn job(mrb: *mut MRState, slf: MRValue) -> MRValue {
unsafe {
MRValue::fixnum(2)
}
}

mrb_define_class_method(mrb, new_class, "job\0".as_ptr(), job, 0);

let code = "Mine.job\0".as_ptr();

assert_eq!(mrb_load_string_cxt(mrb, code, context).to_i32().unwrap(), 2);

mrb_close(mrb);
}
}

#[test]
pub fn test_define_module_function() {
unsafe {
let mrb = mrb_open();
let context = mrbc_context_new(mrb);

let new_module = "Mine\0".as_ptr();
let new_module = mrb_define_module(mrb, new_module);

extern "C" fn job(mrb: *mut MRState, slf: MRValue) -> MRValue {
unsafe {
MRValue::fixnum(2)
}
}

mrb_define_module_function(mrb, new_module, "job\0".as_ptr(), job, 0);

let code = "Mine.job\0".as_ptr();

assert_eq!(mrb_load_string_cxt(mrb, code, context).to_i32().unwrap(), 2);

mrb_close(mrb);
}
}

#[test]
pub fn test_obj_new() {
use std::ptr;

unsafe {
let mrb = mrb_open();
let context = mrbc_context_new(mrb);

let obj_class = "Object\0".as_ptr();
let obj_class = mrb_class_get(mrb, "Object\0".as_ptr());

mrb_obj_new(mrb, obj_class, 0, ptr::null() as *const MRValue);

mrb_close(mrb);
}
}

#[test]
pub fn test_proc_new_cfunc() {
use std::ptr;

unsafe {
let mrb = mrb_open();
let context = mrbc_context_new(mrb);

extern "C" fn job(mrb: *mut MRState, slf: MRValue) -> MRValue {
unsafe {
MRValue::fixnum(2)
}
}

let prc = MRValue::prc(mrb, mrb_proc_new_cfunc(mrb, job));

mrb_funcall_with_block(mrb, MRValue::fixnum(5), mrb_intern_cstr(mrb, "times\0".as_ptr()), 0, ptr::null() as *const MRValue, prc);

mrb_close(mrb);
}
}
}
#[path="tests/mruby_ffi.rs"]
#[cfg(test)]
mod tests;
15 changes: 15 additions & 0 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// mrusty. mruby bindings for Rust
// Copyright (C) 2016 Dragoș Tiselice
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
Loading

0 comments on commit f37cb40

Please sign in to comment.