Debian Codegraph @

debian/rust-vendor/icu_plurals/tests/rules.rs

raw
1 // This file is part of ICU4X. For terms of use, please see the file
2 // called LICENSE at the top level of the ICU4X source tree
3 // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4
5 mod fixtures;
6
7 use icu_plurals::PluralOperands;
8 use icu_plurals::provider::rules::{
9 reference::test_condition,
10 reference::{Lexer, parse, parse_condition, serialize},
11 };
12
13 #[test]
14 fn test_parsing_operands() {
15 let test_set: fixtures::RuleTestSet = serde_json::from_str(include_str!("fixtures/rules.json"))
16 .expect("Failed to read a fixture");
17
18 for test in test_set.0 {
19 match test.output {
20 fixtures::RuleTestOutput::Value(val) => {
21 // Test that lexer completes.
22 let lex = Lexer::new(test.rule.as_bytes());
23 lex.count();
24
25 // Test that rule matches test.
26 let ast = parse_condition(test.rule.as_bytes()).expect("Failed to parse.");
27 let operands: PluralOperands = test.input.into();
28
29 if val {
30 assert!(
31 test_condition(&ast, &operands),
32 "\nExpected true\n\
33 AST: {ast:#?}\n\
34 Operands: {operands:#?}\n"
35 );
36 } else {
37 assert!(
38 !test_condition(&ast, &operands),
39 "\nExpected false\n\
40 AST: {ast:#?}\n\
41 Operands: {operands:#?}\n"
42 );
43 }
44
45 // Test that parse/serialize roundtrip completes.
46 let ast = parse(test.rule.as_bytes()).expect("Failed to parse.");
47 let mut string = String::new();
48 assert!(serialize(&ast, &mut string).is_ok());
49 assert_eq!(string, test.rule);
50 }
51 fixtures::RuleTestOutput::Error(val) => {
52 let err = parse(test.rule.as_bytes()).unwrap_err();
53 assert_eq!(format!("{err:?}"), val);
54 }
55 }
56 }
57 }