cli-test: Support '[..]'s which preserve spacing

By mapping all '[.. ]'s  with any number of spaces inside them into
'[..]'s, examples can preserve text formatting while using this glob
pattern.

The downside is failed output will show '[..]' in place of the original
version with spaces.
This commit is contained in:
Slack Coder 2023-04-18 16:38:12 -05:00 committed by Alexis Sellier
parent f11991737f
commit 11d10b4e47
No known key found for this signature in database
4 changed files with 76 additions and 15 deletions

View File

@ -163,6 +163,31 @@ impl TestFormula {
Ok(self)
}
/// Convert instances of '[.. ]' to '[..]' where the number of ' 's are arbitrary.
///
/// Supporting these bracket types help support using the '[..]' pattern while preserving
/// spaces important for text alignment.
fn map_spaced_brackets(s: &str) -> String {
let mut ret = String::new();
let mut pos = 0;
for c in s.chars() {
match (c, pos) {
('[', 0) => pos += 1,
(' ', 1) => continue,
('.', 1) => pos += 1,
('.', 2) => pos += 1,
('.', 3) => continue,
(' ', 3) => continue,
(']', 3) => pos = 0,
(_, _) => pos = 0,
}
ret.push(c);
}
ret
}
pub fn run(&mut self) -> Result<bool, io::Error> {
let assert = Assert::new().substitutions(self.subs.clone());
@ -213,12 +238,14 @@ impl TestFormula {
match result {
Ok(output) => {
let assert = OutputAssert::new(output).with_assert(assert.clone());
let expected = Self::map_spaced_brackets(&assertion.expected);
let matches = assert.stdout_matches(&expected);
match assertion.exit {
ExitStatus::Success => {
assert.stdout_matches(&assertion.expected).success();
matches.success();
}
ExitStatus::Failure => {
assert.stdout_matches(&assertion.expected).failure();
matches.failure();
}
}
}
@ -340,4 +367,38 @@ name = "radicle-cli-test"
.unwrap();
formula.run().unwrap();
}
#[test]
fn test_example_spaced_brackets() {
let input = r#"
Running a simple command such as `head`:
```
$ echo " hello"
[..]hello
$ echo " hello"
[.. ]hello
$ echo " hello"
[ ..]hello
$ echo "[bug, good-first-issue]"
[bug, good-first-issue]
$ echo "[bug, good-first-issue]"
[bug, [ .. ]-issue]
$ echo "[bug, good-first-issue]"
[bug, [ ... ]-issue]
```
"#
.trim()
.as_bytes()
.to_owned();
let mut formula = TestFormula::new();
formula
.cwd(env!("CARGO_MANIFEST_DIR"))
.read(
Path::new("test.md"),
io::BufReader::new(io::Cursor::new(input)),
)
.unwrap();
formula.run().unwrap();
}
}

View File

@ -44,7 +44,7 @@ $ rad patch
╭─────────────────────────────────────────────────────────────────────────────────────────╮
│ Define power requirements 191a14e R0 3e674d1 (flux-capacitor-power) ahead 1, behind 0 │
├─────────────────────────────────────────────────────────────────────────────────────────┤
│ ● opened by did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi (you) 3 months ago
│ ● opened by did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi (you) [.. ]
╰─────────────────────────────────────────────────────────────────────────────────────────╯
```
```
@ -57,7 +57,7 @@ $ rad patch show 191a14e520f2eeff7c0e3ee0a5523c5217eecb89
│ │
│ See details. │
├─────────────────────────────────────────────────────────────────────────────────────────┤
│ ● opened by did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi (you) 3 months ago
│ ● opened by did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi (you) [.. ]
╰─────────────────────────────────────────────────────────────────────────────────────────╯
commit 3e674d1a1df90807e934f9ae5da2591dd6848a33
@ -117,8 +117,8 @@ $ rad patch
╭──────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ Define power requirements 191a14e R1 27857ec (flux-capacitor-power, patch/191a14e) ahead 2, behind 0 │
├──────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ ● opened by did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi (you) 3 months ago
│ ↑ updated to b8f7bfbbb3c6a207b349e9f45bf535c706805871 (27857ec) 3 months ago
│ ✓ accepted by z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi (you) 3 months ago
│ ● opened by did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi (you) [.. ]
│ ↑ updated to b8f7bfbbb3c6a207b349e9f45bf535c706805871 (27857ec) [.. ]
│ ✓ accepted by z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi (you) [.. ]
╰──────────────────────────────────────────────────────────────────────────────────────────────────────╯
```

View File

@ -44,7 +44,7 @@ $ rad patch
╭─────────────────────────────────────────────────────────────────────────────────────────╮
│ Define power requirements a07ef77 R0 3e674d1 (flux-capacitor-power) ahead 1, behind 0 │
├─────────────────────────────────────────────────────────────────────────────────────────┤
│ ● opened by did:key:z6Mkt67GdsW7715MEfRuP4pSZxJRJh6kj6Y48WRqVv4N1tRk (you) 3 months ago
│ ● opened by did:key:z6Mkt67GdsW7715MEfRuP4pSZxJRJh6kj6Y48WRqVv4N1tRk (you) [.. ]
╰─────────────────────────────────────────────────────────────────────────────────────────╯
$ rad patch show a07ef7743a32a2e902672ea3526d1db6ee08108a
╭─────────────────────────────────────────────────────────────────────────────────────────╮
@ -55,7 +55,7 @@ $ rad patch show a07ef7743a32a2e902672ea3526d1db6ee08108a
│ │
│ See details. │
├─────────────────────────────────────────────────────────────────────────────────────────┤
│ ● opened by did:key:z6Mkt67GdsW7715MEfRuP4pSZxJRJh6kj6Y48WRqVv4N1tRk (you) 3 months ago
│ ● opened by did:key:z6Mkt67GdsW7715MEfRuP4pSZxJRJh6kj6Y48WRqVv4N1tRk (you) [.. ]
╰─────────────────────────────────────────────────────────────────────────────────────────╯
commit 3e674d1a1df90807e934f9ae5da2591dd6848a33

View File

@ -30,8 +30,8 @@ $ rad patch
╭───────────────────────────────────────────────────────────────────────────────────╮
│ Define power requirements a07ef77 R1 27857ec ahead 2, behind 0 │
├───────────────────────────────────────────────────────────────────────────────────┤
│ ● opened by did:key:z6Mkt67GdsW7715MEfRuP4pSZxJRJh6kj6Y48WRqVv4N1tRk 3 months ago
│ ↑ updated to 11483929d8714a92992229f65433e06288f3b760 (27857ec) 3 months ago
│ ● opened by did:key:z6Mkt67GdsW7715MEfRuP4pSZxJRJh6kj6Y48WRqVv4N1tRk [.. ]
│ ↑ updated to 11483929d8714a92992229f65433e06288f3b760 (27857ec) [.. ]
╰───────────────────────────────────────────────────────────────────────────────────╯
```
@ -80,9 +80,9 @@ $ rad patch --merged
╭───────────────────────────────────────────────────────────────────────────────────────────────╮
│ Define power requirements a07ef77 R2 f6484e0 (flux-capacitor-power, master) ahead 3, behind 0 │
├───────────────────────────────────────────────────────────────────────────────────────────────┤
│ ● opened by did:key:z6Mkt67GdsW7715MEfRuP4pSZxJRJh6kj6Y48WRqVv4N1tRk 3 months ago
│ ↑ updated to 11483929d8714a92992229f65433e06288f3b760 (27857ec) 3 months ago
│ ↑ updated to 0795d619232479e910f95bb9c873ee1ec305c43c (f6484e0) 3 months ago
│ ✓ merged by did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi (you) 3 months ago
│ ● opened by did:key:z6Mkt67GdsW7715MEfRuP4pSZxJRJh6kj6Y48WRqVv4N1tRk [.. ]
│ ↑ updated to 11483929d8714a92992229f65433e06288f3b760 (27857ec) [.. ]
│ ↑ updated to 0795d619232479e910f95bb9c873ee1ec305c43c (f6484e0) [.. ]
│ ✓ merged by did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi (you) [.. ]
╰───────────────────────────────────────────────────────────────────────────────────────────────╯
```