#[cfg(test)]
mod tests {
use crate::BufferSource;
use crate::error::messages::*;
use crate::nodes::node::Node;
use crate::parser::default::parse;
#[test]
fn test_zero_length_string() {
let mut source = BufferSource::new(b"0:");
assert!(matches!(parse(&mut source), Ok(Node::Str(s)) if s.is_empty()));
}
#[test]
fn test_invalid_string_colon_only() {
let mut source = BufferSource::new(b":");
assert!(matches!(parse(&mut source), Err(s) if s == ERR_INVALID_STRING_LENGTH));
}
#[test]
fn test_invalid_string_length() {
let mut source = BufferSource::new(b"a:test");
assert!(matches!(parse(&mut source), Err(s) if s.contains("Unexpected character")));
}
#[test]
fn test_valid_string() {
let mut source = BufferSource::new(b"4:test");
assert!(matches!(parse(&mut source), Ok(Node::Str(s)) if s == "test"));
}
#[test]
fn test_string_with_longer_content() {
let mut source = BufferSource::new(b"11:hello world");
assert!(matches!(parse(&mut source), Ok(Node::Str(s)) if s == "hello world"));
}
#[test]
fn test_string_truncated() {
let mut source = BufferSource::new(b"10:short");
assert!(matches!(parse(&mut source), Err(s) if s == ERR_INVALID_STRING_LENGTH));
}
#[test]
fn test_string_with_digits() {
let mut source = BufferSource::new(b"5:12345");
assert!(matches!(parse(&mut source), Ok(Node::Str(s)) if s == "12345"));
}
#[test]
fn test_string_with_special_chars() {
let mut source = BufferSource::new(b"3:a:b");
assert!(matches!(parse(&mut source), Ok(Node::Str(s)) if s == "a:b"));
}
#[test]
fn test_string_with_unicode() {
let utf8_str = "test"; let byte_len = utf8_str.as_bytes().len();
let data_str = format!("{}:{}", byte_len, utf8_str);
let mut source = BufferSource::new(data_str.as_bytes());
assert!(matches!(parse(&mut source), Ok(Node::Str(s)) if s == utf8_str));
}
#[test]
fn test_very_large_string_length() {
let mut source = BufferSource::new(b"999999999999:test");
assert!(matches!(parse(&mut source), Err(_)));
}
#[test]
fn test_string_single_char() {
let mut source = BufferSource::new(b"1:x");
assert!(matches!(parse(&mut source), Ok(Node::Str(s)) if s == "x"));
}
#[test]
fn test_string_containing_newline() {
let mut source = BufferSource::new(b"1:\n");
assert!(matches!(parse(&mut source), Ok(Node::Str(s)) if s == "\n"));
}
#[test]
fn test_string_containing_null_byte() {
let mut source = BufferSource::new(b"1:\x00");
assert!(matches!(parse(&mut source), Ok(Node::Str(_))));
}
#[test]
fn test_string_containing_bencode_delimiters() {
let mut source = BufferSource::new(b"4:i42e");
assert!(matches!(parse(&mut source), Ok(Node::Str(s)) if s == "i42e"));
}
#[test]
fn test_string_containing_spaces() {
let mut source = BufferSource::new(b"5:hello");
assert!(matches!(parse(&mut source), Ok(Node::Str(s)) if s == "hello"));
}
#[test]
fn test_string_all_ascii_printable() {
let content = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
let encoded = format!("{}:{}", content.len(), content);
let mut source = BufferSource::new(encoded.as_bytes());
assert!(matches!(parse(&mut source), Ok(Node::Str(s)) if s == content));
}
#[test]
fn test_string_utf8_ascii_only() {
let content = "hello world 123";
let encoded = format!("{}:{}", content.len(), content);
let mut source = BufferSource::new(encoded.as_bytes());
assert!(matches!(parse(&mut source), Ok(Node::Str(s)) if s == content));
}
#[test]
fn test_string_length_with_extra_digits() {
let mut source = BufferSource::new(b"010:helloworld");
assert!(matches!(parse(&mut source), Ok(Node::Str(s)) if s == "helloworld"));
}
#[test]
fn test_string_missing_colon() {
let mut source = BufferSource::new(b"4test");
assert!(matches!(parse(&mut source), Err(_)));
}
#[test]
fn test_string_length_is_negative() {
let mut source = BufferSource::new(b"-4:test");
assert!(matches!(parse(&mut source), Err(s) if s.contains("Unexpected character")));
}
#[test]
fn test_parse_bytes_string() {
use crate::parse_bytes;
assert_eq!(
parse_bytes(b"5:world").unwrap(),
Node::Str("world".to_string())
);
}
#[test]
fn test_parse_bytes_empty_string() {
use crate::parse_bytes;
assert_eq!(parse_bytes(b"0:").unwrap(), Node::Str("".to_string()));
}
#[test]
fn test_parse_str_string() {
use crate::parse_str;
assert_eq!(parse_str("4:rust").unwrap(), Node::Str("rust".to_string()));
}
#[test]
fn test_iterative_empty_string() {
use crate::parse_str_iterative;
assert_eq!(
parse_str_iterative("0:").unwrap(),
Node::Str("".to_string())
);
}
#[test]
fn test_iterative_plain_string() {
use crate::parse_bytes_iterative;
assert_eq!(
parse_bytes_iterative(b"5:hello").unwrap(),
Node::Str("hello".to_string())
);
}
#[test]
fn test_iterative_string_with_special_chars() {
use crate::parse_str_iterative;
assert_eq!(
parse_str_iterative("3:a:b").unwrap(),
Node::Str("a:b".to_string())
);
}
#[test]
fn test_iterative_truncated_string_fails() {
use crate::parse_bytes_iterative;
assert!(parse_bytes_iterative(b"10:hi").is_err());
}
#[test]
fn test_borrowed_basic_string() {
use crate::parse_borrowed;
let node = parse_borrowed(b"4:test").unwrap();
assert!(node.is_bytes());
assert_eq!(node.as_bytes(), Some(b"test".as_ref()));
}
#[test]
fn test_borrowed_empty_string() {
use crate::parse_borrowed;
let node = parse_borrowed(b"0:").unwrap();
assert!(node.is_bytes());
assert_eq!(node.as_bytes(), Some(b"".as_ref()));
}
#[test]
fn test_borrowed_string_to_owned() {
use crate::parse_borrowed;
let borrowed = parse_borrowed(b"5:hello").unwrap();
let owned = borrowed.to_node();
assert_eq!(owned, Node::Str("hello".to_string()));
}
#[test]
fn test_borrowed_truncated_string_fails() {
use crate::{error::messages::ERR_STRING_TOO_SHORT, parse_borrowed};
assert!(matches!(parse_borrowed(b"10:hi"), Err(s) if s == ERR_STRING_TOO_SHORT));
}
#[test]
fn test_round_trip_empty_string() {
use crate::{parse_str, stringify_to_string};
let node = Node::Str("".to_string());
let encoded = stringify_to_string(&node).unwrap();
assert_eq!(encoded, "0:");
assert_eq!(parse_str(&encoded).unwrap(), node);
}
#[test]
fn test_round_trip_plain_string() {
use crate::{parse_bytes, stringify_to_bytes};
let node = Node::Str("bencode".to_string());
let encoded = stringify_to_bytes(&node).unwrap();
assert_eq!(parse_bytes(&encoded).unwrap(), node);
}
#[test]
fn test_round_trip_string_with_delimiters() {
use crate::{parse_bytes, stringify_to_bytes};
let node = Node::Str("d3:keyi42ee".to_string());
let encoded = stringify_to_bytes(&node).unwrap();
assert_eq!(parse_bytes(&encoded).unwrap(), node);
}
#[test]
fn test_string_as_dict_value() {
use crate::parse_str;
let node = parse_str("d3:key5:valuee").unwrap();
assert_eq!(node.get("key"), Some(&Node::Str("value".to_string())));
}
#[test]
fn test_string_inside_list() {
use crate::parse_str;
let node = parse_str("l4:spam4:eggse").unwrap();
assert_eq!(
node,
Node::List(vec![
Node::Str("spam".to_string()),
Node::Str("eggs".to_string())
])
);
}
}