tux 0.2.2

Test utilities for unit and integration tests
Documentation
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/// Single element of a diff between a `source` and `result` lists. A complete
/// diff consists of a sequence of these elements (see [`DiffResult`]).
///
/// The sequence of [`Diff`] elements describes the differences between both
/// lists in terms of what happens for each item in them, starting with
/// the `source` list to reach the given `result`. This representation direct
/// translates to common textual representations of a diff.
///
/// Each [`Diff`] elements contains a `count` of items it applies to. Elements
/// must be considered in order when using the diff result:
///
/// - [`Diff::Output`]: the next `count` items from both lists are the same,
/// that is, the sequence from `source` has not been changed in the `result`.
///
/// - [`Diff::Delete`]: the next `count` items from `source` have been removed
/// and don't appear in the `result`.
///
/// - [`Diff::Insert`]: the next `count` items from `result` have been added
/// to the current position in `source`.
///
/// Note that for a position that has both deleted and inserted items, the
/// result will always have the [`Diff::Delete`] before the [`Diff::Insert`].
#[derive(Debug)]
pub enum Diff {
	/// Represents a sequence of items in `source` and `result` that are the
	/// same.
	Output(usize),

	/// Represents a sequence of items from `source` that has been removed
	/// and does not appear in `result`.
	Delete(usize),

	/// Represents a sequence of items from `result` that have been added
	/// at the current position in `source`.
	Insert(usize),
}

/// Computes the difference between two lists containing lines of text.
///
/// # Example
///
/// ```
/// use tux::diff;
///
/// let source = vec!["1", "2", "3"];
/// let result = vec!["0", "2", "4"];
///
/// let diff = diff::lines(&source, &result);
/// println!("{}", diff);
/// ```
///
/// This will output:
///
/// ```text
/// -1
/// +0
///  2
/// -3
/// +4
/// ```
pub fn lines<'a, T>(source: &'a [T], result: &'a [T]) -> DiffResult<'a, T>
where
	T: AsRef<str> + std::cmp::PartialEq,
{
	let full_source = source;
	let full_result = result;

	let common_prefix_len = {
		let mut len = 0;
		while len < source.len() && len < result.len() && source[len] == result[len] {
			len += 1;
		}
		len
	};

	let source = &source[common_prefix_len..];
	let result = &result[common_prefix_len..];

	let both_are_equal = source.len() == 0 && result.len() == 0;
	if both_are_equal {
		return DiffResult {
			items: Vec::new(),
			source: full_source,
			result: full_result,
		};
	}

	let common_suffix_len = {
		let from_last = |slice: &[T], offset| slice.len() - offset - 1;
		let mut len = 0;
		while len < source.len()
			&& len < result.len()
			&& source[from_last(source, len)] == result[from_last(result, len)]
		{
			len += 1;
		}
		len
	};

	let source = &source[..source.len() - common_suffix_len];
	let result = &result[..result.len() - common_suffix_len];

	let common_subsequence = super::lcs(source, result);

	let mut diff = Vec::new();
	if common_prefix_len > 0 {
		diff.push(Diff::Output(common_prefix_len));
	}

	let mut cur_source = 0;
	let mut cur_result = 0;

	for (line_source, line_result) in common_subsequence {
		if line_source > cur_source {
			diff.push(Diff::Delete(line_source - cur_source));
		}

		if line_result > cur_result {
			diff.push(Diff::Insert(line_result - cur_result));
		}

		// if we already have an output element, append to it
		match diff.last_mut() {
			Some(Diff::Output(count)) => {
				*count += 1;
			}
			_ => {
				diff.push(Diff::Output(1));
			}
		}

		cur_source = line_source + 1;
		cur_result = line_result + 1;
	}

	if cur_source < source.len() {
		diff.push(Diff::Delete(source.len() - cur_source));
	}

	if cur_result < result.len() {
		diff.push(Diff::Insert(result.len() - cur_result));
	}

	if common_suffix_len > 0 {
		diff.push(Diff::Output(common_suffix_len));
	}

	DiffResult {
		items: diff,
		source: full_source,
		result: full_result,
	}
}

/// Result of a diff operation between a `source` list and a `result` list.
///
/// This struct contains a sequence of [`Diff`] elements, and the `source` and
/// `result` lists that the diff sequence was computed from.
///
/// See the [`Diff`] documentation for more details on how the diff is
/// represented.
///
/// # Diff output
///
/// The result maintains a reference to both the `source` and `result` lists
/// that the diff was computed from. This allows it to generate the textual
/// representation of the diff by implementing [`std::fmt::Display`].
///
/// This requires that the `source`/`result` items implement the [`Display`](std::fmt::Display)
/// trait.
pub struct DiffResult<'a, T> {
	items: Vec<Diff>,
	source: &'a [T],
	result: &'a [T],
}

