Skip to content

Commit f5409bf

Browse files
committed
Test coverage
1 parent d811999 commit f5409bf

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

src/ast/comments.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ impl From<Comments> for Vec<CommentWithSpan> {
102102
}
103103

104104
/// A source code comment with information of its entire span.
105-
#[derive(Debug, Clone)]
105+
#[derive(Debug, Clone, PartialEq, Eq)]
106106
pub struct CommentWithSpan {
107107
/// The source code comment iself
108108
pub comment: Comment,
@@ -119,7 +119,7 @@ impl Deref for CommentWithSpan {
119119
}
120120

121121
/// A unified type of the different source code comment formats.
122-
#[derive(Debug, Clone)]
122+
#[derive(Debug, Clone, PartialEq, Eq)]
123123
pub enum Comment {
124124
/// A single line comment, typically introduced with a prefix and spanning
125125
/// until end-of-line or end-of-file in the source code.

tests/sqlparser_comments.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
#![warn(clippy::all)]
19+
//! Test comment extraction from SQL source code.
20+
21+
#[cfg(test)]
22+
use pretty_assertions::assert_eq;
23+
24+
use sqlparser::{ast::comments::{Comment, CommentWithSpan}, dialect::GenericDialect, parser::Parser, tokenizer::Span};
25+
26+
#[test]
27+
fn parse_sql_with_comments() {
28+
let sql = r#"
29+
-- second line comment
30+
select * from /* inline comment after `from` */ dual;
31+
32+
/*select
33+
some
34+
more*/
35+
36+
-- end-of-script-with-no-newline"#;
37+
38+
let comments = match Parser::parse_sql_with_comments(&GenericDialect, sql) {
39+
Ok((_, comments)) => comments,
40+
Err(e) => panic!("Invalid sql script: {e}"),
41+
};
42+
43+
assert_eq!(Vec::from(comments), vec![
44+
CommentWithSpan {
45+
comment: Comment::SingleLine {
46+
content: " second line comment\n".into(),
47+
prefix: "--".into()
48+
},
49+
span: Span::new((2, 1).into(), (3, 1).into()),
50+
},
51+
CommentWithSpan {
52+
comment: Comment::MultiLine(" inline comment after `from` ".into()),
53+
span: Span::new((3, 15).into(), (3, 48).into()),
54+
},
55+
CommentWithSpan {
56+
comment: Comment::MultiLine("select\nsome\nmore".into()),
57+
span: Span::new((5, 1).into(), (7, 7).into())
58+
},
59+
CommentWithSpan {
60+
comment: Comment::SingleLine {
61+
content: " end-of-script-with-no-newline".into(),
62+
prefix: "--".into()
63+
},
64+
span: Span::new((9, 3).into(), (9, 35).into()),
65+
}
66+
]);
67+
}

0 commit comments

Comments
 (0)