|
7 | 7 | use core::fmt; |
8 | 8 | use std::{ |
9 | 9 | error, |
| 10 | + fmt::Debug, |
10 | 11 | path::{Path, PathBuf}, |
11 | 12 | }; |
12 | 13 |
|
@@ -37,7 +38,7 @@ pub enum DocError { |
37 | 38 | impl fmt::Display for DocError { |
38 | 39 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
39 | 40 | match self { |
40 | | - Self::FileReadError(error) => write!(f, "file read error:{error}"), |
| 41 | + Self::FileReadError(error) => write!(f, "file read error: {error}"), |
41 | 42 | Self::CommentError(comment_error) => { |
42 | 43 | write!(f, "comment parse error: {comment_error}") |
43 | 44 | } |
@@ -199,3 +200,83 @@ fn test_with_no_deny_list_from_files() { |
199 | 200 | } |
200 | 201 | } |
201 | 202 | } |
| 203 | +#[test] |
| 204 | +fn test_doc_errors() { |
| 205 | + use std::fs; |
| 206 | + |
| 207 | + use crate::comments::Location; |
| 208 | + let invalid_dir = "INVALID"; |
| 209 | + let io_error = fs::read_dir(invalid_dir).unwrap_err(); |
| 210 | + let io_error_str = io_error.to_string(); |
| 211 | + let read_error = DocError::FileReadError(io_error); |
| 212 | + let expected_read_error = "file read error: ".to_owned().to_string() + &io_error_str; |
| 213 | + assert!(read_error.to_string().contains(&expected_read_error)); |
| 214 | + |
| 215 | + let comment_error = DocError::CommentError(CommentError::UnmatchedMultilineCommentStart { |
| 216 | + location: Location::default(), |
| 217 | + }); |
| 218 | + let expected_comment_error = |
| 219 | + "comment parse error: unmatched block comment start at line 1, column 1"; |
| 220 | + assert_eq!(comment_error.to_string(), expected_comment_error); |
| 221 | + |
| 222 | + let sql_error = DocError::SqlParserError(ParserError::RecursionLimitExceeded); |
| 223 | + let expected_sql_error = "SQL parse error sql parser error: recursion limit exceeded"; |
| 224 | + assert_eq!(sql_error.to_string(), expected_sql_error); |
| 225 | +} |
| 226 | + |
| 227 | +#[test] |
| 228 | +fn test_doc_errors_from() { |
| 229 | + use std::fs; |
| 230 | + |
| 231 | + use crate::comments::Location; |
| 232 | + let invalid_dir = "INVALID"; |
| 233 | + let io_error = fs::read_dir(invalid_dir).unwrap_err(); |
| 234 | + let io_kind = io_error.kind(); |
| 235 | + let doc_io_error = DocError::from(io_error); |
| 236 | + match doc_io_error { |
| 237 | + DocError::FileReadError(inner) => assert_eq!(inner.kind(), io_kind), |
| 238 | + _ => panic!("expected instance of DocError::FileReadError"), |
| 239 | + } |
| 240 | + |
| 241 | + let comment_error = |
| 242 | + CommentError::UnmatchedMultilineCommentStart { location: Location::default() }; |
| 243 | + let comment_error_str = comment_error.to_string(); |
| 244 | + let doc_comment_error: DocError = comment_error.into(); |
| 245 | + match doc_comment_error { |
| 246 | + DocError::CommentError(inner) => assert_eq!(inner.to_string(), comment_error_str), |
| 247 | + _ => panic!("expected instance of DocError::CommentError"), |
| 248 | + } |
| 249 | + |
| 250 | + let parser_error = ParserError::RecursionLimitExceeded; |
| 251 | + let parser_error_str = parser_error.to_string(); |
| 252 | + let doc_parser_error: DocError = parser_error.into(); |
| 253 | + match doc_parser_error { |
| 254 | + DocError::SqlParserError(inner) => assert_eq!(inner.to_string(), parser_error_str), |
| 255 | + _ => panic!("expected instance of DocError::SqlParserError"), |
| 256 | + } |
| 257 | +} |
| 258 | + |
| 259 | +#[test] |
| 260 | +fn test_doc_error_source() { |
| 261 | + use std::{error::Error, fs}; |
| 262 | + |
| 263 | + use crate::comments::Location; |
| 264 | + |
| 265 | + let io_err = fs::read_dir("INVALID").unwrap_err(); |
| 266 | + let io_err_str = io_err.to_string(); |
| 267 | + let doc_io = DocError::FileReadError(io_err); |
| 268 | + let src = doc_io.source().expect("expected Some(source) for FileReadError"); |
| 269 | + assert_eq!(src.to_string(), io_err_str); |
| 270 | + |
| 271 | + let comment = CommentError::UnmatchedMultilineCommentStart { location: Location::default() }; |
| 272 | + let comment_str = comment.to_string(); |
| 273 | + let doc_comment = DocError::CommentError(comment); |
| 274 | + let src = doc_comment.source().expect("expected Some(source) for CommentError"); |
| 275 | + assert_eq!(src.to_string(), comment_str); |
| 276 | + |
| 277 | + let parser = ParserError::RecursionLimitExceeded; |
| 278 | + let parser_str = parser.to_string(); |
| 279 | + let doc_parser = DocError::SqlParserError(parser); |
| 280 | + let src = doc_parser.source().expect("expected Some(source) for SqlParserError"); |
| 281 | + assert_eq!(src.to_string(), parser_str); |
| 282 | +} |
0 commit comments