Skip to content

Commit

Permalink
Merge pull request #105 from mobusoperandi/expected_result_no_option
Browse files Browse the repository at this point in the history
refactor: no Option<ExpectedResult>
  • Loading branch information
mightyiam authored May 19, 2024
2 parents ca9be99 + 328a8d9 commit 36700a6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
14 changes: 6 additions & 8 deletions src/app/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,9 @@ impl State {
if !acc.ends_with('\n') {
vec![]
} else if Self::sanitize(acc)? == expected.as_str() {
session_live.expecting = if let Some(expected_result) = expected_result {
ReplSessionExpecting::ResultAndNextPrompt {
acc: String::new(),
expected_result: expected_result.clone(),
}
} else {
ReplSessionExpecting::Prompt(String::new())
session_live.expecting = ReplSessionExpecting::ResultAndNextPrompt {
acc: String::new(),
expected_result: expected_result.clone(),
};
vec![]
} else {
Expand All @@ -173,10 +169,12 @@ impl State {

let sanitized = Self::sanitize(acc)?;

let Some(result) = sanitized.strip_suffix("\n\nnix-repl> ") else {
let Some(result) = sanitized.strip_suffix("\nnix-repl> ") else {
break 'arm vec![];
};

let result = result.trim_end_matches('\n');

if result != expected_result.as_str() {
anyhow::bail!(indoc::formatdoc! {"
{id}
Expand Down
7 changes: 6 additions & 1 deletion src/app/state/repl_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub(crate) enum ReplSessionExpecting {
Echo {
acc: String,
last_query: ReplQuery,
expected_result: Option<ExpectedResult>,
expected_result: ExpectedResult,
},
ResultAndNextPrompt {
acc: String,
Expand Down Expand Up @@ -77,6 +77,11 @@ impl Iterator for ReplSessionLive {

#[derive(Debug, Clone, PartialEq, Eq, derive_more::Deref, derive_more::Display)]
pub(crate) struct ExpectedResult(pub(crate) String);
impl ExpectedResult {
pub(crate) fn empty() -> ExpectedResult {
Self(String::new())
}
}

impl From<LFLine> for ExpectedResult {
fn from(expected_result: LFLine) -> Self {
Expand Down
12 changes: 7 additions & 5 deletions src/repl/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ enum Expecting {
#[default]
PromptAndQuery,
ResultOrBlankLine(ReplQuery),
BlankLine(ReplQuery, Option<ExpectedResult>),
BlankLine(ReplQuery, ExpectedResult),
}

impl std::str::FromStr for ReplExampleEntries {
Expand All @@ -53,10 +53,12 @@ impl std::str::FromStr for ReplExampleEntries {
}
Expecting::ResultOrBlankLine(query) => {
if line.as_str() == "\n" {
state.entries.push(ReplEntry::new(query, None));
state
.entries
.push(ReplEntry::new(query, ExpectedResult::empty()));
state.expecting = Expecting::PromptAndQuery;
} else {
let expected = Some(ExpectedResult::from(line));
let expected = ExpectedResult::from(line);
state.expecting = Expecting::BlankLine(query, expected);
}
}
Expand Down Expand Up @@ -84,11 +86,11 @@ impl std::str::FromStr for ReplExampleEntries {
#[derive(Debug, Clone)]
pub(crate) struct ReplEntry {
pub(crate) query: ReplQuery,
pub(crate) expected_result: Option<ExpectedResult>,
pub(crate) expected_result: ExpectedResult,
}

impl ReplEntry {
pub(crate) fn new(query: ReplQuery, expected_result: Option<ExpectedResult>) -> Self {
pub(crate) fn new(query: ReplQuery, expected_result: ExpectedResult) -> Self {
Self {
query,
expected_result,
Expand Down

0 comments on commit 36700a6

Please sign in to comment.