Exemplo n.º 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);
            }
Exemplo n.º 2
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}");
            }
Exemplo n.º 3
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);
            }
Exemplo n.º 4
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);
            }
Exemplo n.º 5
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
            });
        }
Exemplo n.º 6
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);
        }
Exemplo n.º 7
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);
            }
Exemplo n.º 8
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}");
            }
Exemplo n.º 9
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);
            }
Exemplo n.º 10
0
            public void Should_ReturnMessage_When_NullPlaceholders()
            {
                var formattedMessage = ArgHelper.FormatMessage("test {test}", null, new[] { Arg.Text("test", "XXX") });

                formattedMessage.Should().Be("test {test}");
            }
Exemplo n.º 11
0
            public void Should_ReturnEmptyString_When_MessageIsNull()
            {
                var formattedMessage = ArgHelper.FormatMessage(null, Array.Empty <ArgPlaceholder>(), Array.Empty <IArg>());

                formattedMessage.Should().Be(string.Empty);
            }
Exemplo n.º 12
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);
            }