wasm-bindgen/tests/all/comments.rs
Robert Masen 19d6cf1488 Copy doc comments from Rust to JS (#265)
* backend comments complete

* better matching

* gen comments

* Add example

* Move test bindings gen to own fn

* move build step into build fn

* add fn to read js, refactor gen_bindings/test to allow for this

* Add comments test

* Update readmes

* add comments to travis

* fix broken tests

* +x on build.sh

* fix wbg cmd in build.sh

* Address fitzgen's comments
2018-06-15 09:20:56 -07:00

56 lines
1.6 KiB
Rust

use super::project;
#[test]
fn works() {
let mut p = project();
p.file("src/lib.rs", r#"
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
/// This comment should exist
pub fn annotated() -> String {
String::new()
}
#[wasm_bindgen]
/// This comment should exist
pub struct Annotated {
/// This comment should not exist
a: String,
/// This comment should exist
pub b: u32
}
#[wasm_bindgen]
impl Annotated {
#[wasm_bindgen(method)]
/// This comment should exist
pub fn get_a(&self) -> String {
self.a.clone()
}
}
"#);
let (root, target) = p.cargo_build();
p.gen_bindings(&root, &target);
let js = p.read_js();
let comments = extract_doc_comments(&js);
assert!(comments.iter().all(|c| c == "This comment should exist"));
}
/// Pull out all lines in a js string that start with
/// '* ', all other lines will either be comment start, comment
/// end or actual js lines.
fn extract_doc_comments(js: &str) -> Vec<String> {
js.lines().filter_map(|l| {
let trimmed = l.trim();
if trimmed.starts_with("* ") {
Some(trimmed[2..].to_owned())
} else {
None
}
}).collect()
}