1
use crate::history::{Operation, OperationType};
2
use crate::Position;
3

            
4
#[test]
5
2
fn test_insert_operation_end_position() {
6
1
    let op = Operation {
7
1
        op_type: OperationType::Insert,
8
2
        content: String::from("Hello"),
9
1
        start_position: Position { x: 0, y: 0 },
10
    };
11
1
    assert_eq!(op.end_position(&[5]), Position { x: 4, y: 0 });
12

            
13
1
    let op_with_newline = Operation {
14
1
        op_type: OperationType::Insert,
15
1
        content: String::from("Hello\nWorld"),
16
1
        start_position: Position { x: 0, y: 0 },
17
    };
18
1
    assert_eq!(
19
1
        op_with_newline.end_position(&[5, 5]),
20
        Position { x: 4, y: 1 }
21
    );
22

            
23
1
    let op_starting_with_newline = Operation {
24
1
        op_type: OperationType::Insert,
25
1
        content: String::from("\nHello\nWorld"),
26
1
        start_position: Position { x: 0, y: 0 },
27
    };
28
1
    assert_eq!(
29
1
        op_starting_with_newline.end_position(&[0, 5, 5]),
30
        Position { x: 4, y: 2 }
31
    );
32

            
33
1
    let op_starting_with_newline_not_at_start_of_doc = Operation {
34
1
        op_type: OperationType::Insert,
35
1
        content: String::from("\nplop"),
36
1
        start_position: Position { x: 11, y: 0 },
37
    };
38
1
    assert_eq!(
39
1
        op_starting_with_newline_not_at_start_of_doc.end_position(&[0, 4]),
40
        Position { x: 3, y: 1 }
41
    );
42
2
}
43

            
44
#[test]
45
2
fn test_insert_operation_with_multiple_adjacent_newlines_end_position() {
46
1
    let op_starting_with_adjacent_newlines = Operation {
47
1
        op_type: OperationType::Insert,
48
2
        content: String::from("hello\n\n\nplop"),
49
1
        start_position: Position { x: 0, y: 0 },
50
    };
51
1
    assert_eq!(
52
1
        op_starting_with_adjacent_newlines.end_position(&[5, 0, 0, 4]),
53
        Position { x: 3, y: 3 }
54
    );
55
2
}
56

            
57
#[test]
58
2
fn test_delete_operation_end_position() {
59
1
    let op = Operation {
60
1
        op_type: OperationType::Delete,
61
2
        content: String::from("olleH"),
62
1
        start_position: Position { x: 4, y: 0 },
63
    };
64
1
    assert_eq!(op.end_position(&[5]), Position { x: 0, y: 0 });
65

            
66
1
    let op_with_newline = Operation {
67
1
        op_type: OperationType::Delete,
68
1
        content: String::from("dlrow\nolleH"),
69
1
        start_position: Position { x: 4, y: 1 },
70
    };
71
1
    assert_eq!(
72
1
        op_with_newline.end_position(&[5, 5]),
73
        Position { x: 0, y: 0 }
74
    );
75
2
}
76

            
77
#[test]
78
2
fn test_operation_reversed() {
79
1
    let op = Operation {
80
1
        op_type: OperationType::Insert,
81
2
        content: String::from("Hello\n"),
82
1
        start_position: Position { x: 0, y: 0 },
83
    };
84
1
    let op_rev = op.reversed(&[5]);
85
1
    assert_eq!(
86
        op_rev,
87
1
        Operation {
88
1
            op_type: OperationType::Delete,
89
1
            content: String::from("\nolleH"),
90
1
            start_position: Position { x: 0, y: 1 },
91
        }
92
    );
93
1
    let op_rev_rev = op_rev.reversed(&[5, 0]);
94
1
    assert_eq!(op_rev_rev, op);
95
2
}
96

            
97
#[test]
98
2
fn test_operation_end_position_insert_single_line() {
99
1
    let op = Operation {
100
1
        op_type: OperationType::Insert,
101
2
        content: String::from("rûst"),
102
1
        start_position: Position { x: 0, y: 0 },
103
    };
104
1
    assert_eq!(op.end_position(&[4]), Position { x: 3, y: 0 });
105
2
}
106

            
107
#[test]
108
2
fn test_operation_end_position_insert_multi_line() {
109
1
    let op = Operation {
110
1
        op_type: OperationType::Insert,
111
2
        content: String::from("rûst\nröcks"),
112
1
        start_position: Position { x: 0, y: 0 },
113
    };
114
1
    assert_eq!(op.end_position(&[4, 5]), Position { x: 4, y: 1 });
115
2
}
116
#[test]
117
2
fn test_operation_end_position_insert_multiple_words() {
118
1
    let op = Operation {
119
1
        op_type: OperationType::Insert,
120
2
        content: String::from("rûst röcks"),
121
1
        start_position: Position { x: 0, y: 0 },
122
    };
123
1
    assert_eq!(op.end_position(&[10]), Position { x: 9, y: 0 });
124
2
}
125

            
126
#[test]
127
2
fn test_operation_end_position_insert_multi_line_starting_with_newline() {
128
1
    let op = Operation {
129
1
        op_type: OperationType::Insert,
130
2
        content: String::from("\nrûst\nröcks"),
131
1
        start_position: Position { x: 0, y: 0 },
132
    };
133
1
    assert_eq!(op.end_position(&[0, 4, 5]), Position { x: 4, y: 2 });
134
2
}
135

            
136
#[test]
137
2
fn test_operation_end_position_delete_single_line() {
138
1
    let op = Operation {
139
1
        op_type: OperationType::Delete,
140
2
        content: String::from("tsûr"),
141
1
        start_position: Position { x: 3, y: 0 },
142
    };
143
1
    assert_eq!(op.end_position(&[4]), Position { x: 0, y: 0 });
144
2
}
145

            
146
#[test]
147
2
fn test_operation_end_position_delete_multiple_words() {
148
1
    let op = Operation {
149
1
        op_type: OperationType::Delete,
150
2
        content: String::from("skcör tsûr"),
151
1
        start_position: Position { x: 9, y: 0 },
152
    };
153
1
    assert_eq!(op.end_position(&[10]), Position { x: 0, y: 0 });
154
2
}
155

            
156
#[test]
157
2
fn test_operation_end_position_delete_multi_line() {
158
1
    let op = Operation {
159
1
        op_type: OperationType::Delete,
160
2
        content: String::from("skcör\ntsur"),
161
1
        start_position: Position { x: 4, y: 1 },
162
    };
163
1
    assert_eq!(op.end_position(&[4, 5]), Position { x: 0, y: 0 });
164
2
}
165

            
166
#[test]
167
2
fn test_operation_end_position_delete_multi_line_starting_with_newline() {
168
1
    let op = Operation {
169
1
        op_type: OperationType::Delete,
170
2
        content: String::from("\nskcör\ntsur"),
171
1
        start_position: Position { x: 0, y: 2 },
172
    };
173
1
    assert_eq!(op.end_position(&[4, 5, 0]), Position { x: 0, y: 0 });
174
2
}