示例#1
0
        public void Get_Should_ReturnSelectedTranslator_And_Translate_When_ExistingPhrases()
        {
            var translations = new[]
            {
                new Translation("test1", new Dictionary <string, string>
                {
                    { "phrase1", "PHRASE_1" },
                    { "phrase2", "PHRASE_2" },
                    { "phrase{0}", "PHRASE_{0}" }
                }),
                new Translation("test2", new Dictionary <string, string>
                {
                    { "phrase1", "PHRASE=1" },
                    { "phrase2", "PHRASE=2" },
                    { "phrase{0}", "PHRASE={0}" }
                })
            };

            var translatorsRepository = new TranslatorsRepository(translations);

            var translator1 = translatorsRepository.Get("test1");
            var translator2 = translatorsRepository.Get("test2");

            Assert.Equal("PHRASE_1", translator1(new Error("phrase1")));
            Assert.Equal("PHRASE_2", translator1(new Error("phrase2")));
            Assert.Equal("PHRASE_123", translator1(new Error("phrase{0}", new[] { Arg.Text("0", "123") })));

            Assert.Equal("PHRASE=1", translator2(new Error("phrase1")));
            Assert.Equal("PHRASE=2", translator2(new Error("phrase2")));
            Assert.Equal("PHRASE=123", translator2(new Error("phrase{0}", new[] { Arg.Text("0", "123") })));
        }
示例#2
0
        public IValidationResult <T> Validate <T>(T model, Func <IValidationOptions, IValidationOptions> setOptions = null)
            where T : class
        {
            if (!Types.Contains(typeof(T)))
            {
                throw new SpecificationNotFoundException(typeof(T));
            }

            var validationOptions = setOptions != null
                ? OptionsService.GetVerifiedValidationOptions(setOptions(Options.ValidationOptions))
                : Options.ValidationOptions;

            var defaultTranslator = validationOptions.TranslationName != null
                ? TranslatorsRepository.Get(validationOptions.TranslationName)
                : TranslatorsRepository.GetOriginal();

            var translationProxy = new TranslationProxy(defaultTranslator, TranslatorsRepository);

            if (model == null)
            {
                switch (validationOptions.NullRootStrategy)
                {
                case NullRootStrategy.ArgumentNullException:
                {
                    throw new ArgumentNullException(nameof(model));
                }

                case NullRootStrategy.NoErrors:
                {
                    return(new ValidationResult <T>(Id, translationProxy, Options.ValidationOptions));
                }

                default:
                {
                    var report = new ErrorsCollection();

                    report.AddError(Options.ValidationOptions.RequiredError);

                    return(new ValidationResult <T>(Id, translationProxy, Options.ValidationOptions, null, report));
                }
                }
            }

            var specification = ValidatorsFactory.GetOrInit <T>();

            var executionContext = new ExecutionContext(validationOptions, ValidatorsFactory);

            var errorsCollection = ValidatorExecutor.Execute(specification, model, executionContext, validationOptions.ValidationStrategy, 0);

            return(new ValidationResult <T>(Id, translationProxy, Options.ValidationOptions, model, errorsCollection));
        }
示例#3
0
        public void Get_Should_ThrowException_When_NullTranslationName()
        {
            var translations = new[]
            {
                new Translation("test1", new Dictionary <string, string>
                {
                    { "phrase1", "PHRASE_1" },
                    { "phrase2", "PHRASE_2" },
                    { "phrase{0}", "PHRASE_{0}" }
                })
            };

            var translatorsRepository = new TranslatorsRepository(translations);

            Assert.Throws <ArgumentNullException>(() => { translatorsRepository.Get(null); });
        }
示例#4
0
        public void Get_Should_ThrowException_When_TranslationNotFound()
        {
            var translations = new[]
            {
                new Translation("test1", new Dictionary <string, string>
                {
                    { "phrase1", "PHRASE_1" },
                    { "phrase2", "PHRASE_2" },
                    { "phrase{0}", "PHRASE_{0}" }
                })
            };

            var translatorsRepository = new TranslatorsRepository(translations);

            var exception = Assert.Throws <TranslationNotFoundException>(() => { translatorsRepository.Get("some_another"); });

            Assert.Equal("some_another", exception.Name);
        }
示例#5
0
        public void Get_Should_GetSelectedTranslator_When_NonExistingPhrases()
        {
            var translations = new[]
            {
                new Translation("test1", new Dictionary <string, string>
                {
                    { "phrase1", "PHRASE_1" },
                    { "phrase2", "PHRASE_2" },
                    { "phrase{0}", "PHRASE_{0}" }
                })
            };

            var translatorsRepository = new TranslatorsRepository(translations);

            var translator = translatorsRepository.Get("test1");

            Assert.Equal("phrase123", translator(new Error("phrase123")));
            Assert.Equal("some_phrase", translator(new Error("some_phrase")));
            Assert.Equal("some_another_phrase_123", translator(new Error("some_another_phrase_{0}", new[] { Arg.Text("0", "123") })));
        }