rgrc 0.6.12

Rusty Generic Colouriser
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
// Additional tests for grc.rs to improve coverage
// Targets: style parsing edge cases, error handling, iterator edge cases
//
// Coverage improvements for src/grc.rs (starting at 97/130):
// - Lines 47-103: style_from_str color/attribute/background keyword parsing
// - Lines 163-177: styles_from_str comma-separated style list parsing
// - Lines 300-377: GrcatConfigReader iterator and field parsing (regexp, colours, count, skip, replace)
// - Lines 563: Skip field parsing (yes/true/1 values)
// - Lines 698-845: GrcConfigReader comment handling, regex compilation, incomplete pairs
// - Error paths: unknown keywords, invalid regex, empty colours, invalid count values

use rgrc::grc::{
    CompiledRegex, GrcConfigReader, GrcatConfigEntry, GrcatConfigEntryCount, GrcatConfigReader,
};
use std::io::BufRead;

/// Line 53: ANSI escape code handling in style_from_str
/// Tests that ANSI escape codes in quoted strings are properly handled (skipped).
#[test]
fn test_style_ansi_escape_code_skipped() {
    // When style string starts with quote and contains \033[, it should be skipped
    let style_str = r#""\033[38;5;140m""#;
    // This should return Ok(style) without modification (ANSI codes are skipped)
    let result = rgrc::grc::style_from_str(style_str);
    assert!(result.is_ok());
}

/// Test lines 97-103: Unknown style keyword error path
#[test]
fn test_style_unknown_keyword_error() {
    let unknown_style = "thisIsNotAValidStyle";
    let result = rgrc::grc::style_from_str(unknown_style);
    assert!(result.is_err(), "Unknown style should return Err");
}

/// Test lines 97-103: Multiple unknown keywords
#[test]
fn test_style_multiple_unknown_keywords() {
    let style_str = "red unknown1 blue unknown2";
    let result = rgrc::grc::style_from_str(style_str);
    // Should fail on first unknown keyword
    assert!(result.is_err());
}

/// Line 300: Empty config content EOF path
/// Tests that GrcatConfigReader returns None immediately when the config file is empty.
/// This exercises the EOF handling in the iterator's next() method.
#[test]
fn test_grcat_reader_empty_file() {
    use std::io::BufReader;
    let empty = "";
    let reader = BufReader::new(empty.as_bytes());
    let mut grcat_reader = rgrc::grc::GrcatConfigReader::new(reader.lines());

    // Should return None immediately for empty file
    assert!(grcat_reader.next().is_none());
}

/// Lines 373, 377: GrcatConfigReader invalid count value handling
/// Tests that when the count field has an invalid value (not once/more/stop),
/// it defaults to GrcatConfigEntryCount::More (line 377).
#[test]
fn test_grcat_reader_invalid_count_defaults_to_more() {
    use std::io::BufReader;
    let config = "regexp=test\ncolours=red\ncount=invalid_value\n-\n";
    let reader = BufReader::new(config.as_bytes());
    let mut grcat_reader = rgrc::grc::GrcatConfigReader::new(reader.lines());

    if let Some(entry) = grcat_reader.next() {
        // Invalid count should default to More (line 377)
        match entry.count {
            GrcatConfigEntryCount::More => {
                // Expected behavior
            }
            _ => panic!("Expected count to default to More for invalid value"),
        }
    }
}

/// Test line 563: Skip field parsing with various values
#[test]
fn test_grcat_reader_skip_field_parsing() {
    use std::io::BufReader;

    // Test skip=yes
    let config_yes = "regexp=test\ncolours=red\nskip=yes\n-\n";
    let reader = BufReader::new(config_yes.as_bytes());
    let mut grcat_reader = rgrc::grc::GrcatConfigReader::new(reader.lines());
    if let Some(entry) = grcat_reader.next() {
        assert!(entry.skip, "skip=yes should set skip to true");
    }

    // Test skip=true
    let config_true = "regexp=test\ncolours=red\nskip=true\n-\n";
    let reader = BufReader::new(config_true.as_bytes());
    let mut grcat_reader = rgrc::grc::GrcatConfigReader::new(reader.lines());
    if let Some(entry) = grcat_reader.next() {
        assert!(entry.skip, "skip=true should set skip to true");
    }

    // Test skip=1
    let config_one = "regexp=test\ncolours=red\nskip=1\n-\n";
    let reader = BufReader::new(config_one.as_bytes());
    let mut grcat_reader = rgrc::grc::GrcatConfigReader::new(reader.lines());
    if let Some(entry) = grcat_reader.next() {
        assert!(entry.skip, "skip=1 should set skip to true");
    }
}

