gilt 1.7.0

Fast, beautiful terminal formatting for Rust — styles, tables, trees, syntax highlighting, progress bars, markdown.
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
//! Tests extracted from traceback.rs for readability.
//! Wired in via `#[path = ...] mod tests;`.

use super::*;
use crate::console::Console;

// -- Sample backtrace strings for testing ---------------------------------

const SAMPLE_BACKTRACE: &str = "\
   0: std::backtrace::Backtrace::force_capture
             at /rustc/abc123/library/std/src/backtrace.rs:331:18
   1: core::panicking::panic_fmt
             at /rustc/abc123/library/core/src/panicking.rs:72:14
   2: myapp::myfunction
             at ./src/main.rs:42:9
   3: myapp::main
             at ./src/main.rs:10:5";

const SINGLE_FRAME_BACKTRACE: &str = "\
   0: myapp::main
             at ./src/main.rs:10:5";

const EMPTY_BACKTRACE: &str = "";

const FRAME_NO_LOCATION: &str = "\
   0: unknown_function
   1: myapp::main
             at ./src/main.rs:10:5";

// -- Frame tests ----------------------------------------------------------

#[test]
fn test_frame_new() {
    let frame = Frame::new("src/main.rs", Some(42), "main");
    assert_eq!(frame.filename, "src/main.rs");
    assert_eq!(frame.lineno, Some(42));
    assert_eq!(frame.name, "main");
    assert!(frame.source_line.is_none());
}

#[test]
fn test_frame_with_source_line() {
    let frame =
        Frame::new("src/main.rs", Some(42), "main").with_source_line("    println!(\"hello\");");
    assert_eq!(
        frame.source_line,
        Some("    println!(\"hello\");".to_string())
    );
}

#[test]
fn test_frame_display_with_lineno() {
    let frame = Frame::new("src/main.rs", Some(42), "main");
    let display = format!("{}", frame);
    assert!(display.contains("main"));
    assert!(display.contains("src/main.rs"));
    assert!(display.contains("42"));
}

#[test]
fn test_frame_display_without_lineno() {
    let frame = Frame::new("src/main.rs", None, "main");
    let display = format!("{}", frame);
    assert!(display.contains("main"));
    assert!(display.contains("src/main.rs"));
    assert!(!display.contains(':'));
}

#[test]
fn test_frame_equality() {
    let a = Frame::new("src/main.rs", Some(42), "main");
    let b = Frame::new("src/main.rs", Some(42), "main");
    assert_eq!(a, b);

    let c = Frame::new("src/lib.rs", Some(42), "main");
    assert_ne!(a, c);
}

#[test]
fn test_frame_clone() {
    let frame = Frame::new("src/main.rs", Some(42), "main").with_source_line("let x = 1;");
    let cloned = frame.clone();
    assert_eq!(frame, cloned);
}

// -- Parse backtrace tests ------------------------------------------------

#[test]
fn test_parse_backtrace_multiple_frames() {
    let frames = parse_backtrace(SAMPLE_BACKTRACE);
    assert_eq!(frames.len(), 4);

    assert_eq!(frames[0].name, "std::backtrace::Backtrace::force_capture");
    assert_eq!(
        frames[0].filename,
        "/rustc/abc123/library/std/src/backtrace.rs"
    );
    assert_eq!(frames[0].lineno, Some(331));

    assert_eq!(frames[1].name, "core::panicking::panic_fmt");
    assert_eq!(
        frames[1].filename,
        "/rustc/abc123/library/core/src/panicking.rs"
    );
    assert_eq!(frames[1].lineno, Some(72));

    assert_eq!(frames[2].name, "myapp::myfunction");
    assert_eq!(frames[2].filename, "./src/main.rs");
    assert_eq!(frames[2].lineno, Some(42));

    assert_eq!(frames[3].name, "myapp::main");
    assert_eq!(frames[3].filename, "./src/main.rs");
    assert_eq!(frames[3].lineno, Some(10));
}

