private static T FindWithAnnotation <T>(IntermediateNode node, object annotation) where T : IntermediateNode
        {
            if (node is T target && object.ReferenceEquals(target.Annotations[annotation], annotation))
            {
                return(target);
            }

            for (var i = 0; i < node.Children.Count; i++)
            {
                var result = FindWithAnnotation <T>(node.Children[i], annotation);
                if (result != null)
                {
                    return(result);
                }
            }

            return(null);
        }
 public static void EndInstrumentation(IntermediateNode node)
 {
     try
     {
         var endNode = Assert.IsType <CSharpCodeIntermediateNode>(node);
         var content = new StringBuilder();
         for (var i = 0; i < endNode.Children.Count; i++)
         {
             var token = Assert.IsAssignableFrom <IntermediateToken>(endNode.Children[i]);
             Assert.Equal(TokenKind.CSharp, token.Kind);
             content.Append(token.Content);
         }
         Assert.Equal("EndContext();", content.ToString());
     }
     catch (XunitException e)
     {
         throw new IntermediateNodeAssertException(node, node.Children, e.Message, e);
     }
 }
        internal static void PreallocatedTagHelperPropertyValue(
            IntermediateNode node,
            string attributeName,
            string value,
            AttributeStructure valueStyle)
        {
            var propertyValueNode = Assert.IsType <PreallocatedTagHelperPropertyValueIntermediateNode>(node);

            try
            {
                Assert.Equal(attributeName, propertyValueNode.AttributeName);
                Assert.Equal(value, propertyValueNode.Value);
                Assert.Equal(valueStyle, propertyValueNode.AttributeStructure);
            }
            catch (XunitException e)
            {
                throw new IntermediateNodeAssertException(propertyValueNode, e.Message);
            }
        }
        internal static void TagHelperHtmlAttribute(
            string name,
            AttributeStructure valueStyle,
            IntermediateNode node,
            params Action <IntermediateNode>[] valueValidators)
        {
            var tagHelperHtmlAttribute = Assert.IsType <TagHelperHtmlAttributeIntermediateNode>(node);

            try
            {
                Assert.Equal(name, tagHelperHtmlAttribute.AttributeName);
                Assert.Equal(valueStyle, tagHelperHtmlAttribute.AttributeStructure);
                Children(tagHelperHtmlAttribute, valueValidators);
            }
            catch (XunitException e)
            {
                throw new IntermediateNodeAssertException(tagHelperHtmlAttribute, tagHelperHtmlAttribute.Children, e.Message, e);
            }
        }
        public static void CSharpCode(string expected, IntermediateNode node)
        {
            try
            {
                var statement = Assert.IsType <CSharpCodeIntermediateNode>(node);
                var content   = new StringBuilder();
                for (var i = 0; i < statement.Children.Count; i++)
                {
                    var token = Assert.IsType <IntermediateToken>(statement.Children[i]);
                    Assert.Equal(TokenKind.CSharp, token.Kind);
                    content.Append(token.Content);
                }

                Assert.Equal(expected, content.ToString());
            }
            catch (XunitException e)
            {
                throw new IntermediateNodeAssertException(node, node.Children, e.Message, e);
            }
        }
        public static void BeginInstrumentation(string expected, IntermediateNode node)
        {
            try
            {
                var beginNode = Assert.IsType <CSharpCodeIntermediateNode>(node);
                var content   = new StringBuilder();
                for (var i = 0; i < beginNode.Children.Count; i++)
                {
                    var token = Assert.IsAssignableFrom <IntermediateToken>(beginNode.Children[i]);
                    Assert.True(token.IsCSharp);
                    content.Append(token.Content);
                }

                Assert.Equal($"BeginContext({expected});", content.ToString());
            }
            catch (XunitException e)
            {
                throw new IntermediateNodeAssertException(node, node.Children, e.Message, e);
            }
        }
        public static void Html(string expected, IntermediateNode node)
        {
            try
            {
                var html    = Assert.IsType <HtmlContentIntermediateNode>(node);
                var content = new StringBuilder();
                for (var i = 0; i < html.Children.Count; i++)
                {
                    var token = Assert.IsAssignableFrom <IntermediateToken>(html.Children[i]);
                    Assert.Equal(TokenKind.Html, token.Kind);
                    content.Append(token.Content);
                }

                Assert.Equal(expected, content.ToString());
            }
            catch (XunitException e)
            {
                throw new IntermediateNodeAssertException(node, node.Children, e.Message, e);
            }
        }
