зеркало из https://github.com/mozilla/gecko-dev.git
bug 1585949: geckodriver: get rid of regex use in tests; r=webdriver-reviewers,whimboo
Regular expressions are expensive, both in runtime cost and compile-time cost of the regex crate. This is the first step towards getting rid of the regex dependency in geckodriver. Differential Revision: https://phabricator.services.mozilla.com/D48053 --HG-- extra : moz-landing-system : lando
This commit is contained in:
Родитель
7a2422f61b
Коммит
c5abf369ca
|
@ -234,65 +234,52 @@ pub struct LogOptions {
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use serde_json::json;
|
||||
|
||||
use super::*;
|
||||
use crate::test::check_deserialize;
|
||||
use std::fs::File;
|
||||
use std::io::Read;
|
||||
use crate::test::assert_de;
|
||||
|
||||
#[test]
|
||||
fn test_json_addon_install_parameters_null() {
|
||||
let json = r#""#;
|
||||
|
||||
assert!(serde_json::from_str::<AddonInstallParameters>(&json).is_err());
|
||||
fn test_json_addon_install_parameters_invalid() {
|
||||
assert!(serde_json::from_str::<AddonInstallParameters>("").is_err());
|
||||
assert!(serde_json::from_value::<AddonInstallParameters>(json!(null)).is_err());
|
||||
assert!(serde_json::from_value::<AddonInstallParameters>(json!({})).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_json_addon_install_parameters_empty() {
|
||||
let json = r#"{}"#;
|
||||
|
||||
assert!(serde_json::from_str::<AddonInstallParameters>(&json).is_err());
|
||||
fn test_json_addon_install_parameters_with_path_and_temporary() {
|
||||
let params = AddonInstallParameters {
|
||||
path: "/path/to.xpi".to_string(),
|
||||
temporary: Some(true),
|
||||
};
|
||||
assert_de(¶ms, json!({"path": "/path/to.xpi", "temporary": true}));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_json_addon_install_parameters_with_path() {
|
||||
let json = r#"{"path": "/path/to.xpi", "temporary": true}"#;
|
||||
let data = AddonInstallParameters {
|
||||
path: "/path/to.xpi".to_string(),
|
||||
temporary: Some(true),
|
||||
};
|
||||
|
||||
check_deserialize(&json, &data);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_json_addon_install_parameters_with_path_only() {
|
||||
let json = r#"{"path": "/path/to.xpi"}"#;
|
||||
let data = AddonInstallParameters {
|
||||
let params = AddonInstallParameters {
|
||||
path: "/path/to.xpi".to_string(),
|
||||
temporary: None,
|
||||
};
|
||||
|
||||
check_deserialize(&json, &data);
|
||||
assert_de(¶ms, json!({"path": "/path/to.xpi"}));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_json_addon_install_parameters_with_path_invalid_type() {
|
||||
let json = r#"{"path": true, "temporary": true}"#;
|
||||
|
||||
assert!(serde_json::from_str::<AddonInstallParameters>(&json).is_err());
|
||||
let json = json!({"path": true, "temporary": true});
|
||||
assert!(serde_json::from_value::<AddonInstallParameters>(json).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_json_addon_install_parameters_with_path_and_temporary_invalid_type() {
|
||||
let json = r#"{"path": "/path/to.xpi", "temporary": "foo"}"#;
|
||||
|
||||
assert!(serde_json::from_str::<AddonInstallParameters>(&json).is_err());
|
||||
let json = json!({"path": "/path/to.xpi", "temporary": "foo"});
|
||||
assert!(serde_json::from_value::<AddonInstallParameters>(json).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_json_addon_install_parameters_with_addon() {
|
||||
let json = r#"{"addon": "aGVsbG8=", "temporary": true}"#;
|
||||
let data = serde_json::from_str::<AddonInstallParameters>(&json).unwrap();
|
||||
let json = json!({"addon": "aGVsbG8=", "temporary": true});
|
||||
let data = serde_json::from_value::<AddonInstallParameters>(json).unwrap();
|
||||
|
||||
assert_eq!(data.temporary, Some(true));
|
||||
let mut file = File::open(data.path).unwrap();
|
||||
|
@ -303,8 +290,8 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn test_json_addon_install_parameters_with_addon_only() {
|
||||
let json = r#"{"addon": "aGVsbG8="}"#;
|
||||
let data = serde_json::from_str::<AddonInstallParameters>(&json).unwrap();
|
||||
let json = json!({"addon": "aGVsbG8="});
|
||||
let data = serde_json::from_value::<AddonInstallParameters>(json).unwrap();
|
||||
|
||||
assert_eq!(data.temporary, None);
|
||||
let mut file = File::open(data.path).unwrap();
|
||||
|
@ -315,158 +302,92 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn test_json_addon_install_parameters_with_addon_invalid_type() {
|
||||
let json = r#"{"addon": true, "temporary": true}"#;
|
||||
|
||||
assert!(serde_json::from_str::<AddonInstallParameters>(&json).is_err());
|
||||
let json = json!({"addon": true, "temporary": true});
|
||||
assert!(serde_json::from_value::<AddonInstallParameters>(json).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_json_addon_install_parameters_with_addon_and_temporary_invalid_type() {
|
||||
let json = r#"{"addon": "aGVsbG8=", "temporary": "foo"}"#;
|
||||
|
||||
assert!(serde_json::from_str::<AddonInstallParameters>(&json).is_err());
|
||||
let json = json!({"addon": "aGVsbG8=", "temporary": "foo"});
|
||||
assert!(serde_json::from_value::<AddonInstallParameters>(json).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_json_install_parameters_with_temporary_only() {
|
||||
let json = r#"{"temporary": true}"#;
|
||||
|
||||
assert!(serde_json::from_str::<AddonInstallParameters>(&json).is_err());
|
||||
let json = json!({"temporary": true});
|
||||
assert!(serde_json::from_value::<AddonInstallParameters>(json).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_json_addon_install_parameters_with_both_path_and_addon() {
|
||||
let json = r#"{
|
||||
"path":"/path/to.xpi",
|
||||
"addon":"aGVsbG8=",
|
||||
"temporary":true
|
||||
}"#;
|
||||
|
||||
assert!(serde_json::from_str::<AddonInstallParameters>(&json).is_err());
|
||||
let json = json!({
|
||||
"path": "/path/to.xpi",
|
||||
"addon": "aGVsbG8=",
|
||||
"temporary": true,
|
||||
});
|
||||
assert!(serde_json::from_value::<AddonInstallParameters>(json).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_json_addon_uninstall_parameters_null() {
|
||||
let json = r#""#;
|
||||
|
||||
assert!(serde_json::from_str::<AddonUninstallParameters>(&json).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_json_addon_uninstall_parameters_empty() {
|
||||
let json = r#"{}"#;
|
||||
|
||||
assert!(serde_json::from_str::<AddonUninstallParameters>(&json).is_err());
|
||||
fn test_json_addon_uninstall_parameters_invalid() {
|
||||
assert!(serde_json::from_str::<AddonUninstallParameters>("").is_err());
|
||||
assert!(serde_json::from_value::<AddonUninstallParameters>(json!(null)).is_err());
|
||||
assert!(serde_json::from_value::<AddonUninstallParameters>(json!({})).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_json_addon_uninstall_parameters() {
|
||||
let json = r#"{"id": "foo"}"#;
|
||||
let data = AddonUninstallParameters {
|
||||
let params = AddonUninstallParameters {
|
||||
id: "foo".to_string(),
|
||||
};
|
||||
|
||||
check_deserialize(&json, &data);
|
||||
assert_de(¶ms, json!({"id": "foo"}));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_json_addon_uninstall_parameters_id_invalid_type() {
|
||||
let json = r#"{"id": true}"#;
|
||||
|
||||
assert!(serde_json::from_str::<AddonUninstallParameters>(&json).is_err());
|
||||
let json = json!({"id": true});
|
||||
assert!(serde_json::from_value::<AddonUninstallParameters>(json).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_json_gecko_context_parameters_content() {
|
||||
let json = r#"{"context": "content"}"#;
|
||||
let data = GeckoContextParameters {
|
||||
let params = GeckoContextParameters {
|
||||
context: GeckoContext::Content,
|
||||
};
|
||||
|
||||
check_deserialize(&json, &data);
|
||||
assert_de(¶ms, json!({"context": "content"}));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_json_gecko_context_parameters_chrome() {
|
||||
let json = r#"{"context": "chrome"}"#;
|
||||
let data = GeckoContextParameters {
|
||||
let params = GeckoContextParameters {
|
||||
context: GeckoContext::Chrome,
|
||||
};
|
||||
|
||||
check_deserialize(&json, &data);
|
||||
assert_de(¶ms, json!({"context": "chrome"}));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_json_gecko_context_parameters_context_missing() {
|
||||
let json = r#"{}"#;
|
||||
|
||||
assert!(serde_json::from_str::<GeckoContextParameters>(&json).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_json_gecko_context_parameters_context_null() {
|
||||
let json = r#"{"context": null}"#;
|
||||
|
||||
assert!(serde_json::from_str::<GeckoContextParameters>(&json).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_json_gecko_context_parameters_context_invalid_value() {
|
||||
let json = r#"{"context": "foo"}"#;
|
||||
|
||||
assert!(serde_json::from_str::<GeckoContextParameters>(&json).is_err());
|
||||
fn test_json_gecko_context_parameters_context_invalid() {
|
||||
type P = GeckoContextParameters;
|
||||
assert!(serde_json::from_value::<P>(json!({})).is_err());
|
||||
assert!(serde_json::from_value::<P>(json!({ "context": null })).is_err());
|
||||
assert!(serde_json::from_value::<P>(json!({"context": "foo"})).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_json_xbl_anonymous_by_attribute() {
|
||||
let json = r#"{
|
||||
"name": "foo",
|
||||
"value": "bar"
|
||||
}"#;
|
||||
|
||||
let data = XblLocatorParameters {
|
||||
let locator = XblLocatorParameters {
|
||||
name: "foo".to_string(),
|
||||
value: "bar".to_string(),
|
||||
};
|
||||
|
||||
check_deserialize(&json, &data);
|
||||
assert_de(&locator, json!({"name": "foo", "value": "bar"}));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_json_xbl_anonymous_by_attribute_with_name_missing() {
|
||||
let json = r#"{
|
||||
"value": "bar"
|
||||
}"#;
|
||||
|
||||
assert!(serde_json::from_str::<XblLocatorParameters>(&json).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_json_xbl_anonymous_by_attribute_with_name_invalid_type() {
|
||||
let json = r#"{
|
||||
"name": null,
|
||||
"value": "bar"
|
||||
}"#;
|
||||
|
||||
assert!(serde_json::from_str::<XblLocatorParameters>(&json).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_json_xbl_anonymous_by_attribute_with_value_missing() {
|
||||
let json = r#"{
|
||||
"name": "foo",
|
||||
}"#;
|
||||
|
||||
assert!(serde_json::from_str::<XblLocatorParameters>(&json).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_json_xbl_anonymous_by_attribute_with_value_invalid_type() {
|
||||
let json = r#"{
|
||||
"name": "foo",
|
||||
"value": null
|
||||
}"#;
|
||||
|
||||
assert!(serde_json::from_str::<XblLocatorParameters>(&json).is_err());
|
||||
fn test_json_xbl_anonymous_by_attribute_with_name_invalid() {
|
||||
type P = XblLocatorParameters;
|
||||
assert!(serde_json::from_value::<P>(json!({"value": "bar"})).is_err());
|
||||
assert!(serde_json::from_value::<P>(json!({"name": null, "value": "bar"})).is_err());
|
||||
assert!(serde_json::from_value::<P>(json!({"name": "foo"})).is_err());
|
||||
assert!(serde_json::from_value::<P>(json!({"name": "foo", "value": null})).is_err());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,19 +1,8 @@
|
|||
use regex::Regex;
|
||||
use serde;
|
||||
use serde_json;
|
||||
use std;
|
||||
|
||||
lazy_static! {
|
||||
static ref MIN_REGEX: Regex = Regex::new(r"[\n\t]|\s{4}").unwrap();
|
||||
}
|
||||
|
||||
pub fn check_deserialize<T>(json: &str, data: &T)
|
||||
pub fn assert_de<T>(data: &T, json: serde_json::Value)
|
||||
where
|
||||
T: std::fmt::Debug,
|
||||
T: std::cmp::PartialEq,
|
||||
T: serde::de::DeserializeOwned,
|
||||
{
|
||||
let min_json = MIN_REGEX.replace_all(json, "");
|
||||
|
||||
assert_eq!(serde_json::from_str::<T>(&min_json).unwrap(), *data);
|
||||
assert_eq!(data, &serde_json::from_value::<T>(json).unwrap());
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче