Skip to content

Commit d50978b

Browse files
committed
test coverage grown to 100% function coverage
1 parent 72abfe2 commit d50978b

File tree

4 files changed

+119
-42
lines changed

4 files changed

+119
-42
lines changed

src/comments.rs

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::ast::ParsedSqlFile;
99

1010
/// Structure for holding a location in the file. Assumes file is first split by
1111
/// lines and then split by characters (column)
12-
#[derive(Debug, Eq, PartialEq, PartialOrd)]
12+
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd)]
1313
pub struct Location {
1414
line: u64,
1515
column: u64,
@@ -46,7 +46,7 @@ impl Default for Location {
4646
}
4747

4848
/// A structure for holding the span of comments found
49-
#[derive(Debug, Eq, PartialEq)]
49+
#[derive(Clone, Debug, Eq, PartialEq)]
5050
pub struct Span {
5151
start: Location,
5252
end: Location,
@@ -104,7 +104,7 @@ impl CommentKind {
104104
}
105105

106106
/// Structure for containing the [`CommentKind`] and the [`Span`] for a comment
107-
#[derive(Debug, Eq, PartialEq)]
107+
#[derive(Clone, Debug, Eq, PartialEq)]
108108
pub struct Comment {
109109
kind: CommentKind,
110110
span: Span,
@@ -184,37 +184,6 @@ impl std::error::Error for CommentError {}
184184
/// Alias for comment results that may return a [`CommentError`]
185185
pub type CommentResult<T> = Result<T, CommentError>;
186186

187-
/// Structure that holds the comment along with its location in the file
188-
pub struct CommentWithSpan {
189-
comment: Comment,
190-
span: Span,
191-
}
192-
193-
impl CommentWithSpan {
194-
/// Method for creating a new [`CommentWithSpan`] from the comment
195-
/// [`String`] and the [`Span`]
196-
///
197-
/// # Parameters
198-
/// - the comment as a [`String`]
199-
/// - the span of the comment as a [`Span`]
200-
#[must_use]
201-
pub const fn new(comment: Comment, span: Span) -> Self {
202-
Self { comment, span }
203-
}
204-
205-
/// Getter method for retrieving the comment content
206-
#[must_use]
207-
pub const fn comment(&self) -> &Comment {
208-
&self.comment
209-
}
210-
211-
/// Getter method for retrieving the [`Span`] of the comment
212-
#[must_use]
213-
pub const fn span(&self) -> &Span {
214-
&self.span
215-
}
216-
}
217-
218187
/// Structure for holding all comments found in the document
219188
#[derive(Debug, Eq, PartialEq)]
220189
pub struct Comments {
@@ -602,3 +571,37 @@ fn multiline_comment_spans_are_correct() {
602571
"end column should be after start column for primary key multiline comment",
603572
);
604573
}
574+
575+
#[test]
576+
fn test_comment_error() {
577+
let unterminated = CommentError::UnterminatedMultiLineComment { start: Location::default() };
578+
let location = Location { line: 1, column: 1 };
579+
let expected = format!(
580+
"unterminated block comment with start at line {}, column {}",
581+
location.line(),
582+
location.column()
583+
);
584+
assert_eq!(unterminated.to_string(), expected);
585+
}
586+
587+
#[test]
588+
fn test_comments() {
589+
let comment_vec = vec![
590+
Comment::new(
591+
CommentKind::SingleLine("a comment".to_string()),
592+
Span { start: Location::new(1, 1), end: Location::new(1, 12) },
593+
),
594+
Comment::new(
595+
CommentKind::SingleLine("a second comment".to_string()),
596+
Span { start: Location::new(1, 1), end: Location::new(2, 19) },
597+
),
598+
];
599+
let length = comment_vec.len();
600+
let comments = Comments::new(comment_vec.clone());
601+
assert!(comments.comments().len() == length);
602+
for (i, comment) in comments.comments().iter().enumerate() {
603+
assert_eq!(comment.kind().comment(), comment_vec[i].kind().comment());
604+
assert_eq!(comment.span().start(), comment_vec[i].span().start());
605+
assert_eq!(comment.span().end(), comment_vec[i].span().end());
606+
}
607+
}

src/docs.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,6 @@ mod tests {
188188

189189
match file.file().path().to_str().unwrap() {
190190
"sql_files/with_single_line_comments.sql" => {
191-
dbg!("testing with single line comments");
192191
let expected = SqlDocs::new(vec![
193192
TableDoc::new(
194193
"users".to_string(),
@@ -234,7 +233,6 @@ mod tests {
234233
assert_eq!(docs, expected);
235234
}
236235
"sql_files/with_multiline_comments.sql" => {
237-
dbg!("testing with multiline comments");
238236
let expected = SqlDocs::new(vec![
239237
TableDoc::new(
240238
"users".to_string(),
@@ -294,7 +292,6 @@ mod tests {
294292
assert_eq!(docs, expected);
295293
}
296294
"sql_files/with_mixed_comments.sql" => {
297-
dbg!("testing with mixed comments");
298295
// Mixed should still produce the same final docs as the pure single-line
299296
// version.
300297
let expected = SqlDocs::new(vec![
@@ -342,7 +339,6 @@ mod tests {
342339
assert_eq!(docs, expected);
343340
}
344341
"sql_files/without_comments.sql" => {
345-
dbg!("testing with no comments");
346342
let expected = SqlDocs::new(vec![
347343
TableDoc::new(
348344
"users".to_string(),
@@ -378,5 +374,4 @@ mod tests {
378374
}
379375
}
380376
}
381-
382377
}

src/files.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,5 @@ mod tests {
303303

304304
let bad_file_list = SqlFileSet::new(Path::new(invalid_dir), None);
305305
assert!(bad_file_list.is_err())
306-
307306
}
308307
}
309-

src/lib.rs

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use core::fmt;
88
use std::{
99
error,
10+
fmt::Debug,
1011
path::{Path, PathBuf},
1112
};
1213

@@ -37,7 +38,7 @@ pub enum DocError {
3738
impl fmt::Display for DocError {
3839
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3940
match self {
40-
Self::FileReadError(error) => write!(f, "file read error:{error}"),
41+
Self::FileReadError(error) => write!(f, "file read error: {error}"),
4142
Self::CommentError(comment_error) => {
4243
write!(f, "comment parse error: {comment_error}")
4344
}
@@ -199,3 +200,83 @@ fn test_with_no_deny_list_from_files() {
199200
}
200201
}
201202
}
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

Comments
 (0)