public void CreateThrowsExceptionWhenRuleDoesNotMatchCriteriaTest()
        {
            var target = new CreationRule(typeof(string), string.Empty, 10, (object)null);

            Action action = () => target.Create(typeof(Guid), string.Empty, null);

            action.ShouldThrow<NotSupportedException>();
        }
        public void CreateReturnsValueFromConstructorTest()
        {
            var type = typeof(string);
            var name = Guid.NewGuid().ToString();
            var priority = Environment.TickCount;
            var expected = Guid.NewGuid().ToString();

            var target = new CreationRule((checkType, referenceName) => true, priority, expected);

            var actual = target.Create(type, name, null);

            actual.Should().Be(expected);
        }
        public void IsMatchReturnsWhetherTypeAndNameMatchTest(
            Type type,
            string name,
            Type matchType,
            string matchName,
            bool expected)
        {
            var priority = Environment.TickCount;

            var target = new CreationRule(type, name, priority, (object)null);

            var actual = target.IsMatch(matchType, matchName);

            actual.Should().Be(expected);
        }
        /// <summary>
        /// Instantiates the hierarchy.
        /// </summary>
        private IList <ILocalNode> InstantiateHierarchy(
            ILocalNode root,
            InstanceDeclarationHierarchy hierarchy,
            ref long counter,
            ushort namespaceIndex)
        {
            List <ILocalNode>          nodesToAdd    = new List <ILocalNode>();
            List <HierarchyBrowsePath> nodesToUpdate = new List <HierarchyBrowsePath>();

            // create an instance for each browse path that does not already have one.
            foreach (HierarchyBrowsePath browsePath in hierarchy.BrowsePaths.Values)
            {
                bool updateReferences = browsePath.BrowsePath == "/";

                // find the instance.
                INode instance = m_nodes.Find(browsePath.InstanceId);

                if (instance == null)
                {
                    // do nothing if optional.
                    if (browsePath.IsOptional)
                    {
                        continue;
                    }

                    // find the declaration.
                    ILocalNode declaration = m_nodes.Find(browsePath.DeclarationId) as ILocalNode;

                    if (declaration == null)
                    {
                        continue;
                    }

                    instance = declaration;

                    // check the creation rule.
                    CreationRule creationRule = GetCreationRule(declaration.ModellingRule);

                    // create a new instance is the creation rule is new.
                    if (creationRule == CreationRule.New)
                    {
                        Node newNode = Node.Copy(declaration);
                        newNode.NodeId = new NodeId(Utils.IncrementIdentifier(ref counter), namespaceIndex);
                        instance       = newNode;
                        nodesToAdd.Add(newNode);
                        updateReferences = true;
                    }
                }

                // update the browse path.
                browsePath.InstanceId = instance.NodeId;
                hierarchy.Instances[(NodeId)instance.NodeId] = instance;

                // check if instance can be traced back to root.
                if (!updateReferences)
                {
                    continue;
                }

                nodesToUpdate.Add(browsePath);
            }

            // create the references.
            foreach (HierarchyBrowsePath browsePath in nodesToUpdate)
            {
                if (browsePath.InstanceId != null)
                {
                    InstantiateReferences(browsePath, hierarchy);
                }
            }

            return(nodesToAdd);
        }
        public void IsMatchReturnsWhetherTypeAndRegularExpressionMatchTest(
            Type type,
            string expression,
            Type matchType,
            string matchName,
            bool expected)
        {
            var priority = Environment.TickCount;
            var regex = new Regex(expression);

            var target = new CreationRule(type, regex, priority, (object)null);

            var actual = target.IsMatch(matchType, matchName);

            actual.Should().Be(expected);
        }
        public void ReturnsConstructorValuesTest()
        {
            var type = typeof(string);
            var name = Guid.NewGuid().ToString();
            var priority = Environment.TickCount;

            var target = new CreationRule(type, name, priority, (object)null);

            target.Priority.Should().Be(priority);
        }
        public void AddWithCreationRuleThrowsExceptionWithNullCompilerTest()
        {
            var rule = new CreationRule(typeof(Person), "FirstName", Environment.TickCount, (object) null);

            IBuildStrategyCompiler target = null;

            Action action = () => target.Add(rule);

            action.ShouldThrow<ArgumentNullException>();
        }
        public void AddWithCreationRuleAddsRuleToCompilerTest()
        {
            var rule = new CreationRule(typeof(Person), "FirstName", Environment.TickCount, (object) null);

            var target = new BuildStrategyCompiler();

            target.Add(rule);

            target.CreationRules.Should().Contain(rule);
        }