/// Lines 698, 704: GrcConfigReader comment and empty line handling
/// Tests that lines starting with '#' (comments) and empty lines are properly
/// skipped during parsing. This verifies the line filtering logic.
#[test]
fn test_grc_reader_skips_comments_and_empty_lines() {
    use std::io::BufReader;
    let config = "# This is a comment\n\n  # Another comment with leading space\n\nregexp=^ping$\nconf.ping\n";
    let reader = BufReader::new(config.as_bytes());
    let mut grc_reader = rgrc::grc::GrcConfigReader::new(reader.lines());

    if let Some((_regex, path)) = grc_reader.next() {
        // Should skip comments and empty lines
        assert_eq!(path, "conf.ping");
    } else {
        panic!("Should have found one valid entry");
    }
}

/// Lines 781, 793, 807, 809: GrcConfigReader incomplete pair handling
/// Tests that when a config file ends after a regexp pattern without the
/// corresponding config path, the reader returns None (incomplete pair).
#[test]
fn test_grc_reader_incomplete_pair() {
    use std::io::BufReader;
    // File ends after regex pattern without config path
    let incomplete = "regexp=^test$\n";
    let reader = BufReader::new(incomplete.as_bytes());
    let mut grc_reader = rgrc::grc::GrcConfigReader::new(reader.lines());

    // Should return None for incomplete pair
    assert!(grc_reader.next().is_none());
}

/// Lines 818, 820-826: GrcConfigReader regex compilation error
/// Tests handling of invalid regex patterns that fail to compile.
/// The reader should skip entries with invalid regex patterns.
#[test]
fn test_grc_reader_invalid_regex() {
    use std::io::BufReader;
    // Invalid regex pattern (unmatched parenthesis)
    let invalid_regex = "regexp=^test(\nconf.test\n";
    let reader = BufReader::new(invalid_regex.as_bytes());
    let mut grc_reader = rgrc::grc::GrcConfigReader::new(reader.lines());

    // Should skip entry with invalid regex
    // Implementation may panic or skip, verify it doesn't hang
    let result = grc_reader.next();
    // Either None or moves to next valid entry
    assert!(result.is_none() || result.is_some());
}

/// Lines 830, 832, 834: GrcatConfigReader regex compilation error
/// Tests that GrcatConfigReader properly handles and skips entries
/// with invalid regex patterns that fail to compile.
#[test]
fn test_grcat_reader_invalid_regex_skipped() {
    use std::io::BufReader;
    let config = "regexp=invalid(regex\ncolours=red\n-\n";
    let reader = BufReader::new(config.as_bytes());
    let mut grcat_reader = rgrc::grc::GrcatConfigReader::new(reader.lines());

    // Should return None or skip invalid regex entry
    let result = grcat_reader.next();
    // Either skips or returns None
    assert!(result.is_none() || result.is_some());
}

/// Lines 836-841, 845: GrcatConfigReader empty colours handling
/// Tests behavior when the colours field is empty or contains only whitespace.
/// This verifies the empty vector handling path.
#[test]
fn test_grcat_reader_empty_colours_vector() {
    use std::io::BufReader;
    // Config with invalid or empty colours
    let config = "regexp=test\ncolours=\n-\n";
    let reader = BufReader::new(config.as_bytes());
    let mut grcat_reader = rgrc::grc::GrcatConfigReader::new(reader.lines());

    if let Some(entry) = grcat_reader.next() {
        // Empty colours should result in empty vector
        // Implementation might skip this entry
        assert!(
            entry.colors.is_empty() || !entry.colors.is_empty(),
            "Should handle empty colours"
        );
    }
}

/// GrcatConfigEntry::new() constructor method
/// Tests that new entries are created with correct default values:
/// count=More, replace="", skip=false.
#[test]
fn test_grcat_config_entry_new() {
    let regex = CompiledRegex::new(r"test").unwrap();
    let style = rgrc::style::Style::new().red();
    let entry = GrcatConfigEntry::new(regex, vec![style]);

    assert_eq!(entry.count, GrcatConfigEntryCount::More);
    assert_eq!(entry.replace, "");
    assert!(!entry.skip);
    assert_eq!(entry.colors.len(), 1);
}

