Skip to content

Commit a7be968

Browse files
committed
progressing on unit testing docs mod
1 parent 3709ec6 commit a7be968

2 files changed

Lines changed: 110 additions & 1 deletion

File tree

src/comments.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ pub enum CommentKind {
9494
}
9595

9696
impl CommentKind {
97-
fn comment(&self) -> &str {
97+
/// Getter for returning the comment from within the enum
98+
pub fn comment(&self) -> &str {
9899
match self {
99100
Self::MultiLine(comment) | Self::SingleLine(comment) => comment,
100101
}

src/docs.rs

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::{ast::ParsedSqlFile, comments::Comments};
66

77
/// Structure for containing the `name` of the `Column` and an [`Option`] for
88
/// the comment as a [`String`]
9+
#[derive(Debug, PartialEq)]
910
pub struct ColumnDoc {
1011
name: String,
1112
doc: Option<String>,
@@ -38,6 +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
42+
#[derive(Debug,PartialEq)]
4143
pub struct TableDoc {
4244
name: String,
4345
doc: Option<String>,
@@ -81,6 +83,7 @@ impl TableDoc {
8183

8284
/// Structure for containing the docs for every `Table` in an `.sql` file as a
8385
/// `Vec` of [`TableDoc`]
86+
#[derive(Debug, PartialEq)]
8487
pub struct SqlDocs {
8588
tables: Vec<TableDoc>,
8689
}
@@ -142,3 +145,108 @@ impl SqlDocs {
142145
&self.tables
143146
}
144147
}
148+
149+
#[cfg(test)]
150+
mod tests {
151+
use crate::docs::{ColumnDoc, SqlDocs, TableDoc};
152+
153+
154+
#[test]
155+
fn test_sql_docs_struct() {
156+
let column_doc = ColumnDoc::new("id".to_string(), Some("The ID for the table".to_string()));
157+
let columns = vec![column_doc];
158+
let table_doc = TableDoc::new("user".to_string(), Some("The table for users".to_string()), columns);
159+
let tables = vec![table_doc];
160+
let sql_doc = SqlDocs::new(tables);
161+
assert_eq!(sql_doc.tables().first().unwrap().name(), "user");
162+
assert_eq!(sql_doc.tables().first().unwrap().columns().first().unwrap().name(), "id");
163+
}
164+
#[test]
165+
fn generate_docs_from_mixed_comments() {
166+
use std::path::Path;
167+
168+
use crate::{ast::ParsedSqlFileSet, files::SqlFileSet, comments::Comments};
169+
let path = Path::new("sql_files");
170+
let set = SqlFileSet::new(path, None).unwrap();
171+
let parsed_set = ParsedSqlFileSet::parse_all(set).unwrap();
172+
for file in parsed_set.files().iter() {
173+
let comments = Comments::parse_all_comments_from_file(file).unwrap();
174+
let docs = SqlDocs::from_parsed_file(file, &comments);
175+
match file.file().path().to_str().unwrap() {
176+
"sql_files/with_single_line_comments.sql" => {
177+
let expected = [
178+
"Users table stores user account information",
179+
"Primary key",
180+
"Username for login",
181+
"Email address",
182+
"When the user registered",
183+
"Posts table stores blog posts",
184+
"Primary key",
185+
"Post title",
186+
"Foreign key linking to users",
187+
"Main body text",
188+
"When the post was created",
189+
];
190+
assert_eq!(expected.len(), comments.comments().len());
191+
comments
192+
.comments()
193+
.iter()
194+
.enumerate()
195+
.for_each(|(i, c)| assert_eq!(expected[i], c.kind().comment()));
196+
}
197+
"sql_files/with_multiline_comments.sql" => {
198+
let expected = [
199+
"Users table stores user account information \nmultiline",
200+
"Primary key \n multiline",
201+
"Username for login \n multiline",
202+
"Email address \n multiline",
203+
"When the user registered \n multiline",
204+
"Posts table stores blog posts \nmultiline",
205+
"Primary key \n multiline",
206+
"Post title \n multiline",
207+
"Foreign key linking to users \n multiline",
208+
"Main body text \n multiline",
209+
"When the post was created \n multiline",
210+
];
211+
assert_eq!(expected.len(), comments.comments().len());
212+
comments
213+
.comments()
214+
.iter()
215+
.enumerate()
216+
.for_each(|(i, c)| assert_eq!(expected[i], c.kind().comment()));
217+
}
218+
"sql_files/with_mixed_comments.sql" => {
219+
let expected = [
220+
"Users table stores user account information",
221+
"Primary key",
222+
"Username for login",
223+
"Email address",
224+
"When the user registered",
225+
"Posts table stores blog posts",
226+
"Primary key",
227+
"Post title",
228+
"Foreign key linking to users",
229+
"Main body text",
230+
"When the post was created",
231+
];
232+
assert_eq!(expected.len(), comments.comments().len());
233+
comments
234+
.comments()
235+
.iter()
236+
.enumerate()
237+
.for_each(|(i, c)| assert_eq!(expected[i], c.kind().comment()));
238+
}
239+
"sql_files/without_comments.sql" => {
240+
assert_eq!(0, comments.comments().len());
241+
}
242+
_ => {
243+
unreachable!(
244+
"This shouldn't be accessible if directory parsed correctly and all test \
245+
files accounted for"
246+
)
247+
}
248+
}
249+
250+
}
251+
}
252+
}

0 commit comments

Comments
 (0)