#![allow(
clippy::unwrap_in_result,
reason = "unwraps are allowed anywhere in tests"
)]
#![allow(
clippy::indexing_slicing,
reason = "unwraps are allowed anywhere in tests"
)]
use assert_matches::assert_matches;
use crate::{
json::{self, FromJson as _},
warning::test::VerdictTestExt,
Verdict,
};
use super::{Code, Warning};
#[test]
fn should_create_currency_without_issue() {
const JSON: &str = r#"{ "currency": "EUR" }"#;
let (code, warnings) = parse_code_from_json(JSON).unwrap().into_parts();
assert_eq!(Code::Eur, code);
assert!(warnings.is_empty(), "{:#?}", warnings.path_id_map());
}
#[test]
fn should_raise_currency_content_issue() {
const JSON: &str = r#"{ "currency": "VVV" }"#;
let error = parse_code_from_json(JSON).unwrap_only_error();
assert_matches!(error.into_warning(), Warning::InvalidCode);
}
#[test]
fn should_raise_currency_case_issue() {
const JSON: &str = r#"{ "currency": "eur" }"#;
let (code, warnings) = parse_code_from_json(JSON).unwrap().into_parts();
let warnings = warnings.path_map();
let warnings = &*warnings["$.currency"];
assert_eq!(code, Code::Eur);
assert_matches!(warnings, [Warning::PreferUpperCase]);
}
#[test]
fn should_raise_currency_xts_issue() {
const JSON: &str = r#"{ "currency": "xts" }"#;
let (code, warnings) = parse_code_from_json(JSON).unwrap().into_parts();
let warnings = warnings.path_map();
let warnings = &*warnings["$.currency"];
assert_eq!(code, Code::Xts);
assert_matches!(
warnings,
[Warning::PreferUpperCase, Warning::InvalidCodeXTS]
);
}
#[test]
fn should_raise_currency_xxx_issue() {
const JSON: &str = r#"{ "currency": "xxx" }"#;
let (code, warnings) = parse_code_from_json(JSON).unwrap().into_parts();
let warnings = warnings.path_map();
let warnings = &*warnings["$.currency"];
assert_eq!(code, Code::Xxx);
assert_matches!(
warnings,
[Warning::PreferUpperCase, Warning::InvalidCodeXXX]
);
}
#[track_caller]
fn parse_code_from_json(json: &str) -> Verdict<Code, Warning> {
let json = json::parse(json).unwrap();
let currency_elem = json.find_field("currency").unwrap();
Code::from_json(currency_elem.element())
}