コード例 #1
0
        public void Child_conditions_can_be_removed()
        {
            var parent = new AnyCondition {
                ID = ConditionID.Parse(0)
            };
            var childOne = new EnabledCondition {
                ID = ConditionID.Parse(1)
            };
            var childTwo = new DisabledCondition {
                ID = ConditionID.Parse(2)
            };

            _collection.Add(parent);
            _collection.Add(childOne, parent.ID);
            _collection.Add(childTwo, parent.ID);

            _collection.Remove(conditionID: childOne.ID);

            _collection
            .All
            .ShouldHaveSingleItem()
            .ShouldBeOfType <AnyCondition>();

            parent.Children.ShouldBe(new[] { childTwo });
        }
コード例 #2
0
        public static ContextFilter FromContextDefinition(string contextString)
        {
            /*
             * Context string format is a list of tokens joined by either & or | but not both
             * and grouped inside (). Groups can also be joined by either & or | but not both
             * Context strings can not be nested
             * Examples: a,b,c are tokens
             * (a)
             * (a&b&c)
             * (a|b|c)
             * (a|b|c)&(a&b&c)
             * (a|b|c)|(a&b&c)
             */

            // parse context string
            bool          supportsZeroDoc     = false;
            ContextFilter ctxFilter           = new ContextFilter();
            var           collectedConditions = new HashSet <Condition>();

            bool capturingSubCondition     = false;
            bool subConditionIsNot         = false;
            CompoundCondition subCondition = new AllCondition();
            var collectedSubConditions     = new HashSet <Condition>();

            bool   capturingToken = false;
            string currentToken   = string.Empty;

            Action captureToken = () => {
                if (capturingToken && currentToken != string.Empty)
                {
                    if (Condition.FromToken(currentToken) is Condition condition)
                    {
                        if (condition is ZeroDocCondition)
                        {
                            supportsZeroDoc = true;
                        }
                        collectedSubConditions.Add(condition);
                    }
                    currentToken = string.Empty;
                }
            };

            Action captureSubConditions = () => {
                if (capturingSubCondition)
                {
                    if (collectedSubConditions.Count > 0)
                    {
                        subCondition.Conditions = collectedSubConditions;
                        subCondition.IsNot      = subConditionIsNot;
                        collectedConditions.Add(subCondition);
                    }
                    collectedSubConditions = new HashSet <Condition>();
                    capturingSubCondition  = false;
                    subConditionIsNot      = false;
                }
            };

            foreach (char c in contextString)
            {
                switch (c)
                {
                // sub conditions
                case '(':
                    if (capturingSubCondition)
                    {
                        captureToken();
                    }
                    else
                    {
                        capturingSubCondition = true;
                        capturingToken        = true;
                    }
                    continue;

                case ')':
                    captureToken();
                    captureSubConditions();
                    continue;

                // (sub)condition types
                case CONTEXT_CONDITION_ALL_SEP:
                    captureToken();
                    if (capturingSubCondition)
                    {
                        subCondition = new AllCondition();
                    }
                    else
                    {
                        ctxFilter.Condition = new AllCondition();
                    }
                    continue;

                case CONTEXT_CONDITION_ANY_SEP:
                    captureToken();
                    if (capturingSubCondition)
                    {
                        subCondition = new AnyCondition();
                    }
                    else
                    {
                        ctxFilter.Condition = new AnyCondition();
                    }
                    continue;

                case CONTEXT_CONDITION_EXACT_SEP:
                    captureToken();
                    if (capturingSubCondition)
                    {
                        subCondition = new ExactCondition();
                    }
                    else
                    {
                        ctxFilter.Condition = new ExactCondition();
                    }
                    continue;

                case CONTEXT_CONDITION_NOT:
                    if (!capturingSubCondition)
                    {
                        subConditionIsNot = true;
                    }
                    continue;

                // tokens
                default:
                    if (capturingToken)
                    {
                        currentToken += c;
                    }
                    continue;
                }
            }


            ctxFilter.Condition.Conditions = collectedConditions;
            ctxFilter.EnsureActiveDocument = !supportsZeroDoc;
            return(ctxFilter);
        }