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
use ustar::{default_config, parse, ErrorFormatMode};
mod snapshot_utils;
/// Test data with various parsing errors for snapshot testing
const ERROR_CASES: &[(&str, &str)] = &[
(
"unclosed_string",
indoc::indoc! {"
data_test
_entry.id 1ABC
_entry.description \"This is an unclosed string that will cause errors
_entry.author 'Smith, J.'
"},
),
(
"tag_instead_of_value",
indoc::indoc! {"
data_test
_entry.id 1ABC
_entry.description _another_tag
_entry.author 'Smith, J.'
"},
),
// Additional test cases (commented out for now):
// ("multiple_expected_tokens", indoc::indoc! {"
// data_test
// _entry.id
// _entry.description 'test'
// "}),
// ("missing_value_simple", indoc::indoc! {"
// data_test
// _entry.id
// "}),
// ("unclosed_string_simple", indoc::indoc! {"
// data_test
// _entry.description \"unclosed string
// "}),
// ("context_lines_error", indoc::indoc! {"
// data_test
// line2
// line3
// line4
// line5
// _entry.invalid_syntax @#$
// line7
// line8
// line9
// "}),
// ("invalid_loop", indoc::indoc! {"
// data_test
// loop_
// _atom_site.id
// _atom_site.x
// ATOM 1 2.5
// ATOM
// _entry.title 'test'
// "}),
];
/// Helper function to test error formatting for a specific mode
fn test_error_format_mode(mode: ErrorFormatMode, context_lines: usize, snapshot_prefix: &str) {
for (case_name, input) in ERROR_CASES {
let config = default_config();
let result = parse(input, &config);
assert!(result.is_err(), "Expected error for case: {}", case_name);
let error = result.unwrap_err();
let formatted = error.format_error(mode, context_lines);
snapshot_utils::assert_snapshot_gz(
&format!("error_handling_tests__{}_{}", snapshot_prefix, case_name),
&formatted,
);
}
}
#[test]
fn test_basic_error_format_snapshots() {
test_error_format_mode(ErrorFormatMode::Basic, 5, "basic_error");
}
#[test]
fn test_ascii_error_format_snapshots() {
test_error_format_mode(ErrorFormatMode::Ascii, 3, "ascii_error");
}
#[test]
#[cfg(feature = "extended-errors")]
fn test_fancy_error_format_snapshots() {
test_error_format_mode(ErrorFormatMode::Fancy, 3, "fancy_error");
}