Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Improved auto-generated test case name sensitization.
  • Loading branch information
budziq committed Aug 11, 2017
1 parent 4237028 commit 3e384a6
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 20 deletions.
10 changes: 9 additions & 1 deletion build.rs
@@ -1,6 +1,14 @@
extern crate skeptic;

fn main() {
skeptic::generate_doc_tests(&["README.md", "template-example.md", "tests/hashtag-test.md", "tests/should-panic-test.md"]);
skeptic::generate_doc_tests(
&[
"README.md",
"template-example.md",
"tests/hashtag-test.md",
"tests/should-panic-test.md",
"tests/section-names.md",
],
);
// skeptic::generate_doc_tests(&skeptic::markdown_files_of_directory("."));
}
36 changes: 17 additions & 19 deletions src/skeptic/lib.rs
Expand Up @@ -290,27 +290,15 @@ fn load_templates(path: &Path) -> Result<HashMap<String, String>, IoError> {
}

fn sanitize_test_name(s: &str) -> String {
to_lowercase(s)
.trim_matches(|c: char| !c.is_alphanumeric())
.chars()
.map(|c| {
if c.is_alphanumeric() {
c
} else {
'_'
}
})
.collect::<String>()
.replace("__", "_")
.replace("__", "_") // remove also odd "_" occurences
}

// Only converting test names to lowercase to avoid style lints
// against test functions.
fn to_lowercase(s: &str) -> String {
use std::ascii::AsciiExt;
// FIXME: unicode
s.to_ascii_lowercase()
.chars()
.map(|ch| if ch.is_ascii() && ch.is_alphanumeric() { ch } else { '_' })
.collect::<String>()
.split("_")
.filter(|s| !s.is_empty())
.collect::<Vec<_>>()
.join("_")
}

fn parse_code_block_info(info: &str) -> CodeBlockInfo {
Expand Down Expand Up @@ -762,7 +750,17 @@ fn test_markdown_files_of_directory() {
"../../README.md.skt.md",
"../../template-example.md",
"../../tests/hashtag-test.md",
"../../tests/section-names.md",
"../../tests/should-panic-test.md",
];
assert_eq!(markdown_files_of_directory("../../"), files);
}

#[test]
fn test_sanitization_of_testnames() {
assert_eq!(sanitize_test_name("My_Fun"), "my_fun");
assert_eq!(sanitize_test_name("__my_fun_"), "my_fun");
assert_eq!(sanitize_test_name("^$@__my@#_fun#$@"), "my_fun");
assert_eq!(sanitize_test_name("my_long__fun___name___with____a_____lot______of_______spaces"), "my_long_fun_name_with_a_lot_of_spaces");
assert_eq!(sanitize_test_name("Löwe 老虎 Léopard"), "l_we_l_opard");
}
26 changes: 26 additions & 0 deletions tests/section-names.md
@@ -0,0 +1,26 @@
## Test Case Names With weird spacing are generated without error.

```rust
struct Person<'a>(&'a str);
fn main() {
let _ = Person("bors");
}
```

## !@#$ Test Cases )(() with {}[] non alphanumeric characters ^$23 characters are "`#`" generated correctly @#$@#$ 22.

```rust
struct Person<'a>(&'a str);
fn main() {
let _ = Person("bors");
}
```

## Test cases with non ASCII ö_老虎_é characters are generated correctly.

```rust
struct Person<'a>(&'a str);
fn main() {
let _ = Person("bors");
}
```

0 comments on commit 3e384a6

Please sign in to comment.