]> Repositorios git - scryer-prolog.git/commitdiff
add new test helper
authorBennet Bleßmann <[email protected]>
Mon, 27 Jan 2025 20:27:29 +0000 (21:27 +0100)
committerBennet Bleßmann <[email protected]>
Fri, 1 Aug 2025 17:08:02 +0000 (19:08 +0200)
src/machine/config.rs
tests/scryer/helper.rs

index b9d3f70394d431459f350824ea1df2a8f2a6aeaa..d4839d6907afcb5d178a0e0ef89367d1efef1ced 100644 (file)
@@ -78,7 +78,7 @@ impl OutputStreamConfig {
 
 #[derive(Debug)]
 enum InputStreamConfigInner {
-    String(String),
+    String(Cow<'static, str>),
     Stdin,
     Channel(Receiver<Vec<u8>>),
 }
@@ -97,7 +97,7 @@ pub struct InputStreamConfig {
 
 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()),
         }
@@ -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(),
         }
index b7c2dd776806f504aee75e0d3647cf3a58bdb8ba..155040d3b63dc7fd7f9b666ed7937cb36bbcb392 100644 (file)
@@ -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<T: Expectable>(file: &str, exp
         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());
+}