Example #8
0
        private void EndNode(IntermediateNode node)
        {
            if (_content != null && (_properties.Count == 0 || ContentMode == FormatterContentMode.PreferContent))
            {
                Writer.Write(" ");
                Writer.Write("\"");
                Writer.Write(EscapeNewlines(_content));
                Writer.Write("\"");
            }

            if (_properties.Count > 0 && (_content == null || ContentMode == FormatterContentMode.PreferProperties))
            {
                Writer.Write(" ");
                Writer.Write("{ ");
                Writer.Write(string.Join(", ", _properties.Select(kvp => $"{kvp.Key}: \"{kvp.Value}\"")));
                Writer.Write(" }");
            }

            _content = null;
            _properties.Clear();
        }
        internal static void SetTagHelperProperty(
            string name,
            string propertyName,
            AttributeStructure valueStyle,
            IntermediateNode node,
            params Action <IntermediateNode>[] valueValidators)
        {
            var propertyNode = Assert.IsType <TagHelperPropertyIntermediateNode>(node);

            try
            {
                Assert.Equal(name, propertyNode.AttributeName);
                Assert.Equal(propertyName, propertyNode.BoundAttribute.GetPropertyName());
                Assert.Equal(valueStyle, propertyNode.AttributeStructure);
                Children(propertyNode, valueValidators);
            }
            catch (XunitException e)
            {
                throw new IntermediateNodeAssertException(propertyNode, propertyNode.Children, e.Message, e);
            }
        }
        public static void ConditionalAttribute(
            string prefix,
            string name,
            string suffix,
            IntermediateNode node,
            params Action <IntermediateNode>[] valueValidators)
        {
            var attribute = Assert.IsType <HtmlAttributeIntermediateNode>(node);

            try
            {
                Assert.Equal(prefix, attribute.Prefix);
                Assert.Equal(name, attribute.AttributeName);
                Assert.Equal(suffix, attribute.Suffix);

                Children(attribute, valueValidators);
            }
            catch (XunitException e)
            {
                throw new IntermediateNodeAssertException(attribute, attribute.Children, e.Message, e);
            }
        }
        public static void LiteralAttributeValue(string prefix, string expected, IntermediateNode node)
        {
            var attributeValue = Assert.IsType <HtmlAttributeValueIntermediateNode>(node);

            try
            {
                var content = new StringBuilder();
                for (var i = 0; i < attributeValue.Children.Count; i++)
                {
                    var token = Assert.IsAssignableFrom <IntermediateToken>(attributeValue.Children[i]);
                    Assert.True(token.IsHtml);
                    content.Append(token.Content);
                }

                Assert.Equal(prefix, attributeValue.Prefix);
                Assert.Equal(expected, content.ToString());
            }
            catch (XunitException e)
            {
                throw new IntermediateNodeAssertException(attributeValue, e.Message);
            }
        }
        public override void VisitDefault(IntermediateNode node)
        {
            var children = node.Children;

            if (node.Children.Count == 0)
            {
                return;
            }

            _ancestors.Insert(0, node);

            try
            {
                for (var i = 0; i < node.Children.Count; i++)
                {
                    var child = children[i];
                    Visit(child);
                }
            }
            finally
            {
                _ancestors.RemoveAt(0);
            }
        }
        public override void Push(IntermediateNode node)
        {
            if (node == null)
            {
                throw new ArgumentNullException(nameof(node));
            }

            if (_depth >= _stack.Count)
            {
                _stack.Add(node);
            }
            else
            {
                _stack[_depth] = node;
            }

            if (_depth > 0)
            {
                var parent = _stack[_depth - 1];
                parent.Children.Add(node);
            }

            _depth++;
        }
 public static void AnnotationEquals(IntermediateNode node, object value)
 {
     AnnotationEquals(node, value, value);
 }
 public IntermediateNodeAssertException(IntermediateNode node, IEnumerable <IntermediateNode> nodes, string userMessage)
     : base(Format(node, null, nodes, userMessage))
 {
     Node  = node;
     Nodes = nodes;
 }
 public IntermediateNodeAssertException(IntermediateNode node, string userMessage)
     : base(Format(node, null, null, userMessage))
 {
     Node = node;
 }
        internal static void TagHelper(string tagName, TagMode tagMode, IEnumerable <TagHelperDescriptor> tagHelpers, IntermediateNode node, params Action <IntermediateNode>[] childValidators)
        {
            var tagHelperNode = Assert.IsType <TagHelperIntermediateNode>(node);

            try
            {
                Assert.Equal(tagName, tagHelperNode.TagName);
                Assert.Equal(tagMode, tagHelperNode.TagMode);

                Assert.Equal(tagHelpers, tagHelperNode.TagHelpers, TagHelperDescriptorComparer.Default);
            }
            catch (XunitException e)
            {
                throw new IntermediateNodeAssertException(tagHelperNode, e.Message);
            }

            Children(node, childValidators);
        }
 public static bool IsImported(this IntermediateNode node)
 {
     return(ReferenceEquals(node.Annotations[CommonAnnotations.Imported], CommonAnnotations.Imported));
 }
 public abstract void Add(IntermediateNode node);
 public void Deconstruct(out IntermediateNode parent, out IntermediateNode node)
 {
     parent = Parent;
     node   = Node;
 }
 public static void DirectiveToken(DirectiveTokenKind expectedKind, string expectedContent, IntermediateNode node)
 {
     try
     {
         var token = Assert.IsType <DirectiveTokenIntermediateNode>(node);
         Assert.Equal(expectedKind, token.DirectiveToken.Kind);
         Assert.Equal(expectedContent, token.Content);
     }
     catch (XunitException e)
     {
         throw new IntermediateNodeAssertException(node, node.Children, e.Message, e);
     }
 }
 public abstract void Push(IntermediateNode node);
 public abstract void Insert(int index, IntermediateNode node);