use xbasic::basic_io::BasicIO;
use xbasic::xbasic::XBasic;
pub struct TestIO {
input: Vec<String>,
expected: &'static str,
actual: String,
}
impl TestIO {
pub fn new(input: &str, expected: &'static str) -> Self {
let mut s = Self {
expected,
input: input
.to_owned()
.split('\n')
.map(|x| x.to_owned())
.collect::<Vec<String>>(),
actual: String::new(),
};
s.input.reverse();
s
}
pub fn check(&self) {
assert_eq!(self.expected, self.actual);
assert!(self.input.is_empty())
}
}
impl BasicIO for TestIO {
fn read_line(&mut self) -> String {
self.input.pop().unwrap()
}
fn write_line(&mut self, line: String) {
self.actual += &(line + "\n");
}
}
#[test]
fn input_test() {
let tio = TestIO::new("Line 1\nLine 2\n3", "3\nLine 2\nLine 1\n");
let mut xb = XBasic::new(tio);
assert!(xb
.run(
"
input a
input b
input c
print c
print b
print a
"
)
.is_ok());
xb.get_io().check();
}
#[test]
fn input_overwrite() {
let tio = TestIO::new("Line 1", "Line 1\n");
let mut xb = XBasic::new(tio);
assert!(xb
.run(
"
a = 1
input a
print a
"
)
.is_ok());
xb.get_io().check();
}