use ferrotype::Ferrotype;
#[test]
fn test_add_section() {
let mut ferrotype = Ferrotype::new();
ferrotype.add("Test Section", "Test content".to_string());
let output = ferrotype.as_string();
assert!(output.contains("Test Section: >"));
assert!(output.contains(" Test content"));
}
#[test]
fn test_add_debug_section() {
let mut snapshot = Ferrotype::new();
let test_data = vec![1, 2, 3];
snapshot.add_debug("Debug Section", &test_data);
let output = snapshot.as_string();
assert!(output.contains("Debug Section: >"));
assert!(output.contains("["));
assert!(output.contains("1"));
assert!(output.contains("2"));
assert!(output.contains("3"));
assert!(output.contains("]"));
ferrotype::assert!(snapshot);
}
#[test]
fn test_multiple_sections() {
let mut snapshot = Ferrotype::new();
snapshot.add("First", "First content".to_string());
snapshot.add("Second", "Second content".to_string());
let output = snapshot.as_string();
assert!(output.contains("First: >"));
assert!(output.contains(" First content"));
assert!(output.contains("Second: >"));
assert!(output.contains(" Second content"));
ferrotype::assert!(snapshot);
}
#[test]
fn test_multiline_content_indentation() {
let mut snapshot = Ferrotype::new();
let multiline_content = "Line 1\nLine 2\nLine 3";
snapshot.add("Multiline", multiline_content.to_string());
let output = snapshot.as_string();
assert!(output.contains(" Line 1"));
assert!(output.contains(" Line 2"));
assert!(output.contains(" Line 3"));
ferrotype::assert!(snapshot);
}
#[test]
fn test_empty_sections() {
let snapshot = Ferrotype::new();
let output = snapshot.as_string();
assert_eq!(output, "");
ferrotype::assert!(snapshot);
}
#[test]
fn test_display_trait() {
let mut snapshot = Ferrotype::new();
snapshot.add("Test", "\x1b[31mcontent\x1b[0m".to_string());
let display_output = format!("{snapshot}");
let as_string_output = snapshot.as_string();
#[cfg(not(feature = "anstream"))]
assert_eq!(display_output, as_string_output);
#[cfg(feature = "anstream")]
assert_ne!(display_output, as_string_output);
ferrotype::assert!(snapshot);
}
#[test]
fn test_debug_trait() {
let mut snapshot = Ferrotype::new();
snapshot.add("Test", "content".to_string());
let debug_output = format!("{snapshot:?}");
assert!(debug_output.contains("Ferrotype"));
assert!(debug_output.contains("expect_errors"));
assert!(debug_output.contains("filter_memory_addresses"));
assert!(debug_output.contains("sections"));
ferrotype::assert!(snapshot);
}
#[test]
fn test_get_output_dir() {
let output_dir = ferrotype::get_output_dir();
#[cfg(feature = "dot_snapshots")]
assert_eq!(output_dir, ".snapshots");
#[cfg(not(feature = "dot_snapshots"))]
assert_eq!(output_dir, "snapshots");
}
#[test]
fn test_title_formatting() {
let mut snapshot = Ferrotype::new();
snapshot.add("snake_case_title", "content".to_string());
snapshot.add("UPPER_CASE_TITLE", "content".to_string());
snapshot.add("mixedCase_title", "content".to_string());
let output = snapshot.as_string();
assert!(output.contains("SnakeCaseTitle: >"));
assert!(output.contains("UpperCaseTitle: >"));
assert!(output.contains("MixedCaseTitle: >"));
ferrotype::assert!(snapshot);
}
#[test]
fn test_empty_title() {
let mut snapshot = Ferrotype::new();
snapshot.add("", "content".to_string());
let output = snapshot.as_string();
assert!(output.contains(": >"));
assert!(output.contains(" content"));
ferrotype::assert!(snapshot);
}
#[test]
fn test_special_characters_in_content() {
let mut snapshot = Ferrotype::new();
let special_content = "Content with\ttabs\nand\rcarriage returns\0and nulls";
snapshot.add("Special", special_content.to_string());
let output = snapshot.as_string();
assert!(output.contains("Special: >"));
assert!(output.contains(" Content with"));
ferrotype::assert!(snapshot);
}
#[test]
fn test_print_methods_dont_panic() {
let mut snapshot = Ferrotype::new();
snapshot.add("Test", "content".to_string());
snapshot.print();
snapshot.print_stdout();
snapshot.print_stderr();
ferrotype::assert!(snapshot);
}
#[test]
fn test_add_debug_various_types() {
let mut snapshot = Ferrotype::new();
snapshot.add_debug("String", "Hello".to_string());
snapshot.add_debug("Integer", 42);
snapshot.add_debug("Float", 1.23456789);
snapshot.add_debug("Boolean", true);
snapshot.add_debug("Option", Some(10));
snapshot.add_debug("Result", Ok::<i32, &str>(42));
let output = snapshot.as_string();
assert!(output.contains("String: >"));
assert!(output.contains("Integer: >"));
assert!(output.contains("Float: >"));
assert!(output.contains("Boolean: >"));
assert!(output.contains("Option: >"));
assert!(output.contains("Result: >"));
ferrotype::assert!(snapshot);
}
#[test]
fn test_sections_are_separated() {
let mut snapshot = Ferrotype::new();
snapshot.add("First", "first content".to_string());
snapshot.add("Second", "second content".to_string());
let output = snapshot.as_string();
assert!(output.contains("first content\n\nSecond: >"));
ferrotype::assert!(snapshot);
}
#[test]
fn test_filter_memory_addresses_enabled() {
let mut snapshot = Ferrotype::new();
assert!(snapshot.filter_memory_addresses());
snapshot.add(
"Memory Addresses",
"Pointer at 0x7fff5fbff710 and another at 0xDEADBEEF".to_string(),
);
snapshot
.add_debug("Debug with Address", format!("Object at {:p}", &snapshot));
let content = snapshot.as_string();
assert!(content.contains("0x7fff5fbff710"));
assert!(content.contains("0xDEADBEEF"));
ferrotype::assert!(snapshot);
}
#[test]
fn test_filter_memory_addresses_disabled() {
let mut snapshot = Ferrotype::new();
snapshot.set_filter_memory_addresses(false);
assert!(!snapshot.filter_memory_addresses());
snapshot.add(
"Memory Addresses",
"Pointer at 0x7fff5fbff710 and another at 0xDEADBEEF".to_string(),
);
snapshot.add_debug("Debug with Address", "Object at 0xCAFEBABE");
let content = snapshot.as_string();
assert!(content.contains("0x7fff5fbff710"));
assert!(content.contains("0xDEADBEEF"));
ferrotype::assert!(snapshot);
}
#[test]
fn test_filter_memory_addresses_various_formats() {
let mut snapshot = Ferrotype::new();
snapshot.set_filter_memory_addresses(true);
snapshot.add("Lowercase", "address: 0xdeadbeef".to_string());
snapshot.add("Uppercase", "ADDRESS: 0xDEADBEEF".to_string());
snapshot.add("Mixed Case", "ptr: 0xDEadBEef".to_string());
snapshot.add("Short", "0x42".to_string());
snapshot.add("Long", "0x7fff5fbff710abcdef".to_string());
snapshot.add("Multiple", "start: 0x1000 end: 0x2000".to_string());
snapshot.add("With Whitespace", "addr: 0x12345678 here".to_string());
ferrotype::assert!(snapshot);
}
#[test]
fn test_filter_memory_addresses_preserves_non_addresses() {
let mut snapshot = Ferrotype::new();
snapshot.set_filter_memory_addresses(true);
snapshot.add("Hex Without 0x", "deadbeef is not an address".to_string());
snapshot.add("Decimal", "12345678 is a number".to_string());
snapshot.add("Partial Match", "0xGHIJK is not valid hex".to_string());
snapshot.add("Email", "user@0x42.com should stay".to_string());
let content = snapshot.as_string();
assert!(content.contains("deadbeef is not an address"));
assert!(content.contains("12345678 is a number"));
assert!(content.contains("0xGHIJK is not valid hex"));
assert!(content.contains("user@0x42.com should stay"));
ferrotype::assert!(snapshot);
}
#[test]
fn test_clone_preserves_filter_setting() {
let mut original = Ferrotype::new();
original.set_filter_memory_addresses(false);
original.add("Test", "Memory at 0x1234".to_string());
let cloned = original.clone();
assert_eq!(
cloned.filter_memory_addresses(),
original.filter_memory_addresses()
);
assert!(!cloned.filter_memory_addresses());
}
#[test]
fn test_filter_uuids_enabled() {
let mut snapshot = Ferrotype::new();
assert!(snapshot.filter_uuids());
snapshot.add(
"UUIDs",
"ID: 550e8400-e29b-41d4-a716-446655440000 and 123e4567-e89b-12d3-a456-426614174000".to_string(),
);
snapshot.add(
"Mixed Content",
"User 6ba7b810-9dad-11d1-80b4-00c04fd430c8 logged in".to_string(),
);
let content = snapshot.as_string();
assert!(content.contains("550e8400-e29b-41d4-a716-446655440000"));
assert!(content.contains("123e4567-e89b-12d3-a456-426614174000"));
assert!(content.contains("6ba7b810-9dad-11d1-80b4-00c04fd430c8"));
ferrotype::assert!(snapshot);
}
#[test]
fn test_filter_uuids_disabled() {
let mut snapshot = Ferrotype::new();
snapshot.set_filter_uuids(false);
assert!(!snapshot.filter_uuids());
snapshot.add(
"UUIDs",
"ID: 550e8400-e29b-41d4-a716-446655440000 and 123e4567-e89b-12d3-a456-426614174000".to_string(),
);
snapshot.add(
"Mixed Content",
"User 6ba7b810-9dad-11d1-80b4-00c04fd430c8 logged in".to_string(),
);
let content = snapshot.as_string();
assert!(content.contains("550e8400-e29b-41d4-a716-446655440000"));
assert!(content.contains("123e4567-e89b-12d3-a456-426614174000"));
assert!(content.contains("6ba7b810-9dad-11d1-80b4-00c04fd430c8"));
ferrotype::assert!(snapshot);
}
#[test]
fn test_filter_uuids_various_formats() {
let mut snapshot = Ferrotype::new();
snapshot.set_filter_uuids(true);
snapshot.add(
"Lowercase",
"uuid: 550e8400-e29b-41d4-a716-446655440000".to_string(),
);
snapshot.add(
"Uppercase",
"UUID: 550E8400-E29B-41D4-A716-446655440000".to_string(),
);
snapshot.add(
"Mixed Case",
"id: 550e8400-E29B-41d4-A716-446655440000".to_string(),
);
snapshot.add("Multiple", "start: 123e4567-e89b-12d3-a456-426614174000 end: 6ba7b810-9dad-11d1-80b4-00c04fd430c8".to_string());
snapshot.add(
"In Sentence",
"The user 550e8400-e29b-41d4-a716-446655440000 was created".to_string(),
);
snapshot.add(
"With Punctuation",
"uuid(550e8400-e29b-41d4-a716-446655440000)".to_string(),
);
ferrotype::assert!(snapshot);
}
#[test]
fn test_filter_uuids_preserves_non_uuids() {
let mut snapshot = Ferrotype::new();
snapshot.set_filter_uuids(true);
snapshot.add("Not Enough Groups", "550e8400-e29b-41d4-a716".to_string());
snapshot.add(
"Wrong Format",
"550e8400e29b41d4a716446655440000".to_string(),
);
snapshot.add("Too Short", "550e8400-e29b-41d4-a716-44665544".to_string());
snapshot.add(
"Too Long",
"550e8400-e29b-41d4-a716-446655440000-extra".to_string(),
);
snapshot.add(
"Invalid Chars",
"550e8400-e29b-41d4-a716-44665544000g".to_string(),
);
snapshot.add(
"Partial Match",
"prefix-550e8400-e29b-41d4-a716-446655440000-suffix".to_string(),
);
let content = snapshot.as_string();
assert!(content.contains("550e8400-e29b-41d4-a716"));
assert!(content.contains("550e8400e29b41d4a716446655440000"));
assert!(content.contains("550e8400-e29b-41d4-a716-44665544"));
assert!(content.contains("550e8400-e29b-41d4-a716-446655440000-extra"));
assert!(content.contains("550e8400-e29b-41d4-a716-44665544000g"));
ferrotype::assert!(snapshot);
}
#[test]
fn test_set_filter_random_ids_enables_all() {
let mut snapshot = Ferrotype::new();
snapshot.set_filter_memory_addresses(false);
snapshot.set_filter_uuids(false);
assert!(!snapshot.filter_memory_addresses());
assert!(!snapshot.filter_uuids());
snapshot.set_filter_random_ids(true);
assert!(snapshot.filter_memory_addresses());
assert!(snapshot.filter_uuids());
snapshot.add(
"Mixed IDs",
"Pointer at 0xDEADBEEF and UUID 550e8400-e29b-41d4-a716-446655440000"
.to_string(),
);
ferrotype::assert!(snapshot);
}
#[test]
fn test_set_filter_random_ids_disables_all() {
let mut snapshot = Ferrotype::new();
assert!(snapshot.filter_memory_addresses());
assert!(snapshot.filter_uuids());
snapshot.set_filter_random_ids(false);
assert!(!snapshot.filter_memory_addresses());
assert!(!snapshot.filter_uuids());
snapshot.add(
"Mixed IDs",
"Pointer at 0xDEADBEEF and UUID 550e8400-e29b-41d4-a716-446655440000"
.to_string(),
);
ferrotype::assert!(snapshot);
}
#[test]
fn test_clone_preserves_uuid_filter_setting() {
let mut original = Ferrotype::new();
original.set_filter_uuids(false);
original.add(
"Test",
"UUID: 550e8400-e29b-41d4-a716-446655440000".to_string(),
);
let cloned = original.clone();
assert_eq!(cloned.filter_uuids(), original.filter_uuids());
assert!(!cloned.filter_uuids());
}
#[test]
fn test_combined_filtering() {
let mut snapshot = Ferrotype::new();
snapshot.set_filter_random_ids(true);
snapshot.add(
"All Types",
"Memory: 0x12345678, UUID: 550e8400-e29b-41d4-a716-446655440000, Regular: 12345".to_string(),
);
snapshot.add(
"Complex",
"Object 0xABCDEF with ID 123e4567-e89b-12d3-a456-426614174000 at index 42"
.to_string(),
);
let content = snapshot.as_string();
assert!(content.contains("0x12345678"));
assert!(content.contains("550e8400-e29b-41d4-a716-446655440000"));
assert!(content.contains("0xABCDEF"));
assert!(content.contains("123e4567-e89b-12d3-a456-426614174000"));
assert!(content.contains("12345"));
ferrotype::assert!(snapshot);
}
#[test]
fn test_filter_type_ids_enabled() {
let mut snapshot = Ferrotype::new();
assert!(snapshot.filter_type_ids());
snapshot.add(
"TypeIds",
"Type: TypeId { t: 12345, } and TypeId { t: 67890, }".to_string(),
);
snapshot.add(
"Spaced TypeId",
"Found TypeId { t: 99999 , } in debug output".to_string(),
);
let content = snapshot.as_string();
assert!(content.contains("TypeId { t: 12345, }"));
assert!(content.contains("TypeId { t: 67890, }"));
ferrotype::assert!(snapshot);
}
#[test]
fn test_filter_type_ids_disabled() {
let mut snapshot = Ferrotype::new();
snapshot.set_filter_type_ids(false);
assert!(!snapshot.filter_type_ids());
snapshot.add(
"TypeIds",
"Type: TypeId { t: 12345, } and TypeId { t: 67890, }".to_string(),
);
snapshot.add(
"Spaced TypeId",
"Found TypeId { t: 99999 , } in debug output".to_string(),
);
ferrotype::assert!(snapshot);
}
#[test]
fn test_filter_hashes_enabled() {
let mut snapshot = Ferrotype::new();
assert!(snapshot.filter_hashes());
snapshot.add(
"Hashes",
"Item with hash: 12345, and another hash: 67890, here".to_string(),
);
snapshot.add(
"Multiple Hashes",
"First hash: 111, second hash: 222, third hash: 333, done".to_string(),
);
let content = snapshot.as_string();
assert!(content.contains("hash: 12345,"));
assert!(content.contains("hash: 67890,"));
ferrotype::assert!(snapshot);
}
#[test]
fn test_filter_hashes_disabled() {
let mut snapshot = Ferrotype::new();
snapshot.set_filter_hashes(false);
assert!(!snapshot.filter_hashes());
snapshot.add(
"Hashes",
"Item with hash: 12345, and another hash: 67890, here".to_string(),
);
snapshot.add(
"Multiple Hashes",
"First hash: 111, second hash: 222, third hash: 333, done".to_string(),
);
ferrotype::assert!(snapshot);
}
#[test]
fn test_filter_hashes_preserves_non_hash_patterns() {
let mut snapshot = Ferrotype::new();
snapshot.set_filter_hashes(true);
snapshot.add("No Comma", "hash: 12345 without comma".to_string());
snapshot.add("Wrong Format", "hash:12345, (no space)".to_string());
snapshot.add("Not Hash", "bash: 12345, is different".to_string());
snapshot.add("Partial", "hashmap: 12345, is not hash:".to_string());
let content = snapshot.as_string();
assert!(content.contains("hash: 12345 without comma"));
assert!(content.contains("hash:12345,")); assert!(content.contains("bash: 12345,"));
assert!(content.contains("hashmap: 12345,"));
ferrotype::assert!(snapshot);
}
#[test]
fn test_set_filter_random_ids_includes_type_ids_and_hashes() {
let mut snapshot = Ferrotype::new();
snapshot.set_filter_memory_addresses(false);
snapshot.set_filter_uuids(false);
snapshot.set_filter_type_ids(false);
snapshot.set_filter_hashes(false);
assert!(!snapshot.filter_memory_addresses());
assert!(!snapshot.filter_uuids());
assert!(!snapshot.filter_type_ids());
assert!(!snapshot.filter_hashes());
snapshot.set_filter_random_ids(true);
assert!(snapshot.filter_memory_addresses());
assert!(snapshot.filter_uuids());
assert!(snapshot.filter_type_ids());
assert!(snapshot.filter_hashes());
snapshot.add(
"All Random IDs",
"Memory: 0xDEADBEEF, UUID: 550e8400-e29b-41d4-a716-446655440000, TypeId { t: 12345, }, hash: 67890,".to_string(),
);
ferrotype::assert!(snapshot);
}
#[test]
fn test_clone_preserves_type_id_and_hash_settings() {
let mut original = Ferrotype::new();
original.set_filter_type_ids(false);
original.set_filter_hashes(false);
original.add("Test", "TypeId { t: 12345, } hash: 67890,".to_string());
let cloned = original.clone();
assert_eq!(cloned.filter_type_ids(), original.filter_type_ids());
assert_eq!(cloned.filter_hashes(), original.filter_hashes());
assert!(!cloned.filter_type_ids());
assert!(!cloned.filter_hashes());
}
#[test]
fn test_combined_all_filters() {
let mut snapshot = Ferrotype::new();
snapshot.set_filter_random_ids(true);
snapshot.add(
"Everything",
"Ptr: 0x12345678, ID: 550e8400-e29b-41d4-a716-446655440000, Type: TypeId { t: 99999, }, Item hash: 12345, done".to_string(),
);
snapshot.add(
"Debug Output",
"Object { ptr: 0xABCDEF, uuid: 123e4567-e89b-12d3-a456-426614174000, type_id: TypeId { t: 777, }, hash: 888, data: [1, 2, 3] }".to_string(),
);
ferrotype::assert!(snapshot);
}