Skip to content

Commit a5cea86

Browse files
committed
completed basic unit testing for comments module
1 parent c8eb640 commit a5cea86

1 file changed

Lines changed: 81 additions & 1 deletion

File tree

src/comments.rs

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ 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)]
12+
#[derive(Debug,PartialEq,PartialOrd)]
1313
pub struct Location {
1414
line: u64,
1515
column: u64,
1616
}
1717

18+
1819
impl Location {
1920
/// Method for instantiating a new [`Location`]
2021
///
@@ -46,6 +47,7 @@ impl Default for Location {
4647
}
4748

4849
/// A structure for holding the span of comments found
50+
#[derive(Debug,PartialEq)]
4951
pub struct Span {
5052
start: Location,
5153
end: Location,
@@ -76,8 +78,16 @@ impl Span {
7678
}
7779
}
7880

81+
impl Default for Span {
82+
fn default() -> Self {
83+
Self::new(Location::default(), Location::default())
84+
}
85+
}
86+
87+
7988
/// Enum for holding the comment content, differentiated by single line `--` and
8089
/// multiline `/* */`
90+
#[derive(Clone, Debug,PartialEq)]
8191
pub enum CommentKind {
8292
/// Enum variant for Multiline Comments
8393
MultiLine(String),
@@ -242,3 +252,73 @@ impl Comments {
242252
&self.comments
243253
}
244254
}
255+
256+
#[cfg(test)]
257+
use super::*;
258+
259+
#[test]
260+
fn location_new_and_default() {
261+
let mut location = Location::new(2, 5);
262+
location.column = 20;
263+
location.line = 43;
264+
265+
assert_eq!(
266+
Location { column: 20, line: 43 },
267+
location
268+
);
269+
270+
let location2 = Location::default();
271+
assert_eq!(location2, Location { line: 0, column: 0 });
272+
}
273+
274+
275+
#[test]
276+
fn span_default_and_updates() {
277+
let default = Span::default();
278+
assert_eq!(default.start, Location::default());
279+
assert_eq!(default.end, Location::default());
280+
281+
let mut span = Span::default();
282+
span.end = Location::new(55, 100);
283+
284+
assert_eq!(span.start, Location::default());
285+
assert_eq!(span.end, Location { line: 55, column: 100 });
286+
}
287+
288+
289+
#[test]
290+
fn comments_with_comment_kind() {
291+
let raw_comment = "-- a comment";
292+
let len = raw_comment.len() as u64;
293+
294+
let singleline = CommentKind::SingleLine(raw_comment.to_string());
295+
let mut span = Span::default();
296+
span.end.column = len - 1;
297+
298+
let comment = Comment::new(singleline.clone(), span);
299+
300+
assert_eq!(comment.kind, singleline);
301+
302+
let expected_span = Span::new(
303+
Location { line: 0, column: 0 },
304+
Location { line: 0, column: len - 1 },
305+
);
306+
307+
assert_eq!(comment.span, expected_span);
308+
}
309+
310+
#[test]
311+
fn multiline_comment_span() {
312+
let kind = CommentKind::MultiLine("/* hello\nworld */".to_string());
313+
let span = Span::new(
314+
Location { line: 1, column: 0 },
315+
Location { line: 2, column: 9 },
316+
);
317+
318+
let comment = Comment::new(kind.clone(), span);
319+
320+
assert_eq!(comment.kind, kind);
321+
assert_eq!(comment.span.start.line, 1);
322+
assert_eq!(comment.span.end.line, 2);
323+
}
324+

0 commit comments

Comments
 (0)