Beispiel #1
0
        public void CompileCalculatesRootRelativePath(string appPath, string viewPath)
        {
            // Arrange
            var host = new Mock <IMvcRazorHost>();

            host.Setup(h => h.GenerateCode(@"views\index\home.cshtml", It.IsAny <Stream>()))
            .Returns(GetGeneratorResult())
            .Verifiable();

            var fileInfo = new Mock <IFileInfo>();

            fileInfo.Setup(f => f.PhysicalPath).Returns(viewPath);
            fileInfo.Setup(f => f.CreateReadStream()).Returns(Stream.Null);

            var compiler = new Mock <ICompilationService>();

            compiler.Setup(c => c.Compile(fileInfo.Object, It.IsAny <string>()))
            .Returns(CompilationResult.Successful(typeof(RazorCompilationServiceTest)));

            var razorService = new RazorCompilationService(compiler.Object, host.Object);

            var relativeFileInfo = new RelativeFileInfo()
            {
                FileInfo     = fileInfo.Object,
                RelativePath = @"views\index\home.cshtml",
            };

            // Act
            razorService.Compile(relativeFileInfo, isInstrumented: false);

            // Assert
            host.Verify();
        }
Beispiel #2
0
        public void CompileSetsEnableInstrumentationOnHost(bool enableInstrumentation)
        {
            // Arrange
            var host = new Mock <IMvcRazorHost>();

            host.SetupAllProperties();
            host.Setup(h => h.GenerateCode(It.IsAny <string>(), It.IsAny <Stream>()))
            .Returns(GetGeneratorResult());

            var compiler = new Mock <ICompilationService>();

            compiler.Setup(c => c.Compile(It.IsAny <IFileInfo>(), It.IsAny <string>()))
            .Returns(CompilationResult.Successful(GetType()));

            var razorService = new RazorCompilationService(compiler.Object, host.Object);

            var relativeFileInfo = new RelativeFileInfo()
            {
                FileInfo     = Mock.Of <IFileInfo>(),
                RelativePath = @"views\index\home.cshtml",
            };

            // Act
            razorService.Compile(relativeFileInfo, isInstrumented: enableInstrumentation);

            // Assert
            Assert.Equal(enableInstrumentation, host.Object.EnableInstrumentation);
        }
        public void Compile_ReturnsFailedResultIfParseFails()
        {
            // Arrange
            var errorSink = new ParserErrorSink();
            errorSink.OnError(new RazorError("some message", 1, 1, 1, 1));
            var generatorResult = new GeneratorResults(
                    new Block(new BlockBuilder { Type = BlockType.Comment }),
                    Enumerable.Empty<TagHelperDescriptor>(),
                    errorSink,
                    new CodeBuilderResult("", new LineMapping[0]),
                    new CodeTree());
            var host = new Mock<IMvcRazorHost>();
            host.Setup(h => h.GenerateCode(It.IsAny<string>(), It.IsAny<Stream>()))
                .Returns(generatorResult)
                .Verifiable();

            var fileInfo = new Mock<IFileInfo>();
            fileInfo.Setup(f => f.CreateReadStream())
                    .Returns(Stream.Null);

            var compiler = new Mock<ICompilationService>(MockBehavior.Strict);
            var relativeFileInfo = new RelativeFileInfo(fileInfo.Object, @"Views\index\home.cshtml");
            var razorService = new RazorCompilationService(compiler.Object, host.Object);

            // Act
            var result = razorService.Compile(relativeFileInfo);

            // Assert
            Assert.NotNull(result.CompilationFailure);
            var message = Assert.Single(result.CompilationFailure.Messages);
            Assert.Equal("some message", message.Message);
            host.Verify();
        }
        public void CompileCalculatesRootRelativePath(string appPath, string viewPath)
        {
            // Arrange
            var host = new Mock<IMvcRazorHost>();
            host.Setup(h => h.GenerateCode(@"Views\index\home.cshtml", It.IsAny<Stream>()))
                .Returns(GetGeneratorResult())
                .Verifiable();

            var fileInfo = new Mock<IFileInfo>();
            fileInfo.Setup(f => f.PhysicalPath).Returns(viewPath);
            fileInfo.Setup(f => f.CreateReadStream()).Returns(Stream.Null);

            var relativeFileInfo = new RelativeFileInfo(fileInfo.Object, @"Views\index\home.cshtml");

            var compiler = new Mock<ICompilationService>();
            compiler.Setup(c => c.Compile(relativeFileInfo, It.IsAny<string>()))
                    .Returns(CompilationResult.Successful(typeof(RazorCompilationServiceTest)));

            var razorService = new RazorCompilationService(compiler.Object, host.Object);

            // Act
            razorService.Compile(relativeFileInfo);

            // Assert
            host.Verify();
        }
        public void Compile_ReturnsFailedResultIfParseFails()
        {
            // Arrange
            var generatorResult = new GeneratorResults(
                new Block(new BlockBuilder {
                Type = BlockType.Comment
            }),
                Enumerable.Empty <TagHelperDescriptor>(),
                new RazorError[] { new RazorError("some message", 1, 1, 1, 1) },
                new CodeBuilderResult("", new LineMapping[0]),
                new CodeTree());
            var host = new Mock <IMvcRazorHost>();

            host.Setup(h => h.GenerateCode(It.IsAny <string>(), It.IsAny <Stream>()))
            .Returns(generatorResult)
            .Verifiable();

            var fileInfo = new Mock <IFileInfo>();

            fileInfo.Setup(f => f.CreateReadStream())
            .Returns(Stream.Null);

            var compiler         = new Mock <ICompilationService>(MockBehavior.Strict);
            var relativeFileInfo = new RelativeFileInfo(fileInfo.Object, @"Views\index\home.cshtml");
            var razorService     = new RazorCompilationService(compiler.Object, host.Object);

            // Act
            var result = razorService.Compile(relativeFileInfo);

            // Assert
            var ex = Assert.Throws <CompilationFailedException>(() => result.CompiledType);

            Assert.Equal("some message", Assert.Single(ex.Messages).Message);
            host.Verify();
        }
        public void Compile_ReturnsResultFromCompilationServiceIfParseSucceeds()
        {
            // Arrange
            var code            = "compiled-content";
            var generatorResult = new GeneratorResults(
                new Block(new BlockBuilder {
                Type = BlockType.Comment
            }),
                Enumerable.Empty <TagHelperDescriptor>(),
                new ParserErrorSink(),
                new CodeBuilderResult(code, new LineMapping[0]),
                new CodeTree());
            var host = new Mock <IMvcRazorHost>();

            host.Setup(h => h.GenerateCode(It.IsAny <string>(), It.IsAny <Stream>()))
            .Returns(generatorResult);

            var fileInfo = new Mock <IFileInfo>();

            fileInfo.Setup(f => f.CreateReadStream())
            .Returns(Stream.Null);

            var compilationResult = CompilationResult.Successful(typeof(object));
            var compiler          = new Mock <ICompilationService>();

            compiler.Setup(c => c.Compile(fileInfo.Object, code))
            .Returns(compilationResult)
            .Verifiable();
            var relativeFileInfo = new RelativeFileInfo(fileInfo.Object, @"Views\index\home.cshtml");
            var razorService     = new RazorCompilationService(compiler.Object, host.Object);

            // Act
            var result = razorService.Compile(relativeFileInfo);

            // Assert
            Assert.Same(compilationResult, result);
            compiler.Verify();
        }
        public void Compile_ReturnsResultFromCompilationServiceIfParseSucceeds()
        {
            // Arrange
            var code = "compiled-content";
            var generatorResult = new GeneratorResults(
                    new Block(new BlockBuilder { Type = BlockType.Comment }),
                    Enumerable.Empty<TagHelperDescriptor>(),
                    new ParserErrorSink(),
                    new CodeBuilderResult(code, new LineMapping[0]),
                    new CodeTree());
            var host = new Mock<IMvcRazorHost>();
            host.Setup(h => h.GenerateCode(It.IsAny<string>(), It.IsAny<Stream>()))
                .Returns(generatorResult);

            var fileInfo = new Mock<IFileInfo>();
            fileInfo.Setup(f => f.CreateReadStream())
                    .Returns(Stream.Null);
            var relativeFileInfo = new RelativeFileInfo(fileInfo.Object, @"Views\index\home.cshtml");

            var compilationResult = CompilationResult.Successful(typeof(object));
            var compiler = new Mock<ICompilationService>();
            compiler.Setup(c => c.Compile(relativeFileInfo, code))
                    .Returns(compilationResult)
                    .Verifiable();
            var razorService = new RazorCompilationService(compiler.Object, host.Object);

            // Act
            var result = razorService.Compile(relativeFileInfo);

            // Assert
            Assert.Same(compilationResult, result);
            compiler.Verify();
        }