@@ -5,16 +5,65 @@ import std::io::writer_util;
5
5
6
6
export mk_pass;
7
7
8
+ // FIXME: This is a really convoluted interface to work around trying
9
+ // to get a writer into a unique closure and then being able to test
10
+ // what was written afterward
8
11
fn mk_pass (
9
- writer : fn ~( ) -> io:: writer
12
+ give_writer : fn ~( fn ( io:: writer ) )
10
13
) -> pass {
11
- ret fn~(
12
- _srv : astsrv:: srv,
14
+ fn ~(
15
+ srv : astsrv:: srv,
13
16
doc: doc:: cratedoc
14
17
) -> doc:: cratedoc {
15
- write_markdown( doc, writer ( ) ) ;
18
+
19
+ fn mods_last ( item1 : doc:: itemtag , item2 : doc:: itemtag ) -> bool {
20
+ fn is_mod ( item : doc:: itemtag ) -> bool {
21
+ alt item {
22
+ doc : : modtag ( _) { true }
23
+ _ { false }
24
+ }
25
+ }
26
+
27
+ let lteq = !is_mod ( item1) || is_mod ( item2) ;
28
+ lteq
29
+ }
30
+
31
+ give_writer { |writer|
32
+ // Sort the items so mods come last. All mods will be
33
+ // output at the same header level so sorting mods last
34
+ // makes the headers come out nested correctly.
35
+ let sorted_doc = sort_pass:: mk_pass ( mods_last) ( srv, doc) ;
36
+
37
+ write_markdown ( sorted_doc, writer) ;
38
+ }
16
39
doc
17
- } ;
40
+ }
41
+ }
42
+
43
+ #[ test]
44
+ fn should_write_modules_last ( ) {
45
+ /*
46
+ Because the markdown pass writes all modules at the same level of
47
+ indentation (it doesn't 'nest' them), we need to make sure that we
48
+ write all of the modules contained in each module after all other
49
+ types of items, or else the header nesting will end up wrong, with
50
+ modules appearing to contain items that they do not.
51
+ */
52
+ let markdown = test:: render (
53
+ "mod a { }\
54
+ fn b() { }\
55
+ mod c { }\
56
+ fn d() { }"
57
+ ) ;
58
+
59
+ let idx_a = str:: find ( markdown, "# Module `a`" ) ;
60
+ let idx_b = str:: find ( markdown, "## Function `b`" ) ;
61
+ let idx_c = str:: find ( markdown, "# Module `c`" ) ;
62
+ let idx_d = str:: find ( markdown, "## Function `d`" ) ;
63
+
64
+ assert idx_b < idx_d;
65
+ assert idx_d < idx_a;
66
+ assert idx_a < idx_c;
18
67
}
19
68
20
69
type ctxt = {
@@ -487,13 +536,13 @@ fn should_write_resource_args() {
487
536
#[ cfg( test) ]
488
537
mod test {
489
538
fn render ( source : str ) -> str {
490
- let doc = create_doc ( source) ;
491
- let markdown = write_markdown_str ( doc) ;
539
+ let ( srv , doc) = create_doc_srv ( source) ;
540
+ let markdown = write_markdown_str_srv ( srv , doc) ;
492
541
#debug ( "markdown: %s" , markdown) ;
493
542
markdown
494
543
}
495
544
496
- fn create_doc ( source : str ) -> doc:: cratedoc {
545
+ fn create_doc_srv ( source : str ) -> ( astsrv :: srv , doc:: cratedoc ) {
497
546
let srv = astsrv:: mk_srv_from_str ( source) ;
498
547
let doc = extract:: from_srv ( srv, "" ) ;
499
548
#debug ( "doc (extract): %?" , doc) ;
@@ -503,6 +552,11 @@ mod test {
503
552
#debug ( "doc (path): %?" , doc) ;
504
553
let doc = attr_pass:: mk_pass ( ) ( srv, doc) ;
505
554
#debug ( "doc (attr): %?" , doc) ;
555
+ ( srv, doc)
556
+ }
557
+
558
+ fn create_doc ( source : str ) -> doc:: cratedoc {
559
+ let ( _, doc) = create_doc_srv ( source) ;
506
560
doc
507
561
}
508
562
@@ -515,6 +569,24 @@ mod test {
515
569
ret io:: mem_buffer_str ( buffer) ;
516
570
}
517
571
572
+ fn write_markdown_str_srv (
573
+ srv : astsrv:: srv ,
574
+ doc : doc:: cratedoc
575
+ ) -> str {
576
+ let port = comm:: port ( ) ;
577
+ let chan = comm:: chan ( port) ;
578
+
579
+ let pass = mk_pass { |f|
580
+ let buffer = io:: mk_mem_buffer ( ) ;
581
+ let writer = io:: mem_buffer_writer ( buffer) ;
582
+ f ( writer) ;
583
+ let result = io:: mem_buffer_str ( buffer) ;
584
+ comm:: send ( chan, result) ;
585
+ } ;
586
+ pass( srv, doc) ;
587
+ ret comm:: recv( port) ;
588
+ }
589
+
518
590
#[ test]
519
591
fn write_markdown_should_write_crate_header ( ) {
520
592
let srv = astsrv:: mk_srv_from_str ( "" ) ;
0 commit comments