#[test]
fn test_parse_backtrace_single_frame() {
    let frames = parse_backtrace(SINGLE_FRAME_BACKTRACE);
    assert_eq!(frames.len(), 1);
    assert_eq!(frames[0].name, "myapp::main");
    assert_eq!(frames[0].filename, "./src/main.rs");
    assert_eq!(frames[0].lineno, Some(10));
}

#[test]
fn test_parse_backtrace_empty() {
    let frames = parse_backtrace(EMPTY_BACKTRACE);
    assert!(frames.is_empty());
}

#[test]
fn test_parse_backtrace_frame_without_location() {
    let frames = parse_backtrace(FRAME_NO_LOCATION);
    assert_eq!(frames.len(), 2);
    // First frame has no location info
    assert_eq!(frames[0].name, "unknown_function");
    assert!(frames[0].filename.is_empty());
    assert!(frames[0].lineno.is_none());
    // Second frame has location
    assert_eq!(frames[1].name, "myapp::main");
    assert_eq!(frames[1].filename, "./src/main.rs");
    assert_eq!(frames[1].lineno, Some(10));
}

#[test]
fn test_parse_backtrace_with_column() {
    let bt = "   0: myapp::handler\n             at ./src/handler.rs:15:23";
    let frames = parse_backtrace(bt);
    assert_eq!(frames.len(), 1);
    assert_eq!(frames[0].filename, "./src/handler.rs");
    assert_eq!(frames[0].lineno, Some(15));
}

// -- Traceback constructors -----------------------------------------------

#[test]
fn test_from_backtrace() {
    let tb = Traceback::from_backtrace(SAMPLE_BACKTRACE);
    assert_eq!(tb.title, "Backtrace");
    assert_eq!(tb.frames.len(), 4);
    assert!(tb.message.is_empty());
}

#[test]
fn test_from_backtrace_empty() {
    let tb = Traceback::from_backtrace(EMPTY_BACKTRACE);
    assert_eq!(tb.title, "Backtrace");
    assert!(tb.frames.is_empty());
}

#[test]
fn test_from_panic() {
    let tb = Traceback::from_panic(
        "thread 'main' panicked at 'index out of bounds'",
        SAMPLE_BACKTRACE,
    );
    assert_eq!(tb.title, "Panic");
    assert!(tb.message.contains("index out of bounds"));
    assert_eq!(tb.frames.len(), 4);
}

#[test]
fn test_from_error_simple() {
    #[derive(Debug)]
    struct SimpleError;
    impl std::fmt::Display for SimpleError {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            write!(f, "something went wrong")
        }
    }
    impl std::error::Error for SimpleError {}

    let err = SimpleError;
    let tb = Traceback::from_error(&err);
    assert!(!tb.title.is_empty());
    assert!(tb.message.contains("something went wrong"));
    assert!(tb.frames.is_empty());
}

#[test]
fn test_from_error_chain() {
    #[derive(Debug)]
    struct InnerError;
    impl std::fmt::Display for InnerError {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            write!(f, "inner failure")
        }
    }
    impl std::error::Error for InnerError {}

    #[derive(Debug)]
    struct OuterError {
        source: InnerError,
    }
    impl std::fmt::Display for OuterError {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            write!(f, "outer failure")
        }
    }
    impl std::error::Error for OuterError {
        fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
            Some(&self.source)
        }
    }

    let err = OuterError { source: InnerError };
    let tb = Traceback::from_error(&err);
    assert!(tb.message.contains("outer failure"));
    assert!(tb.message.contains("inner failure"));
    assert!(tb.message.contains("Caused by"));
}

// -- Builder methods ------------------------------------------------------

#[test]
fn test_builder_methods() {
    let tb = Traceback::new()
        .with_title("Custom Error")
        .with_message("details here")
        .with_show_locals(true)
        .with_width(120)
        .with_extra_lines(5)
        .with_theme("base16-mocha.dark")
        .with_word_wrap(false)
        .with_max_frames(50);

    assert_eq!(tb.title, "Custom Error");
    assert_eq!(tb.message, "details here");
    assert!(tb.show_locals);
    assert_eq!(tb.width, Some(120));
    assert_eq!(tb.extra_lines, 5);
    assert_eq!(tb.theme, "base16-mocha.dark");
    assert!(!tb.word_wrap);
    assert_eq!(tb.max_frames, 50);
}

