Skip to content

Commit cb94d6a

Browse files
committed
Test coverage
1 parent d811999 commit cb94d6a

File tree

2 files changed

+77
-2
lines changed

2 files changed

+77
-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: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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::{
25+
ast::comments::{Comment, CommentWithSpan},
26+
dialect::GenericDialect,
27+
parser::Parser,
28+
tokenizer::Span,
29+
};
30+
31+
#[test]
32+
fn parse_sql_with_comments() {
33+
let sql = r#"
34+
-- second line comment
35+
select * from /* inline comment after `from` */ dual;
36+
37+
/*select
38+
some
39+
more*/
40+
41+
-- end-of-script-with-no-newline"#;
42+
43+
let comments = match Parser::parse_sql_with_comments(&GenericDialect, sql) {
44+
Ok((_, comments)) => comments,
45+
Err(e) => panic!("Invalid sql script: {e}"),
46+
};
47+
48+
assert_eq!(
49+
Vec::from(comments),
50+
vec![
51+
CommentWithSpan {
52+
comment: Comment::SingleLine {
53+
content: " second line comment\n".into(),
54+
prefix: "--".into()
55+
},
56+
span: Span::new((2, 1).into(), (3, 1).into()),
57+
},
58+
CommentWithSpan {
59+
comment: Comment::MultiLine(" inline comment after `from` ".into()),
60+
span: Span::new((3, 15).into(), (3, 48).into()),
61+
},
62+
CommentWithSpan {
63+
comment: Comment::MultiLine("select\nsome\nmore".into()),
64+
span: Span::new((5, 1).into(), (7, 7).into())
65+
},
66+
CommentWithSpan {
67+
comment: Comment::SingleLine {
68+
content: " end-of-script-with-no-newline".into(),
69+
prefix: "--".into()
70+
},
71+
span: Span::new((9, 3).into(), (9, 35).into()),
72+
}
73+
]
74+
);
75+
}

0 commit comments

Comments
 (0)