Skip to content

Commit

Permalink
Fiddle with coverage changes (#141)
Browse files Browse the repository at this point in the history
* Fiddle with coverage changes

* fiddle
  • Loading branch information
CrockAgile committed Dec 9, 2023
1 parent 67a7054 commit 86c441a
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 128 deletions.
26 changes: 1 addition & 25 deletions src/append_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub(crate) use append_only_vec_id;

/// Vector type which does *not* allow item removal during lifetime.
/// Useful for data structures with complex, shared ownership, such as graphs.
#[derive(Debug, Clone)]
#[derive(Debug)]
pub(crate) struct AppendOnlyVec<T, I> {
vec: Vec<T>,
id_type: std::marker::PhantomData<I>,
Expand Down Expand Up @@ -79,27 +79,3 @@ impl<T, K> From<Vec<T>> for AppendOnlyVec<T, K> {
}
}
}

impl<T, K> IntoIterator for AppendOnlyVec<T, K> {
type Item = <Vec<T> as IntoIterator>::Item;
type IntoIter = <Vec<T> as IntoIterator>::IntoIter;
fn into_iter(self) -> Self::IntoIter {
self.vec.into_iter()
}
}

impl<'a, T, K> IntoIterator for &'a AppendOnlyVec<T, K> {
type Item = &'a T;
type IntoIter = std::slice::Iter<'a, T>;
fn into_iter(self) -> Self::IntoIter {
self.vec.iter()
}
}

impl<'a, T, K> IntoIterator for &'a mut AppendOnlyVec<T, K> {
type Item = &'a mut T;
type IntoIter = std::slice::IterMut<'a, T>;
fn into_iter(self) -> Self::IntoIter {
self.vec.iter_mut()
}
}
44 changes: 12 additions & 32 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@ impl fmt::Display for Error {
}
}

impl error::Error for Error {
fn description(&self) -> &str {
"BNF error"
}
}
impl error::Error for Error {}

