tmc_langs_r/
policy.rs

1//! Contains the R student file policy
2
3use std::{ffi::OsStr, path::Path};
4use tmc_langs_framework::{StudentFilePolicy, TmcProjectYml};
5
6pub struct RStudentFilePolicy {
7    project_config: TmcProjectYml,
8}
9
10impl StudentFilePolicy for RStudentFilePolicy {
11    fn new_with_project_config(project_config: TmcProjectYml) -> Self
12    where
13        Self: Sized,
14    {
15        Self { project_config }
16    }
17
18    fn get_project_config(&self) -> &TmcProjectYml {
19        &self.project_config
20    }
21
22    fn is_non_extra_student_file(&self, path: &Path) -> bool {
23        path.starts_with("R") && path.extension() == Some(OsStr::new("R"))
24    }
25}
26
27#[cfg(test)]
28mod test {
29    use super::*;
30
31    fn init() {
32        use log::*;
33        use simple_logger::*;
34        let _ = SimpleLogger::new().with_level(LevelFilter::Debug).init();
35    }
36
37    #[test]
38    fn is_student_file() {
39        init();
40
41        let policy = RStudentFilePolicy::new(Path::new(".")).unwrap();
42        assert!(policy.is_student_file(Path::new("R/file.R")));
43    }
44
45    #[test]
46    fn is_not_student_source_file() {
47        init();
48
49        let policy = RStudentFilePolicy::new(Path::new(".")).unwrap();
50        assert!(!policy.is_student_file(Path::new("a.R")));
51        assert!(!policy.is_student_file(Path::new("dir/R")));
52        assert!(!policy.is_student_file(Path::new("dir/R/file")));
53    }
54}