-
Notifications
You must be signed in to change notification settings - Fork 213
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
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
94a1813
WIP on E2E data-driven tests
echeran ba7f34c
adds JSON parser for test data
echeran c43c78c
adds second unit test file for test runner
echeran 799da31
Add SerDe for Locale to make it serializable
echeran ed7c990
add Locale -> LocaleTestOutput fn
echeran 3164351
Add e2e runner test for Locale parsing
echeran 3d82314
formatting fixes
echeran 1c9ba02
fixes test paths in Cargo.toml
echeran File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,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"] |
This file contains hidden or 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,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; |
This file contains hidden or 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,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"); | ||
|
||
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); | ||
} |
This file contains hidden or 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,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 ®ion { | ||
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; |
This file contains hidden or 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,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); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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