summaryrefslogtreecommitdiffstats
path: root/src/test_helpers
diff options
context:
space:
mode:
Diffstat (limited to 'src/test_helpers')
-rw-r--r--src/test_helpers/assertions.rs2
-rw-r--r--src/test_helpers/assertions/assert_rendered_output/patterns.rs2
-rw-r--r--src/test_helpers/assertions/assert_results.rs95
-rw-r--r--src/test_helpers/testers.rs2
-rw-r--r--src/test_helpers/testers/searchable.rs27
5 files changed, 121 insertions, 7 deletions
diff --git a/src/test_helpers/assertions.rs b/src/test_helpers/assertions.rs
index bf4f166..5c51ebc 100644
--- a/src/test_helpers/assertions.rs
+++ b/src/test_helpers/assertions.rs
@@ -3,4 +3,4 @@ mod assert_not_empty;
pub(crate) mod assert_rendered_output;
mod assert_results;
-pub(crate) use assert_results::_assert_results;
+pub(crate) use assert_results::{AnyArtifact, ArtifactCompareWrapper, _assert_results};
diff --git a/src/test_helpers/assertions/assert_rendered_output/patterns.rs b/src/test_helpers/assertions/assert_rendered_output/patterns.rs
index 2ad0d74..b7e9e20 100644
--- a/src/test_helpers/assertions/assert_rendered_output/patterns.rs
+++ b/src/test_helpers/assertions/assert_rendered_output/patterns.rs
@@ -377,7 +377,7 @@ impl LinePattern for ActionPattern {
replace_invisibles(format!("> {}", self.line.to_text()).as_str())
}
else {
- replace_invisibles(format!(" {}", self.line.to_text()).as_str())
+ replace_invisibles(format!(" {}", self.line.to_text()).as_str())
}
}
diff --git a/src/test_helpers/assertions/assert_results.rs b/src/test_helpers/assertions/assert_results.rs
index 12ad20c..e53c01b 100644
--- a/src/test_helpers/assertions/assert_results.rs
+++ b/src/test_helpers/assertions/assert_results.rs
@@ -1,3 +1,5 @@
+use std::fmt::{Debug, Formatter};
+
use pretty_assertions::assert_eq;
use crate::process::{Artifact, Results};
@@ -29,10 +31,87 @@ fn _assert_results_format(artifacts: &[Artifact]) -> String {
.join("\n")
}
-pub(crate) fn _assert_results(results: Results, expected_artifacts: &[Artifact]) {
+fn compare_artifact(a: &Artifact, b: &Artifact) -> bool {
+ match (a, b) {
+ (Artifact::ChangeState(self_state), Artifact::ChangeState(other_state)) => self_state == other_state,
+ (Artifact::Error(self_error, self_state), Artifact::Error(other_error, other_state)) => {
+ self_state == other_state && format!("{self_error:#}") == format!("{other_error:#}")
+ },
+ (Artifact::Event(self_event), Artifact::Event(other_event)) => self_event == other_event,
+ (Artifact::ExitStatus(self_exit_status), Artifact::ExitStatus(other_exit_status)) => {
+ self_exit_status == other_exit_status
+ },
+ (Artifact::ExternalCommand(self_command), Artifact::ExternalCommand(other_command)) => {
+ self_command == other_command
+ },
+ (Artifact::SearchTerm(self_term), Artifact::SearchTerm(other_term)) => self_term == other_term,
+ (Artifact::SearchCancel, Artifact::SearchCancel)
+ | (Artifact::EnqueueResize, Artifact::EnqueueResize)
+ | (Artifact::Searchable(_), Artifact::Searchable(_)) => true,
+ _ => false,
+ }
+}
+
+pub(crate) struct AnyArtifact;
+
+pub(crate) enum ArtifactCompareWrapper {
+ Any,
+ Artifact(Artifact),
+}
+
+impl PartialEq for ArtifactCompareWrapper {
+ fn eq(&self, other: &Self) -> bool {
+ match self {
+ ArtifactCompareWrapper::Any => true,
+ ArtifactCompareWrapper::Artifact(a) => {
+ match other {
+ ArtifactCompareWrapper::Any => true,
+ ArtifactCompareWrapper::Artifact(o) => compare_artifact(a, o),
+ }
+ },
+ }
+ }
+}
+
+// impl PartialEq<Artifact> for ArtifactCompareWrapper {
+// fn eq(&self, other: &Artifact) -> bool {
+// match self {
+// ArtifactCompareWrapper::Any => true,
+// ArtifactCompareWrapper::Artifact(a) => compare_artifact(a, other),
+// }
+// }
+// }
+
+impl Debug for ArtifactCompareWrapper {
+ fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
+ match self {
+ ArtifactCompareWrapper::Any => write!(f, "Any Result"),
+ ArtifactCompareWrapper::Artifact(a) => a.fmt(f),
+ }
+ }
+}
+
+impl From<Artifact> for ArtifactCompareWrapper {
+ fn from(value: Artifact) -> Self {
+ Self::Artifact(value)
+ }
+}
+
+impl From<AnyArtifact> for ArtifactCompareWrapper {
+ fn from(value: AnyArtifact) -> Self {
+ Self::Any
+ }
+}
+
+pub(crate) fn _assert_results(results: Results, expected_artifacts: &[ArtifactCompareWrapper]) {
assert_eq!(
- _assert_results_format(expected_artifacts),
- _assert_results_format(results.artifacts.into_iter().collect::<Vec<_>>().as_slice())
+ expected_artifacts,
+ results
+ .artifacts
+ .into_iter()
+ .map(ArtifactCompareWrapper::from)
+ .collect::<Vec<_>>()
+ .as_slice()
);
}
@@ -43,8 +122,14 @@ macro_rules! assert_results {
_assert_results($actual, &[]);
}};
($actual:expr, $($arg:expr),*) => {{
- use $crate::test_helpers::assertions::_assert_results;
- let expected = vec![$( $arg, )*];
+ use $crate::test_helpers::assertions::{
+ _assert_results,
+ ArtifactCompareWrapper,
+ };
+
+ let expected= vec![$(
+ ArtifactCompareWrapper::from($arg),
+ )*];
_assert_results($actual, &expected);
}};
}
diff --git a/src/test_helpers/testers.rs b/src/test_helpers/testers.rs
index 3da49d0..02fddde 100644
--- a/src/test_helpers/testers.rs
+++ b/src/test_helpers/testers.rs
@@ -1,11 +1,13 @@
mod module;
mod process;
mod read_event;
+mod searchable;
mod threadable;
pub(crate) use self::{
module::{module_test as module, ModuleTestContext},
process::{process, ProcessTestContext},
read_event::read_event,
+ searchable::SearchableRunner,
threadable::Threadable,
};
diff --git a/src/test_helpers/testers/searchable.rs b/src/test_helpers/testers/searchable.rs
new file mode 100644
index 0000000..f183b81
--- /dev/null
+++ b/src/test_helpers/testers/searchable.rs
@@ -0,0 +1,27 @@
+use std::time::Duration;
+
+use crate::search::{Interrupter, SearchResult, Searchable};
+
+const SEARCH_INTERRUPT_TIME: Duration = Duration::from_secs(1);
+
+pub(crate) struct SearchableRunner<S: Searchable + Clone> {
+ searchable: S,
+}
+
+impl<S: Searchable + Clone> SearchableRunner<S> {
+ pub(crate) fn new(searchable: &S) -> Self {
+ Self {
+ searchable: searchable.clone(),
+ }
+ }
+
+ pub(crate) fn run_search(&mut self, search_term: &str) -> SearchResult {
+ self.searchable
+ .search(Interrupter::new(SEARCH_INTERRUPT_TIME), search_term)
+ }
+
+ pub(crate) fn run_search_with_time(&mut self, search_term: &str, millis: u64) -> SearchResult {
+ self.searchable
+ .search(Interrupter::new(Duration::from_millis(millis)), search_term)
+ }
+}