difference/
display.rs

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


use super::{Changeset, Difference};
use std::fmt;

impl fmt::Display for Changeset {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        for d in &self.diffs {
            match *d {
                Difference::Same(ref x) => {
                    try!(write!(f, "{}{}", x, self.split));
                }
                Difference::Add(ref x) => {
                    try!(write!(f, "\x1b[92m{}\x1b[0m{}", x, self.split));
                }
                Difference::Rem(ref x) => {
                    try!(write!(f, "\x1b[91m{}\x1b[0m{}", x, self.split));
                }
            }
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::super::Changeset;
    use std::io::Write;
    use std::iter::FromIterator;
    use std::thread;
    use std::time;

    /// convert slice to vector for assert_eq
    fn vb(b: &'static [u8]) -> Vec<u8> {
        Vec::from_iter(b.iter().cloned())
    }

    /// if the format changes, you can use this to help create the test for color
    /// just pass it in and copy-paste (validating that it looks right first of course...)
    #[allow(dead_code)]
    fn debug_bytes(result: &[u8], expected: &[u8]) {
        // sleep for a bit so stderr passes us
        thread::sleep(time::Duration::new(0, 2e8 as u32));
        println!("Debug Result:");
        for b in result {
            print!("{}", *b as char);
        }
        println!("Repr Result:");
        repr_bytes(result);
        println!("");
        println!("--Result Repr DONE");

        println!("Debug Expected:");
        for b in expected {
            print!("{}", *b as char);
        }
        println!("Repr Expected:");
        repr_bytes(expected);
        println!("");
        println!("--Expected Repr DONE");
    }

    /// for helping debugging what the actual bytes are
    /// for writing user tests
    fn repr_bytes(bytes: &[u8]) {
        for b in bytes {
            match *b {
                // 9 => print!("{}", *b as char), // TAB
                b'\n' => print!("\\n"),
                b'\r' => print!("\\r"),
                32...126 => print!("{}", *b as char), // visible ASCII
                _ => print!(r"\x{:0>2x}", b),

            }
        }
    }

    #[test]
    fn test_display() {
        let text1 = "Roses are red, violets are blue,\n\
                     I wrote this library,\n\
                     just for you.\n\
                     (It's true).";

        let text2 = "Roses are red, violets are blue,\n\
                     I wrote this documentation,\n\
                     just for you.\n\
                     (It's quite true).";
        let expected = b"Roses are red, violets are blue,\n\x1b[91mI wrote this library,\x1b\
            [0m\n\x1b[92mI wrote this documentation,\x1b[0m\njust for you.\n\x1b\
            [91m(It's true).\x1b[0m\n\x1b[92m(It's quite true).\x1b[0m\n";

        let ch = Changeset::new(text1, text2, "\n");
        let mut result: Vec<u8> = Vec::new();
        write!(result, "{}", ch).unwrap();
        debug_bytes(&result, expected);
        assert_eq!(result, vb(expected));

    }
}