Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions tonic-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@
let mut stream = TokenStream::new();

for comment in comments {
let comment = comment.as_ref();
if comment.trim().is_empty() {
continue;
}
stream.extend(generate_doc_comment(comment));
}

Expand Down Expand Up @@ -268,7 +272,7 @@

while let Some(x) = it.next() {
s.push(x.to_ascii_lowercase());
if let Some(y) = it.peek() {

Check warning on line 275 in tonic-build/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

this `if` statement can be collapsed
if y.is_uppercase() {
s.push('_');
}
Expand Down Expand Up @@ -314,4 +318,53 @@
assert_eq!(naive_snake_case(case.0), case.1)
}
}

#[test]
fn test_generate_doc_comments_skips_empty_and_whitespace() {
assert!(generate_doc_comments(&[] as &[&str]).is_empty());
assert!(generate_doc_comments(&[""]).is_empty());
assert!(generate_doc_comments(&[" "]).is_empty());
assert!(generate_doc_comments(&[" ", "\t", "\n"]).is_empty());

let stream = generate_doc_comments(&["", "hello", " ", "world", ""]);
let s = stream.to_string();
assert!(s.contains("doc = \" hello\""));
assert!(s.contains("doc = \" world\""));
assert!(!s.contains("doc = \"\""));
assert!(!s.contains("doc = \" \""));
}

#[test]
fn test_service_method_with_no_comment_does_not_emit_blank_doc() {
let service = manual::Service::builder()
.name("Greeter")
.package("helloworld")
.method(
manual::Method::builder()
.name("say_hello")
.route_name("SayHello")
.input_type("crate::HelloRequest")
.output_type("crate::HelloResponse")
.codec_path("crate::JsonCodec")
.build(),
)
.build();

let client = CodeGenBuilder::new()
.emit_package(true)
.compile_well_known_types(false)
.build_transport(false)
.generate_client(&service, "");

let server = CodeGenBuilder::new()
.emit_package(true)
.compile_well_known_types(false)
.generate_server(&service, "");

let client_str = client.to_string();
let server_str = server.to_string();

assert!(!client_str.contains("doc = \"\""));
assert!(!server_str.contains("doc = \"\""));
}
}
Loading