Esempio n. 1
0
 public static void ParseReturnsTheCorrectDecimalSamples(string count, string rule, params string [] expectedSamples)
 {
     Assert.Equal(expectedSamples, PluralRule.Parse(count, rule, out _).DecimalSamples);
 }
Esempio n. 2
0
        public static PluralRules [] Parse(XDocument document)
        {
            var ruleSets = new List <PluralRules> ( );

            var plurals = document.Descendants("plurals").Single( );
            var type    = plurals.Attribute("type")?.Value;
            var prefix  = type == "cardinal" ? "CardinalRuleSet" :
                          type == "ordinal"  ? "OrdinalRuleSet"  :
                          throw new XmlException("Invalid plurals type attribute");
            var names = new Dictionary <string, int> ( );

            foreach (var pluralRules in plurals.Descendants("pluralRules"))
            {
                var ruleSet = new PluralRules( );

                ruleSet.Locales = pluralRules.Attribute("locales").Value
                                  .Replace("_", "-")
                                  .Split(' ');

                var operands    = new HashSet <string>  ( );
                var rules       = new List <PluralRule> ( );
                var defaultRule = DefaultPluralRule;

                foreach (var pluralRule in pluralRules.Descendants("pluralRule"))
                {
                    var rule = PluralRule.Parse(pluralRule.Attribute("count").Value,
                                                pluralRule.Value,
                                                out var ruleOperands);

                    if (ruleOperands != null)
                    {
                        foreach (var operand in ruleOperands)
                        {
                            operands.Add(operand);
                        }
                    }

                    if (rule.Rule != null)
                    {
                        rules.Add(rule);
                    }
                    else
                    {
                        defaultRule = rule;
                    }
                }

                foreach (var rule in rules)
                {
                    ruleSet.PluralForm |= rule.PluralForm;
                }

                var name = (string)null;

                var isDefaultRuleSet = rules.Count == 0 && ruleSet.PluralForm == PluralForm.Other;
                if (!isDefaultRuleSet)
                {
                    name = prefix + ruleSet.PluralForm.ToString( ).Replace(", ", string.Empty);
                    if (!names.TryGetValue(name, out var index))
                    {
                        index = -1;
                    }

                    names [name] = ++index;

                    name += (char)('A' + index);
                }
                else
                {
                    name = "Default" + prefix;
                }

                ruleSet.Name        = name;
                ruleSet.Operands    = operands.ToArray( );
                ruleSet.Rules       = rules.ToArray( );
                ruleSet.DefaultRule = defaultRule;

                ruleSets.Add(ruleSet);
            }

            return(ruleSets.ToArray( ));
        }
Esempio n. 3
0
 public static void ParseReturnsTheCorrectRuleCode(string count, string rule, string expectedRuleCode)
 {
     Assert.Equal(expectedRuleCode, PluralRule.Parse(count, rule, out _).RuleCode);
 }
Esempio n. 4
0
        public static void ParseReturnsTheCorrectOperands(string count, string rule, params string [] expectedOperands)
        {
            PluralRule.Parse(count, rule, out var operands);

            Assert.Equal(expectedOperands, operands);
        }
Esempio n. 5
0
 public static void ParseReturnsTheCorrectRule(string count, string rule, string expectedExpression)
 {
     Assert.Equal(expectedExpression, PluralRule.Parse(count, rule, out _).Rule?.ToString( ));
 }
Esempio n. 6
0
 public static void ParseThrowsOnInvalidPluralForm(string count, string rule)
 {
     Assert.Throws <FormatException> (() => PluralRule.Parse(count, rule, out _));
 }
Esempio n. 7
0
 public static void ParseReturnsTheCorrectPluralForm(string count, string rule, PluralForm expectedPluralForm)
 {
     Assert.Equal(expectedPluralForm, PluralRule.Parse(count, rule, out _).PluralForm);
 }