librustdoc: move tests into dedicated tests module.

This commit is contained in:
Huon Wilson 2013-04-16 01:09:55 +10:00
parent d3be98e9f5
commit ef3a8ebb9b
15 changed files with 1632 additions and 1627 deletions

View file

@ -25,26 +25,6 @@ pub struct CrateAttrs {
name: Option<~str>
}
#[cfg(test)]
mod test {
use syntax::ast;
use syntax;
use core::option::None;
pub fn parse_attributes(source: ~str) -> ~[ast::attribute] {
use syntax::parse;
use syntax::parse::attr::parser_attr;
use syntax::codemap;
let parse_sess = syntax::parse::new_parse_sess(None);
let parser = parse::new_parser_from_source_str(
parse_sess, ~[], ~"-", codemap::FssNone, @source);
parser.parse_outer_attributes()
}
}
fn doc_metas(
attrs: ~[ast::attribute]
) -> ~[@ast::meta_item] {
@ -66,30 +46,6 @@ pub fn parse_crate(attrs: ~[ast::attribute]) -> CrateAttrs {
}
}
#[test]
fn should_extract_crate_name_from_link_attribute() {
let source = ~"#[link(name = \"snuggles\")]";
let attrs = test::parse_attributes(source);
let attrs = parse_crate(attrs);
assert!(attrs.name == Some(~"snuggles"));
}
#[test]
fn should_not_extract_crate_name_if_no_link_attribute() {
let source = ~"";
let attrs = test::parse_attributes(source);
let attrs = parse_crate(attrs);
assert!(attrs.name == None);
}
#[test]
fn should_not_extract_crate_name_if_no_name_value_in_link_attribute() {
let source = ~"#[link(whatever)]";
let attrs = test::parse_attributes(source);
let attrs = parse_crate(attrs);
assert!(attrs.name == None);
}
pub fn parse_desc(attrs: ~[ast::attribute]) -> Option<~str> {
let doc_strs = do doc_metas(attrs).filter_mapped |meta| {
attr::get_meta_item_value_str(*meta).map(|s| copy **s)
@ -101,60 +57,103 @@ pub fn parse_desc(attrs: ~[ast::attribute]) -> Option<~str> {
}
}
#[test]
fn parse_desc_should_handle_undocumented_mods() {
let source = ~"";
let attrs = test::parse_attributes(source);
let attrs = parse_desc(attrs);
assert!(attrs == None);
}
#[test]
fn parse_desc_should_parse_simple_doc_attributes() {
let source = ~"#[doc = \"basic\"]";
let attrs = test::parse_attributes(source);
let attrs = parse_desc(attrs);
assert!(attrs == Some(~"basic"));
}
pub fn parse_hidden(attrs: ~[ast::attribute]) -> bool {
do doc_metas(attrs).find |meta| {
match attr::get_meta_item_list(*meta) {
Some(metas) => {
let hiddens = attr::find_meta_items_by_name(metas, ~"hidden");
!hiddens.is_empty()
}
None => false
Some(metas) => {
let hiddens = attr::find_meta_items_by_name(metas, ~"hidden");
!hiddens.is_empty()
}
None => false
}
}.is_some()
}
#[test]
fn should_parse_hidden_attribute() {
let source = ~"#[doc(hidden)]";
let attrs = test::parse_attributes(source);
assert!(parse_hidden(attrs) == true);
#[cfg(test)]
mod test {
use syntax::ast;
use syntax;
use super::{parse_hidden, parse_crate, parse_desc};
use core::prelude::*;
fn parse_attributes(source: ~str) -> ~[ast::attribute] {
use syntax::parse;
use syntax::parse::attr::parser_attr;
use syntax::codemap;
let parse_sess = syntax::parse::new_parse_sess(None);
let parser = parse::new_parser_from_source_str(
parse_sess, ~[], ~"-", codemap::FssNone, @source);
parser.parse_outer_attributes()
}
#[test]
fn should_extract_crate_name_from_link_attribute() {
let source = ~"#[link(name = \"snuggles\")]";
let attrs = parse_attributes(source);
let attrs = parse_crate(attrs);
assert!(attrs.name == Some(~"snuggles"));
}
#[test]
fn should_not_extract_crate_name_if_no_link_attribute() {
let source = ~"";
let attrs = parse_attributes(source);
let attrs = parse_crate(attrs);
assert!(attrs.name == None);
}
#[test]
fn should_not_extract_crate_name_if_no_name_value_in_link_attribute() {
let source = ~"#[link(whatever)]";
let attrs = parse_attributes(source);
let attrs = parse_crate(attrs);
assert!(attrs.name == None);
}
#[test]
fn parse_desc_should_handle_undocumented_mods() {
let source = ~"";
let attrs = parse_attributes(source);
let attrs = parse_desc(attrs);
assert!(attrs == None);
}
#[test]
fn parse_desc_should_parse_simple_doc_attributes() {
let source = ~"#[doc = \"basic\"]";
let attrs = parse_attributes(source);
let attrs = parse_desc(attrs);
assert!(attrs == Some(~"basic"));
}
#[test]
fn should_parse_hidden_attribute() {
let source = ~"#[doc(hidden)]";
let attrs = parse_attributes(source);
assert!(parse_hidden(attrs) == true);
}
#[test]
fn should_parse_hidden_attribute_with_other_docs() {
let source = ~"#[doc = \"foo\"] #[doc(hidden)] #[doc = \"foo\"]";
let attrs = parse_attributes(source);
assert!(parse_hidden(attrs) == true);
}
#[test]
fn should_not_parse_non_hidden_attribute() {
let source = ~"#[doc = \"\"]";
let attrs = parse_attributes(source);
assert!(parse_hidden(attrs) == false);
}
#[test]
fn should_concatenate_multiple_doc_comments() {
let source = ~"/// foo\n/// bar";
let desc = parse_desc(parse_attributes(source));
assert!(desc == Some(~"foo\nbar"));
}
}
#[test]
fn should_parse_hidden_attribute_with_other_docs() {
let source = ~"#[doc = \"foo\"] #[doc(hidden)] #[doc = \"foo\"]";
let attrs = test::parse_attributes(source);
assert!(parse_hidden(attrs) == true);
}
#[test]
fn should_not_parse_non_hidden_attribute() {
let source = ~"#[doc = \"\"]";
let attrs = test::parse_attributes(source);
assert!(parse_hidden(attrs) == false);
}
#[test]
fn should_concatenate_multiple_doc_comments() {
let source = ~"/// foo\n/// bar";
let desc = parse_desc(test::parse_attributes(source));
assert!(desc == Some(~"foo\nbar"));
}

View file

@ -78,12 +78,6 @@ fn fold_crate(
}
}
#[test]
fn should_replace_top_module_name_with_crate_name() {
let doc = test::mk_doc(~"#[link(name = \"bond\")];");
assert!(doc.cratemod().name() == ~"bond");
}
fn fold_item(
fold: &fold::Fold<astsrv::Srv>,
doc: doc::ItemDoc
@ -113,38 +107,14 @@ fn parse_item_attrs<T:Owned>(
parse_attrs: ~fn(a: ~[ast::attribute]) -> T) -> T {
do astsrv::exec(srv) |ctxt| {
let attrs = match *ctxt.ast_map.get(&id) {
ast_map::node_item(item, _) => copy item.attrs,
ast_map::node_foreign_item(item, _, _, _) => copy item.attrs,
_ => fail!(~"parse_item_attrs: not an item")
ast_map::node_item(item, _) => copy item.attrs,
ast_map::node_foreign_item(item, _, _, _) => copy item.attrs,
_ => fail!(~"parse_item_attrs: not an item")
};
parse_attrs(attrs)
}
}
#[test]
fn should_should_extract_mod_attributes() {
let doc = test::mk_doc(~"#[doc = \"test\"] mod a { }");
assert!(doc.cratemod().mods()[0].desc() == Some(~"test"));
}
#[test]
fn should_extract_top_mod_attributes() {
let doc = test::mk_doc(~"#[doc = \"test\"];");
assert!(doc.cratemod().desc() == Some(~"test"));
}
#[test]
fn should_extract_foreign_fn_attributes() {
let doc = test::mk_doc(~"extern { #[doc = \"test\"] fn a(); }");
assert!(doc.cratemod().nmods()[0].fns[0].desc() == Some(~"test"));
}
#[test]
fn should_extract_fn_attributes() {
let doc = test::mk_doc(~"#[doc = \"test\"] fn a() -> int { }");
assert!(doc.cratemod().fns()[0].desc() == Some(~"test"));
}
fn fold_enum(
fold: &fold::Fold<astsrv::Srv>,
doc: doc::EnumDoc
@ -174,8 +144,8 @@ fn fold_enum(
}
_ => {
fail!(fmt!("Enum variant %s has id that's \
not bound to an enum item",
variant.name))
not bound to an enum item",
variant.name))
}
}
}
@ -190,19 +160,6 @@ fn fold_enum(
}
}
#[test]
fn should_extract_enum_docs() {
let doc = test::mk_doc(~"#[doc = \"b\"]\
enum a { v }");
assert!(doc.cratemod().enums()[0].desc() == Some(~"b"));
}
#[test]
fn should_extract_variant_docs() {
let doc = test::mk_doc(~"enum a { #[doc = \"c\"] v }");
assert!(doc.cratemod().enums()[0].variants[0].desc == Some(~"c"));
}
fn fold_trait(
fold: &fold::Fold<astsrv::Srv>,
doc: doc::TraitDoc
@ -225,30 +182,30 @@ fn merge_method_attrs(
// Create an assoc list from method name to attributes
let attrs: ~[(~str, Option<~str>)] = do astsrv::exec(srv) |ctxt| {
match *ctxt.ast_map.get(&item_id) {
ast_map::node_item(@ast::item {
node: ast::item_trait(_, _, ref methods), _
}, _) => {
vec::map(*methods, |method| {
match copy *method {
ast::required(ty_m) => {
(to_str(ty_m.ident),
attr_parser::parse_desc(copy ty_m.attrs))
}
ast::provided(m) => {
(to_str(m.ident), attr_parser::parse_desc(copy m.attrs))
}
}
})
}
ast_map::node_item(@ast::item {
node: ast::item_impl(_, _, _, ref methods), _
}, _) => {
vec::map(*methods, |method| {
(to_str(method.ident),
attr_parser::parse_desc(copy method.attrs))
})
}
_ => fail!(~"unexpected item")
ast_map::node_item(@ast::item {
node: ast::item_trait(_, _, ref methods), _
}, _) => {
vec::map(*methods, |method| {
match copy *method {
ast::required(ty_m) => {
(to_str(ty_m.ident),
attr_parser::parse_desc(copy ty_m.attrs))
}
ast::provided(m) => {
(to_str(m.ident), attr_parser::parse_desc(copy m.attrs))
}
}
})
}
ast_map::node_item(@ast::item {
node: ast::item_impl(_, _, _, ref methods), _
}, _) => {
vec::map(*methods, |method| {
(to_str(method.ident),
attr_parser::parse_desc(copy method.attrs))
})
}
_ => fail!(~"unexpected item")
}
};
@ -263,22 +220,6 @@ fn merge_method_attrs(
}
}
#[test]
fn should_extract_trait_docs() {
let doc = test::mk_doc(~"#[doc = \"whatever\"] trait i { fn a(); }");
assert!(doc.cratemod().traits()[0].desc() == Some(~"whatever"));
}
#[test]
fn should_extract_trait_method_docs() {
let doc = test::mk_doc(
~"trait i {\
#[doc = \"desc\"]\
fn f(a: bool) -> bool;\
}");
assert!(doc.cratemod().traits()[0].methods[0].desc == Some(~"desc"));
}
fn fold_impl(
fold: &fold::Fold<astsrv::Srv>,
@ -293,34 +234,94 @@ fn fold_impl(
}
}
#[test]
fn should_extract_impl_docs() {
let doc = test::mk_doc(
~"#[doc = \"whatever\"] impl int { fn a() { } }");
assert!(doc.cratemod().impls()[0].desc() == Some(~"whatever"));
}
#[test]
fn should_extract_impl_method_docs() {
let doc = test::mk_doc(
~"impl int {\
#[doc = \"desc\"]\
fn f(a: bool) -> bool { }\
}");
assert!(doc.cratemod().impls()[0].methods[0].desc == Some(~"desc"));
}
#[cfg(test)]
mod test {
use astsrv;
use attr_pass::run;
use doc;
use extract;
use core::prelude::*;
pub fn mk_doc(source: ~str) -> doc::Doc {
fn mk_doc(source: ~str) -> doc::Doc {
do astsrv::from_str(copy source) |srv| {
let doc = extract::from_srv(srv.clone(), ~"");
run(srv.clone(), doc)
}
}
#[test]
fn should_replace_top_module_name_with_crate_name() {
let doc = mk_doc(~"#[link(name = \"bond\")];");
assert!(doc.cratemod().name() == ~"bond");
}
#[test]
fn should_should_extract_mod_attributes() {
let doc = mk_doc(~"#[doc = \"test\"] mod a { }");
assert!(doc.cratemod().mods()[0].desc() == Some(~"test"));
}
#[test]
fn should_extract_top_mod_attributes() {
let doc = mk_doc(~"#[doc = \"test\"];");
assert!(doc.cratemod().desc() == Some(~"test"));
}
#[test]
fn should_extract_foreign_fn_attributes() {
let doc = mk_doc(~"extern { #[doc = \"test\"] fn a(); }");
assert!(doc.cratemod().nmods()[0].fns[0].desc() == Some(~"test"));
}
#[test]
fn should_extract_fn_attributes() {
let doc = mk_doc(~"#[doc = \"test\"] fn a() -> int { }");
assert!(doc.cratemod().fns()[0].desc() == Some(~"test"));
}
#[test]
fn should_extract_enum_docs() {
let doc = mk_doc(~"#[doc = \"b\"]\
enum a { v }");
assert!(doc.cratemod().enums()[0].desc() == Some(~"b"));
}
#[test]
fn should_extract_variant_docs() {
let doc = mk_doc(~"enum a { #[doc = \"c\"] v }");
assert!(doc.cratemod().enums()[0].variants[0].desc == Some(~"c"));
}
#[test]
fn should_extract_trait_docs() {
let doc = mk_doc(~"#[doc = \"whatever\"] trait i { fn a(); }");
assert!(doc.cratemod().traits()[0].desc() == Some(~"whatever"));
}
#[test]
fn should_extract_trait_method_docs() {
let doc = mk_doc(
~"trait i {\
#[doc = \"desc\"]\
fn f(a: bool) -> bool;\
}");
assert!(doc.cratemod().traits()[0].methods[0].desc == Some(~"desc"));
}
#[test]
fn should_extract_impl_docs() {
let doc = mk_doc(
~"#[doc = \"whatever\"] impl int { fn a() { } }");
assert!(doc.cratemod().impls()[0].desc() == Some(~"whatever"));
}
#[test]
fn should_extract_impl_method_docs() {
let doc = mk_doc(
~"impl int {\
#[doc = \"desc\"]\
fn f(a: bool) -> bool { }\
}");
assert!(doc.cratemod().impls()[0].methods[0].desc == Some(~"desc"));
}
}

View file

@ -209,7 +209,7 @@ fn parse_output_style(output_style: &str) -> Result<OutputStyle, ~str> {
}
}
fn maybe_find_pandoc(
pub fn maybe_find_pandoc(
config: &Config,
maybe_pandoc_cmd: Option<~str>,
program_output: Process
@ -243,140 +243,140 @@ fn maybe_find_pandoc(
}
}
#[test]
fn should_find_pandoc() {
let config = Config {
output_format: PandocHtml,
.. default_config(&Path("test"))
};
let mock_program_output: ~fn(&str, &[~str]) -> ProgramOutput = |_, _| {
ProgramOutput { status: 0, out: ~"pandoc 1.8.2.1", err: ~"" }
};
let result = maybe_find_pandoc(&config, None, mock_program_output);
assert!(result == result::Ok(Some(~"pandoc")));
}
#[test]
fn should_error_with_no_pandoc() {
let config = Config {
output_format: PandocHtml,
.. default_config(&Path("test"))
};
let mock_program_output: ~fn(&str, &[~str]) -> ProgramOutput = |_, _| {
ProgramOutput { status: 1, out: ~"", err: ~"" }
};
let result = maybe_find_pandoc(&config, None, mock_program_output);
assert!(result == result::Err(~"couldn't find pandoc"));
}
#[cfg(test)]
mod test {
use config::{Config, mock_program_output, parse_config_};
use config::*;
use core::prelude::*;
use core::run::ProgramOutput;
use core::result::Result;
pub fn parse_config(args: &[~str]) -> Result<Config, ~str> {
fn parse_config(args: &[~str]) -> Result<Config, ~str> {
parse_config_(args, mock_program_output)
}
}
#[test]
fn should_error_with_no_crates() {
let config = test::parse_config(~[~"rustdoc"]);
assert!(config.get_err() == ~"no crates specified");
}
#[test]
fn should_find_pandoc() {
let config = Config {
output_format: PandocHtml,
.. default_config(&Path("test"))
};
let mock_program_output: ~fn(&str, &[~str]) -> ProgramOutput = |_, _| {
ProgramOutput { status: 0, out: ~"pandoc 1.8.2.1", err: ~"" }
};
let result = maybe_find_pandoc(&config, None, mock_program_output);
assert!(result == result::Ok(Some(~"pandoc")));
}
#[test]
fn should_error_with_multiple_crates() {
let config =
test::parse_config(~[~"rustdoc", ~"crate1.rc", ~"crate2.rc"]);
assert!(config.get_err() == ~"multiple crates specified");
}
#[test]
fn should_error_with_no_pandoc() {
let config = Config {
output_format: PandocHtml,
.. default_config(&Path("test"))
};
let mock_program_output: ~fn(&str, &[~str]) -> ProgramOutput = |_, _| {
ProgramOutput { status: 1, out: ~"", err: ~"" }
};
let result = maybe_find_pandoc(&config, None, mock_program_output);
assert!(result == result::Err(~"couldn't find pandoc"));
}
#[test]
fn should_set_output_dir_to_cwd_if_not_provided() {
let config = test::parse_config(~[~"rustdoc", ~"crate.rc"]);
assert!(config.get().output_dir == Path("."));
}
#[test]
fn should_error_with_no_crates() {
let config = parse_config(~[~"rustdoc"]);
assert!(config.get_err() == ~"no crates specified");
}
#[test]
fn should_set_output_dir_if_provided() {
let config = test::parse_config(~[
~"rustdoc", ~"crate.rc", ~"--output-dir", ~"snuggles"
]);
assert!(config.get().output_dir == Path("snuggles"));
}
#[test]
fn should_error_with_multiple_crates() {
let config =
parse_config(~[~"rustdoc", ~"crate1.rc", ~"crate2.rc"]);
assert!(config.get_err() == ~"multiple crates specified");
}
#[test]
fn should_set_output_format_to_pandoc_html_if_not_provided() {
let config = test::parse_config(~[~"rustdoc", ~"crate.rc"]);
assert!(config.get().output_format == PandocHtml);
}
#[test]
fn should_set_output_dir_to_cwd_if_not_provided() {
let config = parse_config(~[~"rustdoc", ~"crate.rc"]);
assert!(config.get().output_dir == Path("."));
}
#[test]
fn should_set_output_format_to_markdown_if_requested() {
let config = test::parse_config(~[
~"rustdoc", ~"crate.rc", ~"--output-format", ~"markdown"
]);
assert!(config.get().output_format == Markdown);
}
#[test]
fn should_set_output_dir_if_provided() {
let config = parse_config(~[
~"rustdoc", ~"crate.rc", ~"--output-dir", ~"snuggles"
]);
assert!(config.get().output_dir == Path("snuggles"));
}
#[test]
fn should_set_output_format_to_pandoc_html_if_requested() {
let config = test::parse_config(~[
~"rustdoc", ~"crate.rc", ~"--output-format", ~"html"
]);
assert!(config.get().output_format == PandocHtml);
}
#[test]
fn should_set_output_format_to_pandoc_html_if_not_provided() {
let config = parse_config(~[~"rustdoc", ~"crate.rc"]);
assert!(config.get().output_format == PandocHtml);
}
#[test]
fn should_error_on_bogus_format() {
let config = test::parse_config(~[
~"rustdoc", ~"crate.rc", ~"--output-format", ~"bogus"
]);
assert!(config.get_err() == ~"unknown output format 'bogus'");
}
#[test]
fn should_set_output_format_to_markdown_if_requested() {
let config = parse_config(~[
~"rustdoc", ~"crate.rc", ~"--output-format", ~"markdown"
]);
assert!(config.get().output_format == Markdown);
}
#[test]
fn should_set_output_style_to_doc_per_mod_by_default() {
let config = test::parse_config(~[~"rustdoc", ~"crate.rc"]);
assert!(config.get().output_style == DocPerMod);
}
#[test]
fn should_set_output_format_to_pandoc_html_if_requested() {
let config = parse_config(~[
~"rustdoc", ~"crate.rc", ~"--output-format", ~"html"
]);
assert!(config.get().output_format == PandocHtml);
}
#[test]
fn should_set_output_style_to_one_doc_if_requested() {
let config = test::parse_config(~[
~"rustdoc", ~"crate.rc", ~"--output-style", ~"doc-per-crate"
]);
assert!(config.get().output_style == DocPerCrate);
}
#[test]
fn should_error_on_bogus_format() {
let config = parse_config(~[
~"rustdoc", ~"crate.rc", ~"--output-format", ~"bogus"
]);
assert!(config.get_err() == ~"unknown output format 'bogus'");
}
#[test]
fn should_set_output_style_to_doc_per_mod_if_requested() {
let config = test::parse_config(~[
~"rustdoc", ~"crate.rc", ~"--output-style", ~"doc-per-mod"
]);
assert!(config.get().output_style == DocPerMod);
}
#[test]
fn should_set_output_style_to_doc_per_mod_by_default() {
let config = parse_config(~[~"rustdoc", ~"crate.rc"]);
assert!(config.get().output_style == DocPerMod);
}
#[test]
fn should_error_on_bogus_output_style() {
let config = test::parse_config(~[
~"rustdoc", ~"crate.rc", ~"--output-style", ~"bogus"
]);
assert!(config.get_err() == ~"unknown output style 'bogus'");
}
#[test]
fn should_set_output_style_to_one_doc_if_requested() {
let config = parse_config(~[
~"rustdoc", ~"crate.rc", ~"--output-style", ~"doc-per-crate"
]);
assert!(config.get().output_style == DocPerCrate);
}
#[test]
fn should_set_pandoc_command_if_requested() {
let config = test::parse_config(~[
~"rustdoc", ~"crate.rc", ~"--pandoc-cmd", ~"panda-bear-doc"
]);
assert!(config.get().pandoc_cmd == Some(~"panda-bear-doc"));
}
#[test]
fn should_set_output_style_to_doc_per_mod_if_requested() {
let config = parse_config(~[
~"rustdoc", ~"crate.rc", ~"--output-style", ~"doc-per-mod"
]);
assert!(config.get().output_style == DocPerMod);
}
#[test]
fn should_set_pandoc_command_when_using_pandoc() {
let config = test::parse_config(~[~"rustdoc", ~"crate.rc"]);
assert!(config.get().pandoc_cmd == Some(~"pandoc"));
}
#[test]
fn should_error_on_bogus_output_style() {
let config = parse_config(~[
~"rustdoc", ~"crate.rc", ~"--output-style", ~"bogus"
]);
assert!(config.get_err() == ~"unknown output style 'bogus'");
}
#[test]
fn should_set_pandoc_command_if_requested() {
let config = parse_config(~[
~"rustdoc", ~"crate.rc", ~"--pandoc-cmd", ~"panda-bear-doc"
]);
assert!(config.get().pandoc_cmd == Some(~"panda-bear-doc"));
}
#[test]
fn should_set_pandoc_command_when_using_pandoc() {
let config = parse_config(~[~"rustdoc", ~"crate.rc"]);
assert!(config.get().pandoc_cmd == Some(~"pandoc"));
}
}

View file

@ -81,44 +81,7 @@ fn fold_impl(fold: &fold::Fold<()>, doc: doc::ImplDoc) -> doc::ImplDoc {
}
}
#[test]
fn should_promote_desc() {
let doc = test::mk_doc(~"#[doc = \"desc\"] mod m { }");
assert!(doc.cratemod().mods()[0].brief() == Some(~"desc"));
}
#[test]
fn should_promote_trait_method_desc() {
let doc = test::mk_doc(~"trait i { #[doc = \"desc\"] fn a(); }");
assert!(doc.cratemod().traits()[0].methods[0].brief ==
Some(~"desc"));
}
#[test]
fn should_promote_impl_method_desc() {
let doc = test::mk_doc(
~"impl int { #[doc = \"desc\"] fn a() { } }");
assert!(doc.cratemod().impls()[0].methods[0].brief == Some(~"desc"));
}
#[cfg(test)]
pub mod test {
use astsrv;
use attr_pass;
use desc_to_brief_pass::run;
use doc;
use extract;
pub fn mk_doc(source: ~str) -> doc::Doc {
do astsrv::from_str(copy source) |srv| {
let doc = extract::from_srv(srv.clone(), ~"");
let doc = (attr_pass::mk_pass().f)(srv.clone(), doc);
run(srv.clone(), doc)
}
}
}
fn extract(desc: Option<~str>) -> Option<~str> {
pub fn extract(desc: Option<~str>) -> Option<~str> {
if desc.is_none() {
return None
}
@ -182,7 +145,7 @@ fn first_sentence_(s: &str) -> ~str {
}
}
fn paragraphs(s: &str) -> ~[~str] {
pub fn paragraphs(s: &str) -> ~[~str] {
let mut lines = ~[];
for str::each_line_any(s) |line| { lines.push(line.to_owned()); }
let mut whitespace_lines = 0;
@ -219,28 +182,65 @@ fn paragraphs(s: &str) -> ~[~str] {
}
}
#[test]
fn test_paragraphs_1() {
let paras = paragraphs(~"1\n\n2");
assert!(paras == ~[~"1", ~"2"]);
}
#[cfg(test)]
mod test {
use astsrv;
use attr_pass;
use super::{extract, paragraphs, run};
use doc;
use extract;
use core::prelude::*;
#[test]
fn test_paragraphs_2() {
let paras = paragraphs(~"\n\n1\n1\n\n2\n\n");
assert!(paras == ~[~"1\n1", ~"2"]);
}
fn mk_doc(source: ~str) -> doc::Doc {
do astsrv::from_str(copy source) |srv| {
let doc = extract::from_srv(srv.clone(), ~"");
let doc = (attr_pass::mk_pass().f)(srv.clone(), doc);
run(srv.clone(), doc)
}
}
#[test]
fn should_promote_short_descs() {
let desc = Some(~"desc");
let brief = extract(copy desc);
assert!(brief == desc);
}
#[test]
fn should_promote_desc() {
let doc = mk_doc(~"#[doc = \"desc\"] mod m { }");
assert!(doc.cratemod().mods()[0].brief() == Some(~"desc"));
}
#[test]
fn should_not_promote_long_descs() {
let desc = Some(~"Warkworth Castle is a ruined medieval building
#[test]
fn should_promote_trait_method_desc() {
let doc = mk_doc(~"trait i { #[doc = \"desc\"] fn a(); }");
assert!(doc.cratemod().traits()[0].methods[0].brief ==
Some(~"desc"));
}
#[test]
fn should_promote_impl_method_desc() {
let doc = mk_doc(
~"impl int { #[doc = \"desc\"] fn a() { } }");
assert!(doc.cratemod().impls()[0].methods[0].brief == Some(~"desc"));
}
#[test]
fn test_paragraphs_1() {
let paras = paragraphs(~"1\n\n2");
assert!(paras == ~[~"1", ~"2"]);
}
#[test]
fn test_paragraphs_2() {
let paras = paragraphs(~"\n\n1\n1\n\n2\n\n");
assert!(paras == ~[~"1\n1", ~"2"]);
}
#[test]
fn should_promote_short_descs() {
let desc = Some(~"desc");
let brief = extract(copy desc);
assert!(brief == desc);
}
#[test]
fn should_not_promote_long_descs() {
let desc = Some(~"Warkworth Castle is a ruined medieval building
in the town of the same name in the English county of Northumberland,
and the town and castle occupy a loop of the River Coquet, less than a mile
from England's north-east coast. When the castle was founded is uncertain,
@ -248,13 +248,13 @@ but traditionally its construction has been ascribed to Prince Henry of
Scotland in the mid 12th century, although it may have been built by
King Henry II of England when he took control of England'snorthern
counties.");
let brief = extract(desc);
assert!(brief == None);
}
let brief = extract(desc);
assert!(brief == None);
}
#[test]
fn should_promote_first_sentence() {
let desc = Some(~"Warkworth Castle is a ruined medieval building
#[test]
fn should_promote_first_sentence() {
let desc = Some(~"Warkworth Castle is a ruined medieval building
in the town. of the same name in the English county of Northumberland,
and the town and castle occupy a loop of the River Coquet, less than a mile
from England's north-east coast. When the castle was founded is uncertain,
@ -262,14 +262,14 @@ but traditionally its construction has been ascribed to Prince Henry of
Scotland in the mid 12th century, although it may have been built by
King Henry II of England when he took control of England'snorthern
counties.");
let brief = extract(desc);
assert!(brief == Some(
~"Warkworth Castle is a ruined medieval building in the town"));
}
let brief = extract(desc);
assert!(brief == Some(
~"Warkworth Castle is a ruined medieval building in the town"));
}
#[test]
fn should_not_consider_double_period_to_end_sentence() {
let desc = Some(~"Warkworth..Castle is a ruined medieval building
#[test]
fn should_not_consider_double_period_to_end_sentence() {
let desc = Some(~"Warkworth..Castle is a ruined medieval building
in the town. of the same name in the English county of Northumberland,
and the town and castle occupy a loop of the River Coquet, less than a mile
from England's north-east coast. When the castle was founded is uncertain,
@ -277,14 +277,14 @@ but traditionally its construction has been ascribed to Prince Henry of
Scotland in the mid 12th century, although it may have been built by
King Henry II of England when he took control of England'snorthern
counties.");
let brief = extract(desc);
assert!(brief == Some(
~"Warkworth..Castle is a ruined medieval building in the town"));
}
let brief = extract(desc);
assert!(brief == Some(
~"Warkworth..Castle is a ruined medieval building in the town"));
}
#[test]
fn should_not_consider_triple_period_to_end_sentence() {
let desc = Some(~"Warkworth... Castle is a ruined medieval building
#[test]
fn should_not_consider_triple_period_to_end_sentence() {
let desc = Some(~"Warkworth... Castle is a ruined medieval building
in the town. of the same name in the English county of Northumberland,
and the town and castle occupy a loop of the River Coquet, less than a mile
from England's north-east coast. When the castle was founded is uncertain,
@ -292,7 +292,8 @@ but traditionally its construction has been ascribed to Prince Henry of
Scotland in the mid 12th century, although it may have been built by
King Henry II of England when he took control of England'snorthern
counties.");
let brief = extract(desc);
assert!(brief == Some(
~"Warkworth... Castle is a ruined medieval building in the town"));
let brief = extract(desc);
assert!(brief == Some(
~"Warkworth... Castle is a ruined medieval building in the town"));
}
}

View file

@ -182,13 +182,6 @@ fn constdoc_from_const(itemdoc: doc::ItemDoc) -> doc::ConstDoc {
}
}
#[test]
fn should_extract_const_name_and_id() {
let doc = test::mk_doc(~"static a: int = 0;");
assert!(doc.cratemod().consts()[0].id() != 0);
assert!(doc.cratemod().consts()[0].name() == ~"a");
}
fn enumdoc_from_enum(
itemdoc: doc::ItemDoc,
variants: ~[ast::variant]
@ -213,19 +206,6 @@ fn variantdoc_from_variant(variant: &ast::variant) -> doc::VariantDoc {
}
}
#[test]
fn should_extract_enums() {
let doc = test::mk_doc(~"enum e { v }");
assert!(doc.cratemod().enums()[0].id() != 0);
assert!(doc.cratemod().enums()[0].name() == ~"e");
}
#[test]
fn should_extract_enum_variants() {
let doc = test::mk_doc(~"enum e { v }");
assert!(doc.cratemod().enums()[0].variants[0].name == ~"v");
}
fn traitdoc_from_trait(
itemdoc: doc::ItemDoc,
methods: ~[ast::trait_method]
@ -259,18 +239,6 @@ fn traitdoc_from_trait(
}
}
#[test]
fn should_extract_traits() {
let doc = test::mk_doc(~"trait i { fn f(); }");
assert!(doc.cratemod().traits()[0].name() == ~"i");
}
#[test]
fn should_extract_trait_methods() {
let doc = test::mk_doc(~"trait i { fn f(); }");
assert!(doc.cratemod().traits()[0].methods[0].name == ~"f");
}
fn impldoc_from_impl(
itemdoc: doc::ItemDoc,
methods: ~[@ast::method]
@ -293,12 +261,6 @@ fn impldoc_from_impl(
}
}
#[test]
fn should_extract_impl_methods() {
let doc = test::mk_doc(~"impl int { fn f() { } }");
assert!(doc.cratemod().impls()[0].methods[0].name == ~"f");
}
fn tydoc_from_ty(
itemdoc: doc::ItemDoc
) -> doc::TyDoc {
@ -308,12 +270,6 @@ fn tydoc_from_ty(
}
}
#[test]
fn should_extract_tys() {
let doc = test::mk_doc(~"type a = int;");
assert!(doc.cratemod().types()[0].name() == ~"a");
}
fn structdoc_from_struct(
itemdoc: doc::ItemDoc,
struct_def: @ast::struct_def
@ -330,18 +286,6 @@ fn structdoc_from_struct(
}
}
#[test]
fn should_extract_structs() {
let doc = test::mk_doc(~"struct Foo { field: () }");
assert!(doc.cratemod().structs()[0].name() == ~"Foo");
}
#[test]
fn should_extract_struct_fields() {
let doc = test::mk_doc(~"struct Foo { field: () }");
assert!(doc.cratemod().structs()[0].fields[0] == ~"field");
}
#[cfg(test)]
mod test {
use astsrv;
@ -351,20 +295,20 @@ mod test {
use core::vec;
pub fn mk_doc(source: ~str) -> doc::Doc {
fn mk_doc(source: ~str) -> doc::Doc {
let ast = parse::from_str(source);
extract(ast, ~"")
}
#[test]
pub fn extract_empty_crate() {
fn extract_empty_crate() {
let doc = mk_doc(~"");
assert!(vec::is_empty(doc.cratemod().mods()));
assert!(vec::is_empty(doc.cratemod().fns()));
}
#[test]
pub fn extract_mods() {
fn extract_mods() {
let doc = mk_doc(~"mod a { mod b { } mod c { } }");
assert!(doc.cratemod().mods()[0].name() == ~"a");
assert!(doc.cratemod().mods()[0].mods()[0].name() == ~"b");
@ -372,26 +316,26 @@ mod test {
}
#[test]
pub fn extract_fns_from_foreign_mods() {
fn extract_fns_from_foreign_mods() {
let doc = mk_doc(~"extern { fn a(); }");
assert!(doc.cratemod().nmods()[0].fns[0].name() == ~"a");
}
#[test]
pub fn extract_mods_deep() {
fn extract_mods_deep() {
let doc = mk_doc(~"mod a { mod b { mod c { } } }");
assert!(doc.cratemod().mods()[0].mods()[0].mods()[0].name() ==
~"c");
}
#[test]
pub fn extract_should_set_mod_ast_id() {
fn extract_should_set_mod_ast_id() {
let doc = mk_doc(~"mod a { }");
assert!(doc.cratemod().mods()[0].id() != 0);
}
#[test]
pub fn extract_fns() {
fn extract_fns() {
let doc = mk_doc(
~"fn a() { } \
mod b { fn c() {
@ -401,13 +345,13 @@ mod test {
}
#[test]
pub fn extract_should_set_fn_ast_id() {
fn extract_should_set_fn_ast_id() {
let doc = mk_doc(~"fn a() { }");
assert!(doc.cratemod().fns()[0].id() != 0);
}
#[test]
pub fn extract_should_use_default_crate_name() {
fn extract_should_use_default_crate_name() {
let source = ~"";
let ast = parse::from_str(source);
let doc = extract(ast, ~"burp");
@ -415,11 +359,67 @@ mod test {
}
#[test]
pub fn extract_from_seq_srv() {
fn extract_from_seq_srv() {
let source = ~"";
do astsrv::from_str(source) |srv| {
let doc = from_srv(srv, ~"name");
assert!(doc.cratemod().name() == ~"name");
}
}
#[test]
fn should_extract_const_name_and_id() {
let doc = mk_doc(~"static a: int = 0;");
assert!(doc.cratemod().consts()[0].id() != 0);
assert!(doc.cratemod().consts()[0].name() == ~"a");
}
#[test]
fn should_extract_enums() {
let doc = mk_doc(~"enum e { v }");
assert!(doc.cratemod().enums()[0].id() != 0);
assert!(doc.cratemod().enums()[0].name() == ~"e");
}
#[test]
fn should_extract_enum_variants() {
let doc = mk_doc(~"enum e { v }");
assert!(doc.cratemod().enums()[0].variants[0].name == ~"v");
}
#[test]
fn should_extract_traits() {
let doc = mk_doc(~"trait i { fn f(); }");
assert!(doc.cratemod().traits()[0].name() == ~"i");
}
#[test]
fn should_extract_trait_methods() {
let doc = mk_doc(~"trait i { fn f(); }");
assert!(doc.cratemod().traits()[0].methods[0].name == ~"f");
}
#[test]
fn should_extract_impl_methods() {
let doc = mk_doc(~"impl int { fn f() { } }");
assert!(doc.cratemod().impls()[0].methods[0].name == ~"f");
}
#[test]
fn should_extract_tys() {
let doc = mk_doc(~"type a = int;");
assert!(doc.cratemod().types()[0].name() == ~"a");
}
#[test]
fn should_extract_structs() {
let doc = mk_doc(~"struct Foo { field: () }");
assert!(doc.cratemod().structs()[0].name() == ~"Foo");
}
#[test]
fn should_extract_struct_fields() {
let doc = mk_doc(~"struct Foo { field: () }");
assert!(doc.cratemod().structs()[0].fields[0] == ~"field");
}
}

View file

@ -115,7 +115,7 @@ fn item_to_entry(
}
}
fn pandoc_header_id(header: &str) -> ~str {
pub fn pandoc_header_id(header: &str) -> ~str {
// http://johnmacfarlane.net/pandoc/README.html#headers
@ -162,110 +162,6 @@ fn pandoc_header_id(header: &str) -> ~str {
fn maybe_use_section_id(s: &str) -> ~str { s.to_str() }
}
#[test]
fn should_remove_punctuation_from_headers() {
assert!(pandoc_header_id(~"impl foo of bar<A>") ==
~"impl-foo-of-bara");
assert!(pandoc_header_id(~"impl of num::num for int")
== ~"impl-of-numnum-for-int");
assert!(pandoc_header_id(~"impl of num::num for int/&")
== ~"impl-of-numnum-for-int");
assert!(pandoc_header_id(~"impl of num::num for ^int")
== ~"impl-of-numnum-for-int");
assert!(pandoc_header_id(~"impl for & condvar")
== ~"impl-for-condvar");
assert!(pandoc_header_id(~"impl of Select<T, U> for (Left, Right)")
== ~"impl-of-selectt-u-for-left-right");
assert!(pandoc_header_id(~"impl of Condition<'self, T, U>")
== ~"impl-of-conditionself-t-u");
assert!(pandoc_header_id(~"impl of Condition<T: Copy + Clone>")
== ~"impl-of-conditiont-copy-clone");
}
#[test]
fn should_trim_whitespace_after_removing_punctuation() {
assert!(pandoc_header_id("impl foo for ()") == ~"impl-foo-for");
}
#[test]
fn should_index_mod_contents() {
let doc = test::mk_doc(
config::DocPerCrate,
~"mod a { } fn b() { }"
);
assert!((&doc.cratemod().index).get().entries[0] == doc::IndexEntry {
kind: ~"Module",
name: ~"a",
brief: None,
link: ~"#module-a"
});
assert!((&doc.cratemod().index).get().entries[1] == doc::IndexEntry {
kind: ~"Function",
name: ~"b",
brief: None,
link: ~"#function-b"
});
}
#[test]
fn should_index_mod_contents_multi_page() {
let doc = test::mk_doc(
config::DocPerMod,
~"mod a { } fn b() { }"
);
assert!((&doc.cratemod().index).get().entries[0] == doc::IndexEntry {
kind: ~"Module",
name: ~"a",
brief: None,
link: ~"a.html"
});
assert!((&doc.cratemod().index).get().entries[1] == doc::IndexEntry {
kind: ~"Function",
name: ~"b",
brief: None,
link: ~"#function-b"
});
}
#[test]
fn should_index_foreign_mod_pages() {
let doc = test::mk_doc(
config::DocPerMod,
~"extern mod a { }"
);
assert!((&doc.cratemod().index).get().entries[0] == doc::IndexEntry {
kind: ~"Foreign module",
name: ~"a",
brief: None,
link: ~"a.html"
});
}
#[test]
fn should_add_brief_desc_to_index() {
let doc = test::mk_doc(
config::DocPerMod,
~"#[doc = \"test\"] mod a { }"
);
assert!((&doc.cratemod().index).get().entries[0].brief
== Some(~"test"));
}
#[test]
fn should_index_foreign_mod_contents() {
let doc = test::mk_doc(
config::DocPerCrate,
~"extern mod a { fn b(); }"
);
assert!((&doc.cratemod().nmods()[0].index).get().entries[0]
== doc::IndexEntry {
kind: ~"Function",
name: ~"b",
brief: None,
link: ~"#function-b"
});
}
#[cfg(test)]
mod test {
use astsrv;
@ -276,10 +172,10 @@ mod test {
use extract;
use markdown_index_pass::run;
use path_pass;
use super::pandoc_header_id;
use core::prelude::*;
use core::path::Path;
pub fn mk_doc(output_style: config::OutputStyle, source: ~str)
fn mk_doc(output_style: config::OutputStyle, source: ~str)
-> doc::Doc {
do astsrv::from_str(source) |srv| {
let config = config::Config {
@ -293,4 +189,108 @@ mod test {
run(srv.clone(), doc, config)
}
}
#[test]
fn should_remove_punctuation_from_headers() {
assert!(pandoc_header_id(~"impl foo of bar<A>") ==
~"impl-foo-of-bara");
assert!(pandoc_header_id(~"impl of num::num for int")
== ~"impl-of-numnum-for-int");
assert!(pandoc_header_id(~"impl of num::num for int/&")
== ~"impl-of-numnum-for-int");
assert!(pandoc_header_id(~"impl of num::num for ^int")
== ~"impl-of-numnum-for-int");
assert!(pandoc_header_id(~"impl for & condvar")
== ~"impl-for-condvar");
assert!(pandoc_header_id(~"impl of Select<T, U> for (Left, Right)")
== ~"impl-of-selectt-u-for-left-right");
assert!(pandoc_header_id(~"impl of Condition<'self, T, U>")
== ~"impl-of-conditionself-t-u");
assert!(pandoc_header_id(~"impl of Condition<T: Copy + Clone>")
== ~"impl-of-conditiont-copy-clone");
}
#[test]
fn should_trim_whitespace_after_removing_punctuation() {
assert!(pandoc_header_id("impl foo for ()") == ~"impl-foo-for");
}
#[test]
fn should_index_mod_contents() {
let doc = mk_doc(
config::DocPerCrate,
~"mod a { } fn b() { }"
);
assert!((&doc.cratemod().index).get().entries[0] == doc::IndexEntry {
kind: ~"Module",
name: ~"a",
brief: None,
link: ~"#module-a"
});
assert!((&doc.cratemod().index).get().entries[1] == doc::IndexEntry {
kind: ~"Function",
name: ~"b",
brief: None,
link: ~"#function-b"
});
}
#[test]
fn should_index_mod_contents_multi_page() {
let doc = mk_doc(
config::DocPerMod,
~"mod a { } fn b() { }"
);
assert!((&doc.cratemod().index).get().entries[0] == doc::IndexEntry {
kind: ~"Module",
name: ~"a",
brief: None,
link: ~"a.html"
});
assert!((&doc.cratemod().index).get().entries[1] == doc::IndexEntry {
kind: ~"Function",
name: ~"b",
brief: None,
link: ~"#function-b"
});
}
#[test]
fn should_index_foreign_mod_pages() {
let doc = mk_doc(
config::DocPerMod,
~"extern mod a { }"
);
assert!((&doc.cratemod().index).get().entries[0] == doc::IndexEntry {
kind: ~"Foreign module",
name: ~"a",
brief: None,
link: ~"a.html"
});
}
#[test]
fn should_add_brief_desc_to_index() {
let doc = mk_doc(
config::DocPerMod,
~"#[doc = \"test\"] mod a { }"
);
assert!((&doc.cratemod().index).get().entries[0].brief
== Some(~"test"));
}
#[test]
fn should_index_foreign_mod_contents() {
let doc = mk_doc(
config::DocPerCrate,
~"extern mod a { fn b(); }"
);
assert!((&doc.cratemod().nmods()[0].index).get().entries[0]
== doc::IndexEntry {
kind: ~"Function",
name: ~"b",
brief: None,
link: ~"#function-b"
});
}
}

File diff suppressed because it is too large Load diff

View file

@ -182,7 +182,7 @@ fn generic_writer(process: ~fn(markdown: ~str)) -> Writer {
result
}
fn make_local_filename(
pub fn make_local_filename(
config: config::Config,
page: doc::Page
) -> Path {
@ -218,65 +218,6 @@ pub fn make_filename(
Path(filename).with_filetype(ext)
}
#[test]
fn should_use_markdown_file_name_based_off_crate() {
let config = config::Config {
output_dir: Path("output/dir"),
output_format: config::Markdown,
output_style: config::DocPerCrate,
.. config::default_config(&Path("input/test.rc"))
};
let doc = test::mk_doc(~"test", ~"");
let page = doc::CratePage(doc.CrateDoc());
let filename = make_local_filename(config, page);
assert!(filename.to_str() == ~"output/dir/test.md");
}
#[test]
fn should_name_html_crate_file_name_index_html_when_doc_per_mod() {
let config = config::Config {
output_dir: Path("output/dir"),
output_format: config::PandocHtml,
output_style: config::DocPerMod,
.. config::default_config(&Path("input/test.rc"))
};
let doc = test::mk_doc(~"", ~"");
let page = doc::CratePage(doc.CrateDoc());
let filename = make_local_filename(config, page);
assert!(filename.to_str() == ~"output/dir/index.html");
}
#[test]
fn should_name_mod_file_names_by_path() {
let config = config::Config {
output_dir: Path("output/dir"),
output_format: config::PandocHtml,
output_style: config::DocPerMod,
.. config::default_config(&Path("input/test.rc"))
};
let doc = test::mk_doc(~"", ~"mod a { mod b { } }");
let modb = copy doc.cratemod().mods()[0].mods()[0];
let page = doc::ItemPage(doc::ModTag(modb));
let filename = make_local_filename(config, page);
assert!(filename == Path("output/dir/a_b.html"));
}
#[cfg(test)]
mod test {
use astsrv;
use doc;
use extract;
use path_pass;
pub fn mk_doc(name: ~str, source: ~str) -> doc::Doc {
do astsrv::from_str(source) |srv| {
let doc = extract::from_srv(srv.clone(), copy name);
let doc = (path_pass::mk_pass().f)(srv.clone(), doc);
doc
}
}
}
fn write_file(path: &Path, s: ~str) {
use core::io::WriterUtil;
@ -322,3 +263,65 @@ fn future_writer() -> (Writer, future::Future<~str>) {
};
(writer, future)
}
#[cfg(test)]
mod test {
use astsrv;
use doc;
use extract;
use path_pass;
use config;
use super::make_local_filename;
use core::prelude::*;
fn mk_doc(name: ~str, source: ~str) -> doc::Doc {
do astsrv::from_str(source) |srv| {
let doc = extract::from_srv(srv.clone(), copy name);
let doc = (path_pass::mk_pass().f)(srv.clone(), doc);
doc
}
}
#[test]
fn should_use_markdown_file_name_based_off_crate() {
let config = config::Config {
output_dir: Path("output/dir"),
output_format: config::Markdown,
output_style: config::DocPerCrate,
.. config::default_config(&Path("input/test.rc"))
};
let doc = mk_doc(~"test", ~"");
let page = doc::CratePage(doc.CrateDoc());
let filename = make_local_filename(config, page);
assert!(filename.to_str() == ~"output/dir/test.md");
}
#[test]
fn should_name_html_crate_file_name_index_html_when_doc_per_mod() {
let config = config::Config {
output_dir: Path("output/dir"),
output_format: config::PandocHtml,
output_style: config::DocPerMod,
.. config::default_config(&Path("input/test.rc"))
};
let doc = mk_doc(~"", ~"");
let page = doc::CratePage(doc.CrateDoc());
let filename = make_local_filename(config, page);
assert!(filename.to_str() == ~"output/dir/index.html");
}
#[test]
fn should_name_mod_file_names_by_path() {
let config = config::Config {
output_dir: Path("output/dir"),
output_format: config::PandocHtml,
output_style: config::DocPerMod,
.. config::default_config(&Path("input/test.rc"))
};
let doc = mk_doc(~"", ~"mod a { mod b { } }");
let modb = copy doc.cratemod().mods()[0].mods()[0];
let page = doc::ItemPage(doc::ModTag(modb));
let filename = make_local_filename(config, page);
assert!(filename == Path("output/dir/a_b.html"));
}
}

View file

@ -149,27 +149,6 @@ fn fold_nmod(
return doc;
}
#[test]
fn should_not_split_the_doc_into_pages_for_doc_per_crate() {
let doc = test::mk_doc_(
config::DocPerCrate,
~"mod a { } mod b { mod c { } }"
);
assert!(doc.pages.len() == 1u);
}
#[test]
fn should_make_a_page_for_every_mod() {
let doc = test::mk_doc(~"mod a { }");
assert!(doc.pages.mods()[0].name() == ~"a");
}
#[test]
fn should_remove_mods_from_containing_mods() {
let doc = test::mk_doc(~"mod a { }");
assert!(vec::is_empty(doc.cratemod().mods()));
}
#[cfg(test)]
mod test {
use astsrv;
@ -177,8 +156,9 @@ mod test {
use doc;
use extract;
use page_pass::run;
use core::vec;
pub fn mk_doc_(
fn mk_doc_(
output_style: config::OutputStyle,
source: ~str
) -> doc::Doc {
@ -188,7 +168,28 @@ mod test {
}
}
pub fn mk_doc(source: ~str) -> doc::Doc {
fn mk_doc(source: ~str) -> doc::Doc {
mk_doc_(config::DocPerMod, copy source)
}
#[test]
fn should_not_split_the_doc_into_pages_for_doc_per_crate() {
let doc = mk_doc_(
config::DocPerCrate,
~"mod a { } mod b { mod c { } }"
);
assert!(doc.pages.len() == 1u);
}
#[test]
fn should_make_a_page_for_every_mod() {
let doc = mk_doc(~"mod a { }");
assert!(doc.pages.mods()[0].name() == ~"a");
}
#[test]
fn should_remove_mods_from_containing_mods() {
let doc = mk_doc(~"mod a { }");
assert!(vec::is_empty(doc.cratemod().mods()));
}
}

View file

@ -61,25 +61,25 @@ fn is_hidden(srv: astsrv::Srv, doc: doc::ItemDoc) -> bool {
}
}
#[test]
fn should_prune_hidden_items() {
use core::vec;
let doc = test::mk_doc(~"#[doc(hidden)] mod a { }");
assert!(vec::is_empty(doc.cratemod().mods()))
}
#[cfg(test)]
pub mod test {
mod test {
use astsrv;
use doc;
use extract;
use prune_hidden_pass::run;
pub fn mk_doc(source: ~str) -> doc::Doc {
fn mk_doc(source: ~str) -> doc::Doc {
do astsrv::from_str(copy source) |srv| {
let doc = extract::from_srv(srv.clone(), ~"");
run(srv.clone(), doc)
}
}
#[test]
fn should_prune_hidden_items() {
use core::vec;
let doc = mk_doc(~"#[doc(hidden)] mod a { }");
assert!(vec::is_empty(doc.cratemod().mods()))
}
}

View file

@ -154,106 +154,106 @@ fn is_visible(srv: astsrv::Srv, doc: doc::ItemDoc) -> bool {
}
}
#[test]
fn should_prune_items_without_pub_modifier() {
let doc = test::mk_doc(~"mod a { }");
assert!(vec::is_empty(doc.cratemod().mods()));
}
#[test]
fn should_not_prune_trait_impls() {
// Impls are more complicated
let doc = test::mk_doc(
~" \
trait Foo { } \
impl Foo for int { } \
");
assert!(!doc.cratemod().impls().is_empty());
}
#[test]
fn should_prune_associated_methods_without_vis_modifier_on_impls_without_vis_modifier() {
let doc = test::mk_doc(
~"impl Foo {\
pub fn bar() { }\
fn baz() { }\
}");
assert!(doc.cratemod().impls()[0].methods.len() == 1);
}
#[test]
fn should_prune_priv_associated_methods_on_impls_without_vis_modifier() {
let doc = test::mk_doc(
~"impl Foo {\
pub fn bar() { }\
priv fn baz() { }\
}");
assert!(doc.cratemod().impls()[0].methods.len() == 1);
}
#[test]
fn should_prune_priv_associated_methods_on_pub_impls() {
let doc = test::mk_doc(
~"pub impl Foo {\
fn bar() { }\
priv fn baz() { }\
}");
assert!(doc.cratemod().impls()[0].methods.len() == 1);
}
#[test]
fn should_prune_associated_methods_without_vis_modifier_on_priv_impls() {
let doc = test::mk_doc(
~"priv impl Foo {\
pub fn bar() { }\
fn baz() { }\
}");
assert!(doc.cratemod().impls()[0].methods.len() == 1);
}
#[test]
fn should_prune_priv_associated_methods_on_priv_impls() {
let doc = test::mk_doc(
~"priv impl Foo {\
pub fn bar() { }\
priv fn baz() { }\
}");
assert!(doc.cratemod().impls()[0].methods.len() == 1);
}
#[test]
fn should_prune_associated_impls_with_no_pub_methods() {
let doc = test::mk_doc(
~"priv impl Foo {\
fn baz() { }\
}");
assert!(doc.cratemod().impls().is_empty());
}
#[test]
fn should_not_prune_associated_impls_with_pub_methods() {
let doc = test::mk_doc(
~" \
impl Foo { pub fn bar() { } } \
");
assert!(!doc.cratemod().impls().is_empty());
}
#[cfg(test)]
pub mod test {
mod test {
use astsrv;
use doc;
use extract;
use tystr_pass;
use prune_private_pass::run;
use core::vec;
pub fn mk_doc(source: ~str) -> doc::Doc {
fn mk_doc(source: ~str) -> doc::Doc {
do astsrv::from_str(copy source) |srv| {
let doc = extract::from_srv(srv.clone(), ~"");
let doc = tystr_pass::run(srv.clone(), doc);
run(srv.clone(), doc)
}
}
}
#[test]
fn should_prune_items_without_pub_modifier() {
let doc = mk_doc(~"mod a { }");
assert!(vec::is_empty(doc.cratemod().mods()));
}
#[test]
fn should_not_prune_trait_impls() {
// Impls are more complicated
let doc = mk_doc(
~" \
trait Foo { } \
impl Foo for int { } \
");
assert!(!doc.cratemod().impls().is_empty());
}
#[test]
fn should_prune_associated_methods_without_vis_modifier_on_impls_without_vis_modifier() {
let doc = mk_doc(
~"impl Foo {\
pub fn bar() { }\
fn baz() { }\
}");
assert!(doc.cratemod().impls()[0].methods.len() == 1);
}
#[test]
fn should_prune_priv_associated_methods_on_impls_without_vis_modifier() {
let doc = mk_doc(
~"impl Foo {\
pub fn bar() { }\
priv fn baz() { }\
}");
assert!(doc.cratemod().impls()[0].methods.len() == 1);
}
#[test]
fn should_prune_priv_associated_methods_on_pub_impls() {
let doc = mk_doc(
~"pub impl Foo {\
fn bar() { }\
priv fn baz() { }\
}");
assert!(doc.cratemod().impls()[0].methods.len() == 1);
}
#[test]
fn should_prune_associated_methods_without_vis_modifier_on_priv_impls() {
let doc = mk_doc(
~"priv impl Foo {\
pub fn bar() { }\
fn baz() { }\
}");
assert!(doc.cratemod().impls()[0].methods.len() == 1);
}
#[test]
fn should_prune_priv_associated_methods_on_priv_impls() {
let doc = mk_doc(
~"priv impl Foo {\
pub fn bar() { }\
priv fn baz() { }\
}");
assert!(doc.cratemod().impls()[0].methods.len() == 1);
}
#[test]
fn should_prune_associated_impls_with_no_pub_methods() {
let doc = mk_doc(
~"priv impl Foo {\
fn baz() { }\
}");
assert!(doc.cratemod().impls().is_empty());
}
#[test]
fn should_not_prune_associated_impls_with_pub_methods() {
let doc = mk_doc(
~" \
impl Foo { pub fn bar() { } } \
");
assert!(!doc.cratemod().impls().is_empty());
}
}

View file

@ -160,106 +160,109 @@ fn parse_header(line: ~str) -> Option<~str> {
}
}
#[test]
fn should_create_section_headers() {
let doc = test::mk_doc(
~"#[doc = \"\
# Header\n\
Body\"]\
mod a {
}");
assert!(str::contains(
doc.cratemod().mods()[0].item.sections[0].header,
~"Header"));
}
#[test]
fn should_create_section_bodies() {
let doc = test::mk_doc(
~"#[doc = \"\
# Header\n\
Body\"]\
mod a {
}");
assert!(str::contains(
doc.cratemod().mods()[0].item.sections[0].body,
~"Body"));
}
#[test]
fn should_not_create_sections_from_indented_headers() {
let doc = test::mk_doc(
~"#[doc = \"\n\
Text\n # Header\n\
Body\"]\
mod a {
}");
assert!(vec::is_empty(doc.cratemod().mods()[0].item.sections));
}
#[test]
fn should_remove_section_text_from_main_desc() {
let doc = test::mk_doc(
~"#[doc = \"\
Description\n\n\
# Header\n\
Body\"]\
mod a {
}");
assert!(!str::contains(
doc.cratemod().mods()[0].desc().get(),
~"Header"));
assert!(!str::contains(
doc.cratemod().mods()[0].desc().get(),
~"Body"));
}
#[test]
fn should_eliminate_desc_if_it_is_just_whitespace() {
let doc = test::mk_doc(
~"#[doc = \"\
# Header\n\
Body\"]\
mod a {
}");
assert!(doc.cratemod().mods()[0].desc() == None);
}
#[test]
fn should_sectionalize_trait_methods() {
let doc = test::mk_doc(
~"trait i {
#[doc = \"\
# Header\n\
Body\"]\
fn a(); }");
assert!(doc.cratemod().traits()[0].methods[0].sections.len() == 1u);
}
#[test]
fn should_sectionalize_impl_methods() {
let doc = test::mk_doc(
~"impl bool {
#[doc = \"\
# Header\n\
Body\"]\
fn a() { } }");
assert!(doc.cratemod().impls()[0].methods[0].sections.len() == 1u);
}
#[cfg(test)]
pub mod test {
mod test {
use astsrv;
use attr_pass;
use doc;
use extract;
use sectionalize_pass::run;
use core::prelude::*;
pub fn mk_doc(source: ~str) -> doc::Doc {
fn mk_doc(source: ~str) -> doc::Doc {
do astsrv::from_str(copy source) |srv| {
let doc = extract::from_srv(srv.clone(), ~"");
let doc = (attr_pass::mk_pass().f)(srv.clone(), doc);
run(srv.clone(), doc)
}
}
#[test]
fn should_create_section_headers() {
let doc = mk_doc(
~"#[doc = \"\
# Header\n\
Body\"]\
mod a {
}");
assert!(str::contains(
doc.cratemod().mods()[0].item.sections[0].header,
~"Header"));
}
#[test]
fn should_create_section_bodies() {
let doc = mk_doc(
~"#[doc = \"\
# Header\n\
Body\"]\
mod a {
}");
assert!(str::contains(
doc.cratemod().mods()[0].item.sections[0].body,
~"Body"));
}
#[test]
fn should_not_create_sections_from_indented_headers() {
let doc = mk_doc(
~"#[doc = \"\n\
Text\n # Header\n\
Body\"]\
mod a {
}");
assert!(vec::is_empty(doc.cratemod().mods()[0].item.sections));
}
#[test]
fn should_remove_section_text_from_main_desc() {
let doc = mk_doc(
~"#[doc = \"\
Description\n\n\
# Header\n\
Body\"]\
mod a {
}");
assert!(!str::contains(
doc.cratemod().mods()[0].desc().get(),
~"Header"));
assert!(!str::contains(
doc.cratemod().mods()[0].desc().get(),
~"Body"));
}
#[test]
fn should_eliminate_desc_if_it_is_just_whitespace() {
let doc = mk_doc(
~"#[doc = \"\
# Header\n\
Body\"]\
mod a {
}");
assert!(doc.cratemod().mods()[0].desc() == None);
}
#[test]
fn should_sectionalize_trait_methods() {
let doc = mk_doc(
~"trait i {
#[doc = \"\
# Header\n\
Body\"]\
fn a(); }");
assert!(doc.cratemod().traits()[0].methods[0].sections.len() == 1u);
}
#[test]
fn should_sectionalize_impl_methods() {
let doc = mk_doc(
~"impl bool {
#[doc = \"\
# Header\n\
Body\"]\
fn a() { } }");
assert!(doc.cratemod().impls()[0].methods[0].sections.len() == 1u);
}
}

View file

@ -137,162 +137,6 @@ fn fold_impl(
}
}
#[test]
fn should_execute_op_on_enum_brief() {
let doc = test::mk_doc(~"#[doc = \" a \"] enum a { b }");
assert!(doc.cratemod().enums()[0].brief() == Some(~"a"));
}
#[test]
fn should_execute_op_on_enum_desc() {
let doc = test::mk_doc(~"#[doc = \" a \"] enum a { b }");
assert!(doc.cratemod().enums()[0].desc() == Some(~"a"));
}
#[test]
fn should_execute_op_on_variant_desc() {
let doc = test::mk_doc(~"enum a { #[doc = \" a \"] b }");
assert!(doc.cratemod().enums()[0].variants[0].desc == Some(~"a"));
}
#[test]
fn should_execute_op_on_trait_brief() {
let doc = test::mk_doc(
~"#[doc = \" a \"] trait i { fn a(); }");
assert!(doc.cratemod().traits()[0].brief() == Some(~"a"));
}
#[test]
fn should_execute_op_on_trait_desc() {
let doc = test::mk_doc(
~"#[doc = \" a \"] trait i { fn a(); }");
assert!(doc.cratemod().traits()[0].desc() == Some(~"a"));
}
#[test]
fn should_execute_op_on_trait_method_brief() {
let doc = test::mk_doc(
~"trait i { #[doc = \" a \"] fn a(); }");
assert!(doc.cratemod().traits()[0].methods[0].brief == Some(~"a"));
}
#[test]
fn should_execute_op_on_trait_method_desc() {
let doc = test::mk_doc(
~"trait i { #[doc = \" a \"] fn a(); }");
assert!(doc.cratemod().traits()[0].methods[0].desc == Some(~"a"));
}
#[test]
fn should_execute_op_on_impl_brief() {
let doc = test::mk_doc(
~"#[doc = \" a \"] impl int { fn a() { } }");
assert!(doc.cratemod().impls()[0].brief() == Some(~"a"));
}
#[test]
fn should_execute_op_on_impl_desc() {
let doc = test::mk_doc(
~"#[doc = \" a \"] impl int { fn a() { } }");
assert!(doc.cratemod().impls()[0].desc() == Some(~"a"));
}
#[test]
fn should_execute_op_on_impl_method_brief() {
let doc = test::mk_doc(
~"impl int { #[doc = \" a \"] fn a() { } }");
assert!(doc.cratemod().impls()[0].methods[0].brief == Some(~"a"));
}
#[test]
fn should_execute_op_on_impl_method_desc() {
let doc = test::mk_doc(
~"impl int { #[doc = \" a \"] fn a() { } }");
assert!(doc.cratemod().impls()[0].methods[0].desc == Some(~"a"));
}
#[test]
fn should_execute_op_on_type_brief() {
let doc = test::mk_doc(
~"#[doc = \" a \"] type t = int;");
assert!(doc.cratemod().types()[0].brief() == Some(~"a"));
}
#[test]
fn should_execute_op_on_type_desc() {
let doc = test::mk_doc(
~"#[doc = \" a \"] type t = int;");
assert!(doc.cratemod().types()[0].desc() == Some(~"a"));
}
#[test]
fn should_execute_on_item_section_headers() {
let doc = test::mk_doc(
~"#[doc = \"\
# Header \n\
Body\"]\
fn a() { }");
assert!(doc.cratemod().fns()[0].sections()[0].header == ~"Header");
}
#[test]
fn should_execute_on_item_section_bodies() {
let doc = test::mk_doc(
~"#[doc = \"\
# Header\n\
Body \"]\
fn a() { }");
assert!(doc.cratemod().fns()[0].sections()[0].body == ~"Body");
}
#[test]
fn should_execute_on_trait_method_section_headers() {
let doc = test::mk_doc(
~"trait i {
#[doc = \"\
# Header \n\
Body\"]\
fn a(); }");
assert!(doc.cratemod().traits()[0].methods[0].sections[0].header
== ~"Header");
}
#[test]
fn should_execute_on_trait_method_section_bodies() {
let doc = test::mk_doc(
~"trait i {
#[doc = \"\
# Header\n\
Body \"]\
fn a(); }");
assert!(doc.cratemod().traits()[0].methods[0].sections[0].body ==
~"Body");
}
#[test]
fn should_execute_on_impl_method_section_headers() {
let doc = test::mk_doc(
~"impl bool {
#[doc = \"\
# Header \n\
Body\"]\
fn a() { } }");
assert!(doc.cratemod().impls()[0].methods[0].sections[0].header
== ~"Header");
}
#[test]
fn should_execute_on_impl_method_section_bodies() {
let doc = test::mk_doc(
~"impl bool {
#[doc = \"\
# Header\n\
Body \"]\
fn a() { } }");
assert!(doc.cratemod().impls()[0].methods[0].sections[0].body ==
~"Body");
}
#[cfg(test)]
mod test {
use astsrv;
@ -303,9 +147,9 @@ mod test {
use sectionalize_pass;
use text_pass::mk_pass;
use core::str;
use core::prelude::*;
pub fn mk_doc(source: ~str) -> doc::Doc {
fn mk_doc(source: ~str) -> doc::Doc {
do astsrv::from_str(copy source) |srv| {
let doc = extract::from_srv(srv.clone(), ~"");
let doc = (attr_pass::mk_pass().f)(srv.clone(), doc);
@ -314,4 +158,160 @@ mod test {
(mk_pass(~"", |s| str::trim(s).to_owned() ).f)(srv.clone(), doc)
}
}
#[test]
fn should_execute_op_on_enum_brief() {
let doc = mk_doc(~"#[doc = \" a \"] enum a { b }");
assert!(doc.cratemod().enums()[0].brief() == Some(~"a"));
}
#[test]
fn should_execute_op_on_enum_desc() {
let doc = mk_doc(~"#[doc = \" a \"] enum a { b }");
assert!(doc.cratemod().enums()[0].desc() == Some(~"a"));
}
#[test]
fn should_execute_op_on_variant_desc() {
let doc = mk_doc(~"enum a { #[doc = \" a \"] b }");
assert!(doc.cratemod().enums()[0].variants[0].desc == Some(~"a"));
}
#[test]
fn should_execute_op_on_trait_brief() {
let doc = mk_doc(
~"#[doc = \" a \"] trait i { fn a(); }");
assert!(doc.cratemod().traits()[0].brief() == Some(~"a"));
}
#[test]
fn should_execute_op_on_trait_desc() {
let doc = mk_doc(
~"#[doc = \" a \"] trait i { fn a(); }");
assert!(doc.cratemod().traits()[0].desc() == Some(~"a"));
}
#[test]
fn should_execute_op_on_trait_method_brief() {
let doc = mk_doc(
~"trait i { #[doc = \" a \"] fn a(); }");
assert!(doc.cratemod().traits()[0].methods[0].brief == Some(~"a"));
}
#[test]
fn should_execute_op_on_trait_method_desc() {
let doc = mk_doc(
~"trait i { #[doc = \" a \"] fn a(); }");
assert!(doc.cratemod().traits()[0].methods[0].desc == Some(~"a"));
}
#[test]
fn should_execute_op_on_impl_brief() {
let doc = mk_doc(
~"#[doc = \" a \"] impl int { fn a() { } }");
assert!(doc.cratemod().impls()[0].brief() == Some(~"a"));
}
#[test]
fn should_execute_op_on_impl_desc() {
let doc = mk_doc(
~"#[doc = \" a \"] impl int { fn a() { } }");
assert!(doc.cratemod().impls()[0].desc() == Some(~"a"));
}
#[test]
fn should_execute_op_on_impl_method_brief() {
let doc = mk_doc(
~"impl int { #[doc = \" a \"] fn a() { } }");
assert!(doc.cratemod().impls()[0].methods[0].brief == Some(~"a"));
}
#[test]
fn should_execute_op_on_impl_method_desc() {
let doc = mk_doc(
~"impl int { #[doc = \" a \"] fn a() { } }");
assert!(doc.cratemod().impls()[0].methods[0].desc == Some(~"a"));
}
#[test]
fn should_execute_op_on_type_brief() {
let doc = mk_doc(
~"#[doc = \" a \"] type t = int;");
assert!(doc.cratemod().types()[0].brief() == Some(~"a"));
}
#[test]
fn should_execute_op_on_type_desc() {
let doc = mk_doc(
~"#[doc = \" a \"] type t = int;");
assert!(doc.cratemod().types()[0].desc() == Some(~"a"));
}
#[test]
fn should_execute_on_item_section_headers() {
let doc = mk_doc(
~"#[doc = \"\
# Header \n\
Body\"]\
fn a() { }");
assert!(doc.cratemod().fns()[0].sections()[0].header == ~"Header");
}
#[test]
fn should_execute_on_item_section_bodies() {
let doc = mk_doc(
~"#[doc = \"\
# Header\n\
Body \"]\
fn a() { }");
assert!(doc.cratemod().fns()[0].sections()[0].body == ~"Body");
}
#[test]
fn should_execute_on_trait_method_section_headers() {
let doc = mk_doc(
~"trait i {
#[doc = \"\
# Header \n\
Body\"]\
fn a(); }");
assert!(doc.cratemod().traits()[0].methods[0].sections[0].header
== ~"Header");
}
#[test]
fn should_execute_on_trait_method_section_bodies() {
let doc = mk_doc(
~"trait i {
#[doc = \"\
# Header\n\
Body \"]\
fn a(); }");
assert!(doc.cratemod().traits()[0].methods[0].sections[0].body ==
~"Body");
}
#[test]
fn should_execute_on_impl_method_section_headers() {
let doc = mk_doc(
~"impl bool {
#[doc = \"\
# Header \n\
Body\"]\
fn a() { } }");
assert!(doc.cratemod().impls()[0].methods[0].sections[0].header
== ~"Header");
}
#[test]
fn should_execute_on_impl_method_section_bodies() {
let doc = mk_doc(
~"impl bool {
#[doc = \"\
# Header\n\
Body \"]\
fn a() { } }");
assert!(doc.cratemod().impls()[0].methods[0].sections[0].body ==
~"Body");
}
}

View file

@ -22,16 +22,6 @@ pub fn mk_pass() -> Pass {
text_pass::mk_pass(~"trim", |s| s.trim().to_owned() )
}
#[test]
fn should_trim_text() {
use core::option::Some;
let doc = test::mk_doc(~"#[doc = \" desc \"] \
mod m {
}");
assert!(doc.cratemod().mods()[0].desc() == Some(~"desc"));
}
#[cfg(test)]
mod test {
use astsrv;
@ -40,11 +30,21 @@ mod test {
use extract;
use trim_pass::mk_pass;
pub fn mk_doc(source: ~str) -> doc::Doc {
fn mk_doc(source: ~str) -> doc::Doc {
do astsrv::from_str(copy source) |srv| {
let doc = extract::from_srv(srv.clone(), ~"");
let doc = (attr_pass::mk_pass().f)(srv.clone(), doc);
(mk_pass().f)(srv.clone(), doc)
}
}
#[test]
fn should_trim_text() {
use core::option::Some;
let doc = mk_doc(~"#[doc = \" desc \"] \
mod m {
}");
assert!(doc.cratemod().mods()[0].desc() == Some(~"desc"));
}
}

View file

@ -67,35 +67,22 @@ fn fold_fn(
fn get_fn_sig(srv: astsrv::Srv, fn_id: doc::AstId) -> Option<~str> {
do astsrv::exec(srv) |ctxt| {
match *ctxt.ast_map.get(&fn_id) {
ast_map::node_item(@ast::item {
ident: ident,
node: ast::item_fn(ref decl, purity, _, ref tys, _), _
}, _) |
ast_map::node_foreign_item(@ast::foreign_item {
ident: ident,
node: ast::foreign_item_fn(ref decl, purity, ref tys), _
}, _, _, _) => {
Some(pprust::fun_to_str(decl, purity, ident, None, tys,
extract::interner()))
}
_ => fail!(~"get_fn_sig: fn_id not bound to a fn item")
ast_map::node_item(@ast::item {
ident: ident,
node: ast::item_fn(ref decl, purity, _, ref tys, _), _
}, _) |
ast_map::node_foreign_item(@ast::foreign_item {
ident: ident,
node: ast::foreign_item_fn(ref decl, purity, ref tys), _
}, _, _, _) => {
Some(pprust::fun_to_str(decl, purity, ident, None, tys,
extract::interner()))
}
_ => fail!(~"get_fn_sig: fn_id not bound to a fn item")
}
}
}
#[test]
fn should_add_fn_sig() {
let doc = test::mk_doc(~"fn a<T>() -> int { }");
assert!(doc.cratemod().fns()[0].sig == Some(~"fn a<T>() -> int"));
}
#[test]
fn should_add_foreign_fn_sig() {
let doc = test::mk_doc(~"extern mod a { fn a<T>() -> int; }");
assert!(doc.cratemod().nmods()[0].fns[0].sig ==
Some(~"fn a<T>() -> int"));
}
fn fold_const(
fold: &fold::Fold<astsrv::Srv>,
doc: doc::ConstDoc
@ -119,12 +106,6 @@ fn fold_const(
}
}
#[test]
fn should_add_const_types() {
let doc = test::mk_doc(~"static a: bool = true;");
assert!(doc.cratemod().consts()[0].sig == Some(~"bool"));
}
fn fold_enum(
fold: &fold::Fold<astsrv::Srv>,
doc: doc::EnumDoc
@ -163,13 +144,6 @@ fn fold_enum(
}
}
#[test]
fn should_add_variant_sigs() {
let doc = test::mk_doc(~"enum a { b(int) }");
assert!(doc.cratemod().enums()[0].variants[0].sig ==
Some(~"b(int)"));
}
fn fold_trait(
fold: &fold::Fold<astsrv::Srv>,
doc: doc::TraitDoc
@ -200,73 +174,66 @@ fn get_method_sig(
) -> Option<~str> {
do astsrv::exec(srv) |ctxt| {
match *ctxt.ast_map.get(&item_id) {
ast_map::node_item(@ast::item {
node: ast::item_trait(_, _, ref methods), _
}, _) => {
match vec::find(*methods, |method| {
match copy *method {
ast::required(ty_m) => to_str(ty_m.ident) == method_name,
ast::provided(m) => to_str(m.ident) == method_name,
}
}) {
Some(method) => {
match method {
ast::required(ty_m) => {
Some(pprust::fun_to_str(
&ty_m.decl,
ty_m.purity,
ty_m.ident,
Some(ty_m.self_ty.node),
&ty_m.generics,
extract::interner()
))
ast_map::node_item(@ast::item {
node: ast::item_trait(_, _, ref methods), _
}, _) => {
match vec::find(*methods, |method| {
match copy *method {
ast::required(ty_m) => to_str(ty_m.ident) == method_name,
ast::provided(m) => to_str(m.ident) == method_name,
}
ast::provided(m) => {
Some(pprust::fun_to_str(
&m.decl,
m.purity,
m.ident,
Some(m.self_ty.node),
&m.generics,
extract::interner()
))
}) {
Some(method) => {
match method {
ast::required(ty_m) => {
Some(pprust::fun_to_str(
&ty_m.decl,
ty_m.purity,
ty_m.ident,
Some(ty_m.self_ty.node),
&ty_m.generics,
extract::interner()
))
}
ast::provided(m) => {
Some(pprust::fun_to_str(
&m.decl,
m.purity,
m.ident,
Some(m.self_ty.node),
&m.generics,
extract::interner()
))
}
}
}
}
_ => fail!(~"method not found")
}
_ => fail!(~"method not found")
}
}
ast_map::node_item(@ast::item {
node: ast::item_impl(_, _, _, ref methods), _
}, _) => {
match vec::find(*methods, |method| {
to_str(method.ident) == method_name
}) {
Some(method) => {
Some(pprust::fun_to_str(
&method.decl,
method.purity,
method.ident,
Some(method.self_ty.node),
&method.generics,
extract::interner()
))
ast_map::node_item(@ast::item {
node: ast::item_impl(_, _, _, ref methods), _
}, _) => {
match vec::find(*methods, |method| {
to_str(method.ident) == method_name
}) {
Some(method) => {
Some(pprust::fun_to_str(
&method.decl,
method.purity,
method.ident,
Some(method.self_ty.node),
&method.generics,
extract::interner()
))
}
None => fail!(~"method not found")
}
None => fail!(~"method not found")
}
}
_ => fail!(~"get_method_sig: item ID not bound to trait or impl")
_ => fail!(~"get_method_sig: item ID not bound to trait or impl")
}
}
}
#[test]
fn should_add_trait_method_sigs() {
let doc = test::mk_doc(~"trait i { fn a<T>(&mut self) -> int; }");
assert!(doc.cratemod().traits()[0].methods[0].sig
== Some(~"fn a<T>(&mut self) -> int"));
}
fn fold_impl(
fold: &fold::Fold<astsrv::Srv>,
doc: doc::ImplDoc
@ -305,37 +272,6 @@ fn fold_impl(
}
}
#[test]
fn should_add_impl_bounds() {
let doc = test::mk_doc(~"impl<T, U: Copy, V: Copy + Clone> Option<T, U, V> { }");
assert!(doc.cratemod().impls()[0].bounds_str == Some(~"<T, U: Copy, V: Copy + Clone>"));
}
#[test]
fn should_add_impl_trait_types() {
let doc = test::mk_doc(~"impl j for int { fn a<T>() { } }");
assert!(doc.cratemod().impls()[0].trait_types[0] == ~"j");
}
#[test]
fn should_not_add_impl_trait_types_if_none() {
let doc = test::mk_doc(~"impl int { fn a() { } }");
assert!(vec::len(doc.cratemod().impls()[0].trait_types) == 0);
}
#[test]
fn should_add_impl_self_ty() {
let doc = test::mk_doc(~"impl int { fn a() { } }");
assert!(doc.cratemod().impls()[0].self_ty == Some(~"int"));
}
#[test]
fn should_add_impl_method_sigs() {
let doc = test::mk_doc(~"impl int { fn a<T>(&self) -> int { fail!() } }");
assert!(doc.cratemod().impls()[0].methods[0].sig
== Some(~"fn a<T>(&self) -> int"));
}
fn fold_type(
fold: &fold::Fold<astsrv::Srv>,
doc: doc::TyDoc
@ -369,12 +305,6 @@ fn fold_type(
}
}
#[test]
fn should_add_type_signatures() {
let doc = test::mk_doc(~"type t<T> = int;");
assert!(doc.cratemod().types()[0].sig == Some(~"type t<T> = int"));
}
fn fold_struct(
fold: &fold::Fold<astsrv::Srv>,
doc: doc::StructDoc
@ -422,38 +352,109 @@ fn strip_struct_extra_stuff(item: @ast::item) -> @ast::item {
}
}
#[test]
fn should_add_struct_defs() {
let doc = test::mk_doc(~"struct S { field: () }");
assert!((&doc.cratemod().structs()[0].sig).get().contains(
"struct S {"));
}
#[test]
fn should_not_serialize_struct_drop_blocks() {
// All we care about are the fields
let doc = test::mk_doc(~"struct S { field: (), drop { } }");
assert!(!(&doc.cratemod().structs()[0].sig).get().contains("drop"));
}
#[test]
fn should_not_serialize_struct_attrs() {
// All we care about are the fields
let doc = test::mk_doc(~"#[wut] struct S { field: () }");
assert!(!(&doc.cratemod().structs()[0].sig).get().contains("wut"));
}
#[cfg(test)]
pub mod test {
mod test {
use astsrv;
use doc;
use extract;
use tystr_pass::run;
use core::prelude::*;
pub fn mk_doc(source: ~str) -> doc::Doc {
fn mk_doc(source: ~str) -> doc::Doc {
do astsrv::from_str(copy source) |srv| {
let doc = extract::from_srv(srv.clone(), ~"");
run(srv.clone(), doc)
}
}
#[test]
fn should_add_fn_sig() {
let doc = mk_doc(~"fn a<T>() -> int { }");
assert!(doc.cratemod().fns()[0].sig == Some(~"fn a<T>() -> int"));
}
#[test]
fn should_add_foreign_fn_sig() {
let doc = mk_doc(~"extern mod a { fn a<T>() -> int; }");
assert!(doc.cratemod().nmods()[0].fns[0].sig ==
Some(~"fn a<T>() -> int"));
}
#[test]
fn should_add_const_types() {
let doc = mk_doc(~"static a: bool = true;");
assert!(doc.cratemod().consts()[0].sig == Some(~"bool"));
}
#[test]
fn should_add_variant_sigs() {
let doc = mk_doc(~"enum a { b(int) }");
assert!(doc.cratemod().enums()[0].variants[0].sig ==
Some(~"b(int)"));
}
#[test]
fn should_add_trait_method_sigs() {
let doc = mk_doc(~"trait i { fn a<T>(&mut self) -> int; }");
assert!(doc.cratemod().traits()[0].methods[0].sig
== Some(~"fn a<T>(&mut self) -> int"));
}
#[test]
fn should_add_impl_bounds() {
let doc = mk_doc(~"impl<T, U: Copy, V: Copy + Clone> Option<T, U, V> { }");
assert!(doc.cratemod().impls()[0].bounds_str == Some(~"<T, U: Copy, V: Copy + Clone>"));
}
#[test]
fn should_add_impl_trait_types() {
let doc = mk_doc(~"impl j for int { fn a<T>() { } }");
assert!(doc.cratemod().impls()[0].trait_types[0] == ~"j");
}
#[test]
fn should_not_add_impl_trait_types_if_none() {
let doc = mk_doc(~"impl int { fn a() { } }");
assert!(vec::len(doc.cratemod().impls()[0].trait_types) == 0);
}
#[test]
fn should_add_impl_self_ty() {
let doc = mk_doc(~"impl int { fn a() { } }");
assert!(doc.cratemod().impls()[0].self_ty == Some(~"int"));
}
#[test]
fn should_add_impl_method_sigs() {
let doc = mk_doc(~"impl int { fn a<T>(&self) -> int { fail!() } }");
assert!(doc.cratemod().impls()[0].methods[0].sig
== Some(~"fn a<T>(&self) -> int"));
}
#[test]
fn should_add_type_signatures() {
let doc = mk_doc(~"type t<T> = int;");
assert!(doc.cratemod().types()[0].sig == Some(~"type t<T> = int"));
}
#[test]
fn should_add_struct_defs() {
let doc = mk_doc(~"struct S { field: () }");
assert!((&doc.cratemod().structs()[0].sig).get().contains(
"struct S {"));
}
#[test]
fn should_not_serialize_struct_drop_blocks() {
// All we care about are the fields
let doc = mk_doc(~"struct S { field: (), drop { } }");
assert!(!(&doc.cratemod().structs()[0].sig).get().contains("drop"));
}
#[test]
fn should_not_serialize_struct_attrs() {
// All we care about are the fields
let doc = mk_doc(~"#[wut] struct S { field: () }");
assert!(!(&doc.cratemod().structs()[0].sig).get().contains("wut"));
}
}