Skip to content

Commit 95e1bd0

Browse files
committed
add tests
1 parent da13969 commit 95e1bd0

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

src/tools/compiletest/src/read2.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// FIXME: This is a complete copy of `cargo/src/cargo/util/read2.rs`
22
// Consider unify the read2() in libstd, cargo and this to prevent further code duplication.
33

4+
#[cfg(test)]
5+
mod tests;
6+
47
pub use self::imp::read2;
58
use std::io::{self, Write};
69
use std::mem::replace;
+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
use crate::read2::{ProcOutput, EXCLUDED_PLACEHOLDER_LEN, HEAD_LEN, TAIL_LEN};
2+
3+
#[test]
4+
fn test_abbreviate_short_string() {
5+
let mut out = ProcOutput::new();
6+
out.extend(b"Hello world!", &[]);
7+
assert_eq!(b"Hello world!", &*out.into_bytes());
8+
}
9+
10+
#[test]
11+
fn test_abbreviate_short_string_multiple_steps() {
12+
let mut out = ProcOutput::new();
13+
14+
out.extend(b"Hello ", &[]);
15+
out.extend(b"world!", &[]);
16+
17+
assert_eq!(b"Hello world!", &*out.into_bytes());
18+
}
19+
20+
#[test]
21+
fn test_abbreviate_long_string() {
22+
let mut out = ProcOutput::new();
23+
24+
let data = vec![b'.'; HEAD_LEN + TAIL_LEN + 16];
25+
out.extend(&data, &[]);
26+
27+
let mut expected = vec![b'.'; HEAD_LEN];
28+
expected.extend_from_slice(b"\n\n<<<<<< SKIPPED 16 BYTES >>>>>>\n\n");
29+
expected.extend_from_slice(&vec![b'.'; TAIL_LEN]);
30+
31+
// We first check the length to avoid endless terminal output if the length differs, since
32+
// `out` is hundreds of KBs in size.
33+
let out = out.into_bytes();
34+
assert_eq!(expected.len(), out.len());
35+
assert_eq!(expected, out);
36+
}
37+
38+
#[test]
39+
fn test_abbreviate_long_string_multiple_steps() {
40+
let mut out = ProcOutput::new();
41+
42+
out.extend(&vec![b'.'; HEAD_LEN], &[]);
43+
out.extend(&vec![b'.'; TAIL_LEN], &[]);
44+
// Also test whether the rotation works
45+
out.extend(&vec![b'!'; 16], &[]);
46+
out.extend(&vec![b'?'; 16], &[]);
47+
48+
let mut expected = vec![b'.'; HEAD_LEN];
49+
expected.extend_from_slice(b"\n\n<<<<<< SKIPPED 32 BYTES >>>>>>\n\n");
50+
expected.extend_from_slice(&vec![b'.'; TAIL_LEN - 32]);
51+
expected.extend_from_slice(&vec![b'!'; 16]);
52+
expected.extend_from_slice(&vec![b'?'; 16]);
53+
54+
// We first check the length to avoid endless terminal output if the length differs, since
55+
// `out` is hundreds of KBs in size.
56+
let out = out.into_bytes();
57+
assert_eq!(expected.len(), out.len());
58+
assert_eq!(expected, out);
59+
}
60+
61+
#[test]
62+
fn test_abbreviate_exclusions_are_detected() {
63+
let mut out = ProcOutput::new();
64+
let exclusions = &["foo".to_string(), "quux".to_string()];
65+
66+
out.extend(b"Hello foo", exclusions);
67+
// Check items from a previous extension are not double-counted.
68+
out.extend(b"! This is a qu", exclusions);
69+
// Check items are detected across extensions.
70+
out.extend(b"ux.", exclusions);
71+
72+
match out {
73+
ProcOutput::Full { excluded_len, .. } => assert_eq!(
74+
excluded_len,
75+
EXCLUDED_PLACEHOLDER_LEN * exclusions.len() as isize
76+
- exclusions.iter().map(|i| i.len() as isize).sum::<isize>()
77+
),
78+
ProcOutput::Abbreviated { .. } => panic!("out should not be abbreviated"),
79+
}
80+
81+
assert_eq!(b"Hello foo! This is a quux.", &*out.into_bytes());
82+
}
83+
84+
#[test]
85+
fn test_abbreviate_exclusions_avoid_abbreviations() {
86+
let mut out = ProcOutput::new();
87+
let exclusions = &[std::iter::repeat('a').take(64).collect::<String>()];
88+
89+
let mut expected = vec![b'.'; HEAD_LEN - EXCLUDED_PLACEHOLDER_LEN as usize];
90+
expected.extend_from_slice(exclusions[0].as_bytes());
91+
expected.extend_from_slice(&vec![b'.'; TAIL_LEN]);
92+
93+
out.extend(&expected, exclusions);
94+
95+
// We first check the length to avoid endless terminal output if the length differs, since
96+
// `out` is hundreds of KBs in size.
97+
let out = out.into_bytes();
98+
assert_eq!(expected.len(), out.len());
99+
assert_eq!(expected, out);
100+
}
101+
102+
#[test]
103+
fn test_abbreviate_exclusions_can_still_cause_abbreviations() {
104+
let mut out = ProcOutput::new();
105+
let exclusions = &[std::iter::repeat('a').take(64).collect::<String>()];
106+
107+
let mut input = vec![b'.'; HEAD_LEN];
108+
input.extend_from_slice(&vec![b'.'; TAIL_LEN]);
109+
input.extend_from_slice(exclusions[0].as_bytes());
110+
111+
let mut expected = vec![b'.'; HEAD_LEN];
112+
expected.extend_from_slice(b"\n\n<<<<<< SKIPPED 64 BYTES >>>>>>\n\n");
113+
expected.extend_from_slice(&vec![b'.'; TAIL_LEN - 64]);
114+
expected.extend_from_slice(&vec![b'a'; 64]);
115+
116+
out.extend(&input, exclusions);
117+
118+
// We first check the length to avoid endless terminal output if the length differs, since
119+
// `out` is hundreds of KBs in size.
120+
let out = out.into_bytes();
121+
assert_eq!(expected.len(), out.len());
122+
assert_eq!(expected, out);
123+
}

0 commit comments

Comments
 (0)