コード例 #1
0
ファイル: Page.cs プロジェクト: weedkiller/form-builder
        public bool CheckPageMeetsConditions(Dictionary <string, dynamic> answers)
        {
            var conditionValidator = new ConditionValidator();

            return(RenderConditions.Count == 0 ||
                   RenderConditions.All(condition => conditionValidator.IsValid(condition, answers)));
        }
コード例 #2
0
        public void IsFileNullOrEmpty_Should_Return_False()
        {
            // Arrange
            var file = new List <FileUploadModel>
            {
                new FileUploadModel
                {
                    Key     = "file-File-fileupload-cf03afbd-4b3d-48f0-a803-b252441aa93f",
                    Content = null,
                    TrustedOriginalFileName   = "All shook up cast.jpg",
                    UntrustedOriginalFileName = "All shook up cast.jpg",
                    FileSize = 88717,
                    FileName = null
                }
            };

            var viewModel = new Dictionary <string, dynamic>
            {
                { "test-fileupload", JsonConvert.SerializeObject(file) }
            };

            var condition1 = new Condition {
                ComparisonValue = "true", QuestionId = "test", ConditionType = ECondition.IsFileUploadNullOrEmpty
            };
            var conditionValidator = new ConditionValidator();

            // Act & Assert
            Assert.False(conditionValidator.IsValid(condition1, viewModel));
        }
コード例 #3
0
        public void AnyCondition_ShouldReturnTrue_When_X_NumberOfConditions_AreTrue(int count)
        {
            // Arrange
            var viewModel = new Dictionary <string, dynamic>
            {
                { "test", "pear" },
                { "test2", "apple" },
                { "test3", "cherry" },
                { "test4", "orange" },
            };

            var conditionOne = new ConditionBuilder()
                               .WithConditionType(ECondition.EqualTo)
                               .WithQuestionId("test")
                               .WithComparisonValue("pear")
                               .Build();

            var conditionTwo = new ConditionBuilder()
                               .WithConditionType(ECondition.EqualTo)
                               .WithQuestionId("test2")
                               .WithComparisonValue("apple")
                               .Build();

            var conditionThree = new ConditionBuilder()
                                 .WithConditionType(ECondition.Contains)
                                 .WithQuestionId("test3")
                                 .WithComparisonValue("cherry")
                                 .Build();

            var conditionFour = new ConditionBuilder()
                                .WithConditionType(ECondition.Contains)
                                .WithQuestionId("test4")
                                .WithComparisonValue("orange")
                                .Build();

            var anyCondition = new ConditionBuilder()
                               .WithComparisonValue($"{count}")
                               .WithConditionType(ECondition.Any)
                               .WithCondition(conditionOne)
                               .WithCondition(conditionTwo)
                               .WithCondition(conditionThree)
                               .WithCondition(conditionFour)
                               .Build();

            // Act & Assert
            Assert.True(_conditionValidator.IsValid(anyCondition, viewModel));
        }
コード例 #4
0
        public void Is_Null_Or_Empty_Should_Return_True_False()
        {
            // Arrange
            var viewModel = new Dictionary <string, dynamic>
            {
                { "test", "" },
                { "test2", "apple" }
            };

            var condition1 = new Condition {
                IsNullOrEmpty = true, QuestionId = "test", ConditionType = ECondition.IsNullOrEmpty
            };
            var condition2 = new Condition {
                IsNullOrEmpty = false, QuestionId = "test2", ConditionType = ECondition.IsNullOrEmpty
            };
            var conditionValidator = new ConditionValidator();

            // Act & Assert
            Assert.True(conditionValidator.IsValid(condition1, viewModel));
            Assert.True(conditionValidator.IsValid(condition2, viewModel));
        }
