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, ignoreLineEndingDifferences: true); #endif }
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 }
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 }
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, ignoreLineEndingDifferences: true); #endif }