Exemplo n.º 1
0
        public void Parser_should_produce_expected_syntax(DataSet dataSet)
        {
            var program    = ParserHelper.Parse(dataSet.Bicep);
            var syntaxList = SyntaxCollectorVisitor.Build(program);

            string getLoggingString(SyntaxCollectorVisitor.SyntaxItem data)
            {
                var depthPrefix = new string(' ', data.Depth);

                if (data.Syntax is Token token)
                {
                    return($"{depthPrefix}{token.Type} |{OutputHelper.EscapeWhitespace(token.Text)}|");
                }

                return($"{depthPrefix}{data.Syntax.GetType().Name}");
            }

            TextSpan getSpan(SyntaxCollectorVisitor.SyntaxItem data) => data.Syntax.Span;

            var sourceTextWithDiags = DataSet.AddDiagsToSourceText(dataSet, syntaxList, getSpan, getLoggingString);
            var resultsFile         = FileHelper.SaveResultFile(this.TestContext, Path.Combine(dataSet.Name, DataSet.TestFileMainSyntax), sourceTextWithDiags);

            sourceTextWithDiags.Should().EqualWithLineByLineDiffOutput(
                TestContext,
                dataSet.Syntax,
                expectedLocation: DataSet.GetBaselineUpdatePath(dataSet, DataSet.TestFileMainSyntax),
                actualLocation: resultsFile);
        }
Exemplo n.º 2
0
        public void Parser_should_produce_expected_syntax(DataSet dataSet)
        {
            var program        = ParserHelper.Parse(dataSet.Bicep);
            var syntaxList     = SyntaxCollectorVisitor.Build(program);
            var syntaxByParent = syntaxList.ToLookup(x => x.Parent);

            string getLoggingString(SyntaxCollectorVisitor.SyntaxItem data)
            {
                // Build a visual graph with lines to help understand the syntax hierarchy
                var graphPrefix = "";

                if (data.Depth > 0)
                {
                    var lastSibling = syntaxByParent[data.Parent].Last();
                    var isLast      = data.Syntax == lastSibling.Syntax;

                    graphPrefix  = string.Concat(Enumerable.Repeat("| ", data.Depth - 1));
                    graphPrefix += isLast switch {
                        true => "└─",
                        _ => "├─",
                    };
                }

                return(data.Syntax switch {
                    Token token => $"{graphPrefix}Token({token.Type}) |{OutputHelper.EscapeWhitespace(token.Text)}|",
                    _ => $"{graphPrefix}{data.Syntax.GetType().Name}",
                });
            }
Exemplo n.º 3
0
        public void Parser_should_produce_expected_syntax(DataSet dataSet)
        {
            var program    = SyntaxFactory.CreateFromText(dataSet.Bicep);
            var syntaxList = SyntaxCollectorVisitor.Build(program);

            string getLoggingString(SyntaxCollectorVisitor.SyntaxOrToken data)
            {
                var depthPrefix = new string(' ', data.Depth);

                if (data.Syntax != null)
                {
                    return($"{depthPrefix}{data.Syntax.GetType().Name}");
                }

                if (data.Token != null)
                {
                    return($"{depthPrefix}{data.Token.Type} |{OutputHelper.EscapeWhitespace(data.Token.Text)}|");
                }

                throw new NotImplementedException();
            }

            TextSpan getSpan(SyntaxCollectorVisitor.SyntaxOrToken data)
            {
                var positionable = (data.Syntax as IPositionable ?? data.Token as IPositionable) !;

                return(positionable.Span);
            }

            var sourceTextWithDiags = OutputHelper.AddDiagsToSourceText(dataSet, syntaxList, getSpan, getLoggingString);
            var resultsFile         = FileHelper.SaveResultFile(this.TestContext !, Path.Combine(dataSet.Name, DataSet.TestFileMainSyntax), sourceTextWithDiags);

            sourceTextWithDiags.Should().EqualWithLineByLineDiffOutput(
                dataSet.Syntax,
                expectedLocation: OutputHelper.GetBaselineUpdatePath(dataSet, DataSet.TestFileMainSyntax),
                actualLocation: resultsFile);
        }