Skip to content

Commit

Permalink
refactor: use local functions for inte and string parser tests
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Oct 9, 2024
1 parent 871bb0a commit 57d2664
Show file tree
Hide file tree
Showing 4 changed files with 225 additions and 136 deletions.
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub mod rw;
///
/// Will panic if the conversion fails.
#[must_use]
pub fn bencode_to_json(input_buffer: &[u8]) -> String {
pub fn bencode_to_json_unchecked(input_buffer: &[u8]) -> String {
let mut output = String::new();

let mut parser = BencodeParser::new(input_buffer);
Expand All @@ -26,7 +26,7 @@ pub fn bencode_to_json(input_buffer: &[u8]) -> String {
/// # Errors
///
/// Will return an error if the conversion fails.
pub fn parse_bencode(input_buffer: &[u8]) -> Result<String, Error> {
pub fn try_bencode_to_json(input_buffer: &[u8]) -> Result<String, Error> {
let mut output = String::new();

let mut parser = BencodeParser::new(input_buffer);
Expand Down
38 changes: 19 additions & 19 deletions src/parsers/integer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,58 +82,58 @@ mod tests {
rw::{byte_reader::ByteReader, string_writer::StringWriter},
};

fn bencode_to_json(input_buffer: &[u8]) -> String {
fn bencode_to_json_unchecked(input_buffer: &[u8]) -> String {
let mut output = String::new();

// Skip the first byte 'i'
let mut reader = ByteReader::new(&input_buffer[1..]);

let mut writer = StringWriter::new(&mut output);

parse(&mut reader, &mut writer, b'i').expect("Bencode to JSON conversion failed");
parse_bencode(input_buffer, &mut output).expect("Bencode to JSON conversion failed");

output
}

fn parse_bencode(input_buffer: &[u8]) -> Result<String, Error> {
fn try_bencode_to_json(input_buffer: &[u8]) -> Result<String, Error> {
let mut output = String::new();

// Skip the first byte 'i'
let mut reader = ByteReader::new(&input_buffer[1..]);

let mut writer = StringWriter::new(&mut output);

match parse(&mut reader, &mut writer, b'i') {
match parse_bencode(input_buffer, &mut output) {
Ok(()) => Ok(output),
Err(err) => Err(err),
}
}

fn parse_bencode(input_buffer: &[u8], output: &mut String) -> Result<(), Error> {
let initial_byte = input_buffer[0];

let mut reader = ByteReader::new(&input_buffer[1..]);

let mut writer = StringWriter::new(output);

parse(&mut reader, &mut writer, initial_byte)
}

#[test]
fn zero() {
assert_eq!(bencode_to_json(b"i0e"), "0".to_string());
assert_eq!(bencode_to_json_unchecked(b"i0e"), "0".to_string());
}

#[test]
fn one_digit_integer() {
assert_eq!(bencode_to_json(b"i1e"), "1".to_string());
assert_eq!(bencode_to_json_unchecked(b"i1e"), "1".to_string());
}

#[test]
fn two_digits_integer() {
assert_eq!(bencode_to_json(b"i42e"), "42".to_string());
assert_eq!(bencode_to_json_unchecked(b"i42e"), "42".to_string());
}

#[test]
fn negative_integer() {
assert_eq!(bencode_to_json(b"i-1e"), "-1".to_string());
assert_eq!(bencode_to_json_unchecked(b"i-1e"), "-1".to_string());
}

#[test]
fn it_should_fail_when_it_finds_an_invalid_byte() {
let int_with_invalid_byte = b"iae";

let result = parse_bencode(int_with_invalid_byte);
let result = try_bencode_to_json(int_with_invalid_byte);

assert!(matches!(
result,
Expand Down
Loading

0 comments on commit 57d2664

Please sign in to comment.