Skip to content

Commit

Permalink
add validate_regexes.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
trasua committed Oct 10, 2022
1 parent 73adba1 commit e918d23
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/data/regex.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
},
{
"Name": "PEM-formatted Private Key",
"Regex": "^(-----BEGIN( ANY| RSA| DSA| ENCRYPTED| EC| OPENSSH)? PRIVATE KEY-----\\\\n?[a-zA-Z0-9/\\\\.\\\\n\\\\:\\\\+\\\\=]+-----END( ANY| RSA| DSA| ENCRYPTED| EC| OPENSSH)? PRIVATE KEY-----)$",
"Regex": "^(-----BEGIN( ANY| RSA| DSA| ENCRYPTED| EC| OPENSSH)? PRIVATE KEY-----\n?[a-zA-Z0-9/\\\\.\\\\n\\\\:\\\\+\\\\=]+-----END( ANY| RSA| DSA| ENCRYPTED| EC| OPENSSH)? PRIVATE KEY-----)$",
"plural_name": false,
"Description": null,
"Rarity": 1,
Expand Down Expand Up @@ -1474,7 +1474,7 @@
},
{
"Name": "Latitude & Longitude Coordinates",
"Regex": "(?i)^((?:(?:N|W|S|E)\\s?\\d+\\s?\\u00B0\\s?\\d+\\.?\\d*\\s?'?\\s?\\d*\\.?,?\\d*?\\\\\"?\\s?){1,2}|(?:\\d+\\s?\\u00B0\\s?\\d+\\s?'?\\s?\\d+\\.?,?\\d{0,}?\\\\\"?\\s?(?:N|W|S|E)\\s?){1,2}|(?:[-+]?(?:[0-8]?\\d+\\.\\d{4,}|90(?:\\.0+)?),\\s*[-+]?(?:180(?:\\.0+)?|(?:(?:1[0-7]\\d)|(?:[1-9]?\\d))(?:\\.\\d+)?))|(?:@\\d+\\.\\d{4,},\\d+.\\d{4,},\\d+z))$",
"Regex": "(?i)^((?:(?:N|W|S|E)\\s?\\d+\\s?\\u00B0\\s?\\d+\\.?\\d*\\s?'?\\s?\\d*\\.?,?\\d*?\"?\\s?){1,2}|(?:\\d+\\s?\\u00B0\\s?\\d+\\s?'?\\s?\\d+\\.?,?\\d{0,}?\"?\\s?(?:N|W|S|E)\\s?){1,2}|(?:[-+]?(?:[0-8]?\\d+\\.\\d{4,}|90(?:\\.0+)?),\\s*[-+]?(?:180(?:\\.0+)?|(?:(?:1[0-7]\\d)|(?:[1-9]?\\d))(?:\\.\\d+)?))|(?:@\\d+\\.\\d{4,},\\d+.\\d{4,},\\d+z))$",
"plural_name": true,
"Description": null,
"Rarity": 0.7,
Expand Down
68 changes: 68 additions & 0 deletions tests/validate_regexes.rs
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);
}
}
}
}
}

0 comments on commit e918d23

Please # to comment.