Skip to content

Commit b3dda98

Browse files
committed
Don't strip leading/trailing whitespace from lines.
This allows us to write things such as: x: a b c and differentiate the leading whitespace in the lines. Note that this change is backwards compatible because `fm` defaults to leading/trailing whitespace-insensitive matching, so unless users explicitly request whitespace-sensitive matching they won't know the difference.
1 parent 4c65a4a commit b3dda98

File tree

7 files changed

+72
-9
lines changed

7 files changed

+72
-9
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ name = "fm_options"
1818
path = "examples/fm_options/run_tests.rs"
1919

2020
[dependencies]
21-
fm = "0.1"
21+
fm = "0.1.1"
2222
getopts = "0.2"
2323
libc = "0.2"
2424
num_cpus = "1.10"

README.md

+15-2
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,21 @@ fn main() {
8181
Test data is specified with a two-level indentation syntax: the outer most
8282
level of indentation defines a test command (multiple command names can be
8383
specified, as in the above); the inner most level of indentation defines
84-
alterations to the general command or sub-tests. Each test command must
85-
define at least one sub-test:
84+
alterations to the general command or sub-tests. Multi-line values are stripped
85+
of their common indentation, such that:
86+
87+
```text
88+
x:
89+
a
90+
b
91+
c
92+
```
93+
94+
defines a test command `x` with a value `a\n b\nc`. Trailing whitespace is
95+
not stripped from each line. Note that by default `fm` ignores leading and
96+
trailing whitespace.
97+
98+
Each test command must define at least one sub-test:
8699

87100
* `status: <success|failure|signal|<int>>`, where `success` and `failure` map
88101
to platform specific notions of a command completing successfully or
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# VM:
2+
# status: success
3+
# stdin:
4+
# a
5+
# b
6+
# a
7+
# stdout:
8+
# $1
9+
# b
10+
# $1
11+
12+
import sys
13+
14+
for l in sys.stdin:
15+
sys.stdout.write(l)

examples/fm_options/run_tests.rs

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ fn main() {
2626
let ptn_re = Regex::new(r"\$.+?\b").unwrap();
2727
let text_re = Regex::new(r".+?\b").unwrap();
2828
fmb.name_matcher(Some((ptn_re, text_re)))
29+
.ignore_leading_whitespace(false)
2930
})
3031
.test_cmds(move |p| {
3132
let mut vm = Command::new("python3");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Run-time:
2+
// status: success
3+
// stdin:
4+
// a
5+
// b
6+
// c
7+
// stdout:
8+
// Hello a
9+
// b
10+
// c
11+
12+
use std::io::{Read, stdin};
13+
14+
fn main() {
15+
let mut buf = String::new();
16+
stdin().read_to_string(&mut buf).unwrap();
17+
println!("Hello {}", buf);
18+
}

src/lib.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,20 @@
7878
//!
7979
//! Test data is specified with a two-level indentation syntax: the outer most level of indentation
8080
//! defines a test command (multiple command names can be specified, as in the above); the inner
81-
//! most level of indentation defines alterations to the general command or sub-tests. Each test
82-
//! command must define at least one sub-test:
81+
//! most level of indentation defines alterations to the general command or sub-tests. Multi-line
82+
//! values are stripped of their common indentation, such that:
83+
//!
84+
//! ```text
85+
//! x:
86+
//! a
87+
//! b
88+
//! c
89+
//! ```
90+
//!
91+
//! defines a key `x` with a value `a\n b\nc`. Trailing whitespace is not stripped from each line.
92+
//! Note that by default `fm` ignores leading and trailing whitespace.
93+
//!
94+
//! Each test command must define at least one sub-test:
8395
//!
8496
//! * `status: <success|failure|signal|<int>>`, where `success` and `failure` map to platform
8597
//! specific notions of a command completing successfully or unsuccessfully respectively.

src/parser.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,11 @@ fn key_val<'a>(lines: &[&'a str], line_off: usize, indent: usize) -> (&'a str, &
122122
.chars()
123123
.take_while(|c| c.is_whitespace())
124124
.count();
125-
(key, &line[content_start..].trim())
125+
(key, &line[content_start..])
126126
}
127127

128128
/// Turn one more lines of the format `key: val` (where `val` may spread over many lines) into its
129-
/// separate components. Guarantees to trim leading and trailing newlines.
129+
/// separate components.
130130
fn key_multiline_val<'a>(
131131
lines: &[&'a str],
132132
mut line_off: usize,
@@ -147,7 +147,7 @@ fn key_multiline_val<'a>(
147147
if cur_indent <= indent {
148148
break;
149149
}
150-
val.push(&lines[line_off][sub_indent..].trim());
150+
val.push(&lines[line_off][sub_indent..]);
151151
line_off += 1;
152152
}
153153
}
@@ -180,7 +180,11 @@ mod test {
180180
);
181181
assert_eq!(
182182
key_multiline_val(&["x:", " z ", " a ", " ", "b"], 0, 0),
183-
(4, "x", vec!["z", "a"])
183+
(4, "x", vec!["z ", "a "])
184+
);
185+
assert_eq!(
186+
key_multiline_val(&["x:", " z ", " a ", " ", " b"], 0, 0),
187+
(5, "x", vec!["z ", " a ", "", "b"])
184188
);
185189
}
186190
}

0 commit comments

Comments
 (0)