ferrotype 0.1.1

An opinionated wrapper for insta.rs
Documentation
// Copyright Two Neutron Stars Incorporated and contributors
// SPDX-License-Identifier: BlueOak-1.0.0

//! Integration tests for ferrotype
//!
//! These tests verify that the complete workflow works as expected,
//! including the interaction between different features.

use ferrotype::Ferrotype;

#[derive(Debug, Clone)]
#[allow(unused)]
struct TestData {
  name: String,
  count: u32,
  items: Vec<String>,
}

#[test]
fn test_complete_workflow() {
  // Step 1: Create a new snapshot
  let mut snapshot = Ferrotype::new();

  // Step 2: Configure the snapshot
  snapshot
    .set_filter_memory_addresses(true)
    .set_expect_errors(false);

  // Step 3: Add various types of content
  snapshot.add("Description", "Integration test for ferrotype".to_string());

  let test_data = TestData {
    name: "integration_test".to_string(),
    count: 42,
    items: vec![
      "first".to_string(),
      "second".to_string(),
      "third".to_string(),
    ],
  };

  snapshot.add_debug("Test Data", &test_data);
  snapshot.add(
    "Summary",
    "This test verifies the complete workflow".to_string(),
  );

  // Step 4: Verify the snapshot content
  let content = snapshot.as_string();

  // Verify all sections are present and properly formatted
  assert!(content.contains("Description: >"));
  assert!(content.contains("Integration test for ferrotype"));
  assert!(content.contains("Test Data: >"));
  assert!(content.contains("name: \"integration_test\""));
  assert!(content.contains("count: 42"));
  assert!(content.contains("Summary: >"));
  assert!(content.contains("complete workflow"));

  // Verify proper formatting and indentation
  assert!(content.contains("  Integration test for ferrotype"));
  assert!(content.contains("  TestData {"));
  assert!(content.contains("      name: \"integration_test\","));

  // Step 5: Test different output methods
  // These should not panic
  snapshot.print();
  snapshot.print_stdout();
  snapshot.print_stderr();

  // Test Display trait
  let display_output = format!("{snapshot}");
  assert_eq!(display_output, content);

  ferrotype::assert!(snapshot);
}

#[test]
fn test_feature_integration() {
  let mut snapshot = Ferrotype::new();

  // Test basic content
  snapshot.add("Basic", "Hello, world!".to_string());

  // Test debug content
  let debug_data = vec![1, 2, 3, 4, 5];
  snapshot.add_debug("Numbers", &debug_data);

  // Test hex feature if available
  #[cfg(feature = "hex")]
  {
    let binary_data = b"Test data for hex dump";
    snapshot.add_hex("Binary", binary_data);
  }

  // Test tokenstream feature if available
  #[cfg(feature = "tokenstream")]
  {
    let tokens: proc_macro2::TokenStream =
      "fn test() { println!(\"Hello from token stream!\"); }"
        .parse()
        .unwrap();
    snapshot.add_token_stream("Code", &tokens);
  }

  // Test anstream feature if available
  #[cfg(feature = "anstream")]
  {
    let colored_text = "\x1b[31mRed\x1b[0m and \x1b[32mGreen\x1b[0m";
    snapshot.add_strip_str("Colors", colored_text.to_string());
  }

  let content = snapshot.as_string();

  // Verify basic content is present
  assert!(content.contains("Basic: >"));
  assert!(content.contains("Hello, world!"));
  assert!(content.contains("Numbers: >"));
  assert!(content.contains("["));
  assert!(content.contains("1"));

  // Verify feature-specific content when available
  #[cfg(feature = "hex")]
  assert!(content.contains("Binary: >"));

  #[cfg(feature = "tokenstream")]
  {
    assert!(content.contains("Code: >"));
    assert!(content.contains("fn test()"));
  }

  #[cfg(feature = "anstream")]
  {
    assert!(content.contains("Colors: >"));
    assert!(content.contains("Red and Green"));
    assert!(!content.contains("\x1b[31m")); // ANSI codes should be stripped
  }

  ferrotype::assert!(snapshot);
}

#[test]
fn test_title_formatting_integration() {
  let mut snapshot = Ferrotype::new();

  // Test various title formats
  snapshot.add("simple_title", "content1".to_string());
  snapshot.add("complex_snake_case_title", "content2".to_string());
  snapshot.add("UPPER_CASE_TITLE", "content3".to_string());
  snapshot.add("mixedCase_Title", "content4".to_string());

  let content = snapshot.as_string();

  // Verify titles are formatted correctly
  assert!(content.contains("SimpleTitle: >"));
  assert!(content.contains("ComplexSnakeCaseTitle: >"));
  assert!(content.contains("UpperCaseTitle: >"));
  assert!(content.contains("MixedCaseTitle: >"));

  // Verify content is properly indented
  assert!(content.contains("  content1"));
  assert!(content.contains("  content2"));
  assert!(content.contains("  content3"));
  assert!(content.contains("  content4"));

  ferrotype::assert!(snapshot);
}

#[test]
fn test_empty_and_edge_cases() {
  // Test empty snapshot
  let empty_snapshot = Ferrotype::new();
  assert_eq!(empty_snapshot.as_string(), "");

  // Test with empty content
  let mut snapshot = Ferrotype::new();
  snapshot.add("Empty", "".to_string());

  let content = snapshot.as_string();
  assert!(content.contains("Empty: >"));

  // Test with multiline content
  let mut multiline_snapshot = Ferrotype::new();
  multiline_snapshot.add("Multiline", "Line 1\nLine 2\nLine 3".to_string());

  let multiline_content = multiline_snapshot.as_string();
  assert!(multiline_content.contains("  Line 1"));
  assert!(multiline_content.contains("  Line 2"));
  assert!(multiline_content.contains("  Line 3"));

  // Test with special characters
  let mut special_snapshot = Ferrotype::new();
  special_snapshot
    .add("Special", "Content with\ttabs and\nnewlines".to_string());

  let special_content = special_snapshot.as_string();
  assert!(special_content.contains("Special: >"));
  assert!(special_content.contains("tabs and"));
}

#[test]
fn test_method_chaining() {
  // Test that method chaining works properly
  let mut snapshot = Ferrotype::new();

  snapshot
    .set_expect_errors(true)
    .set_filter_memory_addresses(false);

  assert!(snapshot.expects_errors());
  assert!(!snapshot.filter_memory_addresses());

  // Test chaining in different order
  let mut snapshot2 = Ferrotype::new();

  snapshot2
    .set_filter_memory_addresses(true)
    .set_expect_errors(false);

  assert!(!snapshot2.expects_errors());
  assert!(snapshot2.filter_memory_addresses());
}

#[test]
fn test_clone_and_equality() {
  let mut original = Ferrotype::new();
  original.add("Test", "content".to_string());
  original.set_expect_errors(true);
  original.set_filter_memory_addresses(false);

  let mut cloned = original.clone();

  // Verify cloned snapshot has same configuration
  assert_eq!(cloned.expects_errors(), original.expects_errors());
  assert_eq!(
    cloned.filter_memory_addresses(),
    original.filter_memory_addresses()
  );

  // Verify cloned snapshot has same content
  assert_eq!(cloned.as_string(), original.as_string());

  // Verify they're independent (modifying one doesn't affect the other)
  cloned.add("Additional", "new content".to_string());
  assert_ne!(cloned.as_string(), original.as_string());
}