-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3d6f0f5
commit 8fe4dcd
Showing
5 changed files
with
164 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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,54 @@ | ||
#[cfg(feature = "fluent")] | ||
mod tests { | ||
use fluent::{FluentBundle, FluentResource}; | ||
use serde::Deserialize; | ||
use serde_json::json; | ||
use serde_valid::{fluent::Localize, Validate}; | ||
use unic_langid::LanguageIdentifier; | ||
|
||
fn get_bundle(source: impl Into<String>) -> FluentBundle<FluentResource> { | ||
let res = FluentResource::try_new(source.into()).expect("Failed to parse an FTL string."); | ||
|
||
let langid_en: LanguageIdentifier = "en-US".parse().expect("Parsing failed"); | ||
let mut bundle = FluentBundle::new(vec![langid_en]); | ||
bundle.add_resource(res).unwrap(); | ||
|
||
bundle | ||
} | ||
|
||
#[test] | ||
fn fluent_error() { | ||
#[derive(Debug, Deserialize, Validate)] | ||
struct Test { | ||
#[validate(minimum = 5, fluent("hello-world"))] | ||
a: u32, | ||
#[validate(maximum = 10, fluent("intro", name = "taro"))] | ||
b: u32, | ||
} | ||
|
||
let test = Test { a: 1, b: 11 }; | ||
let a = test.validate().unwrap_err().localize(&get_bundle( | ||
["hello-world = Hello, world!", "intro = Welcome, { $name }."].join("\n"), | ||
)); | ||
|
||
assert_eq!( | ||
a.to_string(), | ||
json!({ | ||
"errors": [], | ||
"properties": { | ||
"a": { | ||
"errors": [ | ||
"Hello, world!" | ||
] | ||
}, | ||
"b": { | ||
"errors": [ | ||
"Welcome, \u{2068}taro\u{2069}." | ||
] | ||
} | ||
} | ||
}) | ||
.to_string() | ||
); | ||
} | ||
} |
This file contains 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 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 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 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