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
//! Unit tests for the `error` module in the metadata-gen library.
//!
//! This module tests the various custom error types defined in `MetadataError`
//! and their functionality.
// The suites exercise the default feature set.
#![cfg(all(
feature = "std",
feature = "yaml",
feature = "toml",
feature = "json"
))]
#[cfg(test)]
mod tests {
use metadata_gen::error::MetadataError;
use noyalib::Error as YamlError;
use serde_json::Error as JsonError;
use std::io;
use toml::de::Error as TomlError;
/// Test `ExtractionError` construction.
///
/// This test ensures that the `ExtractionError` variant is created and its message is correct.
#[test]
fn test_extraction_error() {
let error = MetadataError::ExtractionError {
message: "No valid front matter found.".to_string(),
};
assert_eq!(
error.to_string(),
"Failed to extract metadata: No valid front matter found."
);
}
/// Test `ProcessingError` construction.
///
/// This test ensures that the `ProcessingError` variant is created and its message is correct.
#[test]
fn test_processing_error() {
let error = MetadataError::ProcessingError {
message: "Unknown field".to_string(),
};
assert_eq!(
error.to_string(),
"Failed to process metadata: Unknown field"
);
}
/// Test `MissingFieldError` construction.
///
/// This test ensures that the `MissingFieldError` variant is created and its message is correct.
#[test]
fn test_missing_field_error() {
let error =
MetadataError::MissingFieldError("description".to_string());
assert_eq!(
error.to_string(),
"Missing required metadata field: description"
);
}
/// Test `DateParseError` construction.
///
/// This test ensures that the `DateParseError` variant is created and its message is correct.
#[test]
fn test_date_parse_error() {
let error = MetadataError::DateParseError(
"Invalid date format".to_string(),
);
assert_eq!(
error.to_string(),
"Failed to parse date: Invalid date format"
);
}
/// Test `IoError` conversion.
///
/// This test ensures that a standard `io::Error` is correctly converted into the `IoError` variant.
#[test]
fn test_io_error() {
let io_error =
io::Error::new(io::ErrorKind::NotFound, "File not found");
let error = MetadataError::from(io_error);
assert_eq!(error.to_string(), "I/O error: File not found");
}
/// Test `YamlError` conversion.
///
/// This test ensures that a `noyalib::Error` is correctly converted into the `YamlError` variant.
#[test]
fn test_yaml_error() {
// Malformed YAML content (unclosed flow mapping)
let invalid_yaml = "{ key: value, ";
// Try to parse the invalid YAML, which will trigger a `noyalib::Error`
let yaml_error: Result<noyalib::Value, YamlError> =
noyalib::from_str(invalid_yaml);
if let Err(yaml_error) = yaml_error {
// Convert the `noyalib::Error` into `MetadataError`
let error = MetadataError::from(yaml_error);
// Check that the error message is correctly formatted
assert!(error.to_string().contains("YAML parsing error"));
} else {
panic!("Expected YAML parsing error, but got Ok");
}
}
/// Test `JsonError` conversion.
///
/// This test ensures that a `serde_json::Error` is correctly converted into the `JsonError` variant.
#[test]
fn test_json_error() {
let invalid_json = "{ invalid json }"; // Malformed JSON
let json_error: Result<serde_json::Value, JsonError> =
serde_json::from_str(invalid_json);
if let Err(json_error) = json_error {
let error = MetadataError::from(json_error);
// Check if the error message contains the correct phrase
assert!(
error.to_string().contains("JSON parsing error"),
"Error message should contain 'JSON parsing error'"
);
} else {
panic!("Expected JSON parsing error, but got Ok");
}
}
/// Test `TomlError` conversion.
///
/// This test ensures that a `toml::de::Error` is correctly converted into the `TomlError` variant.
#[test]
fn test_toml_error() {
let invalid_toml = "invalid = toml"; // Malformed TOML
let toml_error: Result<toml::Value, TomlError> =
toml::from_str(invalid_toml);
if let Err(toml_error) = toml_error {
let error = MetadataError::from(toml_error);
assert!(error.to_string().contains("TOML parsing error"));
} else {
panic!("Expected TOML parsing error, but got Ok");
}
}
}