#[test]
fn test_default_values() {
    let tb = Traceback::new();
    assert!(tb.title.is_empty());
    assert!(tb.message.is_empty());
    assert!(tb.frames.is_empty());
    assert!(!tb.show_locals);
    assert!(tb.width.is_none());
    assert_eq!(tb.extra_lines, 3);
    assert_eq!(tb.theme, "base16-ocean.dark");
    // Finding #13: rich defaults word_wrap to false (was incorrectly true).
    assert!(!tb.word_wrap);
    assert_eq!(tb.max_frames, 100);
}

#[test]
fn test_default_trait() {
    let tb = Traceback::default();
    assert!(tb.title.is_empty());
    assert!(tb.frames.is_empty());
}

// -- Display / Debug traits -----------------------------------------------

#[test]
fn test_display_empty() {
    let tb = Traceback::new();
    let display = format!("{}", tb);
    assert!(display.is_empty());
}

#[test]
fn test_display_with_title_and_message() {
    let tb = Traceback::new()
        .with_title("Error")
        .with_message("something failed");
    let display = format!("{}", tb);
    assert!(display.contains("Error"));
    assert!(display.contains("something failed"));
}

#[test]
fn test_display_with_frames() {
    let tb = Traceback::from_backtrace(SAMPLE_BACKTRACE);
    let display = format!("{}", tb);
    assert!(display.contains("myapp::main"));
    assert!(display.contains("myapp::myfunction"));
}

#[test]
fn test_debug_trait() {
    let tb = Traceback::new().with_title("Debug Test");
    let debug = format!("{:?}", tb);
    assert!(debug.contains("Debug Test"));
}

// -- Max frames -----------------------------------------------------------

#[test]
fn test_max_frames_limit() {
    let mut tb = Traceback::new().with_max_frames(2);
    for i in 0..10 {
        tb.frames.push(Frame::new(
            "src/main.rs",
            Some(i + 1),
            &format!("func_{}", i),
        ));
    }

    // When rendering the content, only max_frames should be shown.
    let content = tb.render_content();
    let plain = content.plain().to_string();
    // Finding #17: wording changed from "omitted" to "hidden" to match rich.
    assert!(plain.contains("hidden"));
}

#[test]
fn test_max_frames_not_truncated_when_under_limit() {
    let mut tb = Traceback::new().with_max_frames(10);
    tb.frames.push(Frame::new("src/main.rs", Some(1), "func_a"));
    tb.frames.push(Frame::new("src/main.rs", Some(2), "func_b"));

    let content = tb.render_content();
    let plain = content.plain().to_string();
    assert!(!plain.contains("omitted"));
    assert!(plain.contains("func_a"));
    assert!(plain.contains("func_b"));
}

// -- Renderable trait -----------------------------------------------------

#[test]
fn test_renderable_produces_segments() {
    let tb = Traceback::from_backtrace(SAMPLE_BACKTRACE);
    let console = Console::builder()
        .width(80)
        .no_color(true)
        .markup(false)
        .build();
    let options = console.options();
    let segments = tb.gilt_console(&console, &options);
    assert!(!segments.is_empty());
}

#[test]
fn test_renderable_contains_title() {
    let tb = Traceback::new()
        .with_title("TestError")
        .with_message("test message");
    let console = Console::builder()
        .width(80)
        .no_color(true)
        .markup(false)
        .build();
    let options = console.options();
    let segments = tb.gilt_console(&console, &options);
    let output: String = segments.iter().map(|s| s.text.as_str()).collect();
    assert!(output.contains("TestError"));
    assert!(output.contains("test message"));
}

