mod common;
use xbasic::expr::ExprValue;
use xbasic::xbasic::XBasic;
#[test]
fn wrong_arity() {
let tio = common::TestIO::new("");
let mut xb = XBasic::new(tio);
assert!(xb
.run(
"
function a(b)
print b
end function
"
)
.is_ok());
assert!(xb.call_function("a", &[]).is_err());
xb.get_io().check();
}
#[test]
fn no_arguments() {
let tio = common::TestIO::new("Hello World!\n");
let mut xb = XBasic::new(tio);
assert!(xb
.run(
"
function a()
print \"Hello World!\"
end function
"
)
.is_ok());
assert!(xb.call_function("a", &[]).is_ok());
xb.get_io().check();
}
#[test]
fn multiple_arguments() {
let tio = common::TestIO::new("abc 3.4\n");
let mut xb = XBasic::new(tio);
assert!(xb
.run(
"
function func(a, b)
print a b
end function
"
)
.is_ok());
assert!(xb
.call_function(
"func",
&[ExprValue::String("abc".to_owned()), ExprValue::Decimal(3.4)]
)
.is_ok());
xb.get_io().check();
}
#[test]
fn return_value() {
let tio = common::TestIO::new("");
let mut xb = XBasic::new(tio);
assert!(xb
.run(
"
function func(a, b)
return a - b
end function
"
)
.is_ok());
assert_eq!(
Ok(ExprValue::Integer(3)),
xb.call_function("func", &[ExprValue::Integer(5), ExprValue::Integer(2)])
);
xb.get_io().check();
}
#[test]
fn multiple_calls() {
let tio = common::TestIO::new("");
let mut xb = XBasic::new(tio);
assert!(xb
.run(
"
function func(a, b)
return a - b
end function
"
)
.is_ok());
assert_eq!(
Ok(ExprValue::Integer(3)),
xb.call_function("func", &[ExprValue::Integer(5), ExprValue::Integer(2)])
);
assert_eq!(
Ok(ExprValue::Integer(4)),
xb.call_function("func", &[ExprValue::Integer(6), ExprValue::Integer(2)])
);
assert_eq!(
Ok(ExprValue::Integer(5)),
xb.call_function("func", &[ExprValue::Integer(7), ExprValue::Integer(2)])
);
assert_eq!(
Ok(ExprValue::Integer(-1)),
xb.call_function("func", &[ExprValue::Integer(5), ExprValue::Integer(6)])
);
assert_eq!(
Ok(ExprValue::Integer(3)),
xb.call_function("func", &[ExprValue::Integer(6), ExprValue::Integer(3)])
);
xb.get_io().check();
}