Skip to content

Commit 4ac4a74

Browse files
committed
Make generated cargo tests easier to work with
While the main executable can also run tests, keeping the standard `cargo test` function useful is also helpful. * Add end character to generated tests to work with substring only matching of tests cases. Regex test matching was removed in early 2015 in: rust-lang/rust#21458 You can only filter tests by substring. What if you wanted to run `spec_test_6`? You would run `spec_test_64` too. Adding the character, `_` to the end allows a workaround of specifying `spec_test_6_` as the match to run if you wanted to only run `spec_test_6_`. E.g. `cargo test spec_test_6_` * Tests are modified to generate tests with spec names in the tests Previously, all tests were generated with `spec_test_#_` as their name, even if they were not generated from `spec.txt`. The `tables.txt` spec would also generate a `spec_test_#_`. This is an issue if you wanted to test `spec_test_6_` and only wanted to run the one from `spec.txt` and not `tables.txt`'s `spec_test_6_`'. While you could build the test executables and only run the specific test executable with the `spec_test_#_` you want, it's kind of annoying. This change derives the test names from the spec filename. For example, `spec.txt` will generate `spec_test_6_` while `tables.txt` and `footnotes.txt` will generate `tables_test_6_` and `footnotes_test_6_` respectively. Future optional specifications that might be added will utilize the same rule. This allows testing of example 6 from the `footnotes.txt` spec by simply doing: `cargo test footnotes_test_6_`
1 parent a47638c commit 4ac4a74

File tree

4 files changed

+649
-646
lines changed

4 files changed

+649
-646
lines changed

build.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ fn generate_tests_from_spec() {
5454
let mut spec_rs = File::create(&rs_test_file)
5555
.expect(&format!("Could not create {:?}", rs_test_file));
5656

57+
let spec_name = file_path.file_stem().unwrap().to_str().unwrap();
58+
5759
let spec = Spec::new(&raw_spec);
5860
let mut n_tests = 0;
5961

@@ -67,7 +69,7 @@ fn generate_tests_from_spec() {
6769
r###"
6870
6971
#[test]
70-
fn spec_test_{i}() {{
72+
fn {}_test_{i}_() {{
7173
let original = r##"{original}"##;
7274
let expected = r##"{expected}"##;
7375
@@ -84,6 +86,7 @@ fn generate_tests_from_spec() {
8486
8587
assert_eq!(expected, s);
8688
}}"###,
89+
spec_name,
8790
i=i+1,
8891
original=testcase.original,
8992
expected=testcase.expected

tests/footnotes.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ extern crate pulldown_cmark;
55

66

77
#[test]
8-
fn spec_test_1() {
8+
fn footnotes_test_1_() {
99
let original = r##"Lorem ipsum.[^a]
1010
1111
[^a]: Cool.
@@ -31,7 +31,7 @@ extern crate pulldown_cmark;
3131
}
3232

3333
#[test]
34-
fn spec_test_2() {
34+
fn footnotes_test_2_() {
3535
let original = r##"> This is the song that never ends.\
3636
> Yes it goes on and on my friends.[^lambchops]
3737
>
@@ -61,7 +61,7 @@ Yes it goes on and on my friends.<sup class="footnote-reference"><a href="#lambc
6161
}
6262

6363
#[test]
64-
fn spec_test_3() {
64+
fn footnotes_test_3_() {
6565
let original = r##"Songs that simply loop are a popular way to annoy people. [^examples]
6666
6767
[^examples]:
@@ -94,7 +94,7 @@ Yes it goes on and on my friends.<sup class="footnote-reference"><a href="#lambc
9494
}
9595

9696
#[test]
97-
fn spec_test_4() {
97+
fn footnotes_test_4_() {
9898
let original = r##"[^lorem]: If heaven ever wishes to grant me a boon, it will be a total effacing of the results of a mere chance which fixed my eye on a certain stray piece of shelf-paper. It was nothing on which I would naturally have stumbled in the course of my daily round, for it was an old number of an Australian journal, the Sydney Bulletin for April 18, 1925. It had escaped even the cutting bureau which had at the time of its issuance been avidly collecting material for my uncle's research.
9999
100100
I had largely given over my inquiries into what Professor Angell called the "Cthulhu Cult", and was visiting a learned friend in Paterson, New Jersey; the curator of a local museum and a mineralogist of note. Examining one day the reserve specimens roughly set on the storage shelves in a rear room of the museum, my eye was caught by an odd picture in one of the old papers spread beneath the stones. It was the Sydney Bulletin I have mentioned, for my friend had wide affiliations in all conceivable foreign parts; and the picture was a half-tone cut of a hideous stone image almost identical with that which Legrasse had found in the swamp.
@@ -120,7 +120,7 @@ I had largely given over my inquiries into what Professor Angell called the "Cth
120120
}
121121

122122
#[test]
123-
fn spec_test_5() {
123+
fn footnotes_test_5_() {
124124
let original = r##"[^ipsum]: How much wood would a woodchuck chuck.
125125
126126
If a woodchuck could chuck wood.
@@ -150,7 +150,7 @@ If a woodchuck could chuck wood.
150150
}
151151

152152
#[test]
153-
fn spec_test_6() {
153+
fn footnotes_test_6_() {
154154
let original = r##"> He's also really stupid. [^why]
155155
>
156156
> [^why]: Because your mamma!
@@ -181,7 +181,7 @@ As such, we can guarantee that the non-childish forms of entertainment are proba
181181
}
182182

183183
#[test]
184-
fn spec_test_7() {
184+
fn footnotes_test_7_() {
185185
let original = r##"Nested footnotes are considered poor style. [^a] [^xkcd]
186186
187187
[^a]: This does not mean that footnotes cannot reference each other. [^b]
@@ -226,7 +226,7 @@ As such, we can guarantee that the non-childish forms of entertainment are proba
226226
}
227227

228228
#[test]
229-
fn spec_test_8() {
229+
fn footnotes_test_8_() {
230230
let original = r##"[^Doh] Ray Me Fa So La Te Do! [^1]
231231
232232
[^Doh]: I know. Wrong Doe. And it won't render right.

0 commit comments

Comments
 (0)