@@ -4,13 +4,15 @@ import std::option;
4
4
import std:: ivec;
5
5
import syntax:: ast;
6
6
import syntax:: fold;
7
+ import front:: attr;
7
8
8
9
export modify_for_testing;
9
10
10
11
type node_id_gen = @fn ( ) -> ast:: node_id ;
11
12
12
13
type test_ctxt = rec ( node_id_gen next_node_id ,
13
- mutable ast:: ident[ ] path) ;
14
+ mutable ast:: ident[ ] path,
15
+ mutable ast:: ident[ ] [ ] testfns) ;
14
16
15
17
// Traverse the crate, collecting all the test functions, eliding any
16
18
// existing main functions, and synthesizing a main test harness
@@ -28,8 +30,9 @@ fn modify_for_testing(@ast::crate crate) -> @ast::crate {
28
30
ret this_node_id;
29
31
} ( next_node_id) ;
30
32
31
- auto cx = rec( next_node_id = next_node_id_fn,
32
- mutable path = ~[ ] ) ;
33
+ let test_ctxt cx = rec( next_node_id = next_node_id_fn,
34
+ mutable path = ~[ ] ,
35
+ mutable testfns = ~[ ] ) ;
33
36
34
37
auto precursor = rec( fold_crate = bind fold_crate( cx, _, _) ,
35
38
fold_item = bind fold_item( cx, _, _)
@@ -103,11 +106,36 @@ fn fold_item(&test_ctxt cx, &@ast::item i,
103
106
104
107
cx. path += ~[ i. ident] ;
105
108
log #fmt( "current path: %s" , ast:: path_name_i( cx. path) ) ;
109
+
110
+ if ( is_test_fn( i) ) {
111
+ log "this is a test function" ;
112
+ cx. testfns += ~[ cx . path] ;
113
+ }
114
+
106
115
auto res = fold:: noop_fold_item ( i , fld ) ;
107
116
ivec:: pop ( cx . path) ;
108
117
ret res;
109
118
}
110
119
120
+ fn is_test_fn ( & @ast:: item i) -> bool {
121
+ auto has_test_attr =
122
+ ivec:: len ( attr:: find_attrs_by_name ( i. attrs , "test" ) ) > 0 u;
123
+
124
+ fn has_test_signature ( & @ast:: item i) -> bool {
125
+ alt ( i. node) {
126
+ case ( ast:: item_fn ( ?f , ?tps ) ) {
127
+ auto input_cnt = ivec:: len ( f. decl. inputs ) ;
128
+ auto no_output = f. decl. output. node == ast:: ty_nil ;
129
+ auto tparm_cnt = ivec:: len ( tps) ;
130
+ input_cnt == 0 u && no_output && tparm_cnt == 0 u
131
+ }
132
+ case ( _) { false }
133
+ }
134
+ }
135
+
136
+ ret has_test_attr && has_test_signature ( i) ;
137
+ }
138
+
111
139
// Local Variables:
112
140
// mode: rust
113
141
// fill-column: 78;
0 commit comments