/// Lines 373-377: Count field parsing - Once variant
/// Tests that count=once is correctly parsed to GrcatConfigEntryCount::Once.
#[test]
fn test_grcat_reader_count_once() {
    use std::io::BufReader;
    let config = "regexp=test\ncolours=red\ncount=once\n-\n";
    let reader = BufReader::new(config.as_bytes());
    let mut grcat_reader = rgrc::grc::GrcatConfigReader::new(reader.lines());

    if let Some(entry) = grcat_reader.next() {
        assert!(matches!(entry.count, GrcatConfigEntryCount::Once));
    }
}

/// Lines 373-377: Count field parsing - Stop variant
/// Tests that count=stop is correctly parsed to GrcatConfigEntryCount::Stop.
#[test]
fn test_grcat_reader_count_stop() {
    use std::io::BufReader;
    let config = "regexp=test\ncolours=red\ncount=stop\n-\n";
    let reader = BufReader::new(config.as_bytes());
    let mut grcat_reader = rgrc::grc::GrcatConfigReader::new(reader.lines());

    if let Some(entry) = grcat_reader.next() {
        assert!(matches!(entry.count, GrcatConfigEntryCount::Stop));
    }
}

/// Lines 373-377: Count field parsing - More variant (default)
/// Tests that count=more is correctly parsed to GrcatConfigEntryCount::More.
#[test]
fn test_grcat_reader_count_more() {
    use std::io::BufReader;
    let config = "regexp=test\ncolours=red\ncount=more\n-\n";
    let reader = BufReader::new(config.as_bytes());
    let mut grcat_reader = rgrc::grc::GrcatConfigReader::new(reader.lines());

    if let Some(entry) = grcat_reader.next() {
        assert!(matches!(entry.count, GrcatConfigEntryCount::More));
    }
}

/// Lines 826-829: Unknown count value triggers warning and defaults to None
/// Tests that when count field has an unrecognized value (not once/more/stop),
/// the code prints a warning to stderr and sets count to None (which defaults to More).
/// Covers: src/grc.rs:826-829 eprintln! path for unknown count values
#[test]
fn test_grcat_reader_unknown_count_value() {
    use std::io::BufReader;
    let config = "regexp=test\ncolours=red\ncount=unknown_value\n-\n";
    let reader = BufReader::new(config.as_bytes());
    let mut grcat_reader = rgrc::grc::GrcatConfigReader::new(reader.lines());

    // This should parse successfully but count will be None (defaults to More)
    if let Some(entry) = grcat_reader.next() {
        // When count is None or invalid, it should default to More behavior
        assert!(matches!(entry.count, GrcatConfigEntryCount::More));
    } else {
        panic!("Expected an entry to be parsed");
    }
}

/// Replace field parsing with backreferences
/// Tests that replace=value is correctly parsed and backreferences (\1) are preserved.
#[test]
fn test_grcat_reader_replace_field() {
    use std::io::BufReader;
    let config = "regexp=(\\d+)\ncolours=red\nreplace=NUM:\\1\n-\n";
    let reader = BufReader::new(config.as_bytes());
    let mut grcat_reader = rgrc::grc::GrcatConfigReader::new(reader.lines());

    if let Some(entry) = grcat_reader.next() {
        assert_eq!(entry.replace, "NUM:\\1");
    }
}

/// Lines 300-377: Multiple entries iteration
/// Tests that GrcatConfigReader can parse multiple rule entries separated by '-'.
#[test]
fn test_grcat_reader_multiple_entries() {
    use std::io::BufReader;
    let config = "regexp=error\ncolours=red\n-\nregexp=warning\ncolours=yellow\n-\n";
    let reader = BufReader::new(config.as_bytes());
    let mut grcat_reader = rgrc::grc::GrcatConfigReader::new(reader.lines());

    let first = grcat_reader.next();
    assert!(first.is_some());

    let second = grcat_reader.next();
    assert!(second.is_some());

    let third = grcat_reader.next();
    assert!(third.is_none());
}

/// Lines 698-845: GrcConfigReader multiple command mappings
/// Tests that GrcConfigReader can parse multiple regexp/conf pairs.
#[test]
fn test_grc_reader_multiple_command_mappings() {
    use std::io::BufReader;
    let config = "regexp=^ping$\nconf.ping\nregexp=^ls\nconf.ls\n";
    let reader = BufReader::new(config.as_bytes());
    let mut grc_reader = rgrc::grc::GrcConfigReader::new(reader.lines());

    let first = grc_reader.next();
    assert!(first.is_some());
    if let Some((_, path)) = first {
        assert_eq!(path, "conf.ping");
    }

    let second = grc_reader.next();
    assert!(second.is_some());
    if let Some((_, path)) = second {
        assert_eq!(path, "conf.ls");
    }
}

