add new test helper

This commit is contained in:
Bennet Bleßmann
2025-01-27 21:27:29 +01:00
committed by Bennet Bleßmann
parent bd1f8bb37e
commit e1246f0c83
2 changed files with 24 additions and 4 deletions

View File

@@ -78,7 +78,7 @@ impl OutputStreamConfig {
#[derive(Debug)] #[derive(Debug)]
enum InputStreamConfigInner { enum InputStreamConfigInner {
String(String), String(Cow<'static, str>),
Stdin, Stdin,
Channel(Receiver<Vec<u8>>), Channel(Receiver<Vec<u8>>),
} }
@@ -97,7 +97,7 @@ pub struct InputStreamConfig {
impl InputStreamConfig { impl InputStreamConfig {
/// Gets input from string. /// Gets input from string.
pub fn string(s: impl Into<String>) -> Self { pub fn string(s: impl Into<Cow<'static, str>>) -> Self {
Self { Self {
inner: InputStreamConfigInner::String(s.into()), inner: InputStreamConfigInner::String(s.into()),
} }
@@ -123,7 +123,10 @@ impl InputStreamConfig {
fn into_stream(self, arena: &mut Arena, add_history: bool) -> Stream { fn into_stream(self, arena: &mut Arena, add_history: bool) -> Stream {
match self.inner { match self.inner {
InputStreamConfigInner::String(s) => Stream::from_owned_string(s, arena), InputStreamConfigInner::String(s) => match s {
Cow::Owned(s) => Stream::from_owned_string(s, arena),
Cow::Borrowed(s) => Stream::from_static_string(s, arena),
},
InputStreamConfigInner::Stdin => Stream::stdin(arena, add_history), InputStreamConfigInner::Stdin => Stream::stdin(arena, add_history),
InputStreamConfigInner::Channel(channel) => Stream::input_channel(channel, arena), InputStreamConfigInner::Channel(channel) => Stream::input_channel(channel, arena),
} }
@@ -156,7 +159,7 @@ impl StreamConfig {
/// Binds the output and error streams to memory buffers and has an empty input. /// Binds the output and error streams to memory buffers and has an empty input.
pub fn in_memory() -> Self { pub fn in_memory() -> Self {
StreamConfig { StreamConfig {
user_input: InputStreamConfig::string(""), user_input: InputStreamConfig::string(String::new()),
user_output: OutputStreamConfig::memory(), user_output: OutputStreamConfig::memory(),
user_error: OutputStreamConfig::memory(), user_error: OutputStreamConfig::memory(),
} }

View File

@@ -1,5 +1,9 @@
use scryer_prolog::MachineBuilder; use scryer_prolog::MachineBuilder;
use std::borrow::Cow;
use scryer_prolog::{InputStreamConfig, StreamConfig};
pub(crate) trait Expectable { pub(crate) trait Expectable {
#[track_caller] #[track_caller]
fn assert_eq(self, other: &[u8]); fn assert_eq(self, other: &[u8]);
@@ -47,3 +51,16 @@ pub(crate) fn load_module_test_with_tokio_runtime<T: Expectable>(file: &str, exp
expected.assert_eq(wam.test_load_file(file).as_slice()) expected.assert_eq(wam.test_load_file(file).as_slice())
}); });
} }
pub(crate) fn load_module_test_with_input<T: Expectable>(
file: &str,
input: Cow<'static, str>,
expected: T,
) {
use scryer_prolog::MachineBuilder;
let mut wam = MachineBuilder::default()
.with_streams(StreamConfig::in_memory().with_user_input(InputStreamConfig::string(input)))
.build();
expected.assert_eq(wam.test_load_file(file).as_slice());
}