Debian Codegraph @

debian/rust-vendor/diff/tests/tests.rs

raw
1 extern crate diff;
2 extern crate quickcheck;
3 extern crate speculate;
4
5 use diff::Result::*;
6 use speculate::speculate;
7
8 pub fn undiff<T: Clone>(diff: &[::diff::Result<&T>]) -> (Vec<T>, Vec<T>) {
9 let (mut left, mut right) = (vec![], vec![]);
10 for d in diff {
11 match *d {
12 Left(l) => left.push(l.clone()),
13 Both(l, r) => {
14 left.push(l.clone());
15 right.push(r.clone());
16 }
17 Right(r) => right.push(r.clone()),
18 }
19 }
20 (left, right)
21 }
22
23 pub fn undiff_str<'a>(diff: &[::diff::Result<&'a str>]) -> (Vec<&'a str>, Vec<&'a str>) {
24 let (mut left, mut right) = (vec![], vec![]);
25 for d in diff {
26 match *d {
27 Left(l) => left.push(l),
28 Both(l, r) => {
29 left.push(l);
30 right.push(r);
31 }
32 Right(r) => right.push(r),
33 }
34 }
35 (left, right)
36 }
37
38 pub fn undiff_lines<'a>(diff: &[::diff::Result<&'a str>]) -> (String, String) {
39 let (left, right) = undiff_str(diff);
40 (left.join("\n"), right.join("\n"))
41 }
42
43 pub fn undiff_chars(diff: &[::diff::Result<char>]) -> (String, String) {
44 let (mut left, mut right) = (vec![], vec![]);
45 for d in diff {
46 match *d {
47 Left(l) => left.push(l),
48 Both(l, r) => {
49 left.push(l);
50 right.push(r);
51 }
52 Right(r) => right.push(r),
53 }
54 }
55 (
56 left.iter().cloned().collect(),
57 right.iter().cloned().collect(),
58 )
59 }
60
61 speculate! {
62 describe "slice" {
63 fn go<T>(left: &[T], right: &[T], len: usize) where
64 T: Clone + ::std::fmt::Debug + PartialEq
65 {
66 let diff = ::diff::slice(left, right);
67 assert_eq!(diff.len(), len);
68 let (left_, right_) = undiff(&diff);
69 assert_eq!(left, &left_[..]);
70 assert_eq!(right, &right_[..]);
71 }
72
73 test "empty slices" {
74 let slice: &[()] = &[];
75 go(slice, slice, 0);
76 }
77
78 test "equal + non-empty slices" {
79 let slice = [1, 2, 3];
80 go(&slice, &slice, 3);
81 }
82
83 test "left empty, right non-empty" {
84 let slice = [1, 2, 3];
85 go(&slice, &[], 3);
86 }
87
88 test "left non-empty, right empty" {
89 let slice = [1, 2, 3];
90 go(&[], &slice, 3);
91 }
92
93 test "misc 1" {
94 let left = [1, 2, 3, 4, 1, 3];
95 let right = [1, 4, 1, 1];
96 go(&left, &right, 7);
97 }
98
99 test "misc 2" {
100 let left = [1, 2, 1, 2, 3, 2, 2, 3, 1, 3];
101 let right = [3, 3, 1, 2, 3, 1, 2, 3, 4, 1];
102 go(&left, &right, 14);
103 }
104
105 test "misc 3" {
106 let left = [1, 3, 4];
107 let right = [2, 3, 4];
108 go(&left, &right, 4);
109 }
110
111 test "quickcheck" {
112 fn prop(left: Vec<i32>, right: Vec<i32>) -> bool {
113 let diff = ::diff::slice(&left, &right);
114 let (left_, right_) = undiff(&diff);
115 left == left_[..] && right == right_[..]
116 }
117
118 ::quickcheck::quickcheck(prop as fn(Vec<i32>, Vec<i32>) -> bool);
119 }
120 }
121
122 describe "lines" {
123 fn go(left: &str, right: &str, len: usize) {
124 let diff = ::diff::lines(left, right);
125 assert_eq!(diff.len(), len);
126 let (left_, right_) = undiff_str(&diff);
127 assert_eq!(left, left_.join("\n"));
128 assert_eq!(right, right_.join("\n"));
129 }
130
131 test "both empty" {
132 go("", "", 0);
133 }
134
135 test "one empty" {
136 go("foo", "", 1);
137 go("", "foo", 1);
138 }
139
140 test "both equal and non-empty" {
141 go("foo\nbar", "foo\nbar", 2);
142 }
143
144 test "misc 1" {
145 go("foo\nbar\nbaz", "foo\nbaz\nquux", 4);
146 }
147
148 test "#10" {
149 go("a\nb\nc", "a\nb\nc\n", 4);
150 go("a\nb\nc\n", "a\nb\nc", 4);
151 let left = "a\nb\n\nc\n\n\n";
152 let right = "a\n\n\nc\n\n";
153 go(left, right, 8);
154 go(right, left, 8);
155 }
156 }
157
158 describe "chars" {
159 fn go(left: &str, right: &str, len: usize) {
160 let diff = ::diff::chars(left, right);
161 assert_eq!(diff.len(), len);
162 let (left_, right_) = undiff_chars(&diff);
163 assert_eq!(left, left_);
164 assert_eq!(right, right_);
165 }
166
167 test "both empty" {
168 go("", "", 0);
169 }
170
171 test "one empty" {
172 go("foo", "", 3);
173 go("", "foo", 3);
174 }
175
176 test "both equal and non-empty" {
177 go("foo bar", "foo bar", 7);
178 }
179
180 test "misc 1" {
181 go("foo bar baz", "foo baz quux", 16);
182 }
183 }
184
185 describe "issues" {
186 test "#4" {
187 assert_eq!(::diff::slice(&[1], &[2]), vec![Left(&1),
188 Right(&2)]);
189 assert_eq!(::diff::lines("a", "b"), vec![Left("a"),
190 Right("b")]);
191 }
192
193 test "#6" {
194 // This produced an overflow in the lines computation because it
195 // was not accounting for the fact that the "right" length was
196 // less than the "left" length.
197 let expected = r#"
198 BacktraceNode {
199 parents: [
200 BacktraceNode {
201 parents: []
202 },
203 BacktraceNode {
204 parents: [
205 BacktraceNode {
206 parents: []
207 }
208 ]
209 }
210 ]
211 }"#;
212 let actual = r#"
213 BacktraceNode {
214 parents: [
215 BacktraceNode {
216 parents: []
217 },
218 BacktraceNode {
219 parents: [
220 BacktraceNode {
221 parents: []
222 },
223 BacktraceNode {
224 parents: []
225 }
226 ]
227 }
228 ]
229 }"#;
230 ::diff::lines(actual, expected);
231 }
232 }
233 }
234
235 #[test]
236 fn gitignores() {
237 let all = std::fs::read_to_string("tests/data/gitignores.txt")
238 .unwrap()
239 .split("!!!")
240 .map(|str| str.trim())
241 .filter(|str| !str.is_empty())
242 .map(|str| str.replace("\r", ""))
243 .collect::<Vec<String>>();
244
245 go(
246 &all,
247 ::diff::lines,
248 undiff_lines,
249 |d| match d {
250 Left(l) => format!("-{}\n", l),
251 Right(r) => format!("+{}\n", r),
252 Both(l, _) => format!(" {}\n", l),
253 },
254 "tests/data/gitignores.lines.diff",
255 );
256
257 go(
258 &all,
259 ::diff::chars,
260 undiff_chars,
261 |d| match d {
262 Left(l) => format!("[-{}-]", l),
263 Right(r) => format!("{{+{}+}}", r),
264 Both(l, _) => format!("{}", l),
265 },
266 "tests/data/gitignores.chars.diff",
267 );
268
269 fn go<'a, T, Diff, Undiff, ToString>(
270 all: &'a [String],
271 diff: Diff,
272 undiff: Undiff,
273 to_string: ToString,
274 path: &str,
275 ) where
276 Diff: Fn(&'a str, &'a str) -> Vec<::diff::Result<T>>,
277 Undiff: Fn(&[::diff::Result<T>]) -> (String, String),
278 ToString: Fn(&::diff::Result<T>) -> String,
279 T: 'a,
280 {
281 let mut actual = String::new();
282 for i in 0..all.len() {
283 let from = i.saturating_sub(2);
284 let to = (i + 2).min(all.len());
285 for j in from..to {
286 actual.push_str(&format!("i = {}, j = {}\n", i, j));
287 let diff = diff(&all[i], &all[j]);
288 for d in &diff {
289 actual.push_str(&to_string(d));
290 }
291 actual.push_str("\n");
292 let undiff = undiff(&diff);
293 assert_eq!(&undiff.0, &all[i]);
294 assert_eq!(&undiff.1, &all[j]);
295 }
296 }
297
298 if let Ok(expected) = std::fs::read_to_string(path) {
299 assert_eq!(expected.replace("\r", ""), actual);
300 } else {
301 std::fs::write(path, actual).unwrap();
302 }
303 }
304 }