#[test]
fn test_renderable_contains_frame_info() {
    let mut tb = Traceback::new().with_title("Error");
    tb.frames.push(
        Frame::new("/some/path/file.rs", Some(42), "my_func").with_source_line("    let x = 1;"),
    );
    let console = Console::builder()
        .width(80)
        .no_color(true)
        .markup(false)
        .build();
    let options = console.options();
    let segments = tb.gilt_console(&console, &options);
    let output: String = segments.iter().map(|s| s.text.as_str()).collect();
    assert!(output.contains("file.rs"));
    assert!(output.contains("42"));
    assert!(output.contains("my_func"));
}

#[test]
fn test_renderable_wrapped_in_panel() {
    let tb = Traceback::new().with_title("PanelTest");
    let console = Console::builder()
        .width(40)
        .no_color(true)
        .markup(false)
        .build();
    let options = console.options();
    let segments = tb.gilt_console(&console, &options);
    let output: String = segments.iter().map(|s| s.text.as_str()).collect();
    // Panel uses ROUNDED box chars by default
    assert!(output.contains('\u{256d}') || output.contains('\u{2500}')); // top-left or horizontal
}

#[test]
fn test_renderable_with_width() {
    let tb = Traceback::new().with_title("WidthTest").with_width(60);
    let console = Console::builder()
        .width(120)
        .no_color(true)
        .markup(false)
        .build();
    let options = console.options();
    let segments = tb.gilt_console(&console, &options);
    // Segments should be produced
    assert!(!segments.is_empty());
}

#[test]
fn test_renderable_empty_traceback() {
    let tb = Traceback::new();
    let console = Console::builder()
        .width(80)
        .no_color(true)
        .markup(false)
        .build();
    let options = console.options();
    let segments = tb.gilt_console(&console, &options);
    // Even empty tracebacks should produce panel segments
    assert!(!segments.is_empty());
    let output: String = segments.iter().map(|s| s.text.as_str()).collect();
    // Should contain "Traceback" as default title
    assert!(output.contains("Traceback"));
}

// -- Error type name extraction -------------------------------------------

#[test]
fn test_error_type_name_from_debug() {
    #[derive(Debug)]
    struct MyCustomError(String);
    impl std::fmt::Display for MyCustomError {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            write!(f, "{}", self.0)
        }
    }
    impl std::error::Error for MyCustomError {}

    let err = MyCustomError("test".to_string());
    let name = error_type_name(&err);
    assert_eq!(name, "MyCustomError");
}

// -- Frame read_source_line -----------------------------------------------

#[test]
fn test_frame_read_source_line_nonexistent_file() {
    let mut frame = Frame::new("/nonexistent/path/foo.rs", Some(1), "main");
    frame.read_source_line();
    // Should remain None since the file doesn't exist
    assert!(frame.source_line.is_none());
}

#[test]
fn test_frame_read_source_line_no_lineno() {
    let mut frame = Frame::new("src/main.rs", None, "main");
    frame.read_source_line();
    assert!(frame.source_line.is_none());
}

#[test]
fn test_frame_read_source_line_zero_lineno() {
    let mut frame = Frame::new("src/main.rs", Some(0), "main");
    frame.read_source_line();
    assert!(frame.source_line.is_none());
}

#[test]
fn test_frame_read_source_line_already_set() {
    let mut frame = Frame::new("src/main.rs", Some(1), "main");
    frame.source_line = Some("existing".to_string());
    frame.read_source_line();
    assert_eq!(frame.source_line, Some("existing".to_string()));
}

// -- Traceback with manually constructed frames ---------------------------

#[test]
fn test_traceback_manual_frames() {
    let tb = Traceback::new()
        .with_title("ManualError")
        .with_message("manual test");

    let mut tb = tb;
    tb.frames.push(Frame::new("src/lib.rs", Some(10), "foo"));
    tb.frames.push(Frame::new("src/main.rs", Some(20), "bar"));

    assert_eq!(tb.frames.len(), 2);
    let display = format!("{}", tb);
    assert!(display.contains("foo"));
    assert!(display.contains("bar"));
    assert!(display.contains("ManualError"));
}

// -- Backtrace with varying whitespace ------------------------------------