impl<'a, T> DiffResult<'a, T> {
	pub fn is_empty(&self) -> bool {
		self.items.len() == 0
	}

	pub fn items(&self) -> &Vec<Diff> {
		&self.items
	}
}

impl<'a, T> std::fmt::Display for DiffResult<'a, T>
where
	T: std::fmt::Display,
{
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		let source = self.source;
		let result = self.result;

		let mut has_some_output = false;
		let mut start_new_line = |f: &mut std::fmt::Formatter| -> std::fmt::Result {
			if has_some_output {
				write!(f, "\n")
			} else {
				has_some_output = true;
				Ok(())
			}
		};

		let mut cur_offset_source = 0;
		let mut cur_offset_result = 0;
		for item in &self.items {
			match item {
				Diff::Output(count) => {
					let sta = cur_offset_source;
					let end = sta + count;
					cur_offset_source += count;
					cur_offset_result += count;
					for x in sta..end {
						start_new_line(f)?;
						write!(f, " {}", source[x])?;
					}
				}
				Diff::Insert(count) => {
					let sta = cur_offset_result;
					let end = sta + count;
					cur_offset_result += count;
					for x in sta..end {
						start_new_line(f)?;
						write!(f, "+{}", result[x])?;
					}
				}
				Diff::Delete(count) => {
					let sta = cur_offset_source;
					let end = sta + count;
					cur_offset_source += count;
					for x in sta..end {
						start_new_line(f)?;
						write!(f, "-{}", source[x])?;
					}
				}
			}
		}
		Ok(())
	}
}

#[cfg(test)]
mod test_lines {
	use super::lines;
	use super::Diff;
	use crate::text;

	#[test]
	fn both_empty() {
		let a: Vec<String> = Vec::new();
		let b: Vec<String> = Vec::new();
		let diff = lines(&a, &b);
		assert!(diff.is_empty());
	}

	#[test]
	fn both_equal() {
		let a = vec!["line 1", "line 2"];
		let b = vec!["line 1", "line 2"];
		let diff = lines(&a, &b);
		assert!(diff.is_empty());
	}

	#[test]
	fn empty_result() {
		let a = vec!["line 1", "line 2"];
		let b = Vec::new();
		let diff = helper::get_diff_text(a, b);
		assert_eq!(diff, text::join_lines(["-line 1", "-line 2"]));
	}

	#[test]
	fn empty_source() {
		let a = Vec::new();
		let b = vec!["line 1", "line 2"];
		let diff = helper::get_diff_text(a, b);
		assert_eq!(diff, text::join_lines(["+line 1", "+line 2"]));
	}

	#[test]
	fn nothing_in_common() {
		let a = vec!["line 1", "line 2"];
		let b = vec!["line A", "line B"];
		let diff = helper::get_diff_text(a, b);
		assert_eq!(
			diff,
			text::join_lines(["-line 1", "-line 2", "+line A", "+line B"])
		);
	}

	#[test]
	fn removed_suffix() {
		let a = vec!["same 1", "same 2", "suffix 1", "suffix 2"];
		let b = vec!["same 1", "same 2"];
		let diff = helper::get_diff_text(a, b);
		assert_eq!(
			diff,
			text::join_lines([" same 1", " same 2", "-suffix 1", "-suffix 2"])
		);
	}

	#[test]
	fn added_suffix() {
		let a = vec!["same 1", "same 2"];
		let b = vec!["same 1", "same 2", "suffix 1", "suffix 2"];
		let diff = helper::get_diff_text(a, b);
		assert_eq!(
			diff,
			text::join_lines([" same 1", " same 2", "+suffix 1", "+suffix 2"])
		);
	}

	#[test]
	fn removed_prefix() {
		let a = vec!["prefix 1", "prefix 2", "same 1", "same 2"];
		let b = vec!["same 1", "same 2"];
		let diff = helper::get_diff_text(a, b);
		assert_eq!(
			diff,
			text::join_lines(["-prefix 1", "-prefix 2", " same 1", " same 2"])
		);
	}

