data.rs

2.0 kB · rust · 75 lines

1use mrlycore::data::{shuffle, Well};2use mrlycore::{json, Json};34/// Returns the wells this crate pours: the glyph enumeration alone.5pub fn wells() -> Vec<Box<dyn Well>> {6    vec![Box::new(Glyphs)]7}89struct Glyphs;1011impl Well for Glyphs {12    fn name(&self) -> &str {13        "glyphs"14    }15    fn about(&self) -> &str {16        "Every supported glyph with its Unicode name, bitmap rows and descender flag."17    }18    fn pour(&self, seed: u64, count: usize) -> Vec<Json> {19        let mut rows: Vec<Json> = crate::all()20            .iter()21            .map(|glyph| {22                json!({23                    "char": glyph.char.to_string(),24                    "name": crate::name_of(glyph.char),25                    "rows": glyph.rows.clone(),26                    "descends": crate::descends(glyph.char),27                })28            })29            .collect();30        shuffle(&mut rows, seed);31        rows.truncate(count);32        rows33    }34}3536#[cfg(test)]37mod tests {38    use super::*;3940    #[test]41    fn a_full_pour_covers_the_font() {42        let rows = Glyphs.pour(7, 999);43        assert_eq!(rows.len(), crate::supported().len());44        let mut chars: Vec<String> = rows45            .iter()46            .map(|row| row["char"].as_str().unwrap().to_string())47            .collect();48        chars.sort();49        chars.dedup();50        assert_eq!(chars.len(), rows.len());51    }5253    #[test]54    fn pours_replay_and_prefix() {55        let a = Glyphs.pour(3, 5);56        let b = Glyphs.pour(3, 5);57        assert_eq!(a, b);58        assert_eq!(a[..], Glyphs.pour(3, 9)[..5]);59    }6061    #[test]62    fn the_seed_deals_the_order() {63        assert_ne!(Glyphs.pour(1, 108), Glyphs.pour(2, 108));64    }6566    #[test]67    fn rows_carry_the_bitmap_shape() {68        for row in Glyphs.pour(0, 4) {69            let rows = row["rows"].as_array().unwrap();70            assert!(rows.len() >= 5);71            assert!(row["name"].as_str().is_some());72            assert!(row["descends"].as_bool().is_some());73        }74    }75}