public void DecorateRazorParser_DesignTimeRazorPathNormalizer_NormalizesChunkInheritanceUtilityPaths( string rootPrefix) { // Arrange var rootedAppPath = $"{rootPrefix}SomeComputer/Location/Project/"; var rootedFilePath = $"{rootPrefix}SomeComputer/Location/Project/src/file.cshtml"; var chunkTreeCache = new DefaultChunkTreeCache(new TestFileProvider()); using (var host = new MvcRazorHost( chunkTreeCache, pathNormalizer: new DesignTimeRazorPathNormalizer(rootedAppPath))) { var parser = new RazorParser( host.CodeLanguage.CreateCodeParser(), host.CreateMarkupParser(), tagHelperDescriptorResolver: null); var chunkInheritanceUtility = new PathValidatingChunkInheritanceUtility(host, chunkTreeCache); host.ChunkInheritanceUtility = chunkInheritanceUtility; // Act host.DecorateRazorParser(parser, rootedFilePath); // Assert Assert.Equal("src/file.cshtml", chunkInheritanceUtility.InheritedChunkTreePagePath, StringComparer.Ordinal); } }
private async Task<CompilationResult> CompileCore(IFileInfo file) { var host = new MvcRazorHost(); var engine = new RazorTemplateEngine(host); GeneratorResults results; using (TextReader rdr = new StreamReader(file.CreateReadStream())) { results = engine.GenerateCode(rdr, '_' + Path.GetFileNameWithoutExtension(file.Name), "Asp", file.PhysicalPath ?? file.Name); } string generatedCode; using (var writer = new StringWriter()) using (var codeProvider = new CSharpCodeProvider()) { codeProvider.GenerateCodeFromCompileUnit(results.GeneratedCode, writer, new CodeGeneratorOptions()); generatedCode = writer.ToString(); } if (!results.Success) { return CompilationResult.Failed(generatedCode, results.ParserErrors.Select(e => new CompilationMessage(e.Message))); } Directory.CreateDirectory(_tempPath); string tempFile = Path.Combine(_tempPath, Path.GetRandomFileName() + ".cs"); File.WriteAllText(tempFile, generatedCode); _tempFileSystem.TryGetFileInfo(tempFile, out file); return await _baseCompilationService.Compile(file); }
public void DecorateCodeGenerator_DesignTimeRazorPathNormalizer_NormalizesChunkInheritanceUtilityPaths( string rootPrefix) { // Arrange var rootedAppPath = $"{rootPrefix}SomeComputer/Location/Project/"; var rootedFilePath = $"{rootPrefix}SomeComputer/Location/Project/src/file.cshtml"; var host = new MvcRazorHost( chunkTreeCache: null, pathNormalizer: new DesignTimeRazorPathNormalizer(rootedAppPath)); var chunkInheritanceUtility = new PathValidatingChunkInheritanceUtility(host); var codeGeneratorContext = new CodeGeneratorContext( new ChunkGeneratorContext( host, host.DefaultClassName, host.DefaultNamespace, rootedFilePath, shouldGenerateLinePragmas: true), new ErrorSink()); var codeGenerator = new CSharpCodeGenerator(codeGeneratorContext); host.ChunkInheritanceUtility = chunkInheritanceUtility; // Act host.DecorateCodeGenerator(codeGenerator, codeGeneratorContext); // Assert Assert.Equal("src/file.cshtml", chunkInheritanceUtility.InheritedChunkTreePagePath, StringComparer.Ordinal); }
public void MvcRazorHost_ParsesAndGeneratesCodeForBasicScenarios(string scenarioName) { // Arrange var host = new MvcRazorHost(new TestFileSystem()); // Act and Assert RunRuntimeTest(host, scenarioName); }
public void MvcRazorHost_EnablesInstrumentationByDefault() { // Arrange var host = new MvcRazorHost(new TestFileProvider()); // Act var instrumented = host.EnableInstrumentation; // Assert Assert.True(instrumented); }
public void MvcRazorHost_EnablesInstrumentationByDefault() { // Arrange var fileProvider = new TestFileProvider(); using (var host = new MvcRazorHost(new DefaultChunkTreeCache(fileProvider))) { // Act var instrumented = host.EnableInstrumentation; // Assert Assert.True(instrumented); } }
public void ModelVisitor_GeneratesCorrectLineMappings() { // Arrange var host = new MvcRazorHost(new TestFileProvider()) { DesignTimeMode = true }; host.NamespaceImports.Clear(); var expectedLineMappings = new[] { BuildLineMapping(7, 0, 7, 194, 6, 7, 30), }; // Act and Assert RunDesignTimeTest(host, "Model", expectedLineMappings); }
public void MvcRazorHost_GeneratesTagHelperModelExpressionCode_DesignTime() { // Arrange var fileProvider = new TestFileProvider(); var host = new MvcRazorHost(new DefaultCodeTreeCache(fileProvider)) { DesignTimeMode = true }; var expectedLineMappings = new List <LineMapping> { BuildLineMapping(documentAbsoluteIndex: 7, documentLineIndex: 0, documentCharacterIndex: 7, generatedAbsoluteIndex: 444, generatedLineIndex: 12, generatedCharacterIndex: 7, contentLength: 8), BuildLineMapping(documentAbsoluteIndex: 33, documentLineIndex: 2, documentCharacterIndex: 14, generatedAbsoluteIndex: 823, generatedLineIndex: 25, generatedCharacterIndex: 14, contentLength: 85), BuildLineMapping(documentAbsoluteIndex: 139, documentLineIndex: 4, documentCharacterIndex: 17, generatedAbsoluteIndex: 2105, generatedLineIndex: 53, generatedCharacterIndex: 95, contentLength: 3), BuildLineMapping( documentAbsoluteIndex: 166, documentLineIndex: 5, documentCharacterIndex: 18, generatedAbsoluteIndex: 2418, generatedLineIndex: 59, generatedCharacterIndex: 87, contentLength: 5), }; // Act and Assert RunDesignTimeTest(host, testName: "ModelExpressionTagHelper", expectedLineMappings: expectedLineMappings); }
public void InjectVisitor_GeneratesCorrectLineMappings() { // Arrange var host = new MvcRazorHost(new TestFileProvider()) { DesignTimeMode = true }; host.NamespaceImports.Clear(); var expectedLineMappings = new List <LineMapping> { BuildLineMapping(1, 0, 1, 59, 3, 0, 17), BuildLineMapping(28, 1, 8, 688, 26, 8, 20) }; // Act and Assert RunDesignTimeTest(host, "Inject", expectedLineMappings); }
public void InjectVisitorWithModel_GeneratesCorrectLineMappings() { // Arrange var host = new MvcRazorHost(new TestFileProvider()) { DesignTimeMode = true }; host.NamespaceImports.Clear(); var expectedLineMappings = new[] { BuildLineMapping(7, 0, 7, 214, 6, 7, 7), BuildLineMapping(24, 1, 8, 713, 26, 8, 20), BuildLineMapping(54, 2, 8, 921, 34, 8, 23) }; // Act and Assert RunDesignTimeTest(host, "InjectWithModel", expectedLineMappings); }
private static void RunRuntimeTest(MvcRazorHost host, string testName) { var inputFile = "TestFiles/Input/" + testName + ".cshtml"; var expectedCode = ReadResource("TestFiles/Output/Runtime/" + testName + ".cs"); // Act GeneratorResults results; using (var stream = GetResourceStream(inputFile)) { results = host.GenerateCode(inputFile, stream); } // Assert Assert.True(results.Success); Assert.Equal(expectedCode, results.GeneratedCode); Assert.Empty(results.ParserErrors); }
public void InjectVisitorWithSemicolon_GeneratesCorrectLineMappings() { // Arrange var host = new MvcRazorHost(new TestFileProvider()) { DesignTimeMode = true }; host.NamespaceImports.Clear(); var expectedLineMappings = new[] { BuildLineMapping(7, 0, 7, 222, 6, 7, 7), BuildLineMapping(24, 1, 8, 729, 26, 8, 20), BuildLineMapping(58, 2, 8, 941, 34, 8, 23), BuildLineMapping(93, 3, 8, 1156, 42, 8, 21), BuildLineMapping(129, 4, 8, 1369, 50, 8, 24), }; // Act and Assert RunDesignTimeTest(host, "InjectWithSemicolon", expectedLineMappings); }
public void DecorateRazorParser_DesignTimeRazorPathNormalizer_NormalizesChunkInheritanceUtilityPaths( string rootPrefix) { // Arrange var rootedAppPath = $"{rootPrefix}SomeComputer/Location/Project/"; var rootedFilePath = $"{rootPrefix}SomeComputer/Location/Project/src/file.cshtml"; var host = new MvcRazorHost( codeTreeCache: null, pathNormalizer: new DesignTimeRazorPathNormalizer(rootedAppPath)); var parser = new RazorParser( host.CodeLanguage.CreateCodeParser(), host.CreateMarkupParser(), tagHelperDescriptorResolver: null); var chunkInheritanceUtility = new PathValidatingChunkInheritanceUtility(host); host.ChunkInheritanceUtility = chunkInheritanceUtility; // Act host.DecorateRazorParser(parser, rootedFilePath); // Assert Assert.Equal("src/file.cshtml", chunkInheritanceUtility.InheritedCodeTreePagePath, StringComparer.Ordinal); }
public MyRazorTemplateEngine(MyMvcRazorHost myHost, MvcRazorHost host) : base(host) { _myHost = myHost; }
public void MvcRazorHost_GeneratesTagHelperModelExpressionCode_DesignTime() { // Arrange var fileProvider = new TestFileProvider(); var host = new MvcRazorHost(new DefaultChunkTreeCache(fileProvider)) { DesignTimeMode = true }; var expectedLineMappings = new List<LineMapping> { BuildLineMapping(documentAbsoluteIndex: 7, documentLineIndex: 0, documentCharacterIndex: 7, generatedAbsoluteIndex: 444, generatedLineIndex: 12, generatedCharacterIndex: 7, contentLength: 8), BuildLineMapping(documentAbsoluteIndex: 33, documentLineIndex: 2, documentCharacterIndex: 14, generatedAbsoluteIndex: 823, generatedLineIndex: 25, generatedCharacterIndex: 14, contentLength: 85), BuildLineMapping(documentAbsoluteIndex: 139, documentLineIndex: 4, documentCharacterIndex: 17, generatedAbsoluteIndex: 2313, generatedLineIndex: 55, generatedCharacterIndex: 95, contentLength: 3), BuildLineMapping( documentAbsoluteIndex: 166, documentLineIndex: 5, documentCharacterIndex: 18, generatedAbsoluteIndex: 2626, generatedLineIndex: 61, generatedCharacterIndex: 87, contentLength: 5), }; // Act and Assert RunDesignTimeTest(host, testName: "ModelExpressionTagHelper", expectedLineMappings: expectedLineMappings); }
public void ModelVisitor_GeneratesCorrectLineMappings() { // Arrange var host = new MvcRazorHost("RazorView") { DesignTimeMode = true }; host.NamespaceImports.Clear(); var engine = new RazorTemplateEngine(host); var source = ReadResource("TestFiles/Input/Model.cshtml"); var expectedCode = ReadResource("TestFiles/Output/Model.cs"); var expectedLineMappings = new List<LineMapping> { BuildLineMapping(7, 0, 7, 126, 6, 7, 30), }; // Act GeneratorResults results; using (var buffer = new StringTextBuffer(source)) { results = engine.GenerateCode(buffer); } // Assert Assert.True(results.Success); Assert.Equal(expectedCode, results.GeneratedCode); Assert.Empty(results.ParserErrors); Assert.Equal(expectedLineMappings, results.DesignTimeLineMappings); }
public void MvcRazorHost_EnablesInstrumentationByDefault() { // Arrange var fileProvider = new TestFileProvider(); var host = new MvcRazorHost(new DefaultChunkTreeCache(fileProvider)); // Act var instrumented = host.EnableInstrumentation; // Assert Assert.True(instrumented); }
private static void RunDesignTimeTest(MvcRazorHost host, string testName, IEnumerable<LineMapping> expectedLineMappings) { var inputFile = "TestFiles/Input/" + testName + ".cshtml"; var outputFile = "TestFiles/Output/DesignTime/" + testName + ".cs"; var expectedCode = ResourceFile.ReadResource(_assembly, outputFile, sourceFile: false); // Act GeneratorResults results; using (var stream = ResourceFile.GetResourceStream(_assembly, inputFile, sourceFile: true)) { results = host.GenerateCode(inputFile, stream); } // Assert Assert.True(results.Success); Assert.Empty(results.ParserErrors); #if GENERATE_BASELINES ResourceFile.UpdateFile(_assembly, outputFile, expectedCode, results.GeneratedCode); Assert.NotNull(results.DesignTimeLineMappings); // Guard if (expectedLineMappings == null || !Enumerable.SequenceEqual(expectedLineMappings, results.DesignTimeLineMappings)) { var lineMappings = new StringBuilder(); lineMappings.AppendLine($"// !!! Do not check in. Instead paste content into test method. !!!"); lineMappings.AppendLine(); var indent = " "; lineMappings.AppendLine($"{ indent }var expectedLineMappings = new[]"); lineMappings.AppendLine($"{ indent }{{"); foreach (var lineMapping in results.DesignTimeLineMappings) { var innerIndent = indent + " "; var documentLocation = lineMapping.DocumentLocation; var generatedLocation = lineMapping.GeneratedLocation; lineMappings.AppendLine($"{ innerIndent }{ nameof(BuildLineMapping) }("); innerIndent += " "; lineMappings.AppendLine($"{ innerIndent }documentAbsoluteIndex: { documentLocation.AbsoluteIndex },"); lineMappings.AppendLine($"{ innerIndent }documentLineIndex: { documentLocation.LineIndex },"); lineMappings.AppendLine($"{ innerIndent }documentCharacterIndex: { documentLocation.CharacterIndex },"); lineMappings.AppendLine($"{ innerIndent }generatedAbsoluteIndex: { generatedLocation.AbsoluteIndex },"); lineMappings.AppendLine($"{ innerIndent }generatedLineIndex: { generatedLocation.LineIndex },"); lineMappings.AppendLine($"{ innerIndent }generatedCharacterIndex: { generatedLocation.CharacterIndex },"); lineMappings.AppendLine($"{ innerIndent }contentLength: { generatedLocation.ContentLength }),"); } lineMappings.AppendLine($"{ indent }}};"); var lineMappingFile = Path.ChangeExtension(outputFile, "lineMappings.cs"); ResourceFile.UpdateFile(_assembly, lineMappingFile, previousContent: null, content: lineMappings.ToString()); } #else Assert.Equal(expectedCode, results.GeneratedCode); Assert.Equal(expectedLineMappings, results.DesignTimeLineMappings); #endif }
public PathValidatingChunkInheritanceUtility(MvcRazorHost razorHost) : base(razorHost, chunkTreeCache: null, defaultInheritedChunks: new Chunk[0]) { }
public void ModelVisitor_GeneratesCorrectLineMappings() { // Arrange var fileProvider = new TestFileProvider(); var host = new MvcRazorHost(new DefaultChunkTreeCache(fileProvider)) { DesignTimeMode = true }; host.NamespaceImports.Clear(); var expectedLineMappings = new[] { BuildLineMapping(7, 0, 7, 194, 6, 7, 30), }; // Act and Assert RunDesignTimeTest(host, "Model", expectedLineMappings); }
private static void RunRuntimeTest(MvcRazorHost host, string testName) { var inputFile = "TestFiles/Input/" + testName + ".cshtml"; var outputFile = "TestFiles/Output/Runtime/" + testName + ".cs"; var expectedCode = ResourceFile.ReadResource(_assembly, outputFile, sourceFile: false); // Act GeneratorResults results; using (var stream = ResourceFile.GetResourceStream(_assembly, inputFile, sourceFile: true)) { results = host.GenerateCode(inputFile, stream); } // Assert Assert.True(results.Success); Assert.Empty(results.ParserErrors); #if GENERATE_BASELINES ResourceFile.UpdateFile(_assembly, outputFile, expectedCode, results.GeneratedCode); #else Assert.Equal(expectedCode, results.GeneratedCode); #endif }
public void InjectVisitorWithSemicolon_GeneratesCorrectLineMappings() { // Arrange var fileProvider = new TestFileProvider(); var host = new MvcRazorHost(new DefaultChunkTreeCache(fileProvider)) { DesignTimeMode = true }; host.NamespaceImports.Clear(); var expectedLineMappings = new[] { BuildLineMapping(7, 0, 7, 222, 6, 7, 7), BuildLineMapping(24, 1, 8, 747, 26, 8, 20), BuildLineMapping(58, 2, 8, 977, 34, 8, 23), BuildLineMapping(93, 3, 8, 1210, 42, 8, 21), BuildLineMapping(129, 4, 8, 1441, 50, 8, 24), }; // Act and Assert RunDesignTimeTest(host, "InjectWithSemicolon", expectedLineMappings); }
public void InjectVisitorWithModel_GeneratesCorrectLineMappings() { // Arrange var fileProvider = new TestFileProvider(); var host = new MvcRazorHost(new DefaultChunkTreeCache(fileProvider)) { DesignTimeMode = true }; host.NamespaceImports.Clear(); var expectedLineMappings = new[] { BuildLineMapping(7, 0, 7, 214, 6, 7, 7), BuildLineMapping(24, 1, 8, 731, 26, 8, 20), BuildLineMapping(54, 2, 8, 957, 34, 8, 23) }; // Act and Assert RunDesignTimeTest(host, "InjectWithModel", expectedLineMappings); }
private static void RunDesignTimeTest(MvcRazorHost host, string testName, IEnumerable<LineMapping> expectedLineMappings) { var inputFile = "TestFiles/Input/" + testName + ".cshtml"; var expectedCode = ReadResource("TestFiles/Output/DesignTime/" + testName + ".cs"); // Act GeneratorResults results; using (var stream = GetResourceStream(inputFile)) { results = host.GenerateCode(inputFile, stream); } // Assert Assert.True(results.Success); Assert.Equal(expectedCode, results.GeneratedCode); Assert.Empty(results.ParserErrors); Assert.Equal(expectedLineMappings, results.DesignTimeLineMappings); }
private static void RunDesignTimeTest( MvcRazorHost host, string testName, IEnumerable <LineMapping> expectedLineMappings) { var inputFile = "TestFiles/Input/" + testName + ".cshtml"; var outputFile = "TestFiles/Output/DesignTime/" + testName + ".cs"; var expectedCode = ResourceFile.ReadResource(_assembly, outputFile, sourceFile: false); // Act GeneratorResults results; using (var stream = ResourceFile.GetResourceStream(_assembly, inputFile, sourceFile: true)) { // VS tooling passes in paths in all lower case. We'll mimic this behavior in our tests. results = host.GenerateCode(inputFile.ToLowerInvariant(), stream); } // Assert Assert.True(results.Success); Assert.Empty(results.ParserErrors); #if GENERATE_BASELINES ResourceFile.UpdateFile(_assembly, outputFile, expectedCode, results.GeneratedCode); Assert.NotNull(results.DesignTimeLineMappings); // Guard if (expectedLineMappings == null || !Enumerable.SequenceEqual(expectedLineMappings, results.DesignTimeLineMappings)) { var lineMappings = new StringBuilder(); lineMappings.AppendLine($"// !!! Do not check in. Instead paste content into test method. !!!"); lineMappings.AppendLine(); var indent = " "; lineMappings.AppendLine($"{ indent }var expectedLineMappings = new[]"); lineMappings.AppendLine($"{ indent }{{"); foreach (var lineMapping in results.DesignTimeLineMappings) { var innerIndent = indent + " "; var documentLocation = lineMapping.DocumentLocation; var generatedLocation = lineMapping.GeneratedLocation; lineMappings.AppendLine($"{ innerIndent }{ nameof(BuildLineMapping) }("); innerIndent += " "; lineMappings.AppendLine($"{ innerIndent }documentAbsoluteIndex: { documentLocation.AbsoluteIndex },"); lineMappings.AppendLine($"{ innerIndent }documentLineIndex: { documentLocation.LineIndex },"); lineMappings.AppendLine($"{ innerIndent }documentCharacterIndex: { documentLocation.CharacterIndex },"); lineMappings.AppendLine($"{ innerIndent }generatedAbsoluteIndex: { generatedLocation.AbsoluteIndex },"); lineMappings.AppendLine($"{ innerIndent }generatedLineIndex: { generatedLocation.LineIndex },"); lineMappings.AppendLine($"{ innerIndent }generatedCharacterIndex: { generatedLocation.CharacterIndex },"); lineMappings.AppendLine($"{ innerIndent }contentLength: { generatedLocation.ContentLength }),"); } lineMappings.AppendLine($"{ indent }}};"); var lineMappingFile = Path.ChangeExtension(outputFile, "lineMappings.cs"); ResourceFile.UpdateFile(_assembly, lineMappingFile, previousContent: null, content: lineMappings.ToString()); } #else Assert.Equal(expectedCode, results.GeneratedCode, ignoreLineEndingDifferences: true); Assert.Equal(expectedLineMappings, results.DesignTimeLineMappings); #endif }
public PathValidatingChunkInheritanceUtility(MvcRazorHost razorHost) : base(razorHost, codeTreeCache: null, defaultInheritedChunks: new Chunk[0]) { }
public void BasicVisitor_GeneratesCorrectLineMappings() { // Arrange var fileProvider = new TestFileProvider(); var host = new MvcRazorHost(new DefaultChunkTreeCache(fileProvider)) { DesignTimeMode = true }; host.NamespaceImports.Clear(); var expectedLineMappings = new[] { BuildLineMapping( documentAbsoluteIndex: 13, documentLineIndex: 0, documentCharacterIndex: 13, generatedAbsoluteIndex: 1269, generatedLineIndex: 32, generatedCharacterIndex: 13, contentLength: 4), BuildLineMapping( documentAbsoluteIndex: 43, documentLineIndex: 2, documentCharacterIndex: 5, generatedAbsoluteIndex: 1353, generatedLineIndex: 37, generatedCharacterIndex: 6, contentLength: 21), }; // Act and Assert RunDesignTimeTest(host, "Basic", expectedLineMappings); }
public void InjectVisitor_GeneratesCorrectLineMappings() { // Arrange var fileProvider = new TestFileProvider(); var host = new MvcRazorHost(new DefaultChunkTreeCache(fileProvider)) { DesignTimeMode = true }; host.NamespaceImports.Clear(); var expectedLineMappings = new List<LineMapping> { BuildLineMapping(1, 0, 1, 59, 3, 0, 17), BuildLineMapping(28, 1, 8, 706, 26, 8, 20) }; // Act and Assert RunDesignTimeTest(host, "Inject", expectedLineMappings); }
public PathValidatingChunkInheritanceUtility(MvcRazorHost razorHost, IChunkTreeCache chunkTreeCache) : base(razorHost, chunkTreeCache, defaultInheritedChunks: new Chunk[0]) { }
//public MyMvcRazorHost(string root) //{ // _host = new MvcRazorHost(root); //} public MyMvcRazorHost(IChunkTreeCache chunkTreeCache) { _chunkTreeCache = chunkTreeCache; _host = new MvcRazorHost(chunkTreeCache); }