public static void ValidateAddTaskMethod(CSharpCompilation compilation, CSharpSyntaxTree tree,
                                                 SemanticModel model, Assembly assembly)
        {
            ValidateTasksProperty(compilation, tree, model, assembly);

            ValidatorsExtensions.ExecuteSafe(() =>
            {
                var viewModel            = (dynamic)assembly.CreateInstance("DotvvmAcademy.Tutorial.ViewModels.Lesson2ViewModel");
                viewModel.AddedTaskTitle = "test";
                viewModel.AddTask();

                if (viewModel.Tasks.Count != 1)
                {
                    throw new CodeValidationException(Lesson2Texts.AddTaskShouldAddTaskObject);
                }
                if (viewModel.Tasks[0].Title != "test")
                {
                    throw new CodeValidationException(Lesson2Texts.AddTaskInvalidTaskText);
                }
                if (viewModel.AddedTaskTitle != "")
                {
                    throw new CodeValidationException(Lesson2Texts.AddTaskTitleNotReset);
                }
            });
        }
        public void Validate(CSharpCompilation compilation, CSharpSyntaxTree tree, SemanticModel model,
                             Assembly assembly)
        {
            Lesson4CommonValidator.ValidateStep12(tree, model, assembly);


            ValidatorsExtensions.ExecuteSafe(() =>
            {
                var viewModel = (dynamic)assembly.CreateInstance("DotvvmAcademy.Tutorial.ViewModels.Lesson4ViewModel");

                var now = DateTime.UtcNow;

                viewModel.SubscriptionFrom = now.AddDays(3);
                viewModel.SubscriptionTo   = now;

                IEnumerable <ValidationResult> validationResults = viewModel.Validate(null);
                if (!validationResults.Any())
                {
                    throw new CodeValidationException(Lesson4Texts.IncorrectValidationRule);
                }
                viewModel.SubscriptionFrom = now;
                viewModel.SubscriptionTo   = now.AddDays(5);

                validationResults = viewModel.Validate(null);
                if (validationResults.Any())
                {
                    throw new CodeValidationException(Lesson4Texts.IncorrectValidationRule);
                }
            });
        }
        public void Validate(CSharpCompilation compilation, CSharpSyntaxTree tree, SemanticModel model, Assembly assembly)
        {
            CSharpCommonValidator.ValidateProperties(tree, model, Lesson3CommonValidator.CreateStep9Properties());

            ValidatorsExtensions.ExecuteSafe(() =>
            {
                var viewModel = (dynamic)assembly.CreateInstance("DotvvmAcademy.Tutorial.ViewModels.Lesson3ViewModel");

                var usa = viewModel?.Countries[0];
                if (usa == null || usa.Name != "USA" || usa.Id != 1)
                {
                    throw new CodeValidationException(string.Format(Lesson3Texts.CountryInfoError, "USA", 1));
                }
                var canada = viewModel?.Countries[1];
                if (canada == null || canada.Name != "Canada" || canada.Id != 2)
                {
                    throw new CodeValidationException(string.Format(Lesson3Texts.CountryInfoError, "Canada", 2));
                }
            });
        }
Example #4
0
        public void Validate(CSharpCompilation compilation, CSharpSyntaxTree tree, SemanticModel model,
                             Assembly assembly)
        {
            CSharpCommonValidator.ValidateProperties(tree, model, Lesson1CommonValidator.CreateStep4Properties());

            var methodName = "Calculate";

            CSharpCommonValidator.ValidateMethod(tree, model, methodName);

            ValidatorsExtensions.ExecuteSafe(() =>
            {
                var viewModel     = (dynamic)assembly.CreateInstance("DotvvmAcademy.Tutorial.ViewModels.Lesson1ViewModel");
                viewModel.Number1 = 15;
                viewModel.Number2 = 30;
                viewModel.Calculate();

                if (viewModel.Result != 45)
                {
                    throw new CodeValidationException(Lesson1Texts.CommandResultError);
                }
            });
        }
        public void Validate(CSharpCompilation compilation, CSharpSyntaxTree tree, SemanticModel model,
                             Assembly assembly)
        {
            Lesson2CommonValidator.ValidateAddTaskMethod(compilation, tree, model, assembly);

            var methodName = "CompleteTask";

            CSharpCommonValidator.ValidateMethod(tree, model, methodName);

            ValidatorsExtensions.ExecuteSafe(() =>
            {
                var viewModel    = (dynamic)assembly.CreateInstance("DotvvmAcademy.Tutorial.ViewModels.Lesson2ViewModel");
                var task         = (dynamic)assembly.CreateInstance("DotvvmAcademy.Tutorial.ViewModels.TaskData");
                task.Title       = "New Task";
                task.IsCompleted = false;
                viewModel.CompleteTask(task);

                if (!task.IsCompleted)
                {
                    throw new CodeValidationException(Lesson2Texts.CompleteTaskMethodError);
                }
            });
        }
        public static void ValidateStep2Properties(CSharpSyntaxTree tree, SemanticModel model, Assembly assembly)
        {
            CSharpCommonValidator.ValidateProperties(tree, model, CreateStep2ControlProperties());

            ValidatorsExtensions.ExecuteSafe(() =>
            {
                var viewModel       = (dynamic)assembly.CreateInstance("DotvvmAcademy.Tutorial.ViewModels.Lesson4ViewModel");
                var modelProperties = (viewModel.GetType().GetProperties() as IEnumerable <PropertyInfo>).ToList();
                foreach (var requiredProperty in CreateStep2RequiredProperties())
                {
                    var viewModelProperty = modelProperties.FirstOrDefault(a => a.Name == requiredProperty.Name);

                    if (viewModelProperty == null)
                    {
                        throw new CodeValidationException(string.Format(ValidationErrorMessages.PropertyNotFound, requiredProperty.Name));
                    }

                    if (!viewModelProperty.IsDefined(typeof(RequiredAttribute)))
                    {
                        throw new CodeValidationException(string.Format(Lesson4Texts.AttributeMissing,
                                                                        requiredProperty.Name, nameof(RequiredAttribute)));
                    }
                }
                var emailPropertyName = CreateStep2EmailAddressProperty().Name;

                var emailProperty = modelProperties.FirstOrDefault(a => a.Name == emailPropertyName);
                if (emailProperty == null)
                {
                    throw new CodeValidationException(string.Format(ValidationErrorMessages.PropertyNotFound, emailPropertyName));
                }
                if (!emailProperty.IsDefined(typeof(EmailAddressAttribute)))
                {
                    throw new CodeValidationException(string.Format(Lesson4Texts.AttributeMissing,
                                                                    emailPropertyName, nameof(EmailAddressAttribute)));
                }
            });
        }