Beispiel #1
0
            public void Should_FormatMessage_SinglePlaceholder()
            {
                var parametersChecked = 0;

                var testArg = new TestArg(parameters =>
                {
                    parameters.Should().BeNull();
                    parametersChecked++;
                });

                testArg.AllowedParameters = Array.Empty <string>();

                testArg.Name  = "test";
                testArg.Value = "testValue";

                var formattedMessage = ArgHelper.FormatMessage(
                    "test {test}",
                    new[]
                {
                    new ArgPlaceholder()
                    {
                        Name        = "test",
                        Placeholder = "{test}",
                        Parameters  = null
                    },
                },
                    new IArg[]
                {
                    testArg
                });

                formattedMessage.Should().Be("test testValue");

                parametersChecked.Should().Be(1);
            }
Beispiel #2
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");
            }
Beispiel #3
0
            public void Should_ReturnMessage_When_NullArgs()
            {
                var formattedMessage = ArgHelper.FormatMessage("test {test}", new[] { new ArgPlaceholder()
                                                                                      {
                                                                                          Name = "test", Placeholder = "{test}"
                                                                                      } }, null);

                formattedMessage.Should().Be("test {test}");
            }
Beispiel #4
0
            public void Should_FormatMessage_ManyArgs_ManyPlaceholders()
            {
                var parametersChecked1 = 0;

                var testArg1 = new TestArg(parameters =>
                {
                    parameters.Should().BeNull();
                    parametersChecked1++;
                })
                {
                    AllowedParameters = Array.Empty <string>(),
                    Name  = "test1",
                    Value = "testValue1",
                };

                var parametersChecked2 = 0;

                var testArg2 = new TestArg(parameters =>
                {
                    parameters.Should().BeNull();
                    parametersChecked2++;
                })
                {
                    AllowedParameters = Array.Empty <string>(),
                    Name  = "test2",
                    Value = "testValue2",
                };

                var formattedMessage = ArgHelper.FormatMessage(
                    "test {test1} {test2} {test1}",
                    new[]
                {
                    new ArgPlaceholder()
                    {
                        Name        = "test1",
                        Placeholder = "{test1}",
                        Parameters  = null
                    },
                    new ArgPlaceholder()
                    {
                        Name        = "test2",
                        Placeholder = "{test2}",
                        Parameters  = null
                    },
                },
                    new IArg[]
                {
                    testArg1,
                    testArg2
                });

                formattedMessage.Should().Be("test testValue1 testValue2 testValue1");

                parametersChecked1.Should().Be(1);
                parametersChecked2.Should().Be(1);
            }
Beispiel #5
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);
            }
Beispiel #6
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");
            }
Beispiel #7
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");
            }
Beispiel #8
0
            public void Should_FormatMessage_NotPassParametersToArgs_When_AtLeastOneParameterNotAllowed()
            {
                var parameters = new Dictionary <string, string>()
                {
                    ["param1"] = "paramValue1",
                    ["param2"] = "paramValue2",
                };

                var parametersChecked1 = 0;

                var testArg1 = new TestArg(p =>
                {
                    p.Should().BeSameAs(parameters);
                    parametersChecked1++;
                })
                {
                    AllowedParameters = new[] { "param1", "param2" },
                    Name  = "test1",
                    Value = "testValue1",
                };

                var formattedMessage = ArgHelper.FormatMessage(
                    "test1 {test1|param1=paramValue1|param2=paramValue2} {test1|param1=paramValue1|param2=paramValue2|invalidParameter=someValue}",
                    new[]
                {
                    new ArgPlaceholder()
                    {
                        Name        = "test1",
                        Placeholder = "{test1|param1=paramValue1|param2=paramValue2}",
                        Parameters  = parameters,
                    },
                    new ArgPlaceholder()
                    {
                        Name        = "test1",
                        Placeholder = "{test1|param1=paramValue1|param2=paramValue2|invalidParameter=someValue}",
                        Parameters  = new Dictionary <string, string>()
                        {
                            ["param1"]           = "paramValue1",
                            ["param2"]           = "paramValue2",
                            ["invalidParameter"] = "someValue"
                        },
                    },
                },
                    new IArg[]
                {
                    testArg1
                });

                formattedMessage.Should().Be("test1 testValue1 {test1|param1=paramValue1|param2=paramValue2|invalidParameter=someValue}");

                parametersChecked1.Should().Be(1);
            }
Beispiel #9
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);
        }
Beispiel #10
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");
            }
Beispiel #11
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();
            }
Beispiel #12
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
            });
        }
Beispiel #13
0
        public static IReadOnlyList <string> TranslateMessagesWithPathPlaceholders(string path, IReadOnlyList <string> errorMessages, IReadOnlyDictionary <int, IReadOnlyList <ArgPlaceholder> > indexedPathsPlaceholders)
        {
            var pathArgs = CreatePathArgsForPath(path);

            var result = new string[errorMessages.Count];

            for (var i = 0; i < errorMessages.Count; ++i)
            {
                if (indexedPathsPlaceholders.ContainsKey(i))
                {
                    result[i] = ArgHelper.FormatMessage(errorMessages[i], indexedPathsPlaceholders[i], pathArgs);
                }
                else
                {
                    result[i] = errorMessages[i];
                }
            }

            return(result);
        }
