Skip to content

Commit

Permalink
Make generated cargo tests easier to work with
Browse files Browse the repository at this point in the history
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_`
  • Loading branch information
nelsonjchen committed Sep 18, 2017
1 parent a47638c commit 4ac4a74
Show file tree
Hide file tree
Showing 4 changed files with 649 additions and 646 deletions.
5 changes: 4 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ fn generate_tests_from_spec() {
let mut spec_rs = File::create(&rs_test_file)
.expect(&format!("Could not create {:?}", rs_test_file));

let spec_name = file_path.file_stem().unwrap().to_str().unwrap();

let spec = Spec::new(&raw_spec);
let mut n_tests = 0;

Expand All @@ -67,7 +69,7 @@ fn generate_tests_from_spec() {
r###"
#[test]
fn spec_test_{i}() {{
fn {}_test_{i}_() {{
let original = r##"{original}"##;
let expected = r##"{expected}"##;
Expand All @@ -84,6 +86,7 @@ fn generate_tests_from_spec() {
assert_eq!(expected, s);
}}"###,
spec_name,
i=i+1,
original=testcase.original,
expected=testcase.expected
Expand Down
16 changes: 8 additions & 8 deletions tests/footnotes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ extern crate pulldown_cmark;


#[test]
fn spec_test_1() {
fn footnotes_test_1_() {
let original = r##"Lorem ipsum.[^a]
[^a]: Cool.
Expand All @@ -31,7 +31,7 @@ extern crate pulldown_cmark;
}

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

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

#[test]
fn spec_test_4() {
fn footnotes_test_4_() {
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.
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.
Expand All @@ -120,7 +120,7 @@ I had largely given over my inquiries into what Professor Angell called the "Cth
}

#[test]
fn spec_test_5() {
fn footnotes_test_5_() {
let original = r##"[^ipsum]: How much wood would a woodchuck chuck.
If a woodchuck could chuck wood.
Expand Down Expand Up @@ -150,7 +150,7 @@ If a woodchuck could chuck wood.
}

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

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

#[test]
fn spec_test_8() {
fn footnotes_test_8_() {
let original = r##"[^Doh] Ray Me Fa So La Te Do! [^1]
[^Doh]: I know. Wrong Doe. And it won't render right.
Expand Down
Loading

0 comments on commit 4ac4a74

Please sign in to comment.