Skip to content

Commit ffa7d0c

Browse files
committed
unit tests complete. Adjusted span to index 1. Fixes #3
1 parent 4ca9e41 commit ffa7d0c

3 files changed

Lines changed: 54 additions & 63 deletions

File tree

sql_files/with_mixed_comments.sql

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
-- interstitial Comment above statements (should be ignored)
2+
13
/* Users table stores user account information */
2-
CREATE TABLE users (
4+
CREATE TABLE users ( /* users interstitial comment
5+
(should be ignored) */
36
-- Primary key
4-
id INTEGER PRIMARY KEY,
7+
id INTEGER PRIMARY KEY, -- Id comment that is interstitial (should be ignored)
58
/* Username for login */
69
username VARCHAR(255) NOT NULL,
710
-- Email address

src/comments.rs

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ impl Location {
4141

4242
impl Default for Location {
4343
fn default() -> Self {
44-
Self::new(0, 0)
44+
Self::new(1, 1)
4545
}
4646
}
4747

@@ -95,6 +95,7 @@ pub enum CommentKind {
9595

9696
impl CommentKind {
9797
/// Getter for returning the comment from within the enum
98+
#[must_use]
9899
pub fn comment(&self) -> &str {
99100
match self {
100101
Self::MultiLine(comment) | Self::SingleLine(comment) => comment,
@@ -269,11 +270,11 @@ impl Comments {
269270
pub fn scan_comments(src: &str) -> CommentResult<Self> {
270271
let mut comments = Vec::new();
271272

272-
let mut start_line = 0u64;
273-
let mut start_col = 0u64;
273+
let mut start_line = 1u64;
274+
let mut start_col = 1u64;
274275

275-
let mut line = 0u64;
276-
let mut col = 0u64;
276+
let mut line = 1u64;
277+
let mut col = 1u64;
277278

278279
let mut in_single = false;
279280
let mut in_multi = false;
@@ -339,7 +340,7 @@ impl Comments {
339340
}
340341
(false, true, ch) | (true, false, ch) => {
341342
buf.push(ch);
342-
},
343+
}
343344
(false, false, _) => {}
344345
(true, true, _) => {
345346
unreachable!("should not be possible to be in multiline and single line")
@@ -387,21 +388,6 @@ impl Comments {
387388
pub fn leading_comment(&self, line: u64) -> Option<&Comment> {
388389
self.comments().iter().rev().find(|comment| comment.span().end().line() + 1 == line)
389390
}
390-
391-
/// Helper method for checking and finding for a comment before a specific
392-
/// line for columns (hopefully temporary)
393-
///
394-
/// # Parameters
395-
/// - `self` an instance of [`Comments`]
396-
/// - `line` an `u64` value representing the desired line to check above.
397-
#[must_use]
398-
pub fn leading_comment_column(&self, line: u64) -> Option<&Comment> {
399-
dbg!(line);
400-
let line = line -2;
401-
let com = self.comments().iter().rev().find(|comment| comment.span().end().line() == line);
402-
dbg!(&com);
403-
com
404-
}
405391
}
406392

407393
#[cfg(test)]
@@ -414,7 +400,7 @@ fn location_new_and_default() {
414400
assert_eq!(Location { column: 20, line: 43 }, location);
415401

416402
let location2 = Location::default();
417-
assert_eq!(location2, Location { line: 0, column: 0 });
403+
assert_eq!(location2, Location { line: 1, column: 1 });
418404
}
419405

420406
#[test]
@@ -444,15 +430,15 @@ fn comments_with_comment_kind() {
444430
assert_eq!(comment.kind, singleline);
445431

446432
let expected_span =
447-
Span::new(Location { line: 0, column: 0 }, Location { line: 0, column: len - 1 });
433+
Span::new(Location { line: 1, column: 1 }, Location { line: 1, column: len - 1 });
448434

449435
assert_eq!(comment.span, expected_span);
450436
}
451437

452438
#[test]
453439
fn multiline_comment_span() {
454440
let kind = CommentKind::MultiLine("/* hello\nworld */".to_string());
455-
let span = Span::new(Location { line: 1, column: 0 }, Location { line: 2, column: 9 });
441+
let span = Span::new(Location { line: 1, column: 1 }, Location { line: 2, column: 9 });
456442

457443
let comment = Comment::new(kind.clone(), span);
458444

@@ -516,8 +502,11 @@ fn parse_comments() {
516502
}
517503
"sql_files/with_mixed_comments.sql" => {
518504
let expected = [
505+
"interstitial Comment above statements (should be ignored)",
519506
"Users table stores user account information",
507+
"users interstitial comment \n(should be ignored)",
520508
"Primary key",
509+
"Id comment that is interstitial (should be ignored)",
521510
"Username for login",
522511
"Email address",
523512
"When the user registered",
@@ -566,12 +555,12 @@ fn single_line_comment_spans_are_correct() {
566555
assert_eq!(comments.len(), 11);
567556
let first = &comments[0];
568557
assert_eq!(first.kind().comment(), "Users table stores user account information");
569-
assert_eq!(first.span().start(), &Location::new(0, 0));
570-
assert_eq!(first.span().end(), &Location::new(0, 46));
558+
assert_eq!(first.span().start(), &Location::new(1, 1));
559+
assert_eq!(first.span().end(), &Location::new(1, 47));
571560
let primary_key = &comments[1];
572561
assert_eq!(primary_key.kind().comment(), "Primary key");
573-
assert_eq!(primary_key.span().start(), &Location::new(2, 4));
574-
assert_eq!(primary_key.span().end(), &Location::new(2, 18));
562+
assert_eq!(primary_key.span().start(), &Location::new(3, 4));
563+
assert_eq!(primary_key.span().end(), &Location::new(3, 18));
575564
assert!(
576565
primary_key.span().end().column() > primary_key.span().start().column(),
577566
"end column should be after start column",
@@ -596,16 +585,16 @@ fn multiline_comment_spans_are_correct() {
596585
assert_eq!(comments.len(), 11);
597586
let first = &comments[0];
598587
assert_eq!(first.kind().comment(), "Users table stores user account information \nmultiline");
599-
assert_eq!(first.span().start(), &Location::new(0, 0));
600-
assert_eq!(first.span().end().line(), 1);
588+
assert_eq!(first.span().start(), &Location::new(1, 1));
589+
assert_eq!(first.span().end().line(), 2);
601590
assert!(
602591
first.span().end().column() > first.span().start().column(),
603592
"end column should be after start column for first multiline comment",
604593
);
605594
let primary_key = &comments[1];
606595
assert_eq!(primary_key.kind().comment(), "Primary key \n multiline");
607-
assert_eq!(primary_key.span().start(), &Location::new(3, 4));
608-
assert_eq!(primary_key.span().end().line(), 4);
596+
assert_eq!(primary_key.span().start(), &Location::new(4, 4));
597+
assert_eq!(primary_key.span().end().line(), 5);
609598
assert!(
610599
primary_key.span().end().column() > primary_key.span().start().column(),
611600
"end column should be after start column for primary key multiline comment",

src/docs.rs

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
//! Module for parsing sql and comments and returning `table` and `column` information, including comments
1+
//! Module for parsing sql and comments and returning `table` and `column`
2+
//! information, including comments
23
use sqlparser::ast::{Spanned, Statement};
34

45
use crate::{ast::ParsedSqlFile, comments::Comments};
56

67
/// Structure for containing the `name` of the `Column` and an [`Option`] for
78
/// the comment as a [`String`]
8-
#[derive(Debug, PartialEq)]
9+
#[derive(Debug, Eq, PartialEq)]
910
pub struct ColumnDoc {
1011
name: String,
1112
doc: Option<String>,
@@ -38,7 +39,7 @@ impl ColumnDoc {
3839

3940
/// Structure for containing the `name` of the `Table`, an [`Option`] for the
4041
/// comment as a [`String`], and a `Vec` of [`ColumnDoc`] contained in the table
41-
#[derive(Debug,PartialEq)]
42+
#[derive(Debug, Eq, PartialEq)]
4243
pub struct TableDoc {
4344
name: String,
4445
doc: Option<String>,
@@ -82,7 +83,7 @@ impl TableDoc {
8283

8384
/// Structure for containing the docs for every `Table` in an `.sql` file as a
8485
/// `Vec` of [`TableDoc`]
85-
#[derive(Debug, PartialEq)]
86+
#[derive(Debug, Eq, PartialEq)]
8687
pub struct SqlDocs {
8788
tables: Vec<TableDoc>,
8889
}
@@ -99,6 +100,7 @@ impl SqlDocs {
99100
#[must_use]
100101
pub fn from_parsed_file(file: &ParsedSqlFile, comments: &Comments) -> Self {
101102
let mut tables = Vec::new();
103+
dbg!(comments);
102104
for statement in file.statements() {
103105
#[allow(clippy::single_match)]
104106
match statement {
@@ -107,10 +109,7 @@ impl SqlDocs {
107109
let mut column_docs = Vec::new();
108110
for column in &table.columns {
109111
let column_start = column.span().start.line;
110-
dbg!(&column_start);
111-
dbg!(&column.span().end.line);
112-
dbg!(&column.name);
113-
let column_leading = comments.leading_comment_column(column_start);
112+
let column_leading = comments.leading_comment(column_start);
114113
let column_name = column.name.value.clone();
115114
let column_doc = match column_leading {
116115
Some(col_comment) => {
@@ -120,10 +119,7 @@ impl SqlDocs {
120119
};
121120
column_docs.push(column_doc);
122121
}
123-
let table_leading = comments.leading_comment(table_start-1);
124-
dbg!(&table_start);
125-
dbg!(&table.span().end.line);
126-
dbg!(&table.name);
122+
let table_leading = comments.leading_comment(table_start);
127123
let table_doc = match table_leading {
128124
Some(comment) => {
129125
TableDoc::new(
@@ -155,19 +151,19 @@ impl SqlDocs {
155151
mod tests {
156152
use crate::docs::{ColumnDoc, SqlDocs, TableDoc};
157153

158-
159154
#[test]
160155
fn test_sql_docs_struct() {
161156
let column_doc = ColumnDoc::new("id".to_string(), Some("The ID for the table".to_string()));
162157
let columns = vec![column_doc];
163-
let table_doc = TableDoc::new("user".to_string(), Some("The table for users".to_string()), columns);
158+
let table_doc =
159+
TableDoc::new("user".to_string(), Some("The table for users".to_string()), columns);
164160
let tables = vec![table_doc];
165161
let sql_doc = SqlDocs::new(tables);
166162
assert_eq!(sql_doc.tables().first().unwrap().name(), "user");
167163
assert_eq!(sql_doc.tables().first().unwrap().columns().first().unwrap().name(), "id");
168164
}
169165

170-
#[test]
166+
#[test]
171167
fn generate_docs_files() {
172168
use std::path::Path;
173169

@@ -183,6 +179,7 @@ mod tests {
183179

184180
match file.file().path().to_str().unwrap() {
185181
"sql_files/with_single_line_comments.sql" => {
182+
dbg!("testing with single line comments");
186183
let expected = SqlDocs::new(vec![
187184
TableDoc::new(
188185
"users".to_string(),
@@ -208,10 +205,7 @@ mod tests {
208205
Some("Posts table stores blog posts".to_string()),
209206
vec![
210207
ColumnDoc::new("id".to_string(), Some("Primary key".to_string())),
211-
ColumnDoc::new(
212-
"title".to_string(),
213-
Some("Post title".to_string()),
214-
),
208+
ColumnDoc::new("title".to_string(), Some("Post title".to_string())),
215209
ColumnDoc::new(
216210
"user_id".to_string(),
217211
Some("Foreign key linking to users".to_string()),
@@ -231,10 +225,14 @@ mod tests {
231225
assert_eq!(docs, expected);
232226
}
233227
"sql_files/with_multiline_comments.sql" => {
228+
dbg!("testing with multiline comments");
234229
let expected = SqlDocs::new(vec![
235230
TableDoc::new(
236231
"users".to_string(),
237-
Some("Users table stores user account information \nmultiline".to_string()),
232+
Some(
233+
"Users table stores user account information \nmultiline"
234+
.to_string(),
235+
),
238236
vec![
239237
ColumnDoc::new(
240238
"id".to_string(),
@@ -268,7 +266,9 @@ mod tests {
268266
),
269267
ColumnDoc::new(
270268
"user_id".to_string(),
271-
Some("Foreign key linking to users \n multiline".to_string()),
269+
Some(
270+
"Foreign key linking to users \n multiline".to_string(),
271+
),
272272
),
273273
ColumnDoc::new(
274274
"body".to_string(),
@@ -285,7 +285,9 @@ mod tests {
285285
assert_eq!(docs, expected);
286286
}
287287
"sql_files/with_mixed_comments.sql" => {
288-
// Mixed should still produce the same final docs as the pure single-line version.
288+
dbg!("testing with mixed comments");
289+
// Mixed should still produce the same final docs as the pure single-line
290+
// version.
289291
let expected = SqlDocs::new(vec![
290292
TableDoc::new(
291293
"users".to_string(),
@@ -311,10 +313,7 @@ mod tests {
311313
Some("Posts table stores blog posts".to_string()),
312314
vec![
313315
ColumnDoc::new("id".to_string(), Some("Primary key".to_string())),
314-
ColumnDoc::new(
315-
"title".to_string(),
316-
Some("Post title".to_string()),
317-
),
316+
ColumnDoc::new("title".to_string(), Some("Post title".to_string())),
318317
ColumnDoc::new(
319318
"user_id".to_string(),
320319
Some("Foreign key linking to users".to_string()),
@@ -334,6 +333,7 @@ mod tests {
334333
assert_eq!(docs, expected);
335334
}
336335
"sql_files/without_comments.sql" => {
336+
dbg!("testing with no comments");
337337
let expected = SqlDocs::new(vec![
338338
TableDoc::new(
339339
"users".to_string(),
@@ -363,11 +363,10 @@ mod tests {
363363
_ => {
364364
unreachable!(
365365
"This shouldn't be accessible if directory parsed correctly and all test \
366-
files accounted for"
366+
files accounted for"
367367
)
368368
}
369369
}
370370
}
371371
}
372-
373-
}
372+
}

0 commit comments

Comments
 (0)