Example #1
0
            public void Should_Parse_When_ManyVariables_With_Parameters()
            {
                IReadOnlyCollection <ArgPlaceholder> placeholders = ArgHelper.ExtractPlaceholders("abc {first|p1=v1} {second|p21=v21|p22=v22} def {third|p31=v31|p32=v32|p33=v33}");

                placeholders.Count.Should().Be(3);

                placeholders.ElementAt(0).Placeholder.Should().Be("{first|p1=v1}");
                placeholders.ElementAt(0).Name.Should().Be("first");
                placeholders.ElementAt(0).Parameters.Count.Should().Be(1);
                placeholders.ElementAt(0).Parameters.ElementAt(0).Key.Should().Be("p1");
                placeholders.ElementAt(0).Parameters.ElementAt(0).Value.Should().Be("v1");

                placeholders.ElementAt(1).Placeholder.Should().Be("{second|p21=v21|p22=v22}");
                placeholders.ElementAt(1).Name.Should().Be("second");
                placeholders.ElementAt(1).Parameters.Count.Should().Be(2);
                placeholders.ElementAt(1).Parameters.ElementAt(0).Key.Should().Be("p21");
                placeholders.ElementAt(1).Parameters.ElementAt(0).Value.Should().Be("v21");
                placeholders.ElementAt(1).Parameters.ElementAt(1).Key.Should().Be("p22");
                placeholders.ElementAt(1).Parameters.ElementAt(1).Value.Should().Be("v22");

                placeholders.ElementAt(2).Placeholder.Should().Be("{third|p31=v31|p32=v32|p33=v33}");
                placeholders.ElementAt(2).Name.Should().Be("third");
                placeholders.ElementAt(2).Parameters.Count.Should().Be(3);
                placeholders.ElementAt(2).Parameters.ElementAt(0).Key.Should().Be("p31");
                placeholders.ElementAt(2).Parameters.ElementAt(0).Value.Should().Be("v31");
                placeholders.ElementAt(2).Parameters.ElementAt(1).Key.Should().Be("p32");
                placeholders.ElementAt(2).Parameters.ElementAt(1).Value.Should().Be("v32");
                placeholders.ElementAt(2).Parameters.ElementAt(2).Key.Should().Be("p33");
                placeholders.ElementAt(2).Parameters.ElementAt(2).Value.Should().Be("v33");
            }
Example #2
0
            public void Should_Parse_When_SingleVariable()
            {
                IReadOnlyCollection <ArgPlaceholder> placeholders = ArgHelper.ExtractPlaceholders("abc {single} def");

                placeholders.Count.Should().Be(1);

                placeholders.ElementAt(0).Placeholder.Should().Be("{single}");
                placeholders.ElementAt(0).Name.Should().Be("single");
                placeholders.ElementAt(0).Parameters.Count.Should().Be(0);
            }
Example #3
0
            public void Should_Parse_When_SingleVariable_With_SingleParameter()
            {
                IReadOnlyCollection <ArgPlaceholder> placeholders = ArgHelper.ExtractPlaceholders("abc {single|param1=value1} def");

                placeholders.Count.Should().Be(1);

                placeholders.ElementAt(0).Placeholder.Should().Be("{single|param1=value1}");
                placeholders.ElementAt(0).Name.Should().Be("single");
                placeholders.ElementAt(0).Parameters.Count.Should().Be(1);
                placeholders.ElementAt(0).Parameters.ElementAt(0).Key.Should().Be("param1");
                placeholders.ElementAt(0).Parameters.ElementAt(0).Value.Should().Be("value1");
            }
Example #4
0
            public void Should_Extract_And_SquashDuplicates()
            {
                IReadOnlyCollection <ArgPlaceholder> placeholders = ArgHelper.ExtractPlaceholders("abc {single|param1=value1}  def {single|param1=value1}");

                placeholders.Should().ContainSingle();

                placeholders.Single().Placeholder.Should().Be("{single|param1=value1}");
                placeholders.Single().Name.Should().Be("single");
                placeholders.Single().Parameters.Should().ContainSingle();
                placeholders.Single().Parameters.Single().Key.Should().Be("param1");
                placeholders.Single().Parameters.Single().Value.Should().Be("value1");
            }
Example #5
0
        private bool TryExtractSpecialArgs(string translationName, string message, out IReadOnlyList <ArgPlaceholder> specialPlaceholders, out IReadOnlyList <IArg> specialArgs)
        {
            specialPlaceholders = ArgHelper.ExtractPlaceholders(message).Where(p => p.Name == TranslationArg.Name).ToArray();

            if (specialPlaceholders.Any())
            {
                specialArgs = TranslationArgs[translationName];

                return(true);
            }

            specialArgs = null;

            return(false);
        }
Example #6
0
            public void Should_ParseOnlyValidOnes()
            {
                IReadOnlyCollection <ArgPlaceholder> placeholders = ArgHelper.ExtractPlaceholders("{valid} abc {invalid|param1=value1|param1=value2} {valid2|param=value} def {invalid|param1=value1param1=value2} xyz {invalid2|param1value1param1value2}");

                placeholders.Count.Should().Be(2);

                placeholders.ElementAt(0).Placeholder.Should().Be("{valid}");
                placeholders.ElementAt(0).Name.Should().Be("valid");
                placeholders.ElementAt(0).Parameters.Count.Should().Be(0);

                placeholders.ElementAt(1).Placeholder.Should().Be("{valid2|param=value}");
                placeholders.ElementAt(1).Name.Should().Be("valid2");
                placeholders.ElementAt(1).Parameters.Count.Should().Be(1);
                placeholders.ElementAt(1).Parameters.ElementAt(0).Key.Should().Be("param");
                placeholders.ElementAt(1).Parameters.ElementAt(0).Value.Should().Be("value");
            }
