|
| 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