Skip to content

Commit

Permalink
fix: poptag being ignored
Browse files Browse the repository at this point in the history
  • Loading branch information
jcornaz committed Oct 20, 2022
1 parent 7a77ec8 commit 965f1e7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ impl<'a> Iterator for Parser<'a> {
return Some(Ok(directive));
}
Chunk::PushTag(tag) => self.tags.push(tag),
Chunk::PopTag(tag) => self.tags.retain(|&t| t != tag),
Chunk::Comment => (),
}
} else {
Expand All @@ -108,6 +109,7 @@ fn chunk(input: &str) -> IResult<&str, Chunk<'_>> {
alt((
map(directive, Chunk::Directive),
map(pushtag, Chunk::PushTag),
map(poptag, Chunk::PopTag),
value(Chunk::Comment, tuple((not_line_ending, opt(line_ending)))),
))(input)
}
Expand All @@ -116,21 +118,33 @@ fn pushtag(input: &str) -> IResult<&str, &str> {
preceded(tuple((tag("pushtag"), space1)), transaction::tag)(input)
}

fn poptag(input: &str) -> IResult<&str, &str> {
preceded(tuple((tag("poptag"), space1)), transaction::tag)(input)
}

#[derive(Debug, Clone)]
enum Chunk<'a> {
Directive(Directive<'a>),
Comment,
PushTag(&'a str),
PopTag(&'a str),
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn next_pushtag() {
fn pushtag() {
let input = "pushtag #test";
let (_, chunk) = chunk(input).expect("should successfully parse the input");
assert!(matches!(chunk, Chunk::PushTag("test")));
}

#[test]
fn poptag() {
let input = "poptag #test";
let (_, chunk) = chunk(input).expect("should successfully parse the input");
assert!(matches!(chunk, Chunk::PopTag("test")));
}
}
20 changes: 20 additions & 0 deletions tests/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,26 @@ fn multiple_pushtags_add_tags_to_next_transaction() {
assert_eq!(transaction.tags(), &["hello", "world"]);
}

#[test]
fn poptag_removes_tag_from_stack() {
let input = "pushtag #hello\npoptag #hello\n2022-10-20 txn";
let transaction = Parser::new(input)
.assert_single_directive()
.into_transaction()
.expect("should be a transaction");
assert!(transaction.tags().is_empty());
}

#[test]
fn poptag_removes_only_concerned_tag_from_stack() {
let input = "pushtag #hello\npushtag #world\npoptag #hello\n2022-10-20 txn";
let transaction = Parser::new(input)
.assert_single_directive()
.into_transaction()
.expect("should be a transaction");
assert_eq!(transaction.tags(), &["world"]);
}

#[test]
fn transaction_with_lot_date() {
let beancount = r#"
Expand Down

0 comments on commit 965f1e7

Please sign in to comment.