Skip to content

Commit

Permalink
Test for callback streams
Browse files Browse the repository at this point in the history
  • Loading branch information
bakaq committed Jan 29, 2025
1 parent 62ea227 commit 40f13b0
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion src/machine/lib_machine/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use std::{cell::RefCell, io::Read, rc::Rc};
use std::io::Write;

use super::*;
use crate::MachineBuilder;
use crate::{MachineBuilder, StreamConfig};

#[test]
#[cfg_attr(miri, ignore = "it takes too long to run")]
Expand Down Expand Up @@ -608,3 +611,33 @@ fn errors_and_exceptions() {
[Ok(LeafAnswer::Exception(Term::atom("a")))]
);
}

#[test]
#[cfg_attr(miri, ignore)]
fn callback_streams() {
let test_string = Rc::new(RefCell::new(String::new()));
let test_string2 = test_string.clone();

let (mut user_input, streams) = StreamConfig::with_callbacks(
Some(Box::new(move |x| { x.read_to_string(&mut *test_string2.borrow_mut()).unwrap(); })),
None
);
let mut machine = MachineBuilder::default().with_streams(streams).build();

write!(&mut user_input, "a(1,2,3).").unwrap();

let complete_answer: Vec<_> = machine.run_query("read(A), write('asdf'), nl, flush_output.").collect();

assert_eq!(
complete_answer,
[Ok(LeafAnswer::from_bindings([
("A", Term::compound("a", [
Term::integer(1),
Term::integer(2),
Term::integer(3)
])),
]))]
);

assert_eq!(*test_string.borrow(), "asdf\n");
}

0 comments on commit 40f13b0

Please sign in to comment.