public void GetQuestionShould_ErrorIfQuestionNotFound() { //Arrange var questionId = "not an ID"; //Act + Assert var ex = Assert.Throws <Exception>(() => formContext.GetQuestion(questionId)); ex.Message.Should().Contain("QuestionId cannot be found"); }
/// <summary> /// Evaluates a condition against source data in formVM and returns true or false /// </summary> /// <param name="condition">Logic source</param> /// /// <param name="formContext">Data source</param> /// <returns></returns> public static bool EvaluateCondition(ConditionVM condition, FormVM formContext) { var result = false; //Load in condition parameters var variableName = condition.Logic.Variable; var answer = condition.Logic.Answer; //var answerArray = condition.Logic.AnswerArray; var answerArray = answer.Split('|'); //Find variable in session formContext string variable = null; switch (condition.Logic.Source) { case "answer": variable = formContext.GetQuestion(variableName).Answer; break; case "formData": if (formContext.SubmissionData == null) { throw new Exception("No submissionData found"); } variable = formContext.SubmissionData.Where(d => d.Id == variableName) .Select(d => d.Value).FirstOrDefault(); break; default: throw new ArgumentException($"Unrecognised variable source type {condition.Logic.Source} supplied by form"); break; } if (variable == null) //Whitespace is now explicitly allowed, to allow for checking of empty answers { throw new Exception($"Unable to resolve dynamic content variable {variableName}"); } //Set result to true if condition is met | CONSIDER making all of these individual methods switch (condition.Logic.Operator) { case "equal": result = answerArray.Any(variable.Equals); //(variable == answer); break; case "is_empty": result = string.IsNullOrWhiteSpace(variable); break; case "not_equal": result = !(answerArray.Any(variable.Equals)); //(variable != answer); break; //TODO: Consider inclusion of 'includes' and other string functions case "contains": result = answerArray.Any(variable.Contains); break; case "contains_all": result = answerArray.All(variable.Contains); break; case "greater_than": try { result = (float.Parse(variable) > float.Parse(answer)); } catch { throw new ArgumentException($"Variable or answer {variable} cannot be parsed to float"); } break; case "greater_than_or_equal": try { result = (float.Parse(variable) >= float.Parse(answer)); } catch { throw new ArgumentException($"Variable or answer {variable} cannot be parsed to float"); } break; case "less_than": try { result = (float.Parse(variable) < float.Parse(answer)); } catch { throw new ArgumentException($"Variable or answer {variable} cannot be parsed to float"); } break; case "less_than_or_equal": try { result = (float.Parse(variable) <= float.Parse(answer)); } catch { throw new ArgumentException($"Variable or answer {variable} cannot be parsed to float"); } break; default: throw new ArgumentException($"Unrecognised logic operator {condition.Logic.Operator} supplied by form"); break; } return(result); }