1
use crate::{Document, LineNumber, Row, RowIndex};
2
use std::path::{Path, PathBuf};
3

            
4
#[test]
5
2
fn test_document_get_row() {
6
1
    let doc = Document::new(
7
1
        vec![Row::from("Hello"), Row::from("world!")],
8
1
        PathBuf::from("test.rs"),
9
    );
10
1
    assert_eq!(
11
1
        doc.get_row(RowIndex::new(0)).unwrap().string,
12
1
        "Hello".to_string()
13
    );
14
1
    assert_eq!(
15
1
        doc.get_row(RowIndex::new(1)).unwrap().string,
16
1
        "world!".to_string()
17
    );
18
1
    assert!(doc.get_row(RowIndex::new(2)).is_none());
19
2
}
20

            
21
#[test]
22
2
fn test_document_is_empty() {
23
1
    assert!(Document::new(vec![], PathBuf::from("test.rs")).is_empty());
24
1
    assert!(!Document::new(vec![Row::from("Hello")], PathBuf::from("test.rs")).is_empty());
25
2
}
26

            
27
#[test]
28
2
fn test_document_num_rows() {
29
1
    assert_eq!(
30
1
        Document::new(vec![], PathBuf::from("test.rs")).num_rows(),
31
        0
32
    );
33
1
    assert_eq!(
34
1
        Document::new(vec![Row::from("")], PathBuf::from("test.rs")).num_rows(),
35
        1
36
    );
37
2
}
38

            
39
#[test]
40
2
fn test_document_num_words() {
41
1
    assert_eq!(
42
1
        Document::new(
43
1
            vec![Row::from("Hello world"), Row::from("dear reviewer!")],
44
1
            PathBuf::from("test.rs")
45
        )
46
        .num_words(),
47
        4
48
    );
49
2
}
50

            
51
#[test]
52
2
fn test_document_row_for_line_number() {
53
1
    let row1 = Row::from("Hello world");
54
1
    let row2 = Row::from("dear reviewer!");
55
1
    assert_eq!(
56
2
        Document::new(vec![row1, row2], PathBuf::from("test.rs"))
57
1
            .row_for_line_number(LineNumber::new(1))
58
            .unwrap()
59
            .string,
60
        "Hello world"
61
    );
62
2
    assert!(Document::default()
63
1
        .row_for_line_number(LineNumber::new(1))
64
        .is_some());
65
2
    assert!(Document::default()
66
1
        .row_for_line_number(LineNumber::new(2))
67
        .is_none());
68
2
}
69

            
70
#[test]
71
2
fn test_document_last_line_number() {
72
1
    assert_eq!(
73
1
        Document::new(
74
1
            vec![Row::from("Hello world"), Row::from("dear reviewer!")],
75
1
            PathBuf::from("test.rs")
76
        )
77
        .last_line_number(),
78
1
        LineNumber::new(2)
79
    );
80
2
}
81

            
82
#[test]
83
2
fn test_document_insert() {
84
1
    let mut doc = Document::new(
85
1
        vec![Row::from("Hello"), Row::from("world!")],
86
1
        PathBuf::from("test.rs"),
87
    );
88
1
    doc.insert(' ', 6, RowIndex::new(1));
89
1
    assert_eq!(doc.rows.get(0).unwrap().string, "Hello");
90
1
    assert_eq!(doc.rows.get(1).unwrap().string, "world! ");
91
1
    doc.insert('W', 0, RowIndex::new(2));
92
1
    assert_eq!(doc.rows.get(2).unwrap().string, "W");
93
2
}
94

            
95
#[test]
96
2
fn test_document_insert_newline_at_the_end() {
97
1
    let mut doc = Document::new(
98
1
        vec![Row::from("Hello"), Row::from("world!")],
99
1
        PathBuf::from("test.rs"),
100
    );
101
1
    assert_eq!(doc.num_rows(), 2);
102
1
    doc.insert_newline(6, RowIndex::new(1));
103
1
    assert_eq!(doc.num_rows(), 3);
104
2
}
105

            
106
#[test]
107
2
fn test_document_delete() {
108
1
    let mut doc = Document::new(
109
1
        vec![Row::from("Hello"), Row::from("world!")],
110
1
        PathBuf::from("test.rs"),
111
    );
112
1
    doc.delete(5, 6, RowIndex::new(1));
113
1
    assert_eq!(doc.rows.get(0).unwrap().string, "Hello");
114
1
    assert_eq!(doc.rows.get(1).unwrap().string, "world");
115
1
    doc.delete(2, 6, RowIndex::new(1));
116
1
    assert_eq!(doc.rows.get(1).unwrap().string, "wold");
117
2
}
118

            
119
#[test]
120
2
fn test_document_delete_at_start_of_line() {
121
1
    let mut doc = Document::new(
122
1
        vec![Row::from("Hello"), Row::from("world!")],
123
1
        PathBuf::from("test.rs"),
124
    );
125
1
    doc.delete(0, 0, RowIndex::new(1));
126
1
    assert_eq!(doc.rows.get(0).unwrap().string, "Helloworld!");
127
1
    assert!(doc.rows.get(1).is_none());
128
2
}
129

            
130
#[test]
131
2
fn test_document_delete_all_rows() {
132
1
    let mut doc = Document::new(
133
1
        vec![Row::from("Hello"), Row::from("world!")],
134
1
        PathBuf::from("test.rs"),
135
    );
136
1
    doc.delete_row(RowIndex::new(1));
137
1
    doc.delete_row(RowIndex::new(0));
138
1
    assert_eq!(doc.get_row(RowIndex::new(0)).unwrap().string, "");
139
2
}
140

            
141
#[test]
142
2
fn test_insert_newline() {
143
1
    let mut doc = Document::new(
144
1
        vec![Row::from("Hello"), Row::from("world!")],
145
1
        PathBuf::from("test.rs"),
146
    );
147
1
    doc.insert_newline(0, RowIndex::new(0));
148
1
    assert_eq!(doc.rows.get(0).unwrap().string, "");
149
1
    assert_eq!(doc.rows.get(1).unwrap().string, "Hello");
150
1
    assert_eq!(doc.rows.get(2).unwrap().string, "world!");
151

            
152
1
    doc.insert_newline(0, RowIndex::new(2));
153
1
    assert_eq!(doc.rows.get(0).unwrap().string, "");
154
1
    assert_eq!(doc.rows.get(1).unwrap().string, "Hello");
155
1
    assert_eq!(doc.rows.get(2).unwrap().string, "");
156
1
    assert_eq!(doc.rows.get(3).unwrap().string, "world!");
157
2
}
158

            
159
#[test]
160
2
fn test_insert_newline_row_split() {
161
1
    let mut doc = Document::new(vec![Row::from("Hello world!")], PathBuf::from("test.rs"));
162
1
    doc.insert_newline(5, RowIndex::new(0));
163
1
    assert_eq!(doc.rows.get(0).unwrap().string, "Hello");
164
1
    assert_eq!(doc.rows.get(1).unwrap().string, " world!");
165
2
}
166

            
167
#[test]
168
2
fn test_document_swapfile() {
169
1
    assert_eq!(
170
1
        Document::swap_filename(Path::new("test.txt")),
171
1
        PathBuf::from(".test.txt.swp")
172
    );
173
1
    assert_eq!(
174
1
        Document::swap_filename(Path::new("/home/br/code/bo/test.txt")),
175
1
        PathBuf::from("/home/br/code/bo/.test.txt.swp")
176
    );
177
2
}
178

            
179
#[test]
180
2
fn test_document_trim_trailing_spaces() {
181
1
    let mut doc = Document::new(
182
1
        vec![Row::from("Hello world!    ")],
183
1
        PathBuf::from("test.rs"),
184
    );
185
1
    doc.trim_trailing_spaces();
186
1
    assert_eq!(doc.rows.get(0).unwrap().string, "Hello world!");
187
2
}
188

            
189
#[test]
190
2
fn test_document_join_row_with_previous_one() {
191
1
    let mut doc = Document::new(
192
1
        vec![Row::from("Hello"), Row::from("world!")],
193
1
        PathBuf::from("test.rs"),
194
    );
195
1
    doc.join_row_with_previous_one(4, RowIndex::new(1), Some(' '));
196
1
    assert_eq!(doc.rows.get(0).unwrap().string, "Hello world!");
197
1
    assert_eq!(doc.num_rows(), 1);
198
2
}
199

            
200
#[test]
201
2
fn test_document_insert_string() {
202
1
    let mut doc = Document::new(
203
1
        vec![Row::from("abcd"), Row::from("ef")],
204
1
        PathBuf::from("test.rs"),
205
    );
206
1
    doc.insert_string("Ä\ngh", 3, RowIndex::new(1));
207
1
    assert_eq!(doc.num_rows(), 3);
208
1
    assert_eq!(doc.rows.get(1).unwrap().string, "efÄ");
209
1
    assert_eq!(doc.rows.get(2).unwrap().string, "gh");
210
2
}
211

            
212
#[test]
213
2
fn test_document_delete_string() {
214
1
    let mut doc = Document::new(
215
1
        vec![Row::from("abcd"), Row::from("efÄ"), Row::from("gh")],
216
1
        PathBuf::from("test.rs"),
217
    );
218
1
    doc.delete_string("hg\nÄ", 1, RowIndex::new(2));
219
1
    assert_eq!(doc.num_rows(), 2);
220
1
    assert_eq!(doc.rows.get(0).unwrap().string, "abcd");
221
1
    assert_eq!(doc.rows.get(1).unwrap().string, "ef");
222
2
}