From: Bennet Bleßmann Date: Mon, 27 Jan 2025 20:27:29 +0000 (+0100) Subject: add new test helper X-Git-Tag: v0.10.0~29^2~13 X-Git-Url: https://git.sagredo.dev/?a=commitdiff_plain;h=e1246f0c83d7ec1557415fc67d1fdf5653fc0ea5;p=scryer-prolog.git add new test helper --- diff --git a/src/machine/config.rs b/src/machine/config.rs index b9d3f703..d4839d69 100644 --- a/src/machine/config.rs +++ b/src/machine/config.rs @@ -78,7 +78,7 @@ impl OutputStreamConfig { #[derive(Debug)] enum InputStreamConfigInner { - String(String), + String(Cow<'static, str>), Stdin, Channel(Receiver>), } @@ -97,7 +97,7 @@ pub struct InputStreamConfig { impl InputStreamConfig { /// Gets input from string. - pub fn string(s: impl Into) -> Self { + pub fn string(s: impl Into>) -> Self { Self { inner: InputStreamConfigInner::String(s.into()), } @@ -123,7 +123,10 @@ impl InputStreamConfig { fn into_stream(self, arena: &mut Arena, add_history: bool) -> Stream { 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::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. pub fn in_memory() -> Self { StreamConfig { - user_input: InputStreamConfig::string(""), + user_input: InputStreamConfig::string(String::new()), user_output: OutputStreamConfig::memory(), user_error: OutputStreamConfig::memory(), } diff --git a/tests/scryer/helper.rs b/tests/scryer/helper.rs index b7c2dd77..155040d3 100644 --- a/tests/scryer/helper.rs +++ b/tests/scryer/helper.rs @@ -1,5 +1,9 @@ use scryer_prolog::MachineBuilder; +use std::borrow::Cow; + +use scryer_prolog::{InputStreamConfig, StreamConfig}; + pub(crate) trait Expectable { #[track_caller] fn assert_eq(self, other: &[u8]); @@ -47,3 +51,16 @@ pub(crate) fn load_module_test_with_tokio_runtime(file: &str, exp expected.assert_eq(wam.test_load_file(file).as_slice()) }); } + +pub(crate) fn load_module_test_with_input( + 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()); +}