#[test]
fn test_parse_backtrace_varying_whitespace() {
    let bt =
        "  0: func_a\n            at /path/a.rs:1:1\n  1: func_b\n            at /path/b.rs:2:2";
    let frames = parse_backtrace(bt);
    assert_eq!(frames.len(), 2);
    assert_eq!(frames[0].name, "func_a");
    assert_eq!(frames[1].name, "func_b");
}

// -- Backtrace with extra text between frames ----------------------------

#[test]
fn test_parse_backtrace_with_noise() {
    let bt = "stack backtrace:\n   0: func_a\n             at /path/a.rs:1:1\n   1: func_b\n             at /path/b.rs:2:2\nnote: Some additional info";
    let frames = parse_backtrace(bt);
    assert_eq!(frames.len(), 2);
    assert_eq!(frames[0].name, "func_a");
    assert_eq!(frames[1].name, "func_b");
}

// -- Traceback Display with all components --------------------------------

#[test]
fn test_display_complete_traceback() {
    let mut tb = Traceback::new()
        .with_title("RuntimeError")
        .with_message("division by zero");
    tb.frames
        .push(Frame::new("src/math.rs", Some(15), "divide"));
    tb.frames.push(Frame::new("src/main.rs", Some(8), "main"));

    let display = format!("{}", tb);
    assert!(display.contains("RuntimeError"));
    assert!(display.contains("division by zero"));
    assert!(display.contains("divide"));
    assert!(display.contains("main"));
    assert!(display.contains("src/math.rs"));
    assert!(display.contains("15"));
}

// -- Wave 5C: suppress_paths -------------------------------------------

fn tb_with_frames(filenames: &[&str]) -> Traceback {
    let mut tb = Traceback::new().with_title("E").with_message("m");
    for f in filenames {
        tb.frames.push(Frame::new(f, Some(1), "fn"));
    }
    tb
}

#[test]
fn suppress_paths_filters_matching_frames() {
    let tb = tb_with_frames(&[
        "/home/u/proj/src/main.rs",
        "/.cargo/registry/src/index/tokio-1.0/lib.rs",
        "/home/u/proj/src/lib.rs",
    ])
    .with_suppress(vec!["/.cargo/registry/src/".to_string()]);

    let visible = tb.visible_frames();
    assert_eq!(visible.len(), 2);
    for f in visible {
        assert!(
            !f.filename.contains("/.cargo/registry/src/"),
            "should have filtered: {}",
            f.filename
        );
    }
}

#[test]
fn suppress_all_frames_shows_placeholder() {
    let tb = tb_with_frames(&["/.cargo/registry/src/a.rs", "/.cargo/registry/src/b.rs"])
        .with_suppress(vec!["/.cargo/registry/src/".to_string()]);

    // Render via Display (which routes through gilt_console + Panel).
    let out = format!("{}", tb);
    assert!(
        out.contains("suppressed 2 frame"),
        "expected placeholder, got:\n{}",
        out
    );
}

#[test]
fn suppress_empty_passes_all_frames() {
    let tb = tb_with_frames(&["/a.rs", "/b.rs", "/c.rs"]);
    assert!(tb.suppress_paths.is_empty());
    assert_eq!(tb.visible_frames().len(), 3);
}

#[test]
fn suppress_path_prefix_match() {
    // Finding #12: suppression now uses starts_with (prefix) to mirror rich.
    // A prefix that actually matches the start of the filename is suppressed.
    let tb = tb_with_frames(&["/path/to/tokio-runtime/lib.rs", "/path/to/myapp/main.rs"])
        .with_suppress(vec!["/path/to/tokio-".to_string()]);
    let visible = tb.visible_frames();
    assert_eq!(visible.len(), 1);
    assert!(visible[0].filename.contains("myapp"));
}

#[test]
fn suppress_path_non_prefix_not_suppressed() {
    // Finding #12: a substring that is NOT a prefix does NOT suppress.
    // "tokio-" does not start "/path/to/tokio-runtime/lib.rs", so no frame is hidden.
    let tb = tb_with_frames(&["/path/to/tokio-runtime/lib.rs", "/path/to/myapp/main.rs"])
        .with_suppress(vec!["tokio-".to_string()]);
    let visible = tb.visible_frames();
    // Neither filename starts with "tokio-", so both are visible.
    assert_eq!(visible.len(), 2);
}