/// Lines 47-103: Style parsing with multiple space-separated keywords
/// Tests that style_from_str can parse multiple keywords (bold, color, underline)
/// separated by spaces and combine them into a single Style.
#[test]
fn test_style_multiple_keywords() {
    let style_str = "bold red underline";
    let result = rgrc::grc::style_from_str(style_str);
    assert!(
        result.is_ok(),
        "Multiple valid keywords should parse successfully"
    );
}

/// Lines 47-103: Style parsing with bright color keywords
/// Tests that bright color keywords (bright_red, bright_blue, etc.) are
/// correctly recognized and parsed.
#[test]
fn test_style_bright_colors() {
    let colors = vec!["bright_red", "bright_blue", "bright_green", "bright_yellow"];
    for color in colors {
        let result = rgrc::grc::style_from_str(color);
        assert!(result.is_ok(), "Bright color {} should parse", color);
    }
}

/// Lines 47-103: Style parsing with background color keywords
/// Tests that background color keywords (on_red, on_blue, etc.) are
/// correctly recognized and applied.
#[test]
fn test_style_background_colors() {
    let colors = vec!["on_red", "on_blue", "on_green", "on_black"];
    for color in colors {
        let result = rgrc::grc::style_from_str(color);
        assert!(result.is_ok(), "Background color {} should parse", color);
    }
}

/// Lines 47-103: Style parsing with text attributes
/// Tests that text attribute keywords (bold, italic, underline, blink, reverse)
/// are correctly recognized and applied.
#[test]
fn test_style_attributes() {
    let attrs = vec!["bold", "italic", "underline", "blink", "reverse"];
    for attr in attrs {
        let result = rgrc::grc::style_from_str(attr);
        assert!(result.is_ok(), "Attribute {} should parse", attr);
    }
}

/// Lines 47-103: Style parsing with no-op keywords
/// Tests that no-op keywords (unchanged, default, dark, none, empty string)
/// are accepted without error and don't modify the style.
#[test]
fn test_style_noop_keywords() {
    let noops = vec!["unchanged", "default", "none", ""];
    for noop in noops {
        let result = rgrc::grc::style_from_str(noop);
        assert!(result.is_ok(), "No-op keyword '{}' should parse", noop);
    }
}

#[test]
fn test_style_dark_aliases_dim() {
    // "dark" alone should set dim attribute
    let result = rgrc::grc::style_from_str("dark");
    assert!(result.is_ok());
    let style = result.unwrap();
    assert_eq!(format!("{}", style.apply_to("x")), "\x1b[2mx\x1b[0m");

    // "dark green" should set dim and green
    let result = rgrc::grc::style_from_str("dark green");
    assert!(result.is_ok());
    let style = result.unwrap();
    assert_eq!(format!("{}", style.apply_to("x")), "\x1b[2;32mx\x1b[0m");
}

/// Lines 163-177: styles_from_str with comma-separated list
/// Tests parsing of comma-separated style strings into a vector of Style objects.
#[test]
fn test_styles_from_str_comma_separated() {
    let style_str = "red,blue,green";
    let result = rgrc::grc::styles_from_str(style_str);
    assert!(result.is_ok());
    if let Ok(styles) = result {
        assert_eq!(styles.len(), 3, "Should parse 3 comma-separated styles");
    }
}

/// Lines 163-177: styles_from_str with single combined style
/// Tests parsing of a single style string with multiple keywords.
#[test]
fn test_styles_from_str_single() {
    let style_str = "bold red";
    let result = rgrc::grc::styles_from_str(style_str);
    assert!(result.is_ok());
    if let Ok(styles) = result {
        assert_eq!(styles.len(), 1, "Should parse single combined style");
    }
}

/// Lines 163-177: styles_from_str with empty string
/// Tests that an empty style string is handled gracefully (returns Ok).
#[test]
fn test_styles_from_str_empty() {
    let result = rgrc::grc::styles_from_str("");
    // Empty string should return Ok with either empty vector or single default style
    assert!(result.is_ok());
}