コード例 #5
0
        public void CheckBox_Contains_Should_Return_True_False()
        {
            // Arrange
            var viewModel = new Dictionary <string, dynamic>
            {
                { "test", "pear,guava" },
                { "test2", "apple,mango" }
            };

            var condition1 = new Condition {
                CheckboxContains = "pear", QuestionId = "test", ConditionType = ECondition.Contains
            };
            var condition2 = new Condition {
                CheckboxContains = "plum", QuestionId = "test2", ConditionType = ECondition.Contains
            };
            var conditionValidator = new ConditionValidator();

            // Act & Assert
            Assert.True(conditionValidator.IsValid(condition1, viewModel));
            Assert.False(conditionValidator.IsValid(condition2, viewModel));
        }
コード例 #6
0
        public void Equal_To_Should_Return_True_False()
        {
            // Arrange
            var viewModel = new Dictionary <string, dynamic>
            {
                { "test", "pear" },
                { "test2", "apple" }
            };

            var condition1 = new Condition {
                EqualTo = "pear", QuestionId = "test", ConditionType = ECondition.EqualTo
            };
            var condition2 = new Condition {
                EqualTo = "plum", QuestionId = "test2", ConditionType = ECondition.EqualTo
            };
            var conditionValidator = new ConditionValidator();

            // Act & Assert
            Assert.True(conditionValidator.IsValid(condition1, viewModel));
            Assert.False(conditionValidator.IsValid(condition2, viewModel));
        }
コード例 #7
0
        public void IsFileNullOrEmpty_Should_Return_True()
        {
            // Arrange
            var viewModel = new Dictionary <string, dynamic>
            {
                { "test-fileupload", null }
            };

            var condition1 = new Condition {
                ComparisonValue = "true", QuestionId = "test", ConditionType = ECondition.IsFileUploadNullOrEmpty
            };
            var conditionValidator = new ConditionValidator();

            // Act & Assert
            Assert.True(conditionValidator.IsValid(condition1, viewModel));
        }
コード例 #8
0
        public void Is_Before_Should_Return_True()
        {
            // Arrange
            var futureDate = DateTime.Today.AddDays(4);
            var viewModel  = new Dictionary <string, dynamic>
            {
                { "test-day", futureDate.Day.ToString() },
                { "test-month", futureDate.Month.ToString() },
                { "test-year", futureDate.Year.ToString() }
            };

            var condition1 = new Condition {
                IsBefore = 10, Unit = EDateUnit.Day, ComparisonDate = "Today", QuestionId = "test", ConditionType = ECondition.IsBefore
            };
            var conditionValidator = new ConditionValidator();

            // Act & Assert
            Assert.True(conditionValidator.IsValid(condition1, viewModel));
        }
コード例 #9
0
ファイル: Page.cs プロジェクト: weedkiller/form-builder
        public Behaviour GetNextPage(Dictionary <string, dynamic> viewModel)
        {
            var conditionValidator = new ConditionValidator();

            if (Behaviours.Count == 1)
            {
                return(Behaviours.FirstOrDefault());
            }

            foreach (var behaviour in Behaviours.OrderByDescending(_ => _.Conditions.Count))
            {
                var isConditionTrue = false;

                foreach (var condition in behaviour.Conditions)
                {
                    isConditionTrue = conditionValidator.IsValid(condition, viewModel);

                    if (!isConditionTrue)
                    {
                        break;
                    }
                }

                if (isConditionTrue || !behaviour.Conditions.Any())
                {
                    return(behaviour);
                }
            }

            var conditionValuesForDebug = Behaviours.OrderByDescending(_ => _.Conditions.Count)
                                          .Where(_ => _.Conditions != null)
                                          .SelectMany(_ => _.Conditions)
                                          .Where(_ => !string.IsNullOrEmpty(_.QuestionId))
                                          .Select(_ => _.QuestionId)
                                          .Distinct()
                                          .Select(_ => $"QuestionId: {_} with answer {(viewModel.ContainsKey(_) ? viewModel[_] : "'null'")}")
                                          .Aggregate((curr, acc) => $"{acc} {curr}");

            throw new ApplicationException($"Page::GetNextPage, There was a problem whilst processing behaviors for page '{PageSlug}', Behaviour Answers and conditions: {conditionValuesForDebug}");
        }