// -- Task 1: Frame locals builder methods ------------------------------------

#[test]
fn test_frame_with_locals_builder() {
    let frame = Frame::new("src/main.rs", Some(10), "foo").with_locals(vec![
        ("x".to_string(), "42".to_string()),
        ("name".to_string(), "\"hello\"".to_string()),
    ]);
    assert_eq!(frame.locals.len(), 2);
    assert_eq!(frame.locals[0], ("x".to_string(), "42".to_string()));
    assert_eq!(frame.locals[1].0, "name");
}

#[test]
fn test_frame_with_local_chaining() {
    let frame = Frame::new("src/main.rs", Some(10), "foo")
        .with_local("a", "1")
        .with_local("b", "2")
        .with_local("c", "3");
    assert_eq!(frame.locals.len(), 3);
    assert_eq!(frame.locals[0], ("a".to_string(), "1".to_string()));
    assert_eq!(frame.locals[2], ("c".to_string(), "3".to_string()));
}

#[test]
fn test_frame_default_locals_empty() {
    let frame = Frame::new("src/main.rs", Some(1), "func");
    assert!(frame.locals.is_empty());
}

// -- Task 1: Traceback show_locals render path --------------------------------

#[test]
fn test_show_locals_true_renders_locals_in_renderable() {
    let mut tb = Traceback::new()
        .with_title("LocalsTest")
        .with_show_locals(true);
    tb.frames.push(
        Frame::new("src/main.rs", Some(5), "run")
            .with_source_line("    let x = 42;")
            .with_local("x", "42")
            .with_local("msg", "\"hello world\""),
    );

    let console = Console::builder()
        .width(80)
        .no_color(true)
        .markup(false)
        .build();
    let options = console.options();
    let segments = tb.gilt_console(&console, &options);
    let output: String = segments.iter().map(|s| s.text.as_str()).collect();

    assert!(
        output.contains("x"),
        "locals 'x' should appear in output; got:\n{}",
        output
    );
    assert!(
        output.contains("42"),
        "local value '42' should appear; got:\n{}",
        output
    );
    assert!(
        output.contains("msg"),
        "local name 'msg' should appear; got:\n{}",
        output
    );
}

#[test]
fn test_show_locals_false_does_not_render_locals() {
    // When show_locals is false, locals should NOT appear in the output.
    let mut tb = Traceback::new()
        .with_title("NoLocalsTest")
        .with_show_locals(false);
    tb.frames
        .push(Frame::new("src/main.rs", Some(5), "run").with_local("secret_var", "99999"));

    let console = Console::builder()
        .width(80)
        .no_color(true)
        .markup(false)
        .build();
    let options = console.options();
    let segments = tb.gilt_console(&console, &options);
    let output: String = segments.iter().map(|s| s.text.as_str()).collect();

    assert!(
        !output.contains("secret_var"),
        "locals should not appear when show_locals=false; got:\n{}",
        output
    );
}

#[test]
fn test_show_locals_true_render_content() {
    let mut tb = Traceback::new().with_show_locals(true);
    tb.frames.push(
        Frame::new("src/lib.rs", Some(1), "f")
            .with_local("foo", "\"bar\"")
            .with_local("count", "10"),
    );

    let content = tb.render_content();
    let plain = content.plain().to_string();

    assert!(
        plain.contains("foo"),
        "render_content should include 'foo'; got: {plain}"
    );
    assert!(
        plain.contains("count"),
        "render_content should include 'count'; got: {plain}"
    );
}

#[test]
fn test_show_locals_false_render_content() {
    let mut tb = Traceback::new().with_show_locals(false);
    tb.frames
        .push(Frame::new("src/lib.rs", Some(1), "f").with_local("hidden_local", "true"));

    let content = tb.render_content();
    let plain = content.plain().to_string();

    assert!(
        !plain.contains("hidden_local"),
        "render_content should NOT include locals when show_locals=false; got: {plain}"
    );
}