sort_pass.rs 2.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
// 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.

11
//! A general sorting pass
B
Brian Anderson 已提交
12

13 14
use astsrv;
use doc;
15
use fold::Fold;
16
use fold;
17
use pass::Pass;
B
Brian Anderson 已提交
18

19 20
#[cfg(test)] use extract;

21
use extra::sort;
22
use std::clone::Clone;
23

24
pub type ItemLtEqOp = @fn(v1: &doc::ItemTag, v2:  &doc::ItemTag) -> bool;
B
Brian Anderson 已提交
25

26 27 28 29 30 31 32 33 34 35 36
struct ItemLtEq {
    op: ItemLtEqOp,
}

impl Clone for ItemLtEq {
    fn clone(&self) -> ItemLtEq {
        ItemLtEq {
            op: self.op,
        }
    }
}
37

B
Brian Anderson 已提交
38
pub fn mk_pass(name: ~str, lteq: ItemLtEqOp) -> Pass {
39
    Pass {
40 41
        name: name.clone(),
        f: |srv, doc| run(srv, doc, ItemLtEq { op: lteq })
B
Brian Anderson 已提交
42 43 44 45
    }
}

fn run(
B
Brian Anderson 已提交
46
    _srv: astsrv::Srv,
B
Brian Anderson 已提交
47 48
    doc: doc::Doc,
    lteq: ItemLtEq
B
Brian Anderson 已提交
49
) -> doc::Doc {
50
    let fold = Fold {
B
Brian Anderson 已提交
51
        fold_mod: fold_mod,
L
Luqman Aden 已提交
52
        .. fold::default_any_fold(lteq)
53
    };
54
    (fold.fold_doc)(&fold, doc)
B
Brian Anderson 已提交
55 56 57
}

fn fold_mod(
58
    fold: &fold::Fold<ItemLtEq>,
B
Brian Anderson 已提交
59
    doc: doc::ModDoc
B
Brian Anderson 已提交
60
) -> doc::ModDoc {
61
    let doc = fold::default_any_fold_mod(fold, doc);
62
    doc::ModDoc {
63
        items: sort::merge_sort(doc.items, fold.ctxt.op),
64 65
        .. doc
    }
B
Brian Anderson 已提交
66 67 68 69
}

#[test]
fn test() {
70
    fn name_lteq(item1: &doc::ItemTag, item2: &doc::ItemTag) -> bool {
71
        (*item1).name() <= (*item2).name()
B
Brian Anderson 已提交
72 73
    }

74
    let source = ~"mod z { mod y { } fn x() { } } mod w { }";
B
Brian Anderson 已提交
75
    do astsrv::from_str(source) |srv| {
76 77
        let doc = extract::from_srv(srv.clone(), ~"");
        let doc = (mk_pass(~"", name_lteq).f)(srv.clone(), doc);
78 79 80 81 82
        // hidden __std_macros module at the start.
        assert_eq!(doc.cratemod().mods()[1].name(), ~"w");
        assert_eq!(doc.cratemod().mods()[2].items[0].name(), ~"x");
        assert_eq!(doc.cratemod().mods()[2].items[1].name(), ~"y");
        assert_eq!(doc.cratemod().mods()[2].name(), ~"z");
83
    }
B
Brian Anderson 已提交
84 85 86 87
}

#[test]
fn should_be_stable() {
88
    fn always_eq(_item1: &doc::ItemTag, _item2: &doc::ItemTag) -> bool {
B
Brian Anderson 已提交
89 90 91
        true
    }

92
    let source = ~"mod a { mod b { } } mod c { mod d { } }";
B
Brian Anderson 已提交
93
    do astsrv::from_str(source) |srv| {
94 95
        let doc = extract::from_srv(srv.clone(), ~"");
        let doc = (mk_pass(~"", always_eq).f)(srv.clone(), doc);
96 97 98
        // hidden __std_macros module at the start.
        assert_eq!(doc.cratemod().mods()[1].items[0].name(), ~"b");
        assert_eq!(doc.cratemod().mods()[2].items[0].name(), ~"d");
99
        let doc = (mk_pass(~"", always_eq).f)(srv.clone(), doc);
100 101
        assert_eq!(doc.cratemod().mods()[1].items[0].name(), ~"b");
        assert_eq!(doc.cratemod().mods()[2].items[0].name(), ~"d");
102
    }
B
Brian Anderson 已提交
103
}