2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2013-05-17 17:28:44 -05:00
|
|
|
|
2013-05-24 21:35:29 -05:00
|
|
|
|
2012-12-23 16:41:37 -06:00
|
|
|
use astsrv;
|
|
|
|
use doc;
|
2013-01-08 21:37:25 -06:00
|
|
|
use time;
|
2012-12-23 16:41:37 -06:00
|
|
|
|
2013-02-28 10:57:33 -06:00
|
|
|
#[cfg(test)] use extract;
|
|
|
|
|
2012-09-21 19:10:46 -05:00
|
|
|
/// A single operation on the document model
|
2013-01-08 16:00:45 -06:00
|
|
|
pub struct Pass {
|
2012-09-21 19:10:46 -05:00
|
|
|
name: ~str,
|
2013-05-07 16:20:56 -05:00
|
|
|
f: @fn(srv: astsrv::Srv, doc: doc::Doc) -> doc::Doc
|
2013-01-08 16:00:45 -06:00
|
|
|
}
|
2012-09-21 19:10:46 -05:00
|
|
|
|
|
|
|
pub fn run_passes(
|
|
|
|
srv: astsrv::Srv,
|
2013-01-30 21:32:36 -06:00
|
|
|
doc: doc::Doc,
|
|
|
|
passes: ~[Pass]
|
2012-09-21 19:10:46 -05:00
|
|
|
) -> doc::Doc {
|
|
|
|
let mut passno = 0;
|
2013-06-08 00:12:39 -05:00
|
|
|
do passes.iter().fold(doc) |doc, pass| {
|
2013-03-08 14:39:42 -06:00
|
|
|
debug!("pass #%d", passno);
|
2012-09-21 19:10:46 -05:00
|
|
|
passno += 1;
|
2013-07-02 14:47:32 -05:00
|
|
|
do time(pass.name.clone()) {
|
|
|
|
(pass.f)(srv.clone(), doc.clone())
|
2012-09-21 19:10:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_run_passes() {
|
|
|
|
fn pass1(
|
|
|
|
_srv: astsrv::Srv,
|
2013-01-30 21:32:36 -06:00
|
|
|
doc: doc::Doc
|
2012-09-21 19:10:46 -05:00
|
|
|
) -> doc::Doc {
|
2013-01-29 21:45:53 -06:00
|
|
|
doc::Doc{
|
2012-09-21 19:10:46 -05:00
|
|
|
pages: ~[
|
2013-01-29 21:45:53 -06:00
|
|
|
doc::CratePage(doc::CrateDoc{
|
|
|
|
topmod: doc::ModDoc{
|
|
|
|
item: doc::ItemDoc {
|
2013-07-19 06:51:37 -05:00
|
|
|
name: doc.cratemod().name_() + "two",
|
2013-07-02 14:47:32 -05:00
|
|
|
.. doc.cratemod().item.clone()
|
2012-09-21 19:10:46 -05:00
|
|
|
},
|
|
|
|
items: ~[],
|
|
|
|
index: None
|
2013-01-29 21:45:53 -06:00
|
|
|
}
|
2012-09-21 19:10:46 -05:00
|
|
|
})
|
|
|
|
]
|
2013-01-29 21:45:53 -06:00
|
|
|
}
|
2012-09-21 19:10:46 -05:00
|
|
|
}
|
|
|
|
fn pass2(
|
|
|
|
_srv: astsrv::Srv,
|
2013-01-30 21:32:36 -06:00
|
|
|
doc: doc::Doc
|
2012-09-21 19:10:46 -05:00
|
|
|
) -> doc::Doc {
|
2013-01-29 21:45:53 -06:00
|
|
|
doc::Doc{
|
2012-09-21 19:10:46 -05:00
|
|
|
pages: ~[
|
2013-01-29 21:45:53 -06:00
|
|
|
doc::CratePage(doc::CrateDoc{
|
|
|
|
topmod: doc::ModDoc{
|
|
|
|
item: doc::ItemDoc {
|
2013-07-19 06:51:37 -05:00
|
|
|
name: doc.cratemod().name_() + "three",
|
2013-07-02 14:47:32 -05:00
|
|
|
.. doc.cratemod().item.clone()
|
2012-09-21 19:10:46 -05:00
|
|
|
},
|
|
|
|
items: ~[],
|
|
|
|
index: None
|
2013-01-29 21:45:53 -06:00
|
|
|
}
|
2012-09-21 19:10:46 -05:00
|
|
|
})
|
|
|
|
]
|
2013-01-29 21:45:53 -06:00
|
|
|
}
|
2012-09-21 19:10:46 -05:00
|
|
|
}
|
|
|
|
let source = ~"";
|
|
|
|
do astsrv::from_str(source) |srv| {
|
|
|
|
let passes = ~[
|
2013-01-08 16:00:45 -06:00
|
|
|
Pass {
|
2012-09-21 19:10:46 -05:00
|
|
|
name: ~"",
|
|
|
|
f: pass1
|
|
|
|
},
|
2013-01-08 16:00:45 -06:00
|
|
|
Pass {
|
2012-09-21 19:10:46 -05:00
|
|
|
name: ~"",
|
|
|
|
f: pass2
|
|
|
|
}
|
|
|
|
];
|
2013-01-31 22:24:03 -06:00
|
|
|
let doc = extract::from_srv(srv.clone(), ~"one");
|
2012-09-21 19:10:46 -05:00
|
|
|
let doc = run_passes(srv, doc, passes);
|
2013-07-19 06:51:37 -05:00
|
|
|
assert_eq!(doc.cratemod().name_(), ~"onetwothree");
|
2012-09-21 19:10:46 -05:00
|
|
|
}
|
|
|
|
}
|