tmc_langs_csharp/
policy.rs

1//! Student file policy for the C# plugin.
2
3use std::{ffi::OsStr, path::Path};
4use tmc_langs_framework::{StudentFilePolicy, TmcProjectYml};
5
6pub struct CSharpStudentFilePolicy {
7    project_config: TmcProjectYml,
8}
9
10impl StudentFilePolicy for CSharpStudentFilePolicy {
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    // .cs files in src
23    fn is_non_extra_student_file(&self, path: &Path) -> bool {
24        path.starts_with("src") && path.extension() == Some(OsStr::new("cs"))
25    }
26}
27
28#[cfg(test)]
29mod test {
30    use super::*;
31
32    #[test]
33    fn file_in_binary_dir_is_not_student_file() {
34        let policy = CSharpStudentFilePolicy::new(Path::new(".")).unwrap();
35        assert!(!policy.is_student_file(Path::new("src/bin/any/file")));
36        assert!(!policy.is_student_file(Path::new("obj/any/src/file.cs")));
37    }
38
39    #[test]
40    fn cs_file_in_src_is_student_file() {
41        let policy = CSharpStudentFilePolicy::new(Path::new(".")).unwrap();
42        assert!(policy.is_student_file(Path::new("src/file.cs")));
43        assert!(policy.is_student_file(Path::new("src/any/file.cs")));
44    }
45
46    #[test]
47    fn csproj_is_not_student_file() {
48        let policy = CSharpStudentFilePolicy::new(Path::new(".")).unwrap();
49        assert!(!policy.is_student_file(Path::new("src/Project.csproj")));
50    }
51}