Skip to content

Commit

Permalink
Merge pull request #432 from ratmice/cleanup_test
Browse files Browse the repository at this point in the history
Cleanup test helpers
  • Loading branch information
ltratt authored Dec 29, 2023
2 parents 8601fcf + d4cb66e commit 13e217c
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 136 deletions.
118 changes: 51 additions & 67 deletions cfgrammar/src/lib/yacc/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1027,26 +1027,6 @@ mod test {
s[..off].lines().count()
}

macro_rules! incorrect_errs {
($src:ident, $es:expr) => {{
let mut line_cache = crate::newlinecache::NewlineCache::new();
line_cache.feed(&$src);
for e in $es {
if let Some((line, column)) =
line_cache.byte_to_line_num_and_col_num(&$src, e.spans.first().unwrap().start())
{
eprintln!(
"Incorrect error returned {} at line {line} column {column}",
e
)
} else {
eprintln!("Incorrect error returned {} with unknown span", e)
}
}
panic!()
}};
}

macro_rules! line_col {
($src:ident, $span: expr) => {{
let mut line_cache = crate::newlinecache::NewlineCache::new();
Expand Down Expand Up @@ -1080,17 +1060,20 @@ mod test {
}

impl ErrorsHelper for Result<GrammarAST, Vec<YaccGrammarError>> {
#[track_caller]
fn expect_error_at_line(self, src: &str, kind: YaccGrammarErrorKind, line: usize) {
match self.as_ref().map_err(Vec::as_slice) {
Ok(_) => panic!("Parsed ok while expecting error"),
Err([e])
if e.kind == kind
&& line_of_offset(src, e.spans()[0].start()) == line
&& e.spans.len() == 1 => {}
Err(e) => incorrect_errs!(src, e),
}
let errs = self
.as_ref()
.map_err(Vec::as_slice)
.expect_err("Parsed ok while expecting error");
assert_eq!(errs.len(), 1);
let e = &errs[0];
assert_eq!(e.kind, kind);
assert_eq!(line_of_offset(src, e.spans()[0].start()), line);
assert_eq!(e.spans.len(), 1);
}

#[track_caller]
fn expect_error_at_line_col(
self,
src: &str,
Expand All @@ -1101,60 +1084,61 @@ mod test {
self.expect_error_at_lines_cols(src, kind, &mut std::iter::once((line, col)))
}

#[track_caller]
fn expect_error_at_lines_cols(
self,
src: &str,
kind: YaccGrammarErrorKind,
lines_cols: &mut dyn Iterator<Item = (usize, usize)>,
) {
match self.as_ref().map_err(Vec::as_slice) {
Ok(_) => panic!("Parsed ok while expecting error"),
Err([e])
if e.kind == kind
&& line_col!(src, e.spans()[0]) == lines_cols.next().unwrap() =>
{
assert_eq!(
e.spans()[1..]
.iter()
.map(|span| line_col!(src, span))
.collect::<Vec<(usize, usize)>>(),
lines_cols.collect::<Vec<(usize, usize)>>()
);
// Check that it is valid to slice.
for span in e.spans() {
let _ = &src[span.start()..span.end()];
}
}
Err(e) => incorrect_errs!(src, e),
let errs = self
.as_ref()
.map_err(Vec::as_slice)
.expect_err("Parsed ok while expecting error");
assert_eq!(errs.len(), 1);
let e = &errs[0];
assert_eq!(e.kind, kind);
assert_eq!(
e.spans()
.iter()
.map(|span| line_col!(src, span))
.collect::<Vec<(usize, usize)>>(),
lines_cols.collect::<Vec<(usize, usize)>>()
);
// Check that it is valid to slice.
for span in e.spans() {
let _ = &src[span.start()..span.end()];
}
}

#[track_caller]
fn expect_multiple_errors(
self,
src: &str,
expected: &mut dyn Iterator<Item = (YaccGrammarErrorKind, Vec<(usize, usize)>)>,
) {
match self {
Ok(_) => panic!("Parsed ok while expecting error"),
Err(errs)
if errs
.iter()
.map(|e| {
// Check that it is valid to slice the source with the spans.
for span in e.spans() {
let _ = &src[span.start()..span.end()];
}
(
e.kind.clone(),
e.spans()
.iter()
.map(|span| line_col!(src, span))
.collect::<Vec<_>>(),
)
})
.eq(expected) => {}
Err(errs) => incorrect_errs!(src, errs),
let errs = self.expect_err("Parsed ok while expecting error");
for e in &errs {
// Check that it is valid to slice the source with the spans.
for span in e.spans() {
let _ = &src[span.start()..span.end()];
}
}

assert_eq!(
errs.iter()
.map(|e| {
(
e.kind.clone(),
e.spans()
.iter()
.map(|span| line_col!(src, span))
.collect::<Vec<_>>(),
)
})
.collect::<Vec<_>>(),
expected.collect::<Vec<_>>()
);
}
}

Expand Down
1 change: 1 addition & 0 deletions lrlex/src/lib/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ where

/// This struct represents, in essence, a .l file in memory. From it one can produce an
/// [LRNonStreamingLexer] which actually lexes inputs.
#[derive(Debug)]
pub struct LRNonStreamingLexerDef<LexerTypesT: LexerTypes>
where
usize: AsPrimitive<LexerTypesT::StorageT>,
Expand Down
119 changes: 50 additions & 69 deletions lrlex/src/lib/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,25 +635,6 @@ mod test {
use std::collections::HashMap;
use std::fmt::Write as _;

macro_rules! incorrect_errs {
($src:ident, $errs:expr) => {{
for e in $errs {
let mut line_cache = ::cfgrammar::newlinecache::NewlineCache::new();
line_cache.feed(&$src);
if let Some((line, column)) = line_cache
.byte_to_line_num_and_col_num(&$src, e.spans().first().unwrap().start())
{
panic!(
"Incorrect error returned {} at line {line} column {column}",
e
)
} else {
panic!("{}", e)
}
}
}};
}

macro_rules! line_col {
($src:ident, $span: expr) => {{
let mut line_cache = ::cfgrammar::newlinecache::NewlineCache::new();
Expand Down Expand Up @@ -685,79 +666,79 @@ mod test {
}

impl ErrorsHelper for Result<LRNonStreamingLexerDef<DefaultLexerTypes<u8>>, Vec<LexBuildError>> {
#[track_caller]
fn expect_error_at_line(self, src: &str, kind: LexErrorKind, line: usize) {
match self.as_ref().map_err(Vec::as_slice) {
Ok(_) => panic!("Parsed ok while expecting error"),
Err([e])
if e.kind == kind
&& line_of_offset(src, e.spans().first().unwrap().start()) == line
&& e.spans.len() == 1 => {}
Err(e) => incorrect_errs!(src, e),
}
let errs = self
.as_ref()
.map_err(Vec::as_slice)
.expect_err("Parsed ok while expecting error");
assert_eq!(errs.len(), 1);
let e = &errs[0];
assert_eq!(e.kind, kind);
assert_eq!(line_of_offset(src, e.spans()[0].start()), line);
assert_eq!(e.spans.len(), 1);
}

#[track_caller]
fn expect_error_at_line_col(self, src: &str, kind: LexErrorKind, line: usize, col: usize) {
self.expect_error_at_lines_cols(src, kind, &mut std::iter::once((line, col)))
}

#[track_caller]
fn expect_error_at_lines_cols(
self,
src: &str,
kind: LexErrorKind,
lines_cols: &mut dyn Iterator<Item = (usize, usize)>,
) {
match self.as_ref().map_err(Vec::as_slice) {
Ok(_) => panic!("Parsed ok while expecting error"),
Err([e])
if e.kind == kind
&& line_col!(src, e.spans().first().unwrap())
== lines_cols.next().unwrap() =>
{
assert_eq!(
e.spans()
.iter()
.skip(1)
.map(|span| line_col!(src, span))
.collect::<Vec<(usize, usize)>>(),
lines_cols.collect::<Vec<(usize, usize)>>()
);
// Check that it is valid to slice the source with the spans.
for span in e.spans() {
let _ = &src[span.start()..span.end()];
}
}
Err(e) => incorrect_errs!(src, e),
let errs = self
.as_ref()
.map_err(Vec::as_slice)
.expect_err("Parsed ok while expecting error");
assert_eq!(errs.len(), 1);
let e = &errs[0];
assert_eq!(e.kind, kind);
assert_eq!(
e.spans()
.iter()
.map(|span| line_col!(src, span))
.collect::<Vec<(usize, usize)>>(),
lines_cols.collect::<Vec<(usize, usize)>>()
);
// Check that it is valid to slice.
for span in e.spans() {
let _ = &src[span.start()..span.end()];
}
}

#[track_caller]
fn expect_multiple_errors(
self,
src: &str,
expected: &mut dyn Iterator<Item = (LexErrorKind, Vec<(usize, usize)>)>,
) {
match self {
Ok(_) => panic!("Parsed ok while expecting error"),
Err(errs) => {
let linecol_errs = errs
.iter()
.map(|e| {
// Check that it is valid to slice the source with the spans.
for span in e.spans() {
let _ = &src[span.start()..span.end()];
}
(
e.kind.clone(),
e.spans()
.iter()
.map(|span| line_col!(src, span))
.collect::<Vec<_>>(),
)
})
.collect::<Vec<_>>();

assert_eq!(expected.collect::<Vec<_>>(), linecol_errs);
let errs = self.expect_err("Parsed ok while expecting error");
for e in &errs {
// Check that it is valid to slice the source with the spans.
for span in e.spans() {
let _ = &src[span.start()..span.end()];
}
}

assert_eq!(
errs.iter()
.map(|e| {
(
e.kind.clone(),
e.spans()
.iter()
.map(|span| line_col!(src, span))
.collect::<Vec<_>>(),
)
})
.collect::<Vec<_>>(),
expected.collect::<Vec<_>>()
);
}
}

Expand Down

0 comments on commit 13e217c

Please sign in to comment.