示例#1
0
        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
        }
示例#2
0
        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);
        }
示例#3
0
        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
        }
示例#4
0
        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
        }
示例#5
0
        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
        }
示例#6
0
        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);
        }