#[derive(Debug)]
enum InputStreamConfigInner {
- String(String),
+ String(Cow<'static, str>),
Stdin,
Channel(Receiver<Vec<u8>>),
}
impl InputStreamConfig {
/// Gets input from string.
- pub fn string(s: impl Into<String>) -> Self {
+ pub fn string(s: impl Into<Cow<'static, str>>) -> Self {
Self {
inner: InputStreamConfigInner::String(s.into()),
}
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),
}
/// 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(),
}
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]);
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());
+}