aboutsummaryrefslogtreecommitdiffstats
path: root/src/tests.rs
blob: 708121ea038fd7fbe25ba05a51f4322d0420c224 (plain)
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
use std::fs::{self, File, OpenOptions};
use std::io::{Read, Write};

use alias::{Alias, AliasSearchError};

#[test]
fn new_alias_with_only_email() {
    assert_eq!(
        "alias producer@765pro.ductions producer@765pro.ductions",
        Alias::new("From: producer@765pro.ductions").to_string()
    );
}

#[test]
fn new_alias_with_1_name() {
    assert_eq!(
        "alias decim Decim <decim@quindecim.jp>",
        Alias::new("From: Decim <decim@quindecim.jp>").to_string()
    );
}

#[test]
fn new_alias_with_2_names() {
    assert_eq!(
        "alias farnsworth-hubert Hubert Farnsworth <professor@planetexpress.com>",
        Alias::new("From: Hubert Farnsworth <professor@planetexpress.com>").to_string()
    );
}

#[test]
fn new_alias_with_3_names() {
    assert_eq!(
        "alias lab-harvard Harvard Innovation Lab <noreply@eventbrite.com>",
        Alias::new("From: Harvard Innovation Lab <noreply@eventbrite.com>").to_string()
    );
}

#[test]
fn new_alias_with_special_characters() {
    assert_eq!(
        "alias celty-ostrulson \"O'Strulson, Celty\" <celty@dollars.co>",
        Alias::new("From: \"O'Strulson, Celty\" <celty@dollars.co>").to_string()
    );
}


#[test]
fn alias_find_in_file_email_already_exists() {
    let alias = Alias {
        alias: "farnsworth-hubert".to_owned(),
        name: "Hubert Farnsworth".to_owned(),
        email: "<professor@planetexpress.com>".to_owned()
    };

    assert_eq!(
        Err(AliasSearchError::EmailExists),
        alias.find_in_file("./testdata/aliases")
    );
}

#[test]
fn alias_find_in_file_alias_is_new() {
    let alias = Alias {
        alias: "fry-philip".to_owned(),
        name: "Philip Fry".to_owned(),
        email: "<fry@planetexpress.com>".to_owned()
    };

    assert_eq!(
        Err(AliasSearchError::NotFound),
        alias.find_in_file("./testdata/aliases")
    );
}

#[test]
fn alias_find_in_file_finds_a_match() {
    let alias = Alias {
        alias: "farnsworth-hubert".to_owned(),
        name: "Hubert Farnsworth".to_owned(),
        email: "<goodnewseveryone@planetexpress.com>".to_owned()
    };

    assert_eq!(
        Ok(vec![
            "farnsworth-hubert".to_owned(),
            "farnsworth-hubert-2".to_owned()
        ]),
        alias.find_in_file("./testdata/aliases")
    );
}


const UPDATE_ALIAS_ID_ALIAS_IDENTIFIER: &'static str = "hooves-derpy";

fn update_alias_id_sample_alias() -> Alias {
    Alias {
        alias: UPDATE_ALIAS_ID_ALIAS_IDENTIFIER.to_owned(),
        name: "Derpy Hooves".to_owned(),
        email: "derpyhooves@postmaster.pv".to_owned()
    }
}

#[test]
fn update_alias_id_does_nothing_given_an_empty_vector() {
    let mut alias = update_alias_id_sample_alias();
    alias.update_alias_id(vec![]);

    assert_eq!(UPDATE_ALIAS_ID_ALIAS_IDENTIFIER, &alias.alias);
}

#[test]
fn update_alias_id_increments_alias() {
    let mut alias = update_alias_id_sample_alias();
    alias.update_alias_id(vec![
        "hooves-derpy".to_owned(),
        "hooves-derpy-2".to_owned(),
        "hooves-derpy-3".to_owned(),
        "hooves-derpy-4".to_owned()
    ]);

    assert_eq!("hooves-derpy-5", &alias.alias);
}


#[test]
fn alias_write_to_file_must_write_given_alias_to_file() {
    let mut alias = update_alias_id_sample_alias();

    // Create a new test file
    let test_file = "./testdata/write_to_file";
    fs::copy("./testdata/aliases", test_file).expect("Alias file copy failed");

    // Write a duplicate alias so that `write_to_file` is able to append a
    // new one
    let mut f = OpenOptions::new().append(true).open(test_file)
        .expect("Failed to open test file for appending");
    writeln!(f, "{}", Alias { email: "derpy@home.pv".to_owned(), .. alias.clone() }
            .to_string())
        .expect("Failed to append matching alias");

    // Write our new alias to the file
    alias.write_to_file(test_file).expect("`write_to_file` failed");

    // Get the file's contents for testing
    let mut f = File::open(test_file).expect("Failed to open test file");
    let mut file_contents = String::new();
    f.read_to_string(&mut file_contents).expect("Failed to read test file contents");
    let file_contents: Vec<&str> = file_contents.split('\n').collect();
    fs::remove_file(test_file).expect("Failed to delete test file");

    assert_eq!(alias.to_string(), file_contents[file_contents.len() - 2]);
}