private void TestWithNull(string value, Enums.ComparisonOperators comparison)
        {
            var c = new ValueComparison("A Property", comparison, new List <string>(1)
            {
                "A Value"
            });
            var result = c.Compare(value);

            Assert.IsTrue(result);
        }
Exemple #2
0
        public ValueComparison(string PropertyName,
                               Enums.ComparisonOperators ComparisonOperator,
                               List <string> ValidValues)
        {
            if (string.IsNullOrWhiteSpace(PropertyName))
            {
                throw new ArgumentNullException("PropertyName");
            }
            this.propertyName = PropertyName;

            this.comparisonOperator = ComparisonOperator;

            if (ValidValues == null)
            {
                throw new ArgumentNullException("ValidValues");
            }
            if (ValidValues.Count == 0)
            {
                throw new ArgumentException("ValidValues List cannot be empty.");
            }
            this.validValues = ValidValues;
        }
        public static void Parse(string Line, ImportDefinition ID)
        {
            Helpers.ParametersValid(Line, ID, "RULE-IS");

            var index = Line.IndexOf(' ') + 1;

            var rulename = Line.GetNextWord(index);

            index += rulename.Length + 1;

            var r = new ValueRule(rulename);

            // This is either the property name, or it may be "REQUIRED"
            var nw = Line.GetNextWord(index);

            index += nw.Length + 1;
            if (nw.Trim() == "REQUIRED")
            {
                r.Required();
                nw     = Line.GetNextWord(index);
                index += nw.Length + 1;
            }

            r.Property(nw);

            var opAsString = Line.GetNextWord(index);

            index += opAsString.Length + 1;

            Enums.ComparisonOperators compop = Enums.ComparisonOperators.EQ;
            try
            {
                compop = (Enums.ComparisonOperators)Enum.Parse(typeof(Enums.ComparisonOperators), opAsString);
            }
            catch
            {
                throw new InvalidOperationException("Operation type is not a valid value: " + opAsString);
            }

            if (compop == Enums.ComparisonOperators.EQ ||
                compop == Enums.ComparisonOperators.NOTEQ)
            {
                var extraction = Line.ExtractDelimitedSection('{', '}', index);
                if (string.IsNullOrWhiteSpace(extraction.Item1))
                {
                    throw new Exception("Rule " + rulename + " contains a valid value that is an empty string.");
                }

                if (compop == Enums.ComparisonOperators.EQ)
                {
                    r.Equals(extraction.Item1);
                }
                else if (compop == Enums.ComparisonOperators.NOTEQ)
                {
                    r.NotEquals(extraction.Item1);
                }
            }

            if (compop == Enums.ComparisonOperators.IN ||
                compop == Enums.ComparisonOperators.NOTIN)
            {
                var           extraction = Line.ExtractDelimitedSection('{', '}', index);
                List <string> values     = extraction.Item1.ExtractListItems('"', '"', ',', 0);
                foreach (var v in values)
                {
                    if (string.IsNullOrWhiteSpace(v))
                    {
                        throw new Exception("Rule " + rulename + " contains a valid value that is an empty string.");
                    }
                }

                if (compop == Enums.ComparisonOperators.IN)
                {
                    r.In(values);
                }
                else if (compop == Enums.ComparisonOperators.NOTIN)
                {
                    r.NotIn(values);
                }
            }

            ID.Rules.Add(r);
        }
        private static int ParseRequiredWhenRulePhrase(string line, int index, WhenRequiredRule r, ImportDefinition id)
        {
            var propertyName = line.GetNextWord(index);

            index += propertyName.Length + 1;
            if (propertyName == "THEN")
            {
                r.Then();

                var      extraction     = line.ExtractDelimitedSection('{', '}', index);
                string[] requiredValues = extraction.Item1.Split(',');

                foreach (var s in requiredValues)
                {
                    if (!string.IsNullOrWhiteSpace(s))
                    {
                        r.Required(s.Trim());
                    }
                }
                index += extraction.Item2 + 3;
                return(index);
            }

            r.Property(propertyName);

            var opAsString = line.GetNextWord(index);

            index += opAsString.Length + 1;

            Enums.ComparisonOperators compop = Enums.ComparisonOperators.EQ;
            try
            {
                compop = (Enums.ComparisonOperators)Enum.Parse(typeof(Enums.ComparisonOperators), opAsString);
            }
            catch
            {
                throw new InvalidOperationException("Operation type is not a valid value: " + opAsString);
            }

            if (compop == Enums.ComparisonOperators.EQ ||
                compop == Enums.ComparisonOperators.NOTEQ)
            {
                var extraction = line.ExtractDelimitedSection('{', '}', index);
                index += extraction.Item2 + 3; // Includes opening delimiter, closing delimiter, and whitespace
                if (string.IsNullOrWhiteSpace(extraction.Item1))
                {
                    throw new Exception("Rule " + r.RuleName + " contains a valid value that is an empty string.");
                }

                if (compop == Enums.ComparisonOperators.EQ)
                {
                    r.Equals(extraction.Item1);
                }
                else if (compop == Enums.ComparisonOperators.NOTEQ)
                {
                    r.NotEquals(extraction.Item1);
                }
            }

            if (compop == Enums.ComparisonOperators.IN ||
                compop == Enums.ComparisonOperators.NOTIN)
            {
                var           extraction = line.ExtractDelimitedSection('{', '}', index);
                List <string> values     = extraction.Item1.ExtractListItems('"', '"', ',', 0);
                index += extraction.Item2 + 3;

                foreach (var v in values)
                {
                    if (string.IsNullOrWhiteSpace(v))
                    {
                        throw new Exception("Rule " + r.RuleName + " contains a valid value that is an empty string.");
                    }
                }

                if (compop == Enums.ComparisonOperators.IN)
                {
                    r.In(values);
                }
                else if (compop == Enums.ComparisonOperators.NOTIN)
                {
                    r.NotIn(values);
                }
            }

            return(index);
        }
        private static int ParseWhenRulePhrase(string line, int index, WhenRuleThen r, ImportDefinition id)
        {
            bool createRequiredRule = false;

            var propertyName = line.GetNextWord(index);

            index += propertyName.Length + 1;
            if (propertyName == "THEN")
            {
                r.Then();
                propertyName = line.GetNextWord(index);
                index       += propertyName.Length + 1;
            }
            r.Property(propertyName);

            var opAsString = line.GetNextWord(index);

            index += opAsString.Length + 1;
            if (opAsString == "REQUIRED")
            {
                createRequiredRule = true;
                opAsString         = line.GetNextWord(index);
                index += opAsString.Length + 1;
            }

            Enums.ComparisonOperators compop = Enums.ComparisonOperators.EQ;
            try
            {
                compop = (Enums.ComparisonOperators)Enum.Parse(typeof(Enums.ComparisonOperators), opAsString);
            }
            catch
            {
                throw new InvalidOperationException("Operation type is not a valid value: " + opAsString);
            }

            if (compop == Enums.ComparisonOperators.EQ ||
                compop == Enums.ComparisonOperators.NOTEQ)
            {
                var extraction = line.ExtractDelimitedSection('{', '}', index);
                index += extraction.Item2 + 3; // Includes opening delimiter, closing delimiter, and whitespace
                if (string.IsNullOrWhiteSpace(extraction.Item1))
                {
                    throw new Exception("Rule " + r.RuleName + " contains a valid value that is an empty string.");
                }

                if (compop == Enums.ComparisonOperators.EQ)
                {
                    r.Equals(extraction.Item1);
                }
                else if (compop == Enums.ComparisonOperators.NOTEQ)
                {
                    r.NotEquals(extraction.Item1);
                }
            }

            if (compop == Enums.ComparisonOperators.IN ||
                compop == Enums.ComparisonOperators.NOTIN)
            {
                var           extraction = line.ExtractDelimitedSection('{', '}', index);
                List <string> values     = extraction.Item1.ExtractListItems('"', '"', ',', 0);
                index += extraction.Item2 + 3;

                foreach (var v in values)
                {
                    if (string.IsNullOrWhiteSpace(v))
                    {
                        throw new Exception("Rule " + r.RuleName + " contains a valid value that is an empty string.");
                    }
                }

                if (compop == Enums.ComparisonOperators.IN)
                {
                    r.In(values);
                }
                else if (compop == Enums.ComparisonOperators.NOTIN)
                {
                    r.NotIn(values);
                }
            }

            if (createRequiredRule)
            {
                var reqRule = new WhenRequiredRule(propertyName + "-WHEN-REQUIRED");
                reqRule.WhenComparisons.AddRange(r.WhenComparisons);
                reqRule.Required(propertyName);
                id.Rules.Add(reqRule);
            }

            return(index);
        }