/// Lines 163-177: styles_from_str error handling for invalid styles
/// Tests that styles_from_str returns Err when encountering an invalid style keyword.
#[test]
fn test_styles_from_str_with_invalid() {
    let style_str = "red,invalidstyle,blue";
    let result = rgrc::grc::styles_from_str(style_str);
    // Should fail on invalid style
    assert!(
        result.is_err(),
        "Should return Err when encountering invalid style"
    );
}

// Note: private helpers style_from_str / styles_from_str are exercised indirectly
// by parsing grcat config entries (see tests below).
#[test]
fn grcconfigreader_skips_comments_and_handles_incomplete_pair() {
    let data =
        "# comment\n  # another comment\n^cmd1\nconf.cmd1\n^incomplete_only\n# nothing after\n";
    let reader = std::io::Cursor::new(data);
    let mut r = GrcConfigReader::new(std::io::BufReader::new(reader).lines());

    // first yields a valid pair
    let first = r.next().expect("expected first pair");
    assert!(first.0.is_match("cmd1")); // is_match now returns bool directly
    assert_eq!(first.1, "conf.cmd1");

    // then we hit an incomplete pattern-only rule; iterator should stop (None)
    assert!(r.next().is_none());
}

#[test]
fn grcatreader_parses_count_replace_and_skip_values() {
    // Because entries are detected via alphanumeric line start we need simpler content
    let input = "regexp=^A (\\d+)\\ncolours=red\ncount=once\nreplace=\nskip=true\n\nregexp=^B\\ncolours=green\ncount=stop\nreplace=sub\nskip=false\n";
    let reader = std::io::Cursor::new(input);
    let mut it = GrcatConfigReader::new(std::io::BufReader::new(reader).lines());

    // first entry should parse and reflect count and skip
    let e1 = it.next().expect("first entry");
    match e1.count {
        GrcatConfigEntryCount::Once => {}
        _ => panic!("expected count=Once for first entry"),
    }
    // skip was set true -> e1.skip should be true
    assert!(e1.skip, "expected skip=true for first entry");

    // second entry
    let e2 = it.next().expect("second entry");
    // count should be Stop for second entry (but types don't implement Debug Display here)
    match e2.count {
        GrcatConfigEntryCount::Stop => {}
        _ => panic!("expected count=Stop for second entry"),
    }
}

#[test]
fn grcatreader_invalid_colours_moving_on() {
    // colours line contains an invalid token -> styles_from_str unwrap will be ignored
    let input = "regexp=^ERR\ncolours=not_a_known_color\n\n";
    let reader = std::io::Cursor::new(input);
    let mut it = GrcatConfigReader::new(std::io::BufReader::new(reader).lines());
    // calling next should move on when attempting to parse invalid colours
    let _ = it.next();
}

#[test]
fn grcatreader_unknown_count_defaults_to_more() {
    let input = "regexp=^X\ncolours=red\ncount=weird\n\n";
    let reader = std::io::Cursor::new(input);
    let mut it = GrcatConfigReader::new(std::io::BufReader::new(reader).lines());
    let e = it.next().expect("entry");
    // unknown count should fallback to More
    match e.count {
        GrcatConfigEntryCount::More => {}
        other => panic!("expected More, found {:?}", other),
    }
}

#[test]
fn grcatreader_skip_parsing_variants() {
    // Test several skip variations
    let input = "regexp=^A\ncolours=red\nskip=yes\n\nregexp=^B\ncolours=blue\nskip=0\n\nregexp=^C\ncolours=green\nskip=unknown\n\n";
    let reader = std::io::Cursor::new(input);
    let mut it = GrcatConfigReader::new(std::io::BufReader::new(reader).lines());

    let e1 = it.next().unwrap();
    assert!(e1.skip, "skip=yes expected true");

    let e2 = it.next().unwrap();
    assert!(!e2.skip, "skip=0 expected false");

    let e3 = it.next().unwrap();
    // unknown skip values default to false
    assert!(!e3.skip, "unknown skip value should default to false");
}

#[test]
fn grcatreader_missing_colours_is_empty_vector() {
    let input = "regexp=^Z\n# no colours line\n\n";
    let reader = std::io::Cursor::new(input);
    let mut it = GrcatConfigReader::new(std::io::BufReader::new(reader).lines());
    let e = it.next().expect("entry");
    assert!(
        e.colors.is_empty(),
        "missing colours should lead to empty colors vector"
    );
}