pub struct PropertySnapshots { /* private fields */ }Expand description
Helper for managing property-based snapshots
This struct provides convenient methods for creating multiple snapshots in property-based tests. Each snapshot gets an automatic sequential name.
§Example
use protest::{Generator, primitives::IntGenerator, config::GeneratorConfig};
use protest_insta::PropertySnapshots;
use rand::SeedableRng;
use rand::rngs::StdRng;
#[test]
fn test_with_property_snapshots() {
let mut rng = StdRng::seed_from_u64(42);
let config = GeneratorConfig::default();
let generator = IntGenerator::new(0, 100);
let mut snapshots = PropertySnapshots::new("test_case");
for _ in 0..5 {
let value = generator.generate(&mut rng, &config);
snapshots.assert_debug_snapshot(&value);
}
}Implementations§
Source§impl PropertySnapshots
impl PropertySnapshots
Sourcepub fn new(base_name: impl Into<String>) -> Self
pub fn new(base_name: impl Into<String>) -> Self
Create a new PropertySnapshots helper with the given base name
§Arguments
base_name- The base name for all snapshots in this group
§Example
use protest_insta::PropertySnapshots;
let mut snapshots = PropertySnapshots::new("my_test");
// Will create snapshots named: my_test_0, my_test_1, my_test_2, ...Examples found in repository?
examples/debug_snapshots.rs (line 57)
46fn main() {
47 println!("=== Debug Snapshot Testing Example ===\n");
48
49 // Create a seeded RNG for reproducibility
50 let mut rng = StdRng::seed_from_u64(123);
51 let config = GeneratorConfig::default();
52
53 // Create generator for small integers
54 let generator = IntGenerator::new(1, 15);
55
56 // Create snapshot helper
57 let mut snapshots = PropertySnapshots::new("computations");
58
59 println!("Computing results for 8 generated inputs...\n");
60
61 // Generate and snapshot 8 computation results
62 for i in 0..8 {
63 let input = generator.generate(&mut rng, &config);
64 let result = compute(input);
65
66 println!("Computation {}: {:?}", i, result);
67
68 // Create a debug snapshot
69 snapshots.assert_debug_snapshot(&result);
70 }
71
72 println!("\n✅ Created 8 debug snapshots in snapshots/ directory");
73 println!(" Review them with: cargo insta review");
74}More examples
examples/json_snapshots.rs (line 41)
29fn main() {
30 println!("=== JSON Snapshot Testing Example ===\n");
31
32 // Create a seeded RNG for reproducibility
33 let mut rng = StdRng::seed_from_u64(42);
34 let config = GeneratorConfig::default();
35
36 // Create generators
37 let id_generator = IntGenerator::new(1, 1000);
38 let score_generator = VecGenerator::new(IntGenerator::new(0, 100), 1, 10);
39
40 // Create snapshot helper
41 let mut snapshots = PropertySnapshots::new("user_profiles");
42
43 println!("Generating 5 user profiles and creating JSON snapshots...\n");
44
45 // Generate and snapshot 5 different user profiles
46 for i in 0..5 {
47 let user_id = id_generator.generate(&mut rng, &config);
48 let username = generate_username(user_id);
49 let scores = score_generator.generate(&mut rng, &config);
50 let total_score: i32 = scores.iter().sum();
51 let is_premium = total_score > 250;
52
53 let profile = UserProfile {
54 user_id,
55 username,
56 scores: scores.clone(),
57 total_score,
58 is_premium,
59 };
60
61 println!("Profile {}: {:?}", i, profile);
62
63 // Create a JSON snapshot - this will create files in snapshots/
64 snapshots.assert_json_snapshot(&profile);
65 }
66
67 println!("\n✅ Created 5 JSON snapshots in snapshots/ directory");
68 println!(" Review them with: cargo insta review");
69}Sourcepub fn assert_json_snapshot<T: Serialize>(&mut self, value: &T)
pub fn assert_json_snapshot<T: Serialize>(&mut self, value: &T)
Assert a JSON snapshot for the given value
This serializes the value to pretty-printed JSON and creates a snapshot.
§Arguments
value- The value to snapshot (must implement Serialize)
§Example
use protest_insta::PropertySnapshots;
use serde::Serialize;
#[derive(Serialize)]
struct Config { port: u16, host: String }
let mut snapshots = PropertySnapshots::new("config");
let cfg = Config { port: 8080, host: "localhost".to_string() };
snapshots.assert_json_snapshot(&cfg);Examples found in repository?
examples/property_snapshot_test.rs (line 44)
31fn main() {
32 println!("=== Property Snapshot Test Function Example ===\n");
33 println!("Testing mathematical operations with property-based inputs...\n");
34
35 // Use the helper function for concise testing
36 property_snapshot_test(
37 "math_operations",
38 IntGenerator::new(-10, 10),
39 10, // Generate 10 test cases
40 456, // Seed for reproducibility
41 |value, snapshots| {
42 let result = perform_operations(value);
43 println!("Operation on {}: {:?}", value, result);
44 snapshots.assert_json_snapshot(&result);
45 },
46 );
47
48 println!("\n✅ Created 10 snapshots in snapshots/ directory");
49 println!(" Review them with: cargo insta review");
50 println!("\nNote: This example uses the property_snapshot_test helper");
51 println!(" which combines generator setup with snapshot management.");
52}More examples
examples/json_snapshots.rs (line 64)
29fn main() {
30 println!("=== JSON Snapshot Testing Example ===\n");
31
32 // Create a seeded RNG for reproducibility
33 let mut rng = StdRng::seed_from_u64(42);
34 let config = GeneratorConfig::default();
35
36 // Create generators
37 let id_generator = IntGenerator::new(1, 1000);
38 let score_generator = VecGenerator::new(IntGenerator::new(0, 100), 1, 10);
39
40 // Create snapshot helper
41 let mut snapshots = PropertySnapshots::new("user_profiles");
42
43 println!("Generating 5 user profiles and creating JSON snapshots...\n");
44
45 // Generate and snapshot 5 different user profiles
46 for i in 0..5 {
47 let user_id = id_generator.generate(&mut rng, &config);
48 let username = generate_username(user_id);
49 let scores = score_generator.generate(&mut rng, &config);
50 let total_score: i32 = scores.iter().sum();
51 let is_premium = total_score > 250;
52
53 let profile = UserProfile {
54 user_id,
55 username,
56 scores: scores.clone(),
57 total_score,
58 is_premium,
59 };
60
61 println!("Profile {}: {:?}", i, profile);
62
63 // Create a JSON snapshot - this will create files in snapshots/
64 snapshots.assert_json_snapshot(&profile);
65 }
66
67 println!("\n✅ Created 5 JSON snapshots in snapshots/ directory");
68 println!(" Review them with: cargo insta review");
69}Sourcepub fn assert_debug_snapshot<T: Debug>(&mut self, value: &T)
pub fn assert_debug_snapshot<T: Debug>(&mut self, value: &T)
Assert a debug snapshot for the given value
This uses the Debug trait to format the value and creates a snapshot.
§Arguments
value- The value to snapshot (must implement Debug)
§Example
use protest_insta::PropertySnapshots;
let mut snapshots = PropertySnapshots::new("numbers");
let values = vec![1, 2, 3, 4, 5];
snapshots.assert_debug_snapshot(&values);Examples found in repository?
examples/debug_snapshots.rs (line 69)
46fn main() {
47 println!("=== Debug Snapshot Testing Example ===\n");
48
49 // Create a seeded RNG for reproducibility
50 let mut rng = StdRng::seed_from_u64(123);
51 let config = GeneratorConfig::default();
52
53 // Create generator for small integers
54 let generator = IntGenerator::new(1, 15);
55
56 // Create snapshot helper
57 let mut snapshots = PropertySnapshots::new("computations");
58
59 println!("Computing results for 8 generated inputs...\n");
60
61 // Generate and snapshot 8 computation results
62 for i in 0..8 {
63 let input = generator.generate(&mut rng, &config);
64 let result = compute(input);
65
66 println!("Computation {}: {:?}", i, result);
67
68 // Create a debug snapshot
69 snapshots.assert_debug_snapshot(&result);
70 }
71
72 println!("\n✅ Created 8 debug snapshots in snapshots/ directory");
73 println!(" Review them with: cargo insta review");
74}Sourcepub fn assert_yaml_snapshot<T: Serialize>(&mut self, value: &T)
pub fn assert_yaml_snapshot<T: Serialize>(&mut self, value: &T)
Assert a YAML snapshot for the given value
This serializes the value to YAML and creates a snapshot.
§Arguments
value- The value to snapshot (must implement Serialize)
§Example
use protest_insta::PropertySnapshots;
use serde::Serialize;
#[derive(Serialize)]
struct Settings { debug: bool, timeout: u64 }
let mut snapshots = PropertySnapshots::new("settings");
let s = Settings { debug: true, timeout: 30 };
snapshots.assert_yaml_snapshot(&s);Sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Reset the counter back to 0
Useful if you want to reuse the same PropertySnapshots instance with a different set of tests.
§Example
use protest_insta::PropertySnapshots;
let mut snapshots = PropertySnapshots::new("test");
// ... use snapshots ...
snapshots.reset();
// Counter is back to 0Auto Trait Implementations§
impl Freeze for PropertySnapshots
impl RefUnwindSafe for PropertySnapshots
impl Send for PropertySnapshots
impl Sync for PropertySnapshots
impl Unpin for PropertySnapshots
impl UnwindSafe for PropertySnapshots
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more