Skip to content

Commit

Permalink
refactor: tests using matches macro
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Oct 8, 2024
1 parent 76cda84 commit b4a85e5
Showing 1 changed file with 35 additions and 9 deletions.
44 changes: 35 additions & 9 deletions src/parsers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ impl<R: Read> BencodeParser<R> {
#[cfg(test)]
mod tests {

use crate::parsers::error::Error;

use super::{error, BencodeParser};

#[test]
Expand Down Expand Up @@ -297,7 +299,10 @@ mod tests {

let result = parse(invalid_bencoded_value);

assert!(result.is_err());
assert!(matches!(
result,
Err(Error::UnrecognizedFirstBencodeValueByte(97, 'a'))
));
}

#[test]
Expand All @@ -307,11 +312,17 @@ mod tests {

let result = parse(integer_with_missing_end_byte);

assert!(result.is_err());
assert!(matches!(
result,
Err(Error::UnexpectedEndOfInputParsingInteger)
));
}

mod integers {
use crate::parsers::tests::{parse, to_json};
use crate::parsers::{
error::Error,
tests::{parse, to_json},
};

#[test]
fn zero() {
Expand Down Expand Up @@ -342,7 +353,10 @@ mod tests {

let result = parse(int_with_invalid_byte);

assert!(result.is_err());
assert!(matches!(
result,
Err(Error::UnexpectedByteParsingInteger(97, 'a'))
));
}
}

Expand Down Expand Up @@ -402,7 +416,10 @@ mod tests {
}

mod lists {
use crate::parsers::tests::{parse, to_json};
use crate::parsers::{
error::Error,
tests::{parse, to_json},
};

#[test]
fn empty_list() {
Expand All @@ -415,7 +432,10 @@ mod tests {

let result = parse(end_list_byte_without_start);

assert!(result.is_err());
assert!(matches!(
result,
Err(Error::NoMatchingStartForListOrDictEnd)
));
}

mod with_one_item {
Expand Down Expand Up @@ -559,7 +579,10 @@ mod tests {
}

mod dictionary {
use crate::parsers::tests::{parse, to_json};
use crate::parsers::{
error::Error,
tests::{parse, to_json},
};

// Note: Keys must be bencoded strings.

Expand All @@ -582,7 +605,10 @@ mod tests {

let result = parse(end_dict_byte_without_start);

assert!(result.is_err());
assert!(matches!(
result,
Err(Error::NoMatchingStartForListOrDictEnd)
));
}

#[test]
Expand All @@ -591,7 +617,7 @@ mod tests {

let result = parse(dict_with_missing_key_value);

assert!(result.is_err());
assert!(matches!(result, Err(Error::PrematureEndOfDict)));
}

mod with_one_key {
Expand Down

0 comments on commit b4a85e5

Please sign in to comment.