difference/
display.rs

1
2
3use super::{Changeset, Difference};
4use std::fmt;
5
6impl fmt::Display for Changeset {
7    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
8        for d in &self.diffs {
9            match *d {
10                Difference::Same(ref x) => {
11                    try!(write!(f, "{}{}", x, self.split));
12                }
13                Difference::Add(ref x) => {
14                    try!(write!(f, "\x1b[92m{}\x1b[0m{}", x, self.split));
15                }
16                Difference::Rem(ref x) => {
17                    try!(write!(f, "\x1b[91m{}\x1b[0m{}", x, self.split));
18                }
19            }
20        }
21        Ok(())
22    }
23}
24
25#[cfg(test)]
26mod tests {
27    use super::super::Changeset;
28    use std::io::Write;
29    use std::iter::FromIterator;
30    use std::thread;
31    use std::time;
32
33    /// convert slice to vector for assert_eq
34    fn vb(b: &'static [u8]) -> Vec<u8> {
35        Vec::from_iter(b.iter().cloned())
36    }
37
38    /// if the format changes, you can use this to help create the test for color
39    /// just pass it in and copy-paste (validating that it looks right first of course...)
40    #[allow(dead_code)]
41    fn debug_bytes(result: &[u8], expected: &[u8]) {
42        // sleep for a bit so stderr passes us
43        thread::sleep(time::Duration::new(0, 2e8 as u32));
44        println!("Debug Result:");
45        for b in result {
46            print!("{}", *b as char);
47        }
48        println!("Repr Result:");
49        repr_bytes(result);
50        println!("");
51        println!("--Result Repr DONE");
52
53        println!("Debug Expected:");
54        for b in expected {
55            print!("{}", *b as char);
56        }
57        println!("Repr Expected:");
58        repr_bytes(expected);
59        println!("");
60        println!("--Expected Repr DONE");
61    }
62
63    /// for helping debugging what the actual bytes are
64    /// for writing user tests
65    fn repr_bytes(bytes: &[u8]) {
66        for b in bytes {
67            match *b {
68                // 9 => print!("{}", *b as char), // TAB
69                b'\n' => print!("\\n"),
70                b'\r' => print!("\\r"),
71                32...126 => print!("{}", *b as char), // visible ASCII
72                _ => print!(r"\x{:0>2x}", b),
73
74            }
75        }
76    }
77
78    #[test]
79    fn test_display() {
80        let text1 = "Roses are red, violets are blue,\n\
81                     I wrote this library,\n\
82                     just for you.\n\
83                     (It's true).";
84
85        let text2 = "Roses are red, violets are blue,\n\
86                     I wrote this documentation,\n\
87                     just for you.\n\
88                     (It's quite true).";
89        let expected = b"Roses are red, violets are blue,\n\x1b[91mI wrote this library,\x1b\
90            [0m\n\x1b[92mI wrote this documentation,\x1b[0m\njust for you.\n\x1b\
91            [91m(It's true).\x1b[0m\n\x1b[92m(It's quite true).\x1b[0m\n";
92
93        let ch = Changeset::new(text1, text2, "\n");
94        let mut result: Vec<u8> = Vec::new();
95        write!(result, "{}", ch).unwrap();
96        debug_bytes(&result, expected);
97        assert_eq!(result, vb(expected));
98
99    }
100}