summaryrefslogtreecommitdiffstats
path: root/src/core/src/testutil
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/src/testutil')
-rw-r--r--src/core/src/testutil/action_line.rs2
-rw-r--r--src/core/src/testutil/assert_results.rs100
-rw-r--r--src/core/src/testutil/mod.rs4
3 files changed, 90 insertions, 16 deletions
diff --git a/src/core/src/testutil/action_line.rs b/src/core/src/testutil/action_line.rs
index a4f100c..baf2805 100644
--- a/src/core/src/testutil/action_line.rs
+++ b/src/core/src/testutil/action_line.rs
@@ -106,7 +106,7 @@ impl LinePattern for ActionPattern {
fn expected(&self) -> String {
if self.selected {
- replace_invisibles(format!("> {}", self.line.to_text()).as_str())
+ replace_invisibles(format!("> {}", self.line.to_text()).as_str())
}
else {
replace_invisibles(format!(" {}", self.line.to_text()).as_str())
diff --git a/src/core/src/testutil/assert_results.rs b/src/core/src/testutil/assert_results.rs
index 76ad5b5..0171040 100644
--- a/src/core/src/testutil/assert_results.rs
+++ b/src/core/src/testutil/assert_results.rs
@@ -1,3 +1,5 @@
+use std::fmt::{Debug, Formatter};
+
use pretty_assertions::assert_eq;
use crate::process::{Artifact, Results};
@@ -29,22 +31,94 @@ 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 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()
);
}
#[macro_export]
macro_rules! assert_results {
- ($actual:expr) => {{
- use $crate::testutil::_assert_results;
- _assert_results($actual, &[]);
- }};
- ($actual:expr, $($arg:expr),*) => {{
- use $crate::testutil::_assert_results;
- let expected = vec![$( $arg, )*];
- _assert_results($actual, &expected);
- }};
- }
+ ($actual:expr) => {{
+ use $crate::testutil::_assert_results;
+ _assert_results($actual, &[]);
+ }};
+ ($actual:expr, $($arg:expr),*) => {{
+ use $crate::testutil::_assert_results;
+ use $crate::testutil::ArtifactCompareWrapper;
+
+ let expected= vec![$(
+ ArtifactCompareWrapper::from($arg),
+ )*];
+ _assert_results($actual, &expected);
+ }};
+}
diff --git a/src/core/src/testutil/mod.rs b/src/core/src/testutil/mod.rs
index fda271a..40e39f6 100644
--- a/src/core/src/testutil/mod.rs
+++ b/src/core/src/testutil/mod.rs
@@ -13,11 +13,11 @@ mod with_search;
pub(crate) use self::{
action_line::ActionPattern,
- assert_results::_assert_results,
+ assert_results::{AnyArtifact, ArtifactCompareWrapper, _assert_results},
create_event_reader::create_event_reader,
create_test_keybindings::{create_test_custom_keybindings, create_test_keybindings},
mocked_searchable::MockedSearchable,
- module_test::module_test,
+ module_test::{module_test, TestContext as ModuleTestContext},
process_test::{process_test, TestContext as ProcessTestContext},
read_event_test::read_event_test,
set_git_directory::set_git_directory,