Example #7
0
            public void Should_Parse_When_ManyVariables()
            {
                IReadOnlyCollection <ArgPlaceholder> placeholders = ArgHelper.ExtractPlaceholders("abc {first} {second} def {third}");

                placeholders.Count.Should().Be(3);

                placeholders.ElementAt(0).Placeholder.Should().Be("{first}");
                placeholders.ElementAt(0).Name.Should().Be("first");
                placeholders.ElementAt(0).Parameters.Should().BeEmpty();

                placeholders.ElementAt(1).Placeholder.Should().Be("{second}");
                placeholders.ElementAt(1).Name.Should().Be("second");
                placeholders.ElementAt(1).Parameters.Should().BeEmpty();

                placeholders.ElementAt(2).Placeholder.Should().Be("{third}");
                placeholders.ElementAt(2).Name.Should().Be("third");
                placeholders.ElementAt(2).Parameters.Should().BeEmpty();
            }
Example #8
0
        public TranslationResult TranslateMessages(string translationName, IError error)
        {
            ThrowHelper.NullArgument(error, nameof(error));
            ThrowHelper.NullInCollection(error.Messages, $"{nameof(error)}.{nameof(error.Messages)}");
            ThrowHelper.NullInCollection(error.Args, $"{nameof(error)}.{nameof(error.Args)}");

            var translation = Translations[translationName];

            var messages = new string[error.Messages.Count];

            Dictionary <int, IReadOnlyList <ArgPlaceholder> > indexedPathPlaceholders = null;

            for (var i = 0; i < error.Messages.Count; ++i)
            {
                var key = error.Messages.ElementAt(i);

                var message = translation.ContainsKey(key) ? translation[key] : key;

                var placeholders = ArgHelper.ExtractPlaceholders(message);

                messages[i] = ArgHelper.FormatMessage(message, placeholders, error.Args);

                if (TryExtractSpecialArgs(translationName, messages[i], out var specialPlaceholders, out var specialArgs))
                {
                    messages[i] = ArgHelper.FormatMessage(messages[i], specialPlaceholders, specialArgs);
                }

                if (TryExtractPathPlaceholders(messages[i], out var pathPlaceholders))
                {
                    if (indexedPathPlaceholders == null)
                    {
                        indexedPathPlaceholders = new Dictionary <int, IReadOnlyList <ArgPlaceholder> >(messages.Length - i);
                    }

                    indexedPathPlaceholders.Add(i, pathPlaceholders);
                }
            }

            return(new TranslationResult
            {
                Messages = messages,
                IndexedPathPlaceholders = indexedPathPlaceholders ?? _emptyIndexedPathPlaceholders
            });
        }
Example #9
0
            public void Should_Parse_When_ManySameVariables_With_DifferentParameters()
            {
                IReadOnlyCollection <ArgPlaceholder> placeholders = ArgHelper.ExtractPlaceholders("abc {first|p1=v1} {first|p21=v21|p22=v22} def {first}");

                placeholders.Count.Should().Be(3);

                placeholders.ElementAt(0).Placeholder.Should().Be("{first|p1=v1}");
                placeholders.ElementAt(0).Name.Should().Be("first");
                placeholders.ElementAt(0).Parameters.Should().ContainSingle();
                placeholders.ElementAt(0).Parameters.Single().Key.Should().Be("p1");
                placeholders.ElementAt(0).Parameters.Single().Value.Should().Be("v1");

                placeholders.ElementAt(1).Placeholder.Should().Be("{first|p21=v21|p22=v22}");
                placeholders.ElementAt(1).Name.Should().Be("first");
                placeholders.ElementAt(1).Parameters.Count.Should().Be(2);
                placeholders.ElementAt(1).Parameters.ElementAt(0).Key.Should().Be("p21");
                placeholders.ElementAt(1).Parameters.ElementAt(0).Value.Should().Be("v21");
                placeholders.ElementAt(1).Parameters.ElementAt(1).Key.Should().Be("p22");
                placeholders.ElementAt(1).Parameters.ElementAt(1).Value.Should().Be("v22");

                placeholders.ElementAt(2).Placeholder.Should().Be("{first}");
                placeholders.ElementAt(2).Name.Should().Be("first");
                placeholders.ElementAt(2).Parameters.Should().BeEmpty();
            }
Example #10
0
        private static bool TryExtractPathPlaceholders(string message, out ArgPlaceholder[] placeholders)
        {
            placeholders = ArgHelper.ExtractPlaceholders(message).Where(p => p.Name == NameArgName || p.Name == PathArgName).ToArray();

            return(placeholders.Any());
        }
Example #11
0
            public void Should_ThrowException_When_NullMessage()
            {
                Action action = () => ArgHelper.ExtractPlaceholders(null);

                action.Should().ThrowExactly <ArgumentNullException>();
            }
Example #12
0
            public void Should_ReturnEmptyCollection_When_DuplicateParameter()
            {
                IReadOnlyCollection <ArgPlaceholder> placeholders = ArgHelper.ExtractPlaceholders("abc  {invalid|param1=value1|param1=value2} def");

                placeholders.Should().BeEmpty();
            }
Example #13
0
            public void Should_ReturnEmptyCollection_When_EmptyName(string message)
            {
                IReadOnlyCollection <ArgPlaceholder> placeholders = ArgHelper.ExtractPlaceholders(message);

                placeholders.Should().BeEmpty();
            }