Skip to content

Data-driven e2e tests #86

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
members = [
"components/data-provider",
"components/data-provider-json",
"components/e2etests",
"components/icu",
"components/icu4x",
"components/locale",
Expand Down
37 changes: 37 additions & 0 deletions components/e2etests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[package]
name = "e2etests"
version = "0.1.0"
authors = ["The ICU4X Project Developers"]
edition = "2018"
readme = "README.md"
repository = "https://github.com/unicode-org/icu4x"
license = "MIT/Apache-2.0"
categories = ["internationalization"]
include = [
"src/**/*",
"Cargo.toml",
"README.md"
]

[dependencies]
icu-locale = { path = "../locale" }

[dependencies.serde]
version = "1.0"
default-features = false
features = ["derive", "alloc"]

[dependencies.serde_json]
version = "1.0"
default-features = false
features = ["alloc"]

[[test]]
name = "test_data_parser"
path = "src/lib.rs"
required-features = ["std"]

[[test]]
name = "test_data_runner"
path = "src/runner/runner.rs"
required-features = ["std"]
56 changes: 56 additions & 0 deletions components/e2etests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use serde::{Deserialize, Serialize};

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub enum TestOp {
Equals,
NotEquals,
Contains,
NotContains,
EqualsAnyOrder,
}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct TestBase {
test_name: String,
test_feature: String,
// might need to customize Serde to create Enums
//op: TestOp,
op: String,
msg: String,
test_data: Vec<TestData>,
}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
// need to see how to make this behave like Protobuf "oneof".
// maybe trait (interface) and derive?
pub struct TestData {
locale_test_data: LocaleTestData,
}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct LocaleTestData {
input: String,
output: Option<LocaleTestOutput>,
}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct LocaleTestOutput {
lang: String,
region: String,
subtags: Vec<Vec<String>>,
}

//
// modules
//

#[path = "./runner/runner.rs"]
mod runner;

//
// tests
//

#[cfg(test)]
#[path = "./parser/parser_test.rs"]
mod parser_tests;
134 changes: 134 additions & 0 deletions components/e2etests/src/parser/parser_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
use super::*;

#[test]
fn locale_test_output_deser() {
let data = r#"
{"lang": "en",
"region": "US",
"subtags": [["u", "hc", "buddhist"]]}
"#;

let act_test_output: LocaleTestOutput =
serde_json::from_str(data).expect("cannot parse sample LocaleTestOutput");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use serde_json::json macro - https://docs.serde.rs/serde_json/macro.json.html


let lang = String::from("en");
let region = String::from("US");
let subtags = vec![vec![
String::from("u"),
String::from("hc"),
String::from("buddhist"),
]];
let exp_test_output = LocaleTestOutput {
lang,
region,
subtags,
};

assert_eq!(act_test_output, exp_test_output);
}

#[test]
fn locale_test_data_deser() {
let data = r#"
{"input": "en-US-u-hc-buddhist",
"output": {"lang": "en",
"region": "US",
"subtags": [["u", "hc", "buddhist"]]}}
"#;

let act_test_data: LocaleTestData =
serde_json::from_str(data).expect("cannot parse sample LocaleTestData");

let input = String::from("en-US-u-hc-buddhist");
let lang = String::from("en");
let region = String::from("US");
let subtags = vec![vec![
String::from("u"),
String::from("hc"),
String::from("buddhist"),
]];
let output = Some(LocaleTestOutput {
lang,
region,
subtags,
});
let exp_test_data = LocaleTestData { input, output };

assert_eq!(act_test_data, exp_test_data);
}

#[test]
fn test_data_deser() {
let data = r#"
{"locale_test_data": {"input": "en-US-u-hc-buddhist",
"output": {"lang": "en",
"region": "US",
"subtags": [["u", "hc", "buddhist"]]}}}
"#;

let act_test_data: TestData = serde_json::from_str(data).expect("cannot parse sample TestData");

let input = String::from("en-US-u-hc-buddhist");
let lang = String::from("en");
let region = String::from("US");
let subtags = vec![vec![
String::from("u"),
String::from("hc"),
String::from("buddhist"),
]];
let output = Some(LocaleTestOutput {
lang,
region,
subtags,
});
let locale_test_data = LocaleTestData { input, output };
let exp_test_data = TestData { locale_test_data };

assert_eq!(act_test_data, exp_test_data);
}

#[test]
fn test_base_data_deser() {
let data = r#"
{"test_name": "locale_parser_test",
"test_feature": "locale.parser",
"op": "equal",
"msg": "Parsed locale mismatched expected",
"test_data": [
{"locale_test_data": {"input": "en-US-u-hc-buddhist",
"output": {"lang": "en",
"region": "US",
"subtags": [["u", "hc", "buddhist"]]}}}]}
"#;

let act_test_base: TestBase = serde_json::from_str(data).expect("cannot parse sample TestBase");

let input = String::from("en-US-u-hc-buddhist");
let lang = String::from("en");
let region = String::from("US");
let subtags = vec![vec![
String::from("u"),
String::from("hc"),
String::from("buddhist"),
]];
let output = Some(LocaleTestOutput {
lang,
region,
subtags,
});
let locale_test_data = LocaleTestData { input, output };
let test_data1 = TestData { locale_test_data };
let test_name = String::from("locale_parser_test");
let test_feature = String::from("locale.parser");
let op = String::from("equal");
let msg = String::from("Parsed locale mismatched expected");
let exp_test_base = TestBase {
test_name,
test_feature,
op,
msg,
test_data: vec![test_data1],
};

assert_eq!(act_test_base, exp_test_base);
}
96 changes: 96 additions & 0 deletions components/e2etests/src/runner/runner.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
use super::*;

use icu_locale::extensions::unicode::Key;
use icu_locale::extensions::unicode::Value;
use icu_locale::extensions::Extensions;
use icu_locale::extensions::Unicode;
use icu_locale::subtags::Region;
use icu_locale::subtags::Variant;
use icu_locale::Locale;

fn concat_unicode_subtags(variants: &mut Vec<Vec<String>>, unicode_exts: &Unicode) {
match unicode_exts {
Unicode { keywords, .. } => {
let key_vals: Vec<(Key, Value)> = keywords.to_vec();
for (key, value) in key_vals {
let key_str = String::from(key.as_str());
let value_str: String = format!("{}", value);
let flattened_subtag: Vec<String> = vec![String::from("u"), key_str, value_str];
variants.push(flattened_subtag);
}
}
}
}

impl From<&Locale> for LocaleTestOutput {
fn from(input: &Locale) -> Self {
let language = input.language;
let _script = input.script;
let variants = &input.variants;

let region: Option<Region> = input.region;
let region: &str = match &region {
Some(r) => r.as_str(),
None => "",
};
let region: String = String::from(region);

let lang: String = String::from(language.as_str());

let _script: String = match _script {
Some(s) => String::from(s.as_str()),
None => String::from(""),
};

let variants: Vec<Variant> = variants.to_vec();
let variants: Vec<String> = variants
.into_iter()
.map(|v: Variant| String::from(v.as_str()))
.collect::<Vec<String>>();

// Create `subtags` as a "flattened" version of Variants. A nested
// sequential Vec<Vec<_>> should help preserve order of subtags.
let mut subtags: Vec<Vec<String>> = if variants.len() > 0 {
vec![variants]
} else {
vec![]
};
if !input.extensions.is_empty() {
match &input.extensions {
Extensions { unicode, .. } => concat_unicode_subtags(&mut subtags, unicode),
}
}

let locale_test_output = LocaleTestOutput {
lang,
region,
subtags,
};

locale_test_output
}
}

pub fn run_locale_test(locale_test_data: &LocaleTestData) {
let input: &String = &locale_test_data.input;
let output: &Option<LocaleTestOutput> = &locale_test_data.output;

let actual_locale: Locale = input
.parse()
.expect("Failed to parse test input locale string");
let actual_locale_test_output = LocaleTestOutput::from(&actual_locale);

let expected_locale_test_output: &LocaleTestOutput = output
.as_ref()
.expect("Expected output data required for test");

assert_eq!(&actual_locale_test_output, expected_locale_test_output);
}

//
// tests
//

#[cfg(test)]
#[path = "./runner_test.rs"]
mod runner_tests;
26 changes: 26 additions & 0 deletions components/e2etests/src/runner/runner_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use super::*;

#[test]
fn locale_test() {
let data = r#"
{"test_name": "locale_parser_test",
"test_feature": "locale.parser",
"op": "equal",
"msg": "Parsed locale mismatched expected",
"test_data": [
{"locale_test_data": {"input": "en-US-u-hc-buddhist",
"output": {"lang": "en",
"region": "US",
"subtags": [["u", "hc", "buddhist"]]}}}]}
"#;

let act_test_base: TestBase = serde_json::from_str(data).expect("cannot parse sample TestBase");
let act_test_cases_data: Vec<TestData> = act_test_base.test_data;

assert_eq!(act_test_cases_data.len(), 1);

let act_test_data = &act_test_cases_data[0];
let act_locale_test_data: &LocaleTestData = &act_test_data.locale_test_data;

run_locale_test(&act_locale_test_data);
}
Loading