1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// SPDX-License-Identifier: MPL-2.0

//! The source module tree of ktests.
//!
//! In the `KtestTree`, the root is abstract, and the children of the root are the
//! crates. The leaves are the test functions. Nodes other than the root and the
//! leaves are modules.
//!

use alloc::{
    collections::{btree_map, BTreeMap},
    string::{String, ToString},
    vec,
    vec::Vec,
};

use crate::{
    path::{KtestPath, PathElement},
    KtestItem,
};

#[derive(Debug)]
pub struct KtestModule {
    nr_tot_tests: usize,
    name: PathElement,
    children: BTreeMap<PathElement, KtestModule>,
    tests: Vec<KtestItem>,
}

impl KtestModule {
    pub fn nr_this_tests(&self) -> usize {
        self.tests.len()
    }

    pub fn nr_tot_tests(&self) -> usize {
        self.nr_tot_tests
    }

    pub fn name(&self) -> &PathElement {
        &self.name
    }

    fn insert(&mut self, module_path: &mut KtestPath, test: KtestItem) {
        self.nr_tot_tests += 1;
        if module_path.is_empty() {
            self.tests.push(test);
        } else {
            let module_name = module_path.pop_front().unwrap();
            let node = self.children.entry(module_name.clone()).or_insert(Self {
                nr_tot_tests: 0,
                name: module_name,
                children: BTreeMap::new(),
                tests: Vec::new(),
            });
            node.nr_tot_tests += 1;
            node.insert(module_path, test);
        }
    }

    fn new(name: PathElement) -> Self {
        Self {
            nr_tot_tests: 0,
            name,
            children: BTreeMap::new(),
            tests: Vec::new(),
        }
    }
}

#[derive(Debug)]
pub struct KtestCrate {
    // Crate behaves just like modules, which can own it's children and tests.
    // But the iterator it provides will only iterate over the modules not tests.
    root_module: KtestModule,
}

impl KtestCrate {
    pub fn nr_tot_tests(&self) -> usize {
        self.root_module.nr_tot_tests()
    }

    pub fn name(&self) -> &str {
        self.root_module.name()
    }
}

pub struct KtestTree {
    nr_tot_tests: usize,
    crates: BTreeMap<String, KtestCrate>,
}

impl FromIterator<KtestItem> for KtestTree {
    fn from_iter<I: IntoIterator<Item = KtestItem>>(iter: I) -> Self {
        let mut tree = Self::new();
        for test in iter {
            tree.add_ktest(test);
        }
        tree
    }
}

impl KtestTree {
    pub fn new() -> Self {
        Self {
            nr_tot_tests: 0,
            crates: BTreeMap::new(),
        }
    }

    pub fn add_ktest(&mut self, test: KtestItem) {
        self.nr_tot_tests += 1;
        let package = test.info().package.to_string();
        let module_path = test.info().module_path;
        let node = self.crates.entry(package.clone()).or_insert(KtestCrate {
            root_module: KtestModule::new(PathElement::from(package)),
        });
        node.root_module
            .insert(&mut KtestPath::from(module_path), test);
    }

    pub fn nr_tot_tests(&self) -> usize {
        self.nr_tot_tests
    }

    pub fn nr_tot_crates(&self) -> usize {
        self.crates.len()
    }
}

impl Default for KtestTree {
    fn default() -> Self {
        Self::new()
    }
}

/// The `KtestTreeIter` will iterate over all crates. Yeilding `KtestCrate`s.
pub struct KtestTreeIter<'a> {
    crate_iter: btree_map::Iter<'a, String, KtestCrate>,
}

impl KtestTree {
    pub fn iter(&self) -> KtestTreeIter<'_> {
        KtestTreeIter {
            crate_iter: self.crates.iter(),
        }
    }
}

impl<'a> Iterator for KtestTreeIter<'a> {
    type Item = &'a KtestCrate;

    fn next(&mut self) -> Option<Self::Item> {
        self.crate_iter.next().map(|(_, v)| v)
    }
}

type CrateChildrenIter<'a> = btree_map::Iter<'a, String, KtestModule>;

/// The `KtestCrateIter` will iterate over all modules in a crate. Yeilding `KtestModule`s.
/// The iterator will return modules in the depth-first-search order of the module tree.
pub struct KtestCrateIter<'a> {
    path: Vec<(&'a KtestModule, CrateChildrenIter<'a>)>,
}

impl KtestCrate {
    pub fn iter(&self) -> KtestCrateIter<'_> {
        KtestCrateIter {
            path: vec![(&self.root_module, self.root_module.children.iter())],
        }
    }
}

