Пример #1
0
        public void ParseModelKeyword_ErrorOnMissingModelType()
        {
            // Arrange + Act
            List <RazorError> errors = new List <RazorError>();
            var document             = "@model   ";
            var spans = ParseDocument(document, errors);

            // Assert
            var factory       = SpanFactory.CreateCsHtml();
            var expectedSpans = new Span[]
            {
                factory.EmptyHtml(),
                factory.CodeTransition(SyntaxConstants.TransitionString)
                .Accepts(AcceptedCharacters.None),
                factory.MetaCode("model ")
                .Accepts(AcceptedCharacters.None),
                factory.Code("  ")
                .As(new ModelCodeGenerator(DefaultBaseType, string.Empty)),
            };
            var expectedErrors = new[]
            {
                new RazorError("The 'model' keyword must be followed by a type name on the same line.", new SourceLocation(9, 0, 9), 1)
            };

            Assert.Equal(expectedSpans, spans.ToArray());
            Assert.Equal(expectedErrors, errors.ToArray());
        }
Пример #2
0
        public void ParseModelKeyword_ErrorOnModelFollowedByInherits()
        {
            // Arrange + Act
            List <RazorError> errors = new List <RazorError>();
            var document             =
                "@model Foo" + Environment.NewLine
                + "@inherits Bar";
            var spans = ParseDocument(document, errors);

            // Assert
            var factory       = SpanFactory.CreateCsHtml();
            var expectedSpans = new Span[]
            {
                factory.EmptyHtml(),
                factory.CodeTransition(SyntaxConstants.TransitionString)
                .Accepts(AcceptedCharacters.None),
                factory.MetaCode("model ")
                .Accepts(AcceptedCharacters.None),
                factory.Code("Foo" + Environment.NewLine)
                .As(new ModelChunkGenerator(DefaultBaseType, "Foo"))
                .Accepts(AcceptedCharacters.AnyExceptNewline),
                factory.EmptyHtml(),
                factory.CodeTransition(SyntaxConstants.TransitionString)
                .Accepts(AcceptedCharacters.None),
                factory.MetaCode("inherits ")
                .Accepts(AcceptedCharacters.None),
                factory.Code("Bar")
                .As(new SetBaseTypeChunkGenerator("Bar"))
                .Accepts(AcceptedCharacters.AnyExceptNewline),
                factory.EmptyHtml()
            };

            var expectedErrors = new[]
            {
                new RazorError(
                    "The 'inherits' keyword is not allowed when a 'model' keyword is used.",
                    PlatformNormalizer.NormalizedSourceLocation(21, 1, 9),
                    1)
            };

            expectedSpans.Zip(spans, (exp, span) => new { expected = exp, span = span }).ToList().ForEach(i => Assert.Equal(i.expected, i.span));
            Assert.Equal(expectedSpans, spans.ToArray());
            Assert.Equal(expectedErrors, errors.ToArray());
        }
Пример #3
0
        public void ParseModelKeyword_ErrorOnMultipleModelStatements()
        {
            // Arrange + Act
            List <RazorError> errors = new List <RazorError>();
            var document             =
                "@model Foo" + Environment.NewLine
                + "@model Bar";
            var spans = ParseDocument(document, errors);

            // Assert
            var factory       = SpanFactory.CreateCsHtml();
            var expectedSpans = new Span[]
            {
                factory.EmptyHtml(),
                factory.CodeTransition(SyntaxConstants.TransitionString)
                .Accepts(AcceptedCharacters.None),
                factory.MetaCode("model ")
                .Accepts(AcceptedCharacters.None),
                factory.Code("Foo" + Environment.NewLine)
                .As(new ModelChunkGenerator("Foo"))
                .Accepts(AcceptedCharacters.AnyExceptNewline),
                factory.EmptyHtml(),
                factory.CodeTransition(SyntaxConstants.TransitionString)
                .Accepts(AcceptedCharacters.None),
                factory.MetaCode("model ")
                .Accepts(AcceptedCharacters.None),
                factory.Code("Bar")
                .As(new ModelChunkGenerator("Bar"))
                .Accepts(AcceptedCharacters.AnyExceptNewline),
                factory.EmptyHtml()
            };

            var expectedErrors = new[]
            {
                new RazorError(
                    "Only one 'model' statement is allowed in a file.",
                    PlatformNormalizer.NormalizedSourceLocation(13, 1, 1),
                    5)
            };

            expectedSpans.Zip(spans, (exp, span) => new { expected = exp, span = span }).ToList().ForEach(i => Assert.Equal(i.expected, i.span));
            Assert.Equal(expectedSpans, spans.ToArray());
            Assert.Equal(expectedErrors, errors.ToArray());
        }
Пример #4
0
        public void ParseModelKeyword_HandlesSingleInstance()
        {
            // Arrange + Act
            var document = "@model    Foo";
            var spans    = ParseDocument(document);

            // Assert
            var factory       = SpanFactory.CreateCsHtml();
            var expectedSpans = new Span[]
            {
                factory.EmptyHtml(),
                factory.CodeTransition(SyntaxConstants.TransitionString)
                .Accepts(AcceptedCharacters.None),
                factory.MetaCode("model ")
                .Accepts(AcceptedCharacters.None),
                factory.Code("   Foo")
                .As(new ModelCodeGenerator(DefaultBaseType, "Foo"))
            };

            Assert.Equal(expectedSpans, spans.ToArray());
        }
Пример #5
0
        public void ParseModelKeyword_HandlesVSTemplateSyntax()
        {
            // Arrange + Act
            var document = "@model $rootnamespace$.MyModel";
            var spans    = ParseDocument(document);

            // Assert
            var factory       = SpanFactory.CreateCsHtml();
            var expectedSpans = new Span[]
            {
                factory.EmptyHtml(),
                factory.CodeTransition(SyntaxConstants.TransitionString)
                .Accepts(AcceptedCharacters.None),
                factory.MetaCode("model ")
                .Accepts(AcceptedCharacters.None),
                factory.Code("$rootnamespace$.MyModel")
                .As(new ModelChunkGenerator(DefaultBaseType, "$rootnamespace$.MyModel"))
                .Accepts(AcceptedCharacters.AnyExceptNewline),
                factory.EmptyHtml()
            };

            Assert.Equal(expectedSpans, spans.ToArray());
        }
Пример #6
0
        public void ParseModelKeyword_HandlesArrays()
        {
            // Arrange + Act
            var document = $"@model Foo[[]][]{Environment.NewLine}Bar";
            var spans    = ParseDocument(document);

            // Assert
            var factory       = SpanFactory.CreateCsHtml();
            var expectedSpans = new Span[]
            {
                factory.EmptyHtml(),
                factory.CodeTransition(SyntaxConstants.TransitionString)
                .Accepts(AcceptedCharacters.None),
                factory.MetaCode("model ")
                .Accepts(AcceptedCharacters.None),
                factory.Code("Foo[[]][]" + Environment.NewLine)
                .As(new ModelChunkGenerator(DefaultBaseType, "Foo[[]][]"))
                .Accepts(AcceptedCharacters.AnyExceptNewline),
                factory.Markup("Bar")
                .With(new MarkupChunkGenerator())
            };

            Assert.Equal(expectedSpans, spans.ToArray());
        }