Skip to content

Commit

Permalink
test: more
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Oct 16, 2024
1 parent 05dbef2 commit 4699f84
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
6 changes: 4 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! This module contains the main functions to convert bencoded bytes into a JSON string.
//! This lib contains functions to convert bencoded bytes into a JSON string.
//!
//! These are high level functions that call the lower level parser.
//! There are high-level functions for common purposes that call the lower level
//! parser. You can use the low-lever parser if the high-level wrappers are not
//! suitable for your needs.
use parsers::{error::Error, BencodeParser};

pub mod parsers;
Expand Down
12 changes: 10 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
//! Converts Bencode to JSON.
//!
//! For example:
//! Usage:
//!
//! Using stdin and stdout:
//!
//! ```text
//! echo "i42e" | cargo run
//! ```
//!
//! Using files:
//!
//! ```text
//! printf "i42e" | cargo run
//! cargo run -- -i ./tests/fixtures/sample.bencode -o output.json
//! ```
use clap::{Arg, Command};
use std::fs::File;
Expand Down
33 changes: 29 additions & 4 deletions src/parsers/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,16 @@ pub struct ReadContext {

impl fmt::Display for ReadContext {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "read context: ")?;
write!(f, "read context:")?;

match self.byte {
None => {}
Some(byte) => write!(f, "byte `{}` (char: `{}`) ", byte, byte as char)?,
Some(byte) => write!(f, " byte `{}` (char: `{}`),", byte, byte as char)?,
}

write!(
f,
"at input pos {}, latest input bytes dump: {:?}",
" input pos {}, latest input bytes dump: {:?}",
self.pos, self.latest_bytes
)?;

Expand All @@ -126,7 +126,32 @@ mod tests {
latest_bytes: vec![b'a', b'b', b'c'],
};

assert_eq!("read context: byte `97` (char: `a`) at input pos 10, latest input bytes dump: [97, 98, 99] (UTF-8 string: `abc`)", read_context.to_string());
assert_eq!( read_context.to_string(),"read context: byte `97` (char: `a`), input pos 10, latest input bytes dump: [97, 98, 99] (UTF-8 string: `abc`)");
}

#[test]
fn it_should_not_display_the_byte_if_it_is_none() {
let read_context = ReadContext {
byte: None,
pos: 10,
latest_bytes: vec![b'a', b'b', b'c'],
};

assert_eq!(read_context.to_string(), "read context: input pos 10, latest input bytes dump: [97, 98, 99] (UTF-8 string: `abc`)");
}

#[test]
fn it_should_not_display_the_latest_bytes_as_string_if_it_is_not_a_valid_string() {
let read_context = ReadContext {
byte: None,
pos: 10,
latest_bytes: vec![b'\xFF', b'\xFE'],
};

assert_eq!(
read_context.to_string(),
"read context: input pos 10, latest input bytes dump: [255, 254]"
);
}
}
}
2 changes: 2 additions & 0 deletions src/parsers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Parsers, including the main parser and the parsers for the basic types
//! (integer and string)
pub mod error;
pub mod integer;
pub mod stack;
Expand Down

0 comments on commit 4699f84

Please sign in to comment.