From ef3a8ebb9bbc55bea4e0b62a1dcb6abb5512b223 Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Tue, 16 Apr 2013 01:09:55 +1000 Subject: [PATCH] librustdoc: move tests into dedicated tests module. --- src/librustdoc/attr_parser.rs | 185 +++--- src/librustdoc/attr_pass.rs | 213 +++--- src/librustdoc/config.rs | 238 +++---- src/librustdoc/desc_to_brief_pass.rs | 163 ++--- src/librustdoc/extract.rs | 132 ++-- src/librustdoc/markdown_index_pass.rs | 216 +++--- src/librustdoc/markdown_pass.rs | 920 +++++++++++++------------- src/librustdoc/markdown_writer.rs | 123 ++-- src/librustdoc/page_pass.rs | 47 +- src/librustdoc/prune_hidden_pass.rs | 20 +- src/librustdoc/prune_private_pass.rs | 176 ++--- src/librustdoc/sectionalize_pass.rs | 177 ++--- src/librustdoc/text_pass.rs | 316 ++++----- src/librustdoc/trim_pass.rs | 22 +- src/librustdoc/tystr_pass.rs | 311 ++++----- 15 files changed, 1632 insertions(+), 1627 deletions(-) diff --git a/src/librustdoc/attr_parser.rs b/src/librustdoc/attr_parser.rs index 213198e6f215..efd061209a50 100644 --- a/src/librustdoc/attr_parser.rs +++ b/src/librustdoc/attr_parser.rs @@ -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")); -} - - diff --git a/src/librustdoc/attr_pass.rs b/src/librustdoc/attr_pass.rs index b550155140f0..a666bff18c97 100644 --- a/src/librustdoc/attr_pass.rs +++ b/src/librustdoc/attr_pass.rs @@ -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, doc: doc::ItemDoc @@ -113,38 +107,14 @@ fn parse_item_attrs( 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, 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, 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, @@ -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")); + } } diff --git a/src/librustdoc/config.rs b/src/librustdoc/config.rs index 62ddec7a1199..ed1a54c49a73 100644 --- a/src/librustdoc/config.rs +++ b/src/librustdoc/config.rs @@ -209,7 +209,7 @@ fn parse_output_style(output_style: &str) -> Result { } } -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 { + fn parse_config(args: &[~str]) -> Result { 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")); + } +} \ No newline at end of file diff --git a/src/librustdoc/desc_to_brief_pass.rs b/src/librustdoc/desc_to_brief_pass.rs index 7c50af40f337..5027b2443559 100644 --- a/src/librustdoc/desc_to_brief_pass.rs +++ b/src/librustdoc/desc_to_brief_pass.rs @@ -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")); + } } diff --git a/src/librustdoc/extract.rs b/src/librustdoc/extract.rs index 1d6d569e1c29..fa3f7d643248 100644 --- a/src/librustdoc/extract.rs +++ b/src/librustdoc/extract.rs @@ -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"); + } } diff --git a/src/librustdoc/markdown_index_pass.rs b/src/librustdoc/markdown_index_pass.rs index 55901b4904aa..866fbba2be87 100644 --- a/src/librustdoc/markdown_index_pass.rs +++ b/src/librustdoc/markdown_index_pass.rs @@ -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") == - ~"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 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") - == ~"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") == + ~"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 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") + == ~"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" + }); + } } diff --git a/src/librustdoc/markdown_pass.rs b/src/librustdoc/markdown_pass.rs index 8c4cd26bb7c1..2dfc04e8ec70 100644 --- a/src/librustdoc/markdown_pass.rs +++ b/src/librustdoc/markdown_pass.rs @@ -22,10 +22,6 @@ use markdown_writer::WriterFactory; use pass::Pass; use sort_pass; -#[cfg(test)] use config; -#[cfg(test)] use markdown_writer; -#[cfg(test)] use page_pass; - use core::cell::Cell; use core::str; use core::vec; @@ -48,8 +44,8 @@ fn run( fn mods_last(item1: &doc::ItemTag, item2: &doc::ItemTag) -> bool { fn is_mod(item: &doc::ItemTag) -> bool { match *item { - doc::ModTag(_) => true, - _ => false + doc::ModTag(_) => true, + _ => false } } @@ -69,33 +65,6 @@ fn run( return doc; } -#[test] -fn should_write_modules_last() { - /* - Because the markdown pass writes all modules at the same level of - indentation (it doesn't 'nest' them), we need to make sure that we - write all of the modules contained in each module after all other - types of items, or else the header nesting will end up wrong, with - modules appearing to contain items that they do not. - */ - let markdown = test::render( - ~"mod a { }\ - fn b() { }\ - mod c { - }\ - fn d() { }" - ); - - let idx_a = str::find_str(markdown, ~"# Module `a`").get(); - let idx_b = str::find_str(markdown, ~"## Function `b`").get(); - let idx_c = str::find_str(markdown, ~"# Module `c`").get(); - let idx_d = str::find_str(markdown, ~"## Function `d`").get(); - - assert!(idx_b < idx_d); - assert!(idx_d < idx_a); - assert!(idx_a < idx_c); -} - struct Ctxt { w: Writer } @@ -118,33 +87,18 @@ pub fn write_markdown( fn write_page(ctxt: &Ctxt, page: &doc::Page) { write_title(ctxt, copy *page); match copy *page { - doc::CratePage(doc) => { - write_crate(ctxt, doc); - } - doc::ItemPage(doc) => { - // We don't write a header for item's pages because their - // header in the html output is created by the page title - write_item_no_header(ctxt, doc); - } + doc::CratePage(doc) => { + write_crate(ctxt, doc); + } + doc::ItemPage(doc) => { + // We don't write a header for item's pages because their + // header in the html output is created by the page title + write_item_no_header(ctxt, doc); + } } ctxt.w.put_done(); } -#[test] -fn should_request_new_writer_for_each_page() { - // This port will send us a (page, str) pair for every writer - // that was created - let (writer_factory, po) = markdown_writer::future_writer_factory(); - let (srv, doc) = test::create_doc_srv(~"mod a { }"); - // Split the document up into pages - let doc = (page_pass::mk_pass(config::DocPerMod).f)(srv, doc); - write_markdown(doc, writer_factory); - // We expect two pages to have been written - for iter::repeat(2) { - po.recv(); - } -} - fn write_title(ctxt: &Ctxt, page: doc::Page) { ctxt.w.put_line(fmt!("%% %s", make_title(page))); ctxt.w.put_line(~""); @@ -152,38 +106,18 @@ fn write_title(ctxt: &Ctxt, page: doc::Page) { fn make_title(page: doc::Page) -> ~str { let item = match page { - doc::CratePage(CrateDoc) => { - doc::ModTag(copy CrateDoc.topmod) - } - doc::ItemPage(ItemTag) => { - ItemTag - } + doc::CratePage(CrateDoc) => { + doc::ModTag(copy CrateDoc.topmod) + } + doc::ItemPage(ItemTag) => { + ItemTag + } }; let title = markdown_pass::header_text(item); let title = str::replace(title, ~"`", ~""); return title; } -#[test] -fn should_write_title_for_each_page() { - let (writer_factory, po) = markdown_writer::future_writer_factory(); - let (srv, doc) = test::create_doc_srv( - ~"#[link(name = \"core\")]; mod a { }"); - let doc = (page_pass::mk_pass(config::DocPerMod).f)(srv, doc); - write_markdown(doc, writer_factory); - for iter::repeat(2) { - let (page, markdown) = po.recv(); - match page { - doc::CratePage(_) => { - assert!(str::contains(markdown, ~"% Crate core")); - } - doc::ItemPage(_) => { - assert!(str::contains(markdown, ~"% Module a")); - } - } - } -} - enum Hlvl { H1 = 1, H2 = 2, @@ -204,94 +138,94 @@ fn write_header_(ctxt: &Ctxt, lvl: Hlvl, title: ~str) { pub fn header_kind(doc: doc::ItemTag) -> ~str { match doc { - doc::ModTag(_) => { - if doc.id() == syntax::ast::crate_node_id { - ~"Crate" - } else { - ~"Module" + doc::ModTag(_) => { + if doc.id() == syntax::ast::crate_node_id { + ~"Crate" + } else { + ~"Module" + } + } + doc::NmodTag(_) => { + ~"Foreign module" + } + doc::FnTag(_) => { + ~"Function" + } + doc::ConstTag(_) => { + ~"Const" + } + doc::EnumTag(_) => { + ~"Enum" + } + doc::TraitTag(_) => { + ~"Trait" + } + doc::ImplTag(_) => { + ~"Implementation" + } + doc::TyTag(_) => { + ~"Type" + } + doc::StructTag(_) => { + ~"Struct" } - } - doc::NmodTag(_) => { - ~"Foreign module" - } - doc::FnTag(_) => { - ~"Function" - } - doc::ConstTag(_) => { - ~"Const" - } - doc::EnumTag(_) => { - ~"Enum" - } - doc::TraitTag(_) => { - ~"Trait" - } - doc::ImplTag(_) => { - ~"Implementation" - } - doc::TyTag(_) => { - ~"Type" - } - doc::StructTag(_) => { - ~"Struct" - } } } pub fn header_name(doc: doc::ItemTag) -> ~str { let fullpath = str::connect(doc.path() + ~[doc.name()], ~"::"); match &doc { - &doc::ModTag(_) if doc.id() != syntax::ast::crate_node_id => { - fullpath - } - &doc::NmodTag(_) => { - fullpath - } - &doc::ImplTag(ref doc) => { - assert!(doc.self_ty.is_some()); - let bounds = if (&doc.bounds_str).is_some() { - fmt!(" where %s", (&doc.bounds_str).get()) - } else { - ~"" - }; - let self_ty = (&doc.self_ty).get(); - let mut trait_part = ~""; - for doc.trait_types.eachi |i, trait_type| { - if i == 0 { - trait_part += ~" of "; - } else { - trait_part += ~", "; - } - trait_part += *trait_type; + &doc::ModTag(_) if doc.id() != syntax::ast::crate_node_id => { + fullpath + } + &doc::NmodTag(_) => { + fullpath + } + &doc::ImplTag(ref doc) => { + assert!(doc.self_ty.is_some()); + let bounds = if (&doc.bounds_str).is_some() { + fmt!(" where %s", (&doc.bounds_str).get()) + } else { + ~"" + }; + let self_ty = (&doc.self_ty).get(); + let mut trait_part = ~""; + for doc.trait_types.eachi |i, trait_type| { + if i == 0 { + trait_part += ~" of "; + } else { + trait_part += ~", "; + } + trait_part += *trait_type; + } + fmt!("%s for %s%s", trait_part, self_ty, bounds) + } + _ => { + doc.name() } - fmt!("%s for %s%s", trait_part, self_ty, bounds) - } - _ => { - doc.name() - } } } pub fn header_text(doc: doc::ItemTag) -> ~str { match &doc { - &doc::ImplTag(ref ImplDoc) => { - let header_kind = header_kind(copy doc); - let bounds = if (&ImplDoc.bounds_str).is_some() { - fmt!(" where `%s`", (&ImplDoc.bounds_str).get()) - } else { - ~"" - }; - let desc = if ImplDoc.trait_types.is_empty() { - fmt!("for `%s`%s", (&ImplDoc.self_ty).get(), bounds) - } else { - fmt!("of `%s` for `%s`%s", - ImplDoc.trait_types[0], - (&ImplDoc.self_ty).get(), - bounds) - }; - return fmt!("%s %s", header_kind, desc); - } - _ => {} + &doc::ImplTag(ref ImplDoc) => { + let header_kind = header_kind(copy doc); + let bounds = if (&ImplDoc.bounds_str).is_some() { + fmt!(" where `%s`", (&ImplDoc.bounds_str).get()) + } else { + ~"" + }; + let desc = if ImplDoc.trait_types.is_empty() { + fmt!("for `%s`%s", (&ImplDoc.self_ty).get(), bounds) + } else { + fmt!("of `%s` for `%s`%s", + ImplDoc.trait_types[0], + (&ImplDoc.self_ty).get(), + bounds) + }; + return fmt!("%s %s", header_kind, desc); + } + _ => {} } header_text_(header_kind(copy doc), @@ -323,12 +257,6 @@ fn write_mod( write_mod_contents(ctxt, ModDoc); } -#[test] -fn should_write_full_path_to_mod() { - let markdown = test::render(~"mod a { mod b { mod c { } } }"); - assert!(str::contains(markdown, ~"# Module `a::b::c`")); -} - fn write_common( ctxt: &Ctxt, desc: Option<~str>, @@ -363,17 +291,6 @@ fn write_section(ctxt: &Ctxt, section: doc::Section) { ctxt.w.put_line(~""); } -#[test] -fn should_write_sections() { - let markdown = test::render( - ~"#[doc = \"\ - # Header\n\ - Body\"]\ - mod a { - }"); - assert!(str::contains(markdown, ~"#### Header\n\nBody\n\n")); -} - fn write_mod_contents( ctxt: &Ctxt, doc: doc::ModDoc @@ -402,15 +319,15 @@ fn write_item_(ctxt: &Ctxt, doc: doc::ItemTag, write_header: bool) { } match doc { - doc::ModTag(ModDoc) => write_mod(ctxt, ModDoc), - doc::NmodTag(nModDoc) => write_nmod(ctxt, nModDoc), - doc::FnTag(FnDoc) => write_fn(ctxt, FnDoc), - doc::ConstTag(ConstDoc) => write_const(ctxt, ConstDoc), - doc::EnumTag(EnumDoc) => write_enum(ctxt, EnumDoc), - doc::TraitTag(TraitDoc) => write_trait(ctxt, TraitDoc), - doc::ImplTag(ImplDoc) => write_impl(ctxt, ImplDoc), - doc::TyTag(TyDoc) => write_type(ctxt, TyDoc), - doc::StructTag(StructDoc) => put_struct(ctxt, StructDoc), + doc::ModTag(ModDoc) => write_mod(ctxt, ModDoc), + doc::NmodTag(nModDoc) => write_nmod(ctxt, nModDoc), + doc::FnTag(FnDoc) => write_fn(ctxt, FnDoc), + doc::ConstTag(ConstDoc) => write_const(ctxt, ConstDoc), + doc::EnumTag(EnumDoc) => write_enum(ctxt, EnumDoc), + doc::TraitTag(TraitDoc) => write_trait(ctxt, TraitDoc), + doc::ImplTag(ImplDoc) => write_impl(ctxt, ImplDoc), + doc::TyTag(TyDoc) => write_type(ctxt, TyDoc), + doc::StructTag(StructDoc) => put_struct(ctxt, StructDoc), } } @@ -420,17 +337,11 @@ fn write_item_header(ctxt: &Ctxt, doc: doc::ItemTag) { fn item_header_lvl(doc: &doc::ItemTag) -> Hlvl { match doc { - &doc::ModTag(_) | &doc::NmodTag(_) => H1, - _ => H2 + &doc::ModTag(_) | &doc::NmodTag(_) => H1, + _ => H2 } } -#[test] -fn should_write_crate_description() { - let markdown = test::render(~"#[doc = \"this is the crate\"];"); - assert!(str::contains(markdown, ~"this is the crate")); -} - fn write_index(ctxt: &Ctxt, index: doc::Index) { if vec::is_empty(index.entries) { return; @@ -444,7 +355,7 @@ fn write_index(ctxt: &Ctxt, index: doc::Index) { let id = copy entry.link; if entry.brief.is_some() { ctxt.w.put_line(fmt!("* [%s](%s) - %s", - header, id, (&entry.brief).get())); + header, id, (&entry.brief).get())); } else { ctxt.w.put_line(fmt!("* [%s](%s)", header, id)); } @@ -454,37 +365,6 @@ fn write_index(ctxt: &Ctxt, index: doc::Index) { ctxt.w.put_line(~""); } -#[test] -fn should_write_index() { - let markdown = test::render(~"mod a { } mod b { }"); - assert!(str::contains( - markdown, - ~"\n\n* [Module `a`](#module-a)\n\ - * [Module `b`](#module-b)\n\n" - )); -} - -#[test] -fn should_write_index_brief() { - let markdown = test::render(~"#[doc = \"test\"] mod a { }"); - assert!(str::contains(markdown, ~"(#module-a) - test\n")); -} - -#[test] -fn should_not_write_index_if_no_entries() { - let markdown = test::render(~""); - assert!(!str::contains(markdown, ~"\n\n\n")); -} - -#[test] -fn should_write_index_for_foreign_mods() { - let markdown = test::render(~"extern mod a { fn a(); }"); - assert!(str::contains( - markdown, - ~"\n\n* [Function `a`](#function-a)\n\n" - )); -} - fn write_nmod(ctxt: &Ctxt, doc: doc::NmodDoc) { write_common(ctxt, doc.desc(), doc.sections()); if doc.index.is_some() { @@ -497,27 +377,6 @@ fn write_nmod(ctxt: &Ctxt, doc: doc::NmodDoc) { } } -#[test] -fn should_write_foreign_mods() { - let markdown = test::render(~"#[doc = \"test\"] extern mod a { }"); - assert!(str::contains(markdown, ~"Foreign module `a`")); - assert!(str::contains(markdown, ~"test")); -} - -#[test] -fn should_write_foreign_fns() { - let markdown = test::render( - ~"extern mod a { #[doc = \"test\"] fn a(); }"); - assert!(str::contains(markdown, ~"test")); -} - -#[test] -fn should_write_foreign_fn_headers() { - let markdown = test::render( - ~"extern mod a { #[doc = \"test\"] fn a(); }"); - assert!(str::contains(markdown, ~"## Function `a`")); -} - fn write_fn( ctxt: &Ctxt, doc: doc::FnDoc @@ -542,11 +401,11 @@ fn write_fnlike( fn write_sig(ctxt: &Ctxt, sig: Option<~str>) { match sig { - Some(sig) => { - ctxt.w.put_line(code_block_indent(sig)); - ctxt.w.put_line(~""); - } - None => fail!(~"unimplemented") + Some(sig) => { + ctxt.w.put_line(code_block_indent(sig)); + ctxt.w.put_line(~""); + } + None => fail!(~"unimplemented") } } @@ -558,51 +417,6 @@ fn code_block_indent(s: ~str) -> ~str { str::connect(indented, "\n") } -#[test] -fn write_markdown_should_write_function_header() { - let markdown = test::render(~"fn func() { }"); - assert!(str::contains(markdown, ~"## Function `func`")); -} - -#[test] -fn should_write_the_function_signature() { - let markdown = test::render(~"#[doc = \"f\"] fn a() { }"); - assert!(str::contains(markdown, ~"\n fn a()\n")); -} - -#[test] -fn should_insert_blank_line_after_fn_signature() { - let markdown = test::render(~"#[doc = \"f\"] fn a() { }"); - assert!(str::contains(markdown, ~"fn a()\n\n")); -} - -#[test] -fn should_correctly_indent_fn_signature() { - let doc = test::create_doc(~"fn a() { }"); - let doc = doc::Doc{ - pages: ~[ - doc::CratePage(doc::CrateDoc{ - topmod: doc::ModDoc{ - items: ~[doc::FnTag(doc::SimpleItemDoc{ - sig: Some(~"line 1\nline 2"), - .. copy doc.cratemod().fns()[0] - })], - .. doc.cratemod() - }, - .. doc.CrateDoc() - }) - ] - }; - let markdown = test::write_markdown_str(doc); - assert!(str::contains(markdown, ~" line 1\n line 2")); -} - -#[test] -fn should_leave_blank_line_between_fn_header_and_sig() { - let markdown = test::render(~"fn a() { }"); - assert!(str::contains(markdown, ~"Function `a`\n\n fn a()")); -} - fn write_const( ctxt: &Ctxt, doc: doc::ConstDoc @@ -611,20 +425,6 @@ fn write_const( write_common(ctxt, doc.desc(), doc.sections()); } -#[test] -fn should_write_const_header() { - let markdown = test::render(~"static a: bool = true;"); - assert!(str::contains(markdown, ~"## Const `a`\n\n")); -} - -#[test] -fn should_write_const_description() { - let markdown = test::render( - ~"#[doc = \"b\"]\ - static a: bool = true;"); - assert!(str::contains(markdown, ~"\n\nb\n\n")); -} - fn write_enum( ctxt: &Ctxt, doc: doc::EnumDoc @@ -633,19 +433,6 @@ fn write_enum( write_variants(ctxt, doc.variants); } -#[test] -fn should_write_enum_header() { - let markdown = test::render(~"enum a { b }"); - assert!(str::contains(markdown, ~"## Enum `a`\n\n")); -} - -#[test] -fn should_write_enum_description() { - let markdown = test::render( - ~"#[doc = \"b\"] enum a { b }"); - assert!(str::contains(markdown, ~"\n\nb\n\n")); -} - fn write_variants( ctxt: &Ctxt, docs: &[doc::VariantDoc] @@ -667,48 +454,15 @@ fn write_variant(ctxt: &Ctxt, doc: doc::VariantDoc) { assert!(doc.sig.is_some()); let sig = (&doc.sig).get(); match copy doc.desc { - Some(desc) => { - ctxt.w.put_line(fmt!("* `%s` - %s", sig, desc)); - } - None => { - ctxt.w.put_line(fmt!("* `%s`", sig)); - } + Some(desc) => { + ctxt.w.put_line(fmt!("* `%s` - %s", sig, desc)); + } + None => { + ctxt.w.put_line(fmt!("* `%s`", sig)); + } } } -#[test] -fn should_write_variant_list() { - let markdown = test::render( - ~"enum a { \ - #[doc = \"test\"] b, \ - #[doc = \"test\"] c }"); - assert!(str::contains( - markdown, - ~"\n\n#### Variants\n\ - \n* `b` - test\ - \n* `c` - test\n\n")); -} - -#[test] -fn should_write_variant_list_without_descs() { - let markdown = test::render(~"enum a { b, c }"); - assert!(str::contains( - markdown, - ~"\n\n#### Variants\n\ - \n* `b`\ - \n* `c`\n\n")); -} - -#[test] -fn should_write_variant_list_with_signatures() { - let markdown = test::render(~"enum a { b(int), #[doc = \"a\"] c(int) }"); - assert!(str::contains( - markdown, - ~"\n\n#### Variants\n\ - \n* `b(int)`\ - \n* `c(int)` - a\n\n")); -} - fn write_trait(ctxt: &Ctxt, doc: doc::TraitDoc) { write_common(ctxt, doc.desc(), doc.sections()); write_methods(ctxt, doc.methods); @@ -730,78 +484,11 @@ fn write_method(ctxt: &Ctxt, doc: doc::MethodDoc) { ); } -#[test] -fn should_write_trait_header() { - let markdown = test::render(~"trait i { fn a(); }"); - assert!(str::contains(markdown, ~"## Trait `i`")); -} - -#[test] -fn should_write_trait_desc() { - let markdown = test::render( - ~"#[doc = \"desc\"] trait i { fn a(); }"); - assert!(str::contains(markdown, ~"desc")); -} - -#[test] -fn should_write_trait_method_header() { - let markdown = test::render( - ~"trait i { fn a(); }"); - assert!(str::contains(markdown, ~"### Method `a`")); -} - -#[test] -fn should_write_trait_method_signature() { - let markdown = test::render( - ~"trait i { fn a(&self); }"); - assert!(str::contains(markdown, ~"\n fn a(&self)")); -} - fn write_impl(ctxt: &Ctxt, doc: doc::ImplDoc) { write_common(ctxt, doc.desc(), doc.sections()); write_methods(ctxt, doc.methods); } -#[test] -fn should_write_impl_header() { - let markdown = test::render(~"impl int { fn a() { } }"); - assert!(str::contains(markdown, ~"## Implementation for `int`")); -} - -#[test] -fn should_write_impl_header_with_bounds() { - let markdown = test::render(~"impl int { }"); - assert!(str::contains(markdown, ~"## Implementation for `int` where ``")); -} - -#[test] -fn should_write_impl_header_with_trait() { - let markdown = test::render(~"impl j for int { fn a() { } }"); - assert!(str::contains(markdown, - ~"## Implementation of `j` for `int`")); -} - -#[test] -fn should_write_impl_desc() { - let markdown = test::render( - ~"#[doc = \"desc\"] impl int { fn a() { } }"); - assert!(str::contains(markdown, ~"desc")); -} - -#[test] -fn should_write_impl_method_header() { - let markdown = test::render( - ~"impl int { fn a() { } }"); - assert!(str::contains(markdown, ~"### Method `a`")); -} - -#[test] -fn should_write_impl_method_signature() { - let markdown = test::render( - ~"impl int { fn a(&mut self) { } }"); - assert!(str::contains(markdown, ~"\n fn a(&mut self)")); -} - fn write_type( ctxt: &Ctxt, doc: doc::TyDoc @@ -810,25 +497,6 @@ fn write_type( write_common(ctxt, doc.desc(), doc.sections()); } -#[test] -fn should_write_type_header() { - let markdown = test::render(~"type t = int;"); - assert!(str::contains(markdown, ~"## Type `t`")); -} - -#[test] -fn should_write_type_desc() { - let markdown = test::render( - ~"#[doc = \"desc\"] type t = int;"); - assert!(str::contains(markdown, ~"\n\ndesc\n\n")); -} - -#[test] -fn should_write_type_signature() { - let markdown = test::render(~"type t = int;"); - assert!(str::contains(markdown, ~"\n\n type t = int\n\n")); -} - fn put_struct( ctxt: &Ctxt, doc: doc::StructDoc @@ -837,12 +505,6 @@ fn put_struct( write_common(ctxt, doc.desc(), doc.sections()); } -#[test] -fn should_put_struct_header() { - let markdown = test::render(~"struct S { field: () }"); - assert!(str::contains(markdown, ~"## Struct `S`\n\n")); -} - #[cfg(test)] mod test { use astsrv; @@ -855,22 +517,21 @@ mod test { use markdown_pass::{mk_pass, write_markdown}; use markdown_writer; use path_pass; + use page_pass; use sectionalize_pass; use trim_pass; use tystr_pass; use unindent_pass; + use core::prelude::*; - use core::path::Path; - use core::str; - - pub fn render(source: ~str) -> ~str { + fn render(source: ~str) -> ~str { let (srv, doc) = create_doc_srv(source); let markdown = write_markdown_str_srv(srv, doc); debug!("markdown: %s", markdown); markdown } - pub fn create_doc_srv(source: ~str) -> (astsrv::Srv, doc::Doc) { + fn create_doc_srv(source: ~str) -> (astsrv::Srv, doc::Doc) { do astsrv::from_str(source) |srv| { let config = config::Config { @@ -901,12 +562,12 @@ mod test { } } - pub fn create_doc(source: ~str) -> doc::Doc { + fn create_doc(source: ~str) -> doc::Doc { let (_, doc) = create_doc_srv(source); doc } - pub fn write_markdown_str( + fn write_markdown_str( doc: doc::Doc ) -> ~str { let (writer_factory, po) = markdown_writer::future_writer_factory(); @@ -914,7 +575,7 @@ mod test { return po.recv().second(); } - pub fn write_markdown_str_srv( + fn write_markdown_str_srv( srv: astsrv::Srv, doc: doc::Doc ) -> ~str { @@ -925,14 +586,349 @@ mod test { } #[test] - pub fn write_markdown_should_write_mod_headers() { + fn write_markdown_should_write_mod_headers() { let markdown = render(~"mod moo { }"); assert!(str::contains(markdown, ~"# Module `moo`")); } #[test] - pub fn should_leave_blank_line_after_header() { + fn should_leave_blank_line_after_header() { let markdown = render(~"mod morp { }"); assert!(str::contains(markdown, ~"Module `morp`\n\n")); } + + #[test] + fn should_write_modules_last() { + /* + Because the markdown pass writes all modules at the same level of + indentation (it doesn't 'nest' them), we need to make sure that we + write all of the modules contained in each module after all other + types of items, or else the header nesting will end up wrong, with + modules appearing to contain items that they do not. + */ + let markdown = render( + ~"mod a { }\ + fn b() { }\ + mod c { +}\ + fn d() { }" + ); + + let idx_a = str::find_str(markdown, ~"# Module `a`").get(); + let idx_b = str::find_str(markdown, ~"## Function `b`").get(); + let idx_c = str::find_str(markdown, ~"# Module `c`").get(); + let idx_d = str::find_str(markdown, ~"## Function `d`").get(); + + assert!(idx_b < idx_d); + assert!(idx_d < idx_a); + assert!(idx_a < idx_c); + } + + #[test] + fn should_request_new_writer_for_each_page() { + // This port will send us a (page, str) pair for every writer + // that was created + let (writer_factory, po) = markdown_writer::future_writer_factory(); + let (srv, doc) = create_doc_srv(~"mod a { }"); + // Split the document up into pages + let doc = (page_pass::mk_pass(config::DocPerMod).f)(srv, doc); + write_markdown(doc, writer_factory); + // We expect two pages to have been written + for iter::repeat(2) { + po.recv(); + } + } + + #[test] + fn should_write_title_for_each_page() { + let (writer_factory, po) = markdown_writer::future_writer_factory(); + let (srv, doc) = create_doc_srv( + ~"#[link(name = \"core\")]; mod a { }"); + let doc = (page_pass::mk_pass(config::DocPerMod).f)(srv, doc); + write_markdown(doc, writer_factory); + for iter::repeat(2) { + let (page, markdown) = po.recv(); + match page { + doc::CratePage(_) => { + assert!(str::contains(markdown, ~"% Crate core")); + } + doc::ItemPage(_) => { + assert!(str::contains(markdown, ~"% Module a")); + } + } + } + } + + #[test] + fn should_write_full_path_to_mod() { + let markdown = render(~"mod a { mod b { mod c { } } }"); + assert!(str::contains(markdown, ~"# Module `a::b::c`")); + } + + #[test] + fn should_write_sections() { + let markdown = render( + ~"#[doc = \"\ + # Header\n\ + Body\"]\ + mod a { +}"); + assert!(str::contains(markdown, ~"#### Header\n\nBody\n\n")); + } + + #[test] + fn should_write_crate_description() { + let markdown = render(~"#[doc = \"this is the crate\"];"); + assert!(str::contains(markdown, ~"this is the crate")); + } + + + #[test] + fn should_write_index() { + let markdown = render(~"mod a { } mod b { }"); + assert!(str::contains( + markdown, + ~"\n\n* [Module `a`](#module-a)\n\ + * [Module `b`](#module-b)\n\n" + )); + } + + #[test] + fn should_write_index_brief() { + let markdown = render(~"#[doc = \"test\"] mod a { }"); + assert!(str::contains(markdown, ~"(#module-a) - test\n")); + } + + #[test] + fn should_not_write_index_if_no_entries() { + let markdown = render(~""); + assert!(!str::contains(markdown, ~"\n\n\n")); + } + + #[test] + fn should_write_index_for_foreign_mods() { + let markdown = render(~"extern mod a { fn a(); }"); + assert!(str::contains( + markdown, + ~"\n\n* [Function `a`](#function-a)\n\n" + )); + } + + #[test] + fn should_write_foreign_mods() { + let markdown = render(~"#[doc = \"test\"] extern mod a { }"); + assert!(str::contains(markdown, ~"Foreign module `a`")); + assert!(str::contains(markdown, ~"test")); + } + + #[test] + fn should_write_foreign_fns() { + let markdown = render( + ~"extern mod a { #[doc = \"test\"] fn a(); }"); + assert!(str::contains(markdown, ~"test")); + } + + #[test] + fn should_write_foreign_fn_headers() { + let markdown = render( + ~"extern mod a { #[doc = \"test\"] fn a(); }"); + assert!(str::contains(markdown, ~"## Function `a`")); + } + + #[test] + fn write_markdown_should_write_function_header() { + let markdown = render(~"fn func() { }"); + assert!(str::contains(markdown, ~"## Function `func`")); + } + + #[test] + fn should_write_the_function_signature() { + let markdown = render(~"#[doc = \"f\"] fn a() { }"); + assert!(str::contains(markdown, ~"\n fn a()\n")); + } + + #[test] + fn should_insert_blank_line_after_fn_signature() { + let markdown = render(~"#[doc = \"f\"] fn a() { }"); + assert!(str::contains(markdown, ~"fn a()\n\n")); + } + + #[test] + fn should_correctly_indent_fn_signature() { + let doc = create_doc(~"fn a() { }"); + let doc = doc::Doc{ + pages: ~[ + doc::CratePage(doc::CrateDoc{ + topmod: doc::ModDoc{ + items: ~[doc::FnTag(doc::SimpleItemDoc{ + sig: Some(~"line 1\nline 2"), + .. copy doc.cratemod().fns()[0] + })], + .. doc.cratemod() + }, + .. doc.CrateDoc() + }) + ] + }; + let markdown = write_markdown_str(doc); + assert!(str::contains(markdown, ~" line 1\n line 2")); + } + + #[test] + fn should_leave_blank_line_between_fn_header_and_sig() { + let markdown = render(~"fn a() { }"); + assert!(str::contains(markdown, ~"Function `a`\n\n fn a()")); + } + + #[test] + fn should_write_const_header() { + let markdown = render(~"static a: bool = true;"); + assert!(str::contains(markdown, ~"## Const `a`\n\n")); + } + + #[test] + fn should_write_const_description() { + let markdown = render( + ~"#[doc = \"b\"]\ + static a: bool = true;"); + assert!(str::contains(markdown, ~"\n\nb\n\n")); + } + + #[test] + fn should_write_enum_header() { + let markdown = render(~"enum a { b }"); + assert!(str::contains(markdown, ~"## Enum `a`\n\n")); + } + + #[test] + fn should_write_enum_description() { + let markdown = render( + ~"#[doc = \"b\"] enum a { b }"); + assert!(str::contains(markdown, ~"\n\nb\n\n")); + } + + #[test] + fn should_write_variant_list() { + let markdown = render( + ~"enum a { \ + #[doc = \"test\"] b, \ + #[doc = \"test\"] c }"); + assert!(str::contains( + markdown, + ~"\n\n#### Variants\n\ + \n* `b` - test\ + \n* `c` - test\n\n")); + } + + #[test] + fn should_write_variant_list_without_descs() { + let markdown = render(~"enum a { b, c }"); + assert!(str::contains( + markdown, + ~"\n\n#### Variants\n\ + \n* `b`\ + \n* `c`\n\n")); + } + + #[test] + fn should_write_variant_list_with_signatures() { + let markdown = render(~"enum a { b(int), #[doc = \"a\"] c(int) }"); + assert!(str::contains( + markdown, + ~"\n\n#### Variants\n\ + \n* `b(int)`\ + \n* `c(int)` - a\n\n")); + } + + #[test] + fn should_write_trait_header() { + let markdown = render(~"trait i { fn a(); }"); + assert!(str::contains(markdown, ~"## Trait `i`")); + } + + #[test] + fn should_write_trait_desc() { + let markdown = render( + ~"#[doc = \"desc\"] trait i { fn a(); }"); + assert!(str::contains(markdown, ~"desc")); + } + + #[test] + fn should_write_trait_method_header() { + let markdown = render( + ~"trait i { fn a(); }"); + assert!(str::contains(markdown, ~"### Method `a`")); + } + + #[test] + fn should_write_trait_method_signature() { + let markdown = render( + ~"trait i { fn a(&self); }"); + assert!(str::contains(markdown, ~"\n fn a(&self)")); + } + + #[test] + fn should_write_impl_header() { + let markdown = render(~"impl int { fn a() { } }"); + assert!(str::contains(markdown, ~"## Implementation for `int`")); + } + + #[test] + fn should_write_impl_header_with_bounds() { + let markdown = render(~"impl int { }"); + assert!(str::contains(markdown, ~"## Implementation for `int` where ``")); + } + + #[test] + fn should_write_impl_header_with_trait() { + let markdown = render(~"impl j for int { fn a() { } }"); + assert!(str::contains(markdown, + ~"## Implementation of `j` for `int`")); + } + + #[test] + fn should_write_impl_desc() { + let markdown = render( + ~"#[doc = \"desc\"] impl int { fn a() { } }"); + assert!(str::contains(markdown, ~"desc")); + } + + #[test] + fn should_write_impl_method_header() { + let markdown = render( + ~"impl int { fn a() { } }"); + assert!(str::contains(markdown, ~"### Method `a`")); + } + + #[test] + fn should_write_impl_method_signature() { + let markdown = render( + ~"impl int { fn a(&mut self) { } }"); + assert!(str::contains(markdown, ~"\n fn a(&mut self)")); + } + + #[test] + fn should_write_type_header() { + let markdown = render(~"type t = int;"); + assert!(str::contains(markdown, ~"## Type `t`")); + } + + #[test] + fn should_write_type_desc() { + let markdown = render( + ~"#[doc = \"desc\"] type t = int;"); + assert!(str::contains(markdown, ~"\n\ndesc\n\n")); + } + + #[test] + fn should_write_type_signature() { + let markdown = render(~"type t = int;"); + assert!(str::contains(markdown, ~"\n\n type t = int\n\n")); + } + + #[test] + fn should_put_struct_header() { + let markdown = render(~"struct S { field: () }"); + assert!(str::contains(markdown, ~"## Struct `S`\n\n")); + } } diff --git a/src/librustdoc/markdown_writer.rs b/src/librustdoc/markdown_writer.rs index 2011649995ec..fcf7011cbc30 100644 --- a/src/librustdoc/markdown_writer.rs +++ b/src/librustdoc/markdown_writer.rs @@ -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")); + } +} diff --git a/src/librustdoc/page_pass.rs b/src/librustdoc/page_pass.rs index 55f17f5c1501..c620e20530e6 100644 --- a/src/librustdoc/page_pass.rs +++ b/src/librustdoc/page_pass.rs @@ -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())); + } } diff --git a/src/librustdoc/prune_hidden_pass.rs b/src/librustdoc/prune_hidden_pass.rs index f46d183ffcfa..a50f1f51765f 100644 --- a/src/librustdoc/prune_hidden_pass.rs +++ b/src/librustdoc/prune_hidden_pass.rs @@ -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())) + } } diff --git a/src/librustdoc/prune_private_pass.rs b/src/librustdoc/prune_private_pass.rs index 08008ab2bcae..0578169e257d 100644 --- a/src/librustdoc/prune_private_pass.rs +++ b/src/librustdoc/prune_private_pass.rs @@ -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()); + } +} diff --git a/src/librustdoc/sectionalize_pass.rs b/src/librustdoc/sectionalize_pass.rs index 788f84b06c2e..1cf43043004c 100644 --- a/src/librustdoc/sectionalize_pass.rs +++ b/src/librustdoc/sectionalize_pass.rs @@ -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); + } } diff --git a/src/librustdoc/text_pass.rs b/src/librustdoc/text_pass.rs index b5e9b452d148..785428f00775 100644 --- a/src/librustdoc/text_pass.rs +++ b/src/librustdoc/text_pass.rs @@ -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"); + } } diff --git a/src/librustdoc/trim_pass.rs b/src/librustdoc/trim_pass.rs index ca543a27189d..e56a5f18ac6f 100644 --- a/src/librustdoc/trim_pass.rs +++ b/src/librustdoc/trim_pass.rs @@ -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")); + } } diff --git a/src/librustdoc/tystr_pass.rs b/src/librustdoc/tystr_pass.rs index 6ccfb1bb8c4b..c0562d306398 100644 --- a/src/librustdoc/tystr_pass.rs +++ b/src/librustdoc/tystr_pass.rs @@ -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() -> int { }"); - assert!(doc.cratemod().fns()[0].sig == Some(~"fn a() -> int")); -} - -#[test] -fn should_add_foreign_fn_sig() { - let doc = test::mk_doc(~"extern mod a { fn a() -> int; }"); - assert!(doc.cratemod().nmods()[0].fns[0].sig == - Some(~"fn a() -> int")); -} - fn fold_const( fold: &fold::Fold, 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, 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, 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(&mut self) -> int; }"); - assert!(doc.cratemod().traits()[0].methods[0].sig - == Some(~"fn a(&mut self) -> int")); -} - fn fold_impl( fold: &fold::Fold, doc: doc::ImplDoc @@ -305,37 +272,6 @@ fn fold_impl( } } -#[test] -fn should_add_impl_bounds() { - let doc = test::mk_doc(~"impl Option { }"); - assert!(doc.cratemod().impls()[0].bounds_str == Some(~"")); -} - -#[test] -fn should_add_impl_trait_types() { - let doc = test::mk_doc(~"impl j for int { fn a() { } }"); - 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(&self) -> int { fail!() } }"); - assert!(doc.cratemod().impls()[0].methods[0].sig - == Some(~"fn a(&self) -> int")); -} - fn fold_type( fold: &fold::Fold, doc: doc::TyDoc @@ -369,12 +305,6 @@ fn fold_type( } } -#[test] -fn should_add_type_signatures() { - let doc = test::mk_doc(~"type t = int;"); - assert!(doc.cratemod().types()[0].sig == Some(~"type t = int")); -} - fn fold_struct( fold: &fold::Fold, 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() -> int { }"); + assert!(doc.cratemod().fns()[0].sig == Some(~"fn a() -> int")); + } + + #[test] + fn should_add_foreign_fn_sig() { + let doc = mk_doc(~"extern mod a { fn a() -> int; }"); + assert!(doc.cratemod().nmods()[0].fns[0].sig == + Some(~"fn a() -> 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(&mut self) -> int; }"); + assert!(doc.cratemod().traits()[0].methods[0].sig + == Some(~"fn a(&mut self) -> int")); + } + + #[test] + fn should_add_impl_bounds() { + let doc = mk_doc(~"impl Option { }"); + assert!(doc.cratemod().impls()[0].bounds_str == Some(~"")); + } + + #[test] + fn should_add_impl_trait_types() { + let doc = mk_doc(~"impl j for int { fn a() { } }"); + 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(&self) -> int { fail!() } }"); + assert!(doc.cratemod().impls()[0].methods[0].sig + == Some(~"fn a(&self) -> int")); + } + + #[test] + fn should_add_type_signatures() { + let doc = mk_doc(~"type t = int;"); + assert!(doc.cratemod().types()[0].sig == Some(~"type 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")); + } }