Beispiel #14
0
            public void Should_FormatMessage_PassParametersToArgs()
            {
                var parameters = new Dictionary <string, string>()
                {
                    ["param1"] = "paramValue1",
                };

                var parametersChecked1 = 0;

                var testArg1 = new TestArg(p =>
                {
                    p.Should().BeSameAs(parameters);
                    parametersChecked1++;
                })
                {
                    AllowedParameters = new[] { "param1" },
                    Name  = "test1",
                    Value = "testValue1",
                };

                var formattedMessage = ArgHelper.FormatMessage(
                    "test1 {test1|param1=paramValue1}",
                    new[]
                {
                    new ArgPlaceholder()
                    {
                        Name        = "test1",
                        Placeholder = "{test1|param1=paramValue1}",
                        Parameters  = parameters
                    },
                },
                    new IArg[]
                {
                    testArg1
                });

                formattedMessage.Should().Be("test1 testValue1");

                parametersChecked1.Should().Be(1);
            }
Beispiel #15
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();
            }
Beispiel #16
0
            public void Should_FormatMessage_ManyPlaceholders_SingleArg()
            {
                var parametersSet = new[]
                {
                    new Dictionary <string, string>()
                    {
                        ["p1"] = "v1",
                    },
                    new Dictionary <string, string>()
                    {
                        ["p2"] = "v2",
                    },
                    new Dictionary <string, string>()
                    {
                        ["p3"] = "v3",
                    }
                };

                var parametersChecked1 = 0;

                var testArg1 = new TestArg(parameters =>
                {
                    parameters.Should().BeSameAs(parametersSet[parametersChecked1]);
                    parametersChecked1++;
                })
                {
                    AllowedParameters = new[] { "p1", "p2", "p3" },
                    Name  = "test1",
                    Value = "testValue1",
                };

                var formattedMessage = ArgHelper.FormatMessage(
                    "test {test1|p1=v1} {test1|p2=v2} {test1|p3=v3}",
                    new[]
                {
                    new ArgPlaceholder()
                    {
                        Name        = "test1",
                        Placeholder = "{test1|p1=v1}",
                        Parameters  = parametersSet[0],
                    },
                    new ArgPlaceholder()
                    {
                        Name        = "test1",
                        Placeholder = "{test1|p2=v2}",
                        Parameters  = parametersSet[1],
                    },
                    new ArgPlaceholder()
                    {
                        Name        = "test1",
                        Placeholder = "{test1|p3=v3}",
                        Parameters  = parametersSet[2],
                    },
                },
                    new IArg[]
                {
                    testArg1
                });

                formattedMessage.Should().Be("test testValue1 testValue1 testValue1");

                parametersChecked1.Should().Be(3);
            }
Beispiel #17
0
            public void Should_ReturnMessage_When_NullPlaceholders()
            {
                var formattedMessage = ArgHelper.FormatMessage("test {test}", null, new[] { Arg.Text("test", "XXX") });

                formattedMessage.Should().Be("test {test}");
            }
Beispiel #18
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());
        }
Beispiel #19
0
            public void Should_ThrowException_When_NullMessage()
            {
                Action action = () => ArgHelper.ExtractPlaceholders(null);

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

                placeholders.Should().BeEmpty();
            }
Beispiel #21
0
            public void Should_FormatMessage_ManyArgs_ManyPlaceholders_ManyParams()
            {
                var paramSet1 = new Dictionary <string, string>()
                {
                    ["p1"] = "v1",
                };

                var parametersChecked1 = 0;

                var testArg1 = new TestArg(parameters =>
                {
                    parameters.Should().BeSameAs(paramSet1);
                    parametersChecked1++;
                })
                {
                    AllowedParameters = new[] { "p1" },
                    Name  = "test1",
                    Value = "testValue1",
                };

                var paramSet2 = new Dictionary <string, string>()
                {
                    ["p2"] = "v2",
                };

                var parametersChecked2 = 0;

                var testArg2 = new TestArg(parameters =>
                {
                    parameters.Should().BeSameAs(paramSet2);
                    parametersChecked2++;
                })
                {
                    AllowedParameters = new[] { "p2" },
                    Name  = "test2",
                    Value = "testValue2",
                };

                var formattedMessage = ArgHelper.FormatMessage(
                    "test {test1|p1=v1} {test2|p2=v2} {test1|p1=v1}",
                    new[]
                {
                    new ArgPlaceholder()
                    {
                        Name        = "test1",
                        Placeholder = "{test1|p1=v1}",
                        Parameters  = paramSet1,
                    },
                    new ArgPlaceholder()
                    {
                        Name        = "test2",
                        Placeholder = "{test2|p2=v2}",
                        Parameters  = paramSet2
                    },
                },
                    new IArg[]
                {
                    testArg1,
                    testArg2
                });

                formattedMessage.Should().Be("test testValue1 testValue2 testValue1");

                parametersChecked1.Should().Be(1);
                parametersChecked2.Should().Be(1);
            }
Beispiel #22
0
            public void Should_ReturnMessage_When_NoPlaceholders()
            {
                var formattedMessage = ArgHelper.FormatMessage("test {test}", Array.Empty <ArgPlaceholder>(), new[] { Arg.Text("test", "XXX") });

                formattedMessage.Should().Be("test {test}");
            }
Beispiel #23
0
            public void Should_ReturnEmptyCollection_When_EmptyName(string message)
            {
                IReadOnlyCollection <ArgPlaceholder> placeholders = ArgHelper.ExtractPlaceholders(message);

                placeholders.Should().BeEmpty();
            }
Beispiel #24
0
            public void Should_ReturnEmptyString_When_MessageIsNull()
            {
                var formattedMessage = ArgHelper.FormatMessage(null, Array.Empty <ArgPlaceholder>(), Array.Empty <IArg>());

                formattedMessage.Should().Be(string.Empty);
            }