-
Notifications
You must be signed in to change notification settings - Fork 544
Expand file tree
/
Copy pathnot.rs
More file actions
74 lines (62 loc) · 2.01 KB
/
Copy pathnot.rs
File metadata and controls
74 lines (62 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use crate::{Span, Token};
use super::Expr;
/// A zero-width assertion that matches when its inner expression does not.
pub struct Not {
inner: Box<dyn Expr>,
}
impl Not {
pub fn new(inner: impl Expr + 'static) -> Self {
Self {
inner: Box::new(inner),
}
}
}
impl Expr for Not {
fn run(&self, cursor: usize, tokens: &[Token], source: &[char]) -> Option<Span<Token>> {
self.inner
.run(cursor, tokens, source)
.is_none()
.then(|| Span::empty(cursor))
}
}
#[cfg(test)]
mod tests {
use crate::{
Document,
expr::{AnchorStart, ExprExt, SequenceExpr},
linting::tests::SpanVecExt,
};
use super::Not;
#[test]
fn rejects_expression_at_start() {
let document = Document::new_plain_english_curated("Give the rise to power.");
let expression = SequenceExpr::with(Not::new(AnchorStart))
.then_any_capitalization_of("give")
.then_whitespace()
.then_any_capitalization_of("the")
.then_whitespace()
.then_any_capitalization_of("rise")
.then_whitespace()
.then_any_capitalization_of("to");
let matches = expression
.iter_matches_in_doc(&document)
.collect::<Vec<_>>();
assert!(matches.is_empty());
}
#[test]
fn matches_expression_after_start() {
let document = Document::new_plain_english_curated("They give the rise to power.");
let expression = SequenceExpr::with(Not::new(AnchorStart))
.then_any_capitalization_of("give")
.then_whitespace()
.then_any_capitalization_of("the")
.then_whitespace()
.then_any_capitalization_of("rise")
.then_whitespace()
.then_any_capitalization_of("to");
let matches = expression
.iter_matches_in_doc(&document)
.collect::<Vec<_>>();
assert_eq!(matches.to_strings(&document), ["give the rise to"]);
}
}