rustdoc: Add regression tests for existing attr parsing behavior

This commit is contained in:
Brian Anderson 2012-01-15 22:47:23 -08:00
parent 01fedcad77
commit 763f3c4d5b
2 changed files with 88 additions and 7 deletions

View File

@ -1,6 +1,6 @@
export parse_fn;
fn parse_fn(attrs: [ast::attribute]) -> doc::fndoc {
fn parse_fn(name: str, attrs: [ast::attribute]) -> doc::fndoc {
let noargdocs = map::new_str_hash::<str>();
let _fndoc = none;
for attr: ast::attribute in attrs {
@ -8,7 +8,7 @@ fn parse_fn(attrs: [ast::attribute]) -> doc::fndoc {
ast::meta_name_value(
"doc", {node: ast::lit_str(value), span: _}) {
_fndoc = some(~{
name: "todo",
name: name,
brief: value,
desc: none,
return: none,
@ -17,7 +17,7 @@ fn parse_fn(attrs: [ast::attribute]) -> doc::fndoc {
}
ast::meta_list("doc", docs) {
_fndoc = some(
parse_fn_(docs));
parse_fn_(name, docs));
}
}
}
@ -26,7 +26,7 @@ fn parse_fn(attrs: [ast::attribute]) -> doc::fndoc {
some(_d) { _d }
none. {
~{
name: "todo",
name: name,
brief: "_undocumented_",
desc: none,
return: none,
@ -50,7 +50,7 @@ fn parse_fn(attrs: [ast::attribute]) -> doc::fndoc {
args(items = "Doc attribute contents"),
return = "Parsed function docs."
)]
fn parse_fn_(items: [@ast::meta_item]) -> doc::fndoc {
fn parse_fn_(name: str, items: [@ast::meta_item]) -> doc::fndoc {
let brief = none;
let desc = none;
let return = none;
@ -97,9 +97,90 @@ fn parse_fn_(items: [@ast::meta_item]) -> doc::fndoc {
};
~{
name: "todo",
name: name,
brief: _brief,
desc: desc,
return: return,
args: argdocs }
}
#[cfg(test)]
mod tests {
fn parse_attributes(source: str) -> [ast::attribute] {
import rustc::driver::diagnostic;
import rustc::syntax::codemap;
import rustc::syntax::parse::parser;
let cm = codemap::new_codemap();
let parse_sess = @{
cm: cm,
mutable next_id: 0,
diagnostic: diagnostic::mk_handler(cm, none)
};
let parser = parser::new_parser_from_source_str(
parse_sess, [], "-", source);
parser::parse_outer_attributes(parser)
}
#[test]
fn parse_fn_should_handle_undocumented_functions() {
let source = "";
let attrs = parse_attributes(source);
let doc = parse_fn("f", attrs);
assert doc.brief == "_undocumented_";
assert doc.desc == none;
assert doc.return == none;
assert doc.args.size() == 0u;
}
#[test]
fn parse_fn_should_parse_simple_doc_attributes() {
let source = "#[doc = \"basic\"]";
let attrs = parse_attributes(source);
let doc = parse_fn("f", attrs);
assert doc.brief == "basic";
}
#[test]
fn parse_fn_should_parse_the_brief_description() {
let source = "#[doc(brief = \"short\")]";
let attrs = parse_attributes(source);
let doc = parse_fn("f", attrs);
assert doc.brief == "short";
}
#[test]
fn parse_fn_should_parse_the_long_description() {
let source = "#[doc(desc = \"description\")]";
let attrs = parse_attributes(source);
let doc = parse_fn("f", attrs);
assert doc.desc == some("description");
}
#[test]
fn parse_fn_should_parse_the_return_value_description() {
let source = "#[doc(return = \"return value\")]";
let attrs = parse_attributes(source);
let doc = parse_fn("f", attrs);
assert doc.return == some("return value");
}
#[test]
fn parse_fn_should_parse_the_argument_descriptions() {
let source = "#[doc(args(a = \"arg a\", b = \"arg b\"))]";
let attrs = parse_attributes(source);
let doc = parse_fn("f", attrs);
assert doc.args.get("a") == "arg a";
assert doc.args.get("b") == "arg b";
}
#[test]
fn parse_fn_should_set_brief_desc_to_undocumented_if_not_exists() {
let source = "#[doc(desc = \"long desc\")]";
let attrs = parse_attributes(source);
let doc = parse_fn("f", attrs);
assert doc.brief == "_undocumented_";
}
}

View File

@ -28,7 +28,7 @@
item = "AST item to document")
)]
fn doc_item(rd: rustdoc, item: @ast::item) {
let _fndoc0 = attr_parser::parse_fn(item.attrs);
let _fndoc0 = attr_parser::parse_fn(item.ident, item.attrs);
alt item.node {
ast::item_const(ty, expr) { }