impl<'a> From<VerboseError<(&'a str, VerboseErrorKind)>> for Error {
fn from(err: VerboseError<(&str, VerboseErrorKind)>) -> Self {
Expand Down Expand Up @@ -60,42 +56,19 @@ mod tests {
#[test]
fn gets_error_error() {
let nom_result = give_error_kind("12340");
let nom_error;
match nom_result {
Result::Err(e) => match e {
Err::Error(_) => nom_error = e,
_ => panic!("gets_error_error should result in IResult::Err(Err::Error(e))"),
},
_ => panic!("gets_error_error should result in IResult::Err"),
}

match Error::from(nom_error) {
Error::ParseError(_) => (),
e => panic!("production error should be error parsing: {e:?}"),
}
assert!(matches!(nom_result, Result::Err(Err::Error(_))));
}

#[test]
fn gets_error_on_incomplete() {
let nom_result = give_error_kind("");
let nom_error = match nom_result {
Result::Err(e) => e,
_ => panic!("gets_error_error should result in IResult::Err"),
};

match Error::from(nom_error) {
Error::ParseError(_) => (),
e => panic!("production error should be parse error: {e:?}"),
}
let nom_result = give_error_kind("").map_err(Error::from);
assert!(matches!(nom_result, Result::Err(Error::ParseError(_))));
}

#[test]
fn uses_error_generate() {
let bnf_error = Error::GenerateError(String::from("error generating!"));
match bnf_error {
Error::GenerateError(_) => (),
e => panic!("should match on generate error: {e:?}"),
}
assert!(matches!(bnf_error, Error::GenerateError(_)));
}

#[test]
Expand All @@ -122,4 +95,11 @@ mod tests {
let verbose_kind = nom::error::VerboseErrorKind::Char('z');
let _ = Error::from((description, verbose_kind));
}

#[test]
fn clone_error() {
let error = Error::ParseError(String::from("parsing error!"));
let clone = error.clone();
assert_eq!(error, clone);
}
}
23 changes: 8 additions & 15 deletions src/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,26 +298,19 @@ mod tests {
#[test]
fn parse_error() {
let expression = Expression::from_str("<base> <dna");
assert!(expression.is_err(), "{expression:?} should be error");

let error = expression.unwrap_err();
match error {
Error::ParseError(_) => (),
_ => panic!("{error} should be should be error"),
}
assert!(
matches!(expression, Err(Error::ParseError(_))),
"{expression:?} should be error"
);
}

#[test]
fn parse_incomplete() {
let result = Expression::from_str("");
assert!(result.is_err(), "{result:?} should be err");
match result {
Err(e) => match e {
Error::ParseError(_) => (),
e => panic!("should should be Error::ParseError: {e:?}"),
},
Ok(s) => panic!("should should be Error::ParseError: {s}"),
}
assert!(
matches!(result, Err(Error::ParseError(_))),
"{result:?} should be ParseError"
);
}

#[test]
Expand Down
28 changes: 10 additions & 18 deletions src/grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,20 +281,16 @@ impl Grammar {
let nonterm = Term::Nonterminal(ident.to_string());
let find_lhs = self.productions_iter().find(|&x| x.lhs == nonterm);

let production = match find_lhs {
Some(p) => p,
None => return Ok(nonterm.to_string()),
let Some(production) = find_lhs else {
return Ok(nonterm.to_string());
};

let expressions = production.rhs_iter().collect::<Vec<&Expression>>();

let expression = match expressions.choose(rng) {
Some(e) => e,
None => {
return Err(Error::GenerateError(String::from(
"Couldn't select random Expression!",
)));
}
let Some(expression) = expressions.choose(rng) else {
return Err(Error::GenerateError(String::from(
"Couldn't select random Expression!",
)));
};

let mut result = String::new();
Expand Down Expand Up @@ -658,14 +654,10 @@ mod tests {
#[test]
fn parse_error_on_incomplete() {
let result: Result<Grammar, _> = "".parse();
assert!(result.is_err(), "{result:?} should be err");
match result {
Err(e) => match e {
Error::ParseError(_) => (),
e => panic!("should should be Error::ParseError: {e:?}"),
},
Ok(s) => panic!("should should be Error::ParseError: {s}"),
}
assert!(
matches!(result, Err(Error::ParseError(_))),
"{result:?} should be ParseError"
);
}

#[test]
Expand Down
45 changes: 21 additions & 24 deletions src/production.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,9 @@ mod tests {

fn prop_to_string_and_back(prop: Production) -> TestResult {
let to_string = prop.to_string();
let from_str = Production::from_str(&to_string);
match from_str {
Ok(from_prod) => TestResult::from_bool(from_prod == prop),
_ => TestResult::error(format!("{prop} to string and back should be safe")),
}
let from_str = Production::from_str(&to_string)
.expect("should be able to convert production to string and back");
TestResult::from_bool(from_str == prop)
}

#[test]
Expand Down Expand Up @@ -253,35 +251,28 @@ mod tests {
);

let production = result.unwrap_err();
match production {
Error::ParseError(_) => (),
e => panic!("production error should be error: {e:?}"),
}
assert!(
matches!(production, Error::ParseError(_)),
"production error should be error: {production:?}"
);
}

#[test]
fn parse_incomplete() {
let result = Production::from_str("");
assert!(result.is_err(), "{result:?} should be err");
match result {
Err(e) => match e {
Error::ParseError(_) => (),
e => panic!("should should be Error::ParseError: {e:?}"),
},
Ok(s) => panic!("should should be Error::ParseError: {s}"),
}
assert!(
matches!(result, Err(Error::ParseError(_))),
"production result should be error {result:?}"
);
}

#[test]
fn parse_semicolon_separated() {
let result = Production::from_str("<base> ::= 'A' ; 'C' ; 'G' ; 'T'");
assert!(result.is_err(), "{result:?} should be error");

let production = result.unwrap_err();
match production {
Error::ParseError(_) => (),
e => panic!("invalid production should be parsing error: {e:?}"),
}
assert!(
matches!(result, Err(Error::ParseError(_))),
"production result should be error {result:?}"
);
}

#[test]
Expand All @@ -290,6 +281,12 @@ mod tests {
assert!(production.is_empty());
}

#[test]
fn production_len() {
let production: Production = "<dna> ::= <base> | <dna> <base>".parse().unwrap();
assert_eq!(production.len(), 2);
}

#[test]
fn format_production() {
let production: Production = "<dna> ::= <base> | <dna> <base>".parse().unwrap();
Expand Down
4 changes: 2 additions & 2 deletions src/slice_iter.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// Generic Iterator wrapper of slice
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, PartialEq)]
pub(crate) struct SliceIter<'a, T> {
pub(crate) slice: &'a [T],
}
Expand All @@ -20,7 +20,7 @@ impl<'a, T> Iterator for SliceIter<'a, T> {
}

/// Generic Iterator wrapper of mutable slice
#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq)]
pub(crate) struct SliceIterMut<'a, T> {
pub(crate) slice: &'a mut [T],
}
Expand Down
14 changes: 2 additions & 12 deletions src/term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,23 +120,13 @@ mod tests {
assert!(incomplete.is_err());

let error = incomplete.unwrap_err();
match error {
Error::ParseError(ref s) => assert!(s.starts_with("Parsing error:")),
_ => panic!("Incomplete term should be parse error"),
}
assert!(matches!(error, Error::ParseError(s) if s.starts_with("Parsing error: ")));
}

#[test]
fn parse_incomplete() {
let result = Term::from_str("");
assert!(result.is_err(), "{result:?} should be err");
match result {
Err(e) => match e {
Error::ParseError(_) => (),
e => panic!("should should be Error::ParseError: {e:?}"),
},
Ok(s) => panic!("should should be Error::ParseError: {s}"),
}
assert!(matches!(result, Err(Error::ParseError(_))));
}

#[test]
Expand Down

0 comments on commit 86c441a

Please sign in to comment.