all tests passing again
This commit is contained in:
@ -1,4 +1,13 @@
|
|||||||
|
use regex::Regex;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
#[serde(untagged)]
|
||||||
|
pub enum SqlExpectation {
|
||||||
|
Single(String),
|
||||||
|
Multi(Vec<String>),
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct ExpectBlock {
|
pub struct ExpectBlock {
|
||||||
@ -6,7 +15,7 @@ pub struct ExpectBlock {
|
|||||||
pub result: Option<serde_json::Value>,
|
pub result: Option<serde_json::Value>,
|
||||||
pub errors: Option<Vec<serde_json::Value>>,
|
pub errors: Option<Vec<serde_json::Value>>,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub sql: Option<Vec<String>>,
|
pub sql: Option<Vec<SqlExpectation>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ExpectBlock {
|
impl ExpectBlock {
|
||||||
@ -29,8 +38,7 @@ impl ExpectBlock {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
use regex::Regex;
|
let ws_re = Regex::new(r"\s+").unwrap();
|
||||||
use std::collections::HashMap;
|
|
||||||
|
|
||||||
let types = HashMap::from([
|
let types = HashMap::from([
|
||||||
(
|
(
|
||||||
@ -53,9 +61,18 @@ impl ExpectBlock {
|
|||||||
// Placeholder regex: {{type:name}} or {{type}}
|
// Placeholder regex: {{type:name}} or {{type}}
|
||||||
let ph_rx = Regex::new(r"\{\{([a-z]+)(?:[:]([^}]+))?\}\}").unwrap();
|
let ph_rx = Regex::new(r"\{\{([a-z]+)(?:[:]([^}]+))?\}\}").unwrap();
|
||||||
|
|
||||||
for (i, pattern_str) in patterns.iter().enumerate() {
|
for (i, pattern_expect) in patterns.iter().enumerate() {
|
||||||
let aline = &actual[i];
|
let aline_raw = &actual[i];
|
||||||
let mut pp = regex::escape(pattern_str);
|
let aline = ws_re.replace_all(aline_raw, " ").trim().to_string();
|
||||||
|
|
||||||
|
let pattern_str_raw = match pattern_expect {
|
||||||
|
SqlExpectation::Single(s) => s.clone(),
|
||||||
|
SqlExpectation::Multi(m) => m.join(" "),
|
||||||
|
};
|
||||||
|
|
||||||
|
let pattern_str = ws_re.replace_all(&pattern_str_raw, " ").trim().to_string();
|
||||||
|
|
||||||
|
let mut pp = regex::escape(&pattern_str);
|
||||||
pp = pp.replace(r"\{\{", "{{").replace(r"\}\}", "}}");
|
pp = pp.replace(r"\{\{", "{{").replace(r"\}\}", "}}");
|
||||||
|
|
||||||
let mut cap_names = HashMap::new(); // cg_X -> var_name
|
let mut cap_names = HashMap::new(); // cg_X -> var_name
|
||||||
@ -96,7 +113,7 @@ impl ExpectBlock {
|
|||||||
Err(e) => return Err(format!("Bad constructed regex: {} -> {}", final_rx_str, e)),
|
Err(e) => return Err(format!("Bad constructed regex: {} -> {}", final_rx_str, e)),
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(captures) = final_rx.captures(aline) {
|
if let Some(captures) = final_rx.captures(&aline) {
|
||||||
for (cg_name, var_name) in cap_names {
|
for (cg_name, var_name) in cap_names {
|
||||||
if let Some(m) = captures.name(&cg_name) {
|
if let Some(m) = captures.name(&cg_name) {
|
||||||
let matched_str = m.as_str();
|
let matched_str = m.as_str();
|
||||||
|
|||||||
Reference in New Issue
Block a user