From 4ac4a74233b49f6249238b7281855b3e82f8a3ed Mon Sep 17 00:00:00 2001 From: Nelson Chen Date: Fri, 15 Sep 2017 23:03:59 -0700 Subject: [PATCH] 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: https://github.com/rust-lang/rust/pull/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_` --- build.rs | 5 +- tests/footnotes.rs | 16 +- tests/spec.rs | 1248 ++++++++++++++++++++++---------------------- tests/table.rs | 26 +- 4 files changed, 649 insertions(+), 646 deletions(-) diff --git a/build.rs b/build.rs index 814f344e0..3a87927f8 100644 --- a/build.rs +++ b/build.rs @@ -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; @@ -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}"##; @@ -84,6 +86,7 @@ fn generate_tests_from_spec() { assert_eq!(expected, s); }}"###, + spec_name, i=i+1, original=testcase.original, expected=testcase.expected diff --git a/tests/footnotes.rs b/tests/footnotes.rs index e1b1903f3..2100d3fea 100644 --- a/tests/footnotes.rs +++ b/tests/footnotes.rs @@ -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. @@ -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] > @@ -61,7 +61,7 @@ Yes it goes on and on my friends. He's also really stupid. [^why] > > [^why]: Because your mamma! @@ -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] @@ -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. diff --git a/tests/spec.rs b/tests/spec.rs index 0af21284c..4f368a55f 100644 --- a/tests/spec.rs +++ b/tests/spec.rs @@ -5,7 +5,7 @@ extern crate pulldown_cmark; #[test] - fn spec_test_1() { + fn spec_test_1_() { let original = r##" foo baz bim "##; let expected = r##"
foo	baz		bim
@@ -27,7 +27,7 @@ extern crate pulldown_cmark;
     }
 
     #[test]
-    fn spec_test_2() {
+    fn spec_test_2_() {
         let original = r##"  	foo	baz		bim
 "##;
         let expected = r##"
foo	baz		bim
@@ -49,7 +49,7 @@ extern crate pulldown_cmark;
     }
 
     #[test]
-    fn spec_test_3() {
+    fn spec_test_3_() {
         let original = r##"    a	a
     ὐ	a
 "##;
@@ -73,7 +73,7 @@ extern crate pulldown_cmark;
     }
 
     #[test]
-    fn spec_test_4() {
+    fn spec_test_4_() {
         let original = r##"  - foo
 
 	bar
@@ -101,7 +101,7 @@ extern crate pulldown_cmark;
     }
 
     #[test]
-    fn spec_test_5() {
+    fn spec_test_5_() {
         let original = r##"- foo
 
 		bar
@@ -130,7 +130,7 @@ extern crate pulldown_cmark;
     }
 
     #[test]
-    fn spec_test_6() {
+    fn spec_test_6_() {
         let original = r##">		foo
 "##;
         let expected = r##"
@@ -154,7 +154,7 @@ extern crate pulldown_cmark; } #[test] - fn spec_test_7() { + fn spec_test_7_() { let original = r##"- foo "##; let expected = r##"