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
//! A collection of [`Criterion`](crate::criterion::Criterion)
//!
//! This is basically a fancy `Vec<Criterion>`. It implements `FromIterator` so you
//! can `collect()` an iterator of criterions into criteria.
//!
//! ## Example
//! ```rust
//! use lab_grader::*;
//!
//! let criteria = Criteria::from(vec! [
//!     Criterion::new("test 1", 15, ("p", "f"), Box::new(|_: &TestData| true)),
//!     Criterion::new("test 2", 10, ("p", "f"), Box::new(|_: &TestData| false)),
//! ]);
//!
//! assert!(criteria.len() == 2);
//! ```
//! **Or**
//! ```rust
//! // same as above..
//! # use lab_grader::*;
//! #
//! # let loose = vec! [
//! #     Criterion::new("test 1", 15, ("p", "f"), Box::new(|_: &TestData| true)),
//! #     Criterion::new("test 2", 10, ("p", "f"), Box::new(|_: &TestData| false)),
//! # ];
//!
//! let criteria: Criteria = loose.into_iter().collect();
//! assert!(criteria.len() == 2);
//! ```

use std::fmt;
use std::process::exit;
use std::iter::FromIterator;

use crate::{Criterion, TestData};

/// The Criteria struct, just a collection of [`Criterion`](crate::criterion::Criterion)
pub struct Criteria(pub Vec<Criterion>);


impl Criteria {
    // Creates a new empty criteria
    fn new() -> Criteria {
        Criteria(Vec::new())
    }

    /// Add a `Criterion` to the collection
    pub fn add(&mut self, criterion: Criterion) {
        self.0.push(criterion);
    }

    /// Gets `Some(Criterion)` that has the given stub. Returns
    /// `None` if there isn't one at that index
    ///
    /// ## Example
    /// ```rust
    /// # use lab_grader::{Criteria, Criterion, TestData};
    /// #
    /// let mut crit = Criterion::new("test criterion", 1, ("passed", "failed"), Box::new(|_: &TestData| true));
    /// crit.stub = String::from("test-crit-1");
    /// let mut criteria = Criteria::from(vec![crit]);
    ///
    /// assert!(criteria.get("test-crit-1").is_some());
    /// assert!(criteria.get("doesnt-exist").is_none());
    /// ```
    pub fn get(&mut self, stub: &str) -> Option<&mut Criterion> {
        self.0.iter_mut().find(|c| c.stub == stub )
    }

    pub fn attach(&mut self, stub: &str, func: Box<dyn Fn(&TestData) -> bool>) {
        match self.get(stub) {
            Some(crit) => crit.attach(func),
            None => {
                eprintln!("Couldn't find criterion with stub {}", stub);
                exit(1);
            }
        }
    }

    /// Creates a `Criteria` collection from a `Vec<Criterion>`
    ///
    /// ## Example
    /// ```rust
    /// use lab_grader::*;
    ///
    /// let critiera = Criteria::from(vec![
    ///     Criterion::new("name", 1, ("p", "f"), Box::new(|_: &TestData| true))
    /// ]);
    /// ```
    pub fn from(criteria: Vec<Criterion>) -> Self {
        criteria.into_iter().collect()
    }

    /// Returns the amount of `Criterion`s in this collection
    pub fn len(&self) -> usize {
        self.0.len()
    }

    /// Returns the total points value of all the criteria, ie.
    /// the maximum score possible for all criteria
    ///
    /// This is *not* a grade, but the maximum possible grade.
    /// Only a [`Submission`](crate::submission::Submission) holds a grade.
    pub fn total_points(&self) -> usize {
        let mut total: usize = 0;
        for crit in &self.0 {
            total += crit.worth as usize;
        }
        total
    }

    /// Returns the amount of points earned
    ///
    /// If you haven't graded a submission against these criteria,
    /// then this will return 0.
    pub fn points(&self) -> usize {
        let mut total: usize = 0;
        for crit in &self.0 {
            if let Some(status) = crit.status {
                if status {
                    // Only add to the total if they've graded
                    // and this criterion passed
                    total += crit.worth as usize;
                }
            }
        }
        total
    }
}

impl FromIterator<Criterion> for Criteria {
    fn from_iter<I: IntoIterator<Item=Criterion>>(iter: I) -> Self {
        let mut criteria = Criteria::new();

        for i in iter {
            criteria.add(i);
        }

        criteria
    }
}


impl fmt::Display for Criteria {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        for crit in &self.0 {
            writeln!(f, "{}", crit).unwrap();
        }
        write!(f, "Grade: {}/{}", self.points(), self.total_points()).unwrap();
        write!(f, "")
    }
}





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

    #[test]
    fn test_build_criteria() {
        let loose = vec![
            Criterion::new("test 1", 1, ("p", "f"), Box::new(|_: &TestData| true)),
            Criterion::new("test 2", 1, ("p", "f"), Box::new(|_: &TestData| true)),
        ].into_iter();
        let criteria: Criteria = loose.collect();
        assert!(criteria.0.len() == 2);
    }

    #[test]
    fn test_build_from_vec() {
        let criteria = Criteria::from(vec![
            Criterion::new("test 1", 1, ("p", "f"), Box::new(|_: &TestData| true)),
            Criterion::new("test 2", 1, ("p", "f"), Box::new(|_: &TestData| true)),
        ]);
        assert!(criteria.0.len() == 2);
    }

    #[test]
    fn test_len() {
        let criteria = Criteria::from(vec![
            Criterion::new("test 1", 1, ("p", "f"), Box::new(|_: &TestData| true)),
            Criterion::new("test 2", 1, ("p", "f"), Box::new(|_: &TestData| true)),
        ]);
        assert!(criteria.len() == 2);
        assert!(criteria.0.len() == criteria.len());
    }

    #[test]
    fn test_add_criterion() {
        let mut criteria = Criteria::from(vec![
            Criterion::new("test 1", 1, ("p", "f"), Box::new(|_: &TestData| true)),
            Criterion::new("test 2", 1, ("p", "f"), Box::new(|_: &TestData| true)),
        ]);

        assert!(criteria.len() == 2);

        criteria.add(Criterion::new(
            "test 3", 1, ("p", "f"), Box::new(|_: &TestData| false)
        ));

        assert!(criteria.len() == 3);
    }

    #[test]
    fn test_total_points() {
        let criteria = Criteria::from(vec![
            Criterion::new("test 1", 10, ("p", "f"), Box::new(|_: &TestData| true)),
            Criterion::new("test 2", 25, ("p", "f"), Box::new(|_: &TestData| true)),
        ]);
        assert!(criteria.total_points() == 35);
    }

    #[test]
    fn test_get_criterion() {
        let expected = "test 1";
        let mut crit1 = Criterion::new("test 1", 10, ("p", "f"), Box::new(|_: &TestData| true));
        crit1.stub = String::from("test1");
        let mut crit2 = Criterion::new("test 2", 25, ("p", "f"), Box::new(|_: &TestData| true));
        crit2.stub = String::from("test2");
        let mut criteria = Criteria::from(vec![crit1, crit2]);
        if let Some(found) = criteria.get("test1") {
            assert_eq!(found.name, expected);
        }
    }
}