insta 1.47.2

A snapshot testing library for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
//! Tests for TOML serialization in insta.
//!
//! These tests verify:
//! - Backward compatibility (single-quoted strings via Pretty)
//! - Support for types that toml 0.5.x couldn't serialize (issue #439)
//! - Proper handling of special characters, escapes, and edge cases

#![cfg(feature = "toml")]

use insta::assert_toml_snapshot;
use serde::Serialize;
use std::collections::BTreeMap;

// =============================================================================
// BACKWARD COMPATIBILITY - Critical for existing snapshots
// =============================================================================
//
// The old `toml` 0.5.x crate used single-quoted (literal) strings by default.
// The new `toml_edit` crate uses double-quoted (basic) strings by default.
//
// To maintain backward compatibility with existing snapshots, the Pretty
// visitor converts strings back to single-quoted format where possible.
//
// This is CRITICAL because changing quote style would break every existing
// TOML snapshot in downstream projects.

/// Verifies that simple strings use single quotes (backward compat with toml 0.5.x)
#[test]
fn test_toml_backward_compat_single_quotes() {
    #[derive(Serialize)]
    struct Config {
        name: String,
        version: String,
        path: String,
    }

    // CRITICAL: These MUST be single-quoted to match toml 0.5.x output
    assert_toml_snapshot!(Config {
        name: "my-package".into(),
        version: "1.0.0".into(),
        path: "/usr/local/bin".into(),
    }, @r"
    name = 'my-package'
    version = '1.0.0'
    path = '/usr/local/bin'
    ");
}

/// Verifies fallback to double quotes only when single quotes are impossible
#[test]
fn test_toml_backward_compat_quote_fallback() {
    #[derive(Serialize)]
    struct Data {
        // Can use single quotes - no special chars
        simple: String,
        // Must use double quotes - contains single quote
        with_apostrophe: String,
        // Must use multi-line - contains newline
        with_newline: String,
    }

    assert_toml_snapshot!(Data {
        simple: "hello world".into(),
        with_apostrophe: "it's here".into(),
        with_newline: "line1\nline2".into(),
    }, @r#"
    simple = 'hello world'
    with_apostrophe = '''it's here'''
    with_newline = '''
    line1
    line2'''
    "#);
}

/// Regression test for issue #439 - types that toml 0.5.x couldn't serialize
/// The old toml crate would panic with "UnsupportedType" for unit struct variants
#[test]
fn test_toml_issue_439_unit_struct_variant() {
    #[derive(Serialize)]
    #[allow(dead_code)]
    enum MyEnum {
        Variant1 {},
        Variant2 {},
    }

    #[derive(Serialize)]
    struct Config {
        value: MyEnum,
    }

    // This would PANIC with toml 0.5.x: "UnsupportedType"
    // Now it works with toml_edit
    assert_toml_snapshot!(Config { value: MyEnum::Variant1 {} }, @"[value.Variant1]");
}

// =============================================================================
// Core Types
// =============================================================================

#[test]
fn test_toml_basic_types() {
    #[derive(Serialize)]
    struct Data {
        string: String,
        integer: i64,
        unsigned: u64,
        float: f64,
        boolean: bool,
    }

    assert_toml_snapshot!(Data {
        string: "hello".into(),
        integer: -42,
        unsigned: 9007199254740991,
        float: 1.5,
        boolean: true,
    }, @r"
    string = 'hello'
    integer = -42
    unsigned = 9007199254740991
    float = 1.5
    boolean = true
    ");
}

#[test]
fn test_toml_special_floats() {
    #[derive(Serialize)]
    struct Floats {
        pos_inf: f64,
        neg_inf: f64,
        nan_value: f64,
    }

    assert_toml_snapshot!(Floats {
        pos_inf: f64::INFINITY,
        neg_inf: f64::NEG_INFINITY,
        nan_value: f64::NAN,
    }, @r"
    pos_inf = inf
    neg_inf = -inf
    nan_value = nan
    ");
}

#[test]
fn test_toml_integer_boundaries() {
    #[derive(Serialize)]
    struct Boundaries {
        min_i64: i64,
        max_i64: i64,
    }

    assert_toml_snapshot!(Boundaries {
        min_i64: i64::MIN,
        max_i64: i64::MAX,
    }, @r"
    min_i64 = -9223372036854775808
    max_i64 = 9223372036854775807
    ");
}

// =============================================================================
// String Handling - Pretty Backward Compatibility
// =============================================================================

#[test]
fn test_toml_string_quoting() {
    #[derive(Serialize)]
    struct Strings {
        simple: String,
        with_double_quotes: String,
        with_single_quotes: String,
        with_both_quotes: String,
        empty: String,
    }

    assert_toml_snapshot!(Strings {
        simple: "hello".into(),
        with_double_quotes: r#"He said "Hello""#.into(),
        with_single_quotes: "It's working".into(),
        with_both_quotes: r#"He said "It's done""#.into(),
        empty: "".into(),
    }, @r#"
    simple = 'hello'
    with_double_quotes = 'He said "Hello"'
    with_single_quotes = '''It's working'''
    with_both_quotes = '''He said "It's done"'''
    empty = ''
    "#);
}

#[test]
fn test_toml_string_escapes() {
    #[derive(Serialize)]
    struct Data {
        with_newline: String,
        with_tab: String,
        with_backslash: String,
        with_null: String,
    }

    assert_toml_snapshot!(Data {
        with_newline: "line1\nline2".into(),
        with_tab: "col1\tcol2".into(),
        with_backslash: "path\\to\\file".into(),
        with_null: "hello\0world".into(),
    }, @r#"
    with_newline = '''
    line1
    line2'''
    with_tab = 'col1	col2'
    with_backslash = 'path\to\file'
    with_null = "hello\u0000world"
    "#);
}

#[test]
fn test_toml_control_characters() {
    #[derive(Serialize)]
    struct Data {
        carriage_return: String,
        form_feed: String,
        bell: String,
    }

    assert_toml_snapshot!(Data {
        carriage_return: "line1\rline2".into(),
        form_feed: "page1\x0cpage2".into(),
        bell: "alert\x07here".into(),
    }, @r#"
    carriage_return = "line1\rline2"
    form_feed = "page1\fpage2"
    bell = "alert\u0007here"
    "#);
}

// =============================================================================
// Structures and Nesting
// =============================================================================

#[test]
fn test_toml_nested_struct() {
    #[derive(Serialize)]
    struct Inner {
        value: i32,
    }

    #[derive(Serialize)]
    struct Outer {
        name: String,
        inner: Inner,
    }

    assert_toml_snapshot!(Outer {
        name: "test".into(),
        inner: Inner { value: 42 },
    }, @r"
    name = 'test'

    [inner]
    value = 42
    ");
}

#[test]
fn test_toml_empty_struct() {
    #[derive(Serialize)]
    struct Empty {}

    #[derive(Serialize)]
    struct Container {
        empty: Empty,
    }

    assert_toml_snapshot!(Container { empty: Empty {} }, @"[empty]");
}

/// Top-level sequences are NOT supported by TOML (issue #879)
#[test]
#[should_panic(expected = "TOML requires the top-level value to be a struct or map")]
fn test_toml_top_level_sequence_unsupported() {
    insta::_macro_support::serialize_value(
        &vec![1, 2, 3],
        insta::_macro_support::SerializationFormat::Toml,
    );
}

/// Unit structs are NOT supported by TOML - this documents the limitation
#[test]
#[should_panic(expected = "unsupported Marker type")]
fn test_toml_unit_struct_unsupported() {
    #[derive(Serialize)]
    struct Marker;

    #[derive(Serialize)]
    struct Data {
        marker: Marker,
    }

    insta::_macro_support::serialize_value(
        &Data { marker: Marker },
        insta::_macro_support::SerializationFormat::Toml,
    );
}

// =============================================================================
// Arrays
// =============================================================================

#[test]
fn test_toml_arrays() {
    #[derive(Serialize)]
    struct Item {
        id: u32,
        name: String,
    }

    #[derive(Serialize)]
    struct Data {
        empty: Vec<i32>,
        numbers: Vec<i32>,
        strings: Vec<String>,
        structs: Vec<Item>,
        nested: Vec<Vec<i32>>,
    }

    assert_toml_snapshot!(Data {
        empty: vec![],
        numbers: vec![1, 2, 3],
        strings: vec!["a".into(), "b".into()],
        structs: vec![
            Item { id: 1, name: "first".into() },
            Item { id: 2, name: "second".into() },
        ],
        nested: vec![vec![1, 2], vec![3, 4]],
    }, @r"
    empty = []
    numbers = [
        1,
        2,
        3,
    ]
    strings = [
        'a',
        'b',
    ]
    nested = [
        [
        1,
        2,
    ],
        [
        3,
        4,
    ],
    ]

    [[structs]]
    id = 1
    name = 'first'

    [[structs]]
    id = 2
    name = 'second'
    ");
}

#[test]
fn test_toml_special_floats_in_array() {
    #[derive(Serialize)]
    struct Data {
        floats: Vec<f64>,
    }

    assert_toml_snapshot!(Data {
        floats: vec![1.5, f64::NAN, f64::INFINITY, f64::NEG_INFINITY],
    }, @r"
    floats = [
        1.5,
        nan,
        inf,
        -inf,
    ]
    ");
}

// =============================================================================
// Maps
// =============================================================================

#[test]
fn test_toml_maps() {
    let mut simple = BTreeMap::new();
    simple.insert("alpha", 1);
    simple.insert("beta", 2);

    let mut nested_inner = BTreeMap::new();
    nested_inner.insert("x".to_string(), 10);
    let mut nested = BTreeMap::new();
    nested.insert("coords".to_string(), nested_inner);

    #[derive(Serialize)]
    struct Data {
        simple: BTreeMap<&'static str, i32>,
        nested: BTreeMap<String, BTreeMap<String, i32>>,
    }

    assert_toml_snapshot!(Data { simple, nested }, @r"
    [simple]
    alpha = 1
    beta = 2
    [nested.coords]
    x = 10
    ");
}

#[test]
fn test_toml_integer_keys() {
    let mut map = BTreeMap::new();
    map.insert(1, "first");
    map.insert(10, "tenth");

    #[derive(Serialize)]
    struct Data {
        items: BTreeMap<i32, &'static str>,
    }

    assert_toml_snapshot!(Data { items: map }, @r"
    [items]
    1 = 'first'
    10 = 'tenth'
    ");
}

// =============================================================================
// Key Edge Cases
// =============================================================================

#[test]
fn test_toml_special_keys() {
    let mut map = BTreeMap::new();
    map.insert("", "empty key");
    map.insert("some.dotted.key", "dotted");
    map.insert("it's", "single quote");
    map.insert("key with spaces", "spaces");
    map.insert("key=value", "equals");
    map.insert("[section]", "brackets");

    #[derive(Serialize)]
    struct Data {
        items: BTreeMap<&'static str, &'static str>,
    }

    assert_toml_snapshot!(Data { items: map }, @r#"
    [items]
    "" = 'empty key'
    "[section]" = 'brackets'
    "it's" = 'single quote'
    "key with spaces" = 'spaces'
    "key=value" = 'equals'
    "some.dotted.key" = 'dotted'
    "#);
}

#[test]
fn test_toml_unicode_keys() {
    let mut map = BTreeMap::new();
    map.insert("é”®", "Chinese");
    map.insert("キー", "Japanese");
    map.insert("ключ", "Russian");

    #[derive(Serialize)]
    struct Data {
        items: BTreeMap<&'static str, &'static str>,
    }

    assert_toml_snapshot!(Data { items: map }, @r#"
    [items]
    "ключ" = 'Russian'
    "キー" = 'Japanese'
    "é”®" = 'Chinese'
    "#);
}

#[test]
fn test_toml_keyword_keys() {
    let mut map = BTreeMap::new();
    map.insert("true", "bool keyword");
    map.insert("false", "bool keyword");
    map.insert("inf", "float keyword");
    map.insert("nan", "float keyword");

    #[derive(Serialize)]
    struct Data {
        items: BTreeMap<&'static str, &'static str>,
    }

    let result = insta::_macro_support::serialize_value(
        &Data { items: map },
        insta::_macro_support::SerializationFormat::Toml,
    );
    assert!(result.contains("bool keyword"));
}

// =============================================================================
// Serde Attributes
// =============================================================================

#[test]
fn test_toml_serde_skip() {
    #[derive(Serialize)]
    #[allow(dead_code)]
    struct Data {
        included: String,
        #[serde(skip)]
        excluded: String,
    }

    assert_toml_snapshot!(Data {
        included: "visible".into(),
        excluded: "hidden".into(),
    }, @"included = 'visible'");
}

#[test]
fn test_toml_serde_rename() {
    #[derive(Serialize)]
    struct Data {
        #[serde(rename = "newName")]
        old_name: String,
    }

    assert_toml_snapshot!(Data {
        old_name: "value".into(),
    }, @"newName = 'value'");
}

#[test]
fn test_toml_serde_flatten() {
    #[derive(Serialize)]
    struct Base {
        name: String,
        age: u32,
    }

    #[derive(Serialize)]
    struct Extended {
        id: i32,
        #[serde(flatten)]
        base: Base,
    }

    assert_toml_snapshot!(Extended {
        id: 1,
        base: Base {
            name: "Alice".into(),
            age: 30,
        },
    }, @r"
    id = 1
    name = 'Alice'
    age = 30
    ");
}

#[test]
fn test_toml_option_skip_serializing_if() {
    #[derive(Serialize)]
    struct Data {
        present: Option<String>,
        #[serde(skip_serializing_if = "Option::is_none")]
        missing: Option<i32>,
    }

    assert_toml_snapshot!(Data {
        present: Some("value".into()),
        missing: None,
    }, @"present = 'value'");
}

// =============================================================================
// Enums
// =============================================================================

#[test]
fn test_toml_enum_externally_tagged() {
    #[derive(Serialize)]
    enum Value {
        Text(String),
        Number(i32),
    }

    #[derive(Serialize)]
    struct Data {
        values: Vec<Value>,
    }

    assert_toml_snapshot!(Data {
        values: vec![Value::Text("hello".into()), Value::Number(42)],
    }, @r"
    [[values]]
    Text = 'hello'

    [[values]]
    Number = 42
    ");
}

#[test]
fn test_toml_enum_internally_tagged() {
    #[derive(Serialize)]
    #[serde(tag = "type")]
    #[allow(dead_code)]
    enum Event {
        Login { user: String },
        Logout { user: String },
    }

    #[derive(Serialize)]
    struct Data {
        event: Event,
    }

    assert_toml_snapshot!(Data {
        event: Event::Login { user: "alice".into() },
    }, @r"
    [event]
    type = 'Login'
    user = 'alice'
    ");
}

#[test]
fn test_toml_enum_adjacently_tagged() {
    #[derive(Serialize)]
    #[serde(tag = "t", content = "c")]
    #[allow(dead_code)]
    enum Message {
        Text(String),
        Number(i32),
    }

    #[derive(Serialize)]
    struct Data {
        msg: Message,
    }

    assert_toml_snapshot!(Data {
        msg: Message::Text("hello".into()),
    }, @r"
    [msg]
    t = 'Text'
    c = 'hello'
    ");
}

#[test]
fn test_toml_enum_untagged() {
    #[derive(Serialize)]
    #[serde(untagged)]
    #[allow(dead_code)]
    enum Mixed {
        Int(i32),
        Str(String),
    }

    #[derive(Serialize)]
    struct Data {
        value: Mixed,
    }

    assert_toml_snapshot!(Data {
        value: Mixed::Str("hello".into()),
    }, @"value = 'hello'");
}

// =============================================================================
// Special Types
// =============================================================================

#[test]
fn test_toml_char() {
    #[derive(Serialize)]
    struct Data {
        letter: char,
        emoji: char,
    }

    assert_toml_snapshot!(Data {
        letter: 'A',
        emoji: '🎉',
    }, @r"
    letter = 'A'
    emoji = '🎉'
    ");
}

#[test]
fn test_toml_newtype_wrapper() {
    #[derive(Serialize)]
    struct UserId(u64);

    #[derive(Serialize)]
    struct Username(String);

    #[derive(Serialize)]
    struct User {
        id: UserId,
        name: Username,
    }

    assert_toml_snapshot!(User {
        id: UserId(12345),
        name: Username("alice".into()),
    }, @r"
    id = 12345
    name = 'alice'
    ");
}

#[test]
fn test_toml_type_distinction() {
    #[derive(Serialize)]
    struct Data {
        actual_bool: bool,
        bool_string: String,
        actual_number: i32,
        number_string: String,
    }

    assert_toml_snapshot!(Data {
        actual_bool: true,
        bool_string: "true".into(),
        actual_number: 123,
        number_string: "123".into(),
    }, @r"
    actual_bool = true
    bool_string = 'true'
    actual_number = 123
    number_string = '123'
    ");
}

// =============================================================================
// Stress Tests
// =============================================================================

#[test]
fn test_toml_deep_nesting() {
    #[derive(Serialize)]
    struct L5 {
        v: i32,
    }
    #[derive(Serialize)]
    struct L4 {
        x: L5,
    }
    #[derive(Serialize)]
    struct L3 {
        x: L4,
    }
    #[derive(Serialize)]
    struct L2 {
        x: L3,
    }
    #[derive(Serialize)]
    struct L1 {
        x: L2,
    }

    let data = L1 {
        x: L2 {
            x: L3 {
                x: L4 { x: L5 { v: 42 } },
            },
        },
    };
    let result = insta::_macro_support::serialize_value(
        &data,
        insta::_macro_support::SerializationFormat::Toml,
    );
    assert!(result.contains("v = 42"));
}

#[test]
fn test_toml_large_array() {
    #[derive(Serialize)]
    struct Data {
        numbers: Vec<i32>,
    }

    let result = insta::_macro_support::serialize_value(
        &Data {
            numbers: (0..1000).collect(),
        },
        insta::_macro_support::SerializationFormat::Toml,
    );
    assert!(result.contains("999"));
}

#[test]
fn test_toml_long_string() {
    #[derive(Serialize)]
    struct Data {
        content: String,
    }

    let long = "x".repeat(10_000);
    let result = insta::_macro_support::serialize_value(
        &Data {
            content: long.clone(),
        },
        insta::_macro_support::SerializationFormat::Toml,
    );
    assert!(result.len() > 10_000);
}