	#[test]
	fn added_prefix() {
		let a = vec!["same 1", "same 2"];
		let b = vec!["prefix 1", "prefix 2", "same 1", "same 2"];
		let diff = helper::get_diff_text(a, b);
		assert_eq!(
			diff,
			text::join_lines(["+prefix 1", "+prefix 2", " same 1", " same 2"])
		);
	}

	#[test]
	fn removed_prefix_and_suffix() {
		let a = vec![
			"prefix 1", "prefix 2", "same 1", "same 2", "suffix 1", "suffix 2",
		];
		let b = vec!["same 1", "same 2"];
		let diff = helper::get_diff_text(a, b);
		assert_eq!(
			diff,
			text::join_lines([
				"-prefix 1",
				"-prefix 2",
				" same 1",
				" same 2",
				"-suffix 1",
				"-suffix 2"
			])
		);
	}

	#[test]
	fn added_prefix_and_suffix() {
		let a = vec!["same 1", "same 2"];
		let b = vec![
			"prefix 1", "prefix 2", "same 1", "same 2", "suffix 1", "suffix 2",
		];
		let diff = helper::get_diff_text(a, b);
		assert_eq!(
			diff,
			text::join_lines([
				"+prefix 1",
				"+prefix 2",
				" same 1",
				" same 2",
				"+suffix 1",
				"+suffix 2"
			])
		);
	}

	#[test]
	fn added_with_two_common_lines() {
		let a = vec!["same 1", "same 2"];
		let b = vec!["prefix", "same 1", "between", "same 2", "suffix"];
		let diff = helper::get_diff_text(a, b);
		assert_eq!(
			diff,
			text::join_lines(["+prefix", " same 1", "+between", " same 2", "+suffix"])
		);
	}

	#[test]
	fn removed_with_two_common_lines() {
		let a = vec!["prefix", "same 1", "between", "same 2", "suffix"];
		let b = vec!["same 1", "same 2"];
		let diff = helper::get_diff_text(a, b);
		assert_eq!(
			diff,
			text::join_lines(["-prefix", " same 1", "-between", " same 2", "-suffix"])
		);
	}

	#[test]
	fn with_different_contents_and_two_common_lines() {
		let a = vec!["prefix A", "same 1", "between A", "same 2", "suffix A"];
		let b = vec!["prefix B", "same 1", "between B", "same 2", "suffix B"];
		let diff = helper::get_diff_text(a, b);
		assert_eq!(
			diff,
			text::join_lines([
				"-prefix A",
				"+prefix B",
				" same 1",
				"-between A",
				"+between B",
				" same 2",
				"-suffix A",
				"+suffix B"
			])
		);
	}

	#[test]
	fn with_non_trivial_common_sequence() {
		let a = vec!["a1", "sX", "a2", "sW", "sX", "a3", "sY", "a4", "sZ"];
		let b = vec!["b1", "b2", "sW", "sX", "b3", "sY", "b4", "sZ"];
		let diff = helper::get_diff_text(a, b);
		assert_eq!(
			diff,
			text::join_lines([
				"-a1", "-sX", "-a2", "+b1", "+b2", " sW", " sX", "-a3", "+b3", " sY", "-a4", "+b4",
				" sZ",
			])
		);
	}

	mod helper {
		use super::*;

		pub fn get_diff_text(a: Vec<&str>, b: Vec<&str>) -> String {
			let diff = lines(&a, &b);
			sanity_check_diff(&diff.items());
			diff.to_string()
		}

		fn sanity_check_diff(diff: &Vec<Diff>) {
			check_diff_does_not_have_empty_entries(diff);
			check_diff_does_not_have_contiguous_output_ranges(diff);
		}

		fn check_diff_does_not_have_empty_entries(diff: &Vec<Diff>) {
			for it in diff.iter() {
				let is_empty = match it {
					Diff::Output(size) | Diff::Insert(size) | Diff::Delete(size) => *size == 0,
				};
				if is_empty {
					panic!("diff produced empty entries:\n{:?}", diff);
				}
			}
		}

		fn check_diff_does_not_have_contiguous_output_ranges(diff: &Vec<Diff>) {
			let mut last_was_output = false;
			let contiguous_output_ranges = diff.iter().filter(|x| {
				if let Diff::Output(_) = x {
					let is_contiguous = last_was_output;
					last_was_output = true;
					is_contiguous
				} else {
					last_was_output = false;
					false
				}
			});

			if contiguous_output_ranges.count() > 0 {
				panic!("diff produced contiguous output ranges:\n{:?}", diff);
			}
		}
	}
}