impl<'a> Iterator for KtestCrateIter<'a> {
    type Item = &'a KtestModule;

    fn next(&mut self) -> Option<Self::Item> {
        let next_module = loop {
            let Some(last) = self.path.last_mut() else {
                break None;
            };
            if let Some((_, next_module)) = last.1.next() {
                break Some(next_module);
            }
            let (_, _) = self.path.pop().unwrap();
        };
        if let Some(next_module) = next_module {
            self.path.push((next_module, next_module.children.iter()));
            Some(next_module)
        } else {
            None
        }
    }
}

/// The `KtestModuleIter` will iterate over all tests in a crate. Yeilding `KtestItem`s.
pub struct KtestModuleIter<'a> {
    test_iter: core::slice::Iter<'a, KtestItem>,
}

impl KtestModule {
    pub fn iter(&self) -> KtestModuleIter<'_> {
        KtestModuleIter {
            test_iter: self.tests.iter(),
        }
    }
}

impl<'a> Iterator for KtestModuleIter<'a> {
    type Item = &'a KtestItem;

    fn next(&mut self) -> Option<Self::Item> {
        self.test_iter.next()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    macro_rules! gen_test_case {
        () => {{
            fn dummy_fn() {
                ()
            }
            let mut tree = KtestTree::new();
            let new = |m: &'static str, f: &'static str, p: &'static str| {
                KtestItem::new(
                    dummy_fn,
                    (false, None),
                    crate::KtestItemInfo {
                        module_path: m,
                        fn_name: f,
                        package: p,
                        source: "unrelated",
                        line: 0,
                        col: 0,
                    },
                )
            };
            tree.add_ktest(new("crate1::mod1::mod2", "test21", "crate1"));
            tree.add_ktest(new("crate1::mod1", "test11", "crate1"));
            tree.add_ktest(new("crate1::mod1::mod2", "test22", "crate1"));
            tree.add_ktest(new("crate1::mod1::mod2", "test23", "crate1"));
            tree.add_ktest(new("crate1::mod1::mod3", "test31", "crate1"));
            tree.add_ktest(new("crate1::mod1::mod3::mod4", "test41", "crate1"));
            tree.add_ktest(new("crate2::mod1::mod2", "test2", "crate2"));
            tree.add_ktest(new("crate2::mod1", "test1", "crate2"));
            tree.add_ktest(new("crate2::mod1::mod2", "test3", "crate2"));
            tree
        }};
    }

    #[test]
    fn test_tree_iter() {
        let tree = gen_test_case!();
        let mut iter = tree.iter();
        let c1 = iter.next().unwrap();
        assert_eq!(c1.name(), "crate1");
        let c2 = iter.next().unwrap();
        assert_eq!(c2.name(), "crate2");
        assert!(iter.next().is_none());
    }

    #[test]
    fn test_crate_iter() {
        let tree = gen_test_case!();
        for crate_ in tree.iter() {
            if crate_.name() == "crate1" {
                let mut len = 0;
                for module in crate_.iter() {
                    len += 1;
                    let modules = ["crate1", "mod1", "mod2", "mod3", "mod4"];
                    assert!(modules.contains(&module.name().as_str()));
                }
                assert_eq!(len, 5);
            } else if crate_.name() == "crate2" {
                let mut len = 0;
                for module in crate_.iter() {
                    len += 1;
                    let modules = ["crate2", "mod1", "mod2"];
                    assert!(modules.contains(&module.name().as_str()));
                }
                assert_eq!(len, 3);
            }
        }
    }

    #[test]
    fn test_module_iter() {
        let tree = gen_test_case!();
        let mut collection = Vec::<&KtestItem>::new();
        for crate_ in tree.iter() {
            for mov in crate_.iter() {
                let module = mov;
                for test in module.iter() {
                    collection.push(&test);
                }
            }
        }
        assert_eq!(collection.len(), 9);
        assert!(collection.iter().any(|t| t.info().fn_name == "test1"));
        assert!(collection.iter().any(|t| t.info().fn_name == "test2"));
        assert!(collection.iter().any(|t| t.info().fn_name == "test3"));
        assert!(collection.iter().any(|t| t.info().fn_name == "test11"));
        assert!(collection.iter().any(|t| t.info().fn_name == "test21"));
        assert!(collection.iter().any(|t| t.info().fn_name == "test22"));
        assert!(collection.iter().any(|t| t.info().fn_name == "test23"));
        assert!(collection.iter().any(|t| t.info().fn_name == "test31"));
        assert!(collection.iter().any(|t| t.info().fn_name == "test41"));
    }
}