-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
trasua
committed
Oct 10, 2022
1 parent
73adba1
commit e918d23
Showing
2 changed files
with
70 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use regex::Regex; | ||
use serde::{Deserialize, Serialize}; | ||
use std::fs; | ||
|
||
#[derive(Serialize, Deserialize, Debug, Default)] | ||
struct Example { | ||
#[serde(rename(deserialize = "Valid"))] | ||
valid: Vec<String>, | ||
#[serde(rename(deserialize = "Invalid"), default)] | ||
invalid: Vec<String>, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug, Default)] | ||
struct TestCase { | ||
#[serde(rename(deserialize = "Name"))] | ||
name: String, | ||
#[serde(rename(deserialize = "Regex"))] | ||
regex: String, | ||
plural_name: bool, | ||
#[serde(rename(deserialize = "Description"))] | ||
description: Option<String>, | ||
#[serde(rename(deserialize = "Rarity"))] | ||
rarity: f64, | ||
#[serde(rename(deserialize = "URL"))] | ||
url: Option<String>, | ||
#[serde(rename(deserialize = "Tags"))] | ||
tags: Vec<String>, | ||
#[serde(rename(deserialize = "Examples"), default)] | ||
examples: Example, | ||
} | ||
|
||
#[test] | ||
fn testcase_works() { | ||
let path = format!("{}/src/data/regex.json", env!("CARGO_MANIFEST_DIR")); | ||
let data: String = fs::read_to_string(path).unwrap(); | ||
|
||
let test_cases: Vec<TestCase> = serde_json::from_str(&data).unwrap(); | ||
for test_case in &test_cases { | ||
if !test_case.examples.valid.is_empty() || !test_case.examples.invalid.is_empty() { | ||
let re = Regex::new(&test_case.regex); | ||
if let Ok(re) = re { | ||
println!("{}", test_case.name); | ||
|
||
for example in &test_case.examples.valid { | ||
let matched = re.is_match(example); | ||
if !matched { | ||
eprintln!( | ||
"test case: {}\nexample: {}\nregex: {}", | ||
test_case.name, example, test_case.regex | ||
); | ||
} | ||
assert!(matched, "{}", true); | ||
} | ||
|
||
for example in &test_case.examples.invalid { | ||
let matched = re.is_match(example); | ||
if matched { | ||
eprintln!( | ||
"test case: {}\nexample: {}\nregex: {}", | ||
test_case.name, example, test_case.regex | ||
); | ||
} | ||
assert!(matched, "{}", false); | ||
} | ||
} | ||
} | ||
} | ||
} |