Example #1
0
        public void CodeTreeWithUsings()
        {
            var syntaxTreeNode     = new Mock <Span>(new SpanBuilder());
            var language           = new CSharpRazorCodeLanguage();
            var host               = new RazorEngineHost(language);
            var codeBuilderContext = new CodeBuilderContext(
                host,
                "TestClass",
                "TestNamespace",
                "Foo.cs",
                shouldGenerateLinePragmas: false);

            codeBuilderContext.CodeTreeBuilder.AddUsingChunk("FakeNamespace1", syntaxTreeNode.Object);
            codeBuilderContext.CodeTreeBuilder.AddUsingChunk("FakeNamespace2.SubNamespace", syntaxTreeNode.Object);
            var codeBuilder = language.CreateCodeBuilder(codeBuilderContext);

            // Act
            var result = codeBuilder.Build();

            BaselineWriter.WriteBaseline(
                @"test\Microsoft.AspNet.Razor.Test\TestFiles\CodeGenerator\CS\Output\CSharpCodeBuilder.cs",
                result.Code);

            var expectedOutput = TestFile.Create("TestFiles/CodeGenerator/CS/Output/CSharpCodeBuilder.cs").ReadAllText();

            // Assert
            Assert.Equal(expectedOutput, result.Code);
        }
Example #2
0
        public void ChunkTreeWithUsings()
        {
            var syntaxTreeNode       = new Mock <Span>(new SpanBuilder());
            var language             = new CSharpRazorCodeLanguage();
            var host                 = new CodeGenTestHost(language);
            var codeGeneratorContext = new CodeGeneratorContext(
                host,
                "TestClass",
                "TestNamespace",
                "Foo.cs",
                shouldGenerateLinePragmas: false,
                errorSink: new ErrorSink());

            codeGeneratorContext.ChunkTreeBuilder.AddUsingChunk("FakeNamespace1", syntaxTreeNode.Object);
            codeGeneratorContext.ChunkTreeBuilder.AddUsingChunk("FakeNamespace2.SubNamespace", syntaxTreeNode.Object);
            var codeGenerator = new CodeGenTestCodeGenerator(codeGeneratorContext);
            var testFile      = TestFile.Create("TestFiles/CodeGenerator/Output/CSharpCodeGenerator.cs");

            string expectedOutput;

#if GENERATE_BASELINES
            if (testFile.Exists())
            {
                expectedOutput = testFile.ReadAllText();
            }
            else
            {
                expectedOutput = null;
            }
#else
            expectedOutput = testFile.ReadAllText();
#endif

            // Act
            var result = codeGenerator.Generate();

            // Assert
#if GENERATE_BASELINES
            // Update baseline files if files do not already match.
            if (!string.Equals(expectedOutput, result.Code, StringComparison.Ordinal))
            {
                BaselineWriter.WriteBaseline(
                    @"test\Microsoft.AspNet.Razor.Test\TestFiles\CodeGenerator\Output\CSharpCodeGenerator.cs",
                    result.Code);
            }
#else
            Assert.Equal(expectedOutput, result.Code);
#endif
        }
Example #3
0
        protected override void Execute(string inPath, string outPath)
        {
            EpochInfoReader reader = new EpochInfoReader(inPath);

            Data = new Dictionary <string, EpochSatData>();
            EpochSatData EpochSatData;

            foreach (var item in reader)
            {
                var siteName = item.SiteName.ToUpper();
                if (!Data.ContainsKey(siteName))
                {
                    Data[siteName] = new EpochSatData();
                }
                Data[siteName].Data.Add(item.ReceiverTime, item);
            }
            //比较计算



            BaselineWriter writer = new BaselineWriter(outPath);
        }
Example #4
0
        private void RunTestInternal(string name,
                                     string baselineName,
                                     bool generatePragmas,
                                     bool designTimeMode,
                                     IList <LineMapping> expectedDesignTimePragmas,
                                     TestSpan[] spans,
                                     bool withTabs,
                                     Func <RazorEngineHost, RazorEngineHost> hostConfig,
                                     Func <RazorTemplateEngine, RazorTemplateEngine> templateEngineConfig,
                                     Action <GeneratorResults> onResults = null)
        {
            // Load the test files
            if (baselineName == null)
            {
                baselineName = name;
            }

            var sourceLocation = string.Format("TestFiles/CodeGenerator/{1}/Source/{0}.{2}", name, LanguageName, FileExtension);
            var expectedOutput = TestFile.Create(string.Format("TestFiles/CodeGenerator/CS/Output/{0}.{1}", baselineName, BaselineExtension)).ReadAllText();

            // Set up the host and engine
            var host = CreateHost();

            host.NamespaceImports.Add("System");
            host.DesignTimeMode   = designTimeMode;
            host.StaticHelpers    = true;
            host.DefaultClassName = name;

            // Add support for templates, etc.
            host.GeneratedClassContext = new GeneratedClassContext(GeneratedClassContext.DefaultExecuteMethodName,
                                                                   GeneratedClassContext.DefaultWriteMethodName,
                                                                   GeneratedClassContext.DefaultWriteLiteralMethodName,
                                                                   "WriteTo",
                                                                   "WriteLiteralTo",
                                                                   "Template",
                                                                   "DefineSection",
                                                                   "Instrumentation.BeginContext",
                                                                   "Instrumentation.EndContext",
                                                                   new GeneratedTagHelperContext())
            {
                LayoutPropertyName   = "Layout",
                ResolveUrlMethodName = "Href"
            };
            if (hostConfig != null)
            {
                host = hostConfig(host);
            }

            host.IsIndentingWithTabs   = withTabs;
            host.EnableInstrumentation = true;

            var engine = new RazorTemplateEngine(host);

            if (templateEngineConfig != null)
            {
                engine = templateEngineConfig(engine);
            }

            // Generate code for the file
            GeneratorResults results = null;

            using (var source = TestFile.Create(sourceLocation).OpenRead())
            {
                var sourceFileName = generatePragmas ? String.Format("{0}.{1}", name, FileExtension) : null;
                results = engine.GenerateCode(source, className: name, rootNamespace: TestRootNamespaceName, sourceFileName: sourceFileName);
            }
            // Only called if GENERATE_BASELINES is set, otherwise compiled out.
            BaselineWriter.WriteBaseline(String.Format(@"test\Microsoft.AspNet.Razor.Test\TestFiles\CodeGenerator\{0}\Output\{1}.{2}", LanguageName, baselineName, BaselineExtension), results.GeneratedCode);

#if !GENERATE_BASELINES
            var textOutput = results.GeneratedCode;

            if (onResults != null)
            {
                onResults(results);
            }

            //// Verify code against baseline
            Assert.Equal(expectedOutput, textOutput);
#endif

            IEnumerable <Span> generatedSpans = results.Document.Flatten();

            foreach (var span in generatedSpans)
            {
                VerifyNoBrokenEndOfLines(span.Content);
            }

            // Verify design-time pragmas
            if (designTimeMode)
            {
                if (spans != null)
                {
                    Assert.Equal(spans, generatedSpans.Select(span => new TestSpan(span)).ToArray());
                }

                if (expectedDesignTimePragmas != null)
                {
                    Assert.True(results.DesignTimeLineMappings != null && results.DesignTimeLineMappings.Count > 0);

                    Assert.Equal(expectedDesignTimePragmas.Count, results.DesignTimeLineMappings.Count);

                    for (var i = 0; i < expectedDesignTimePragmas.Count; i++)
                    {
                        if (!expectedDesignTimePragmas[i].Equals(results.DesignTimeLineMappings[i]))
                        {
                            Assert.True(false, String.Format("Line mapping {0} is not equivalent.", i));
                        }
                    }
                }
            }
        }
Example #5
0
        private void RunTestInternal(
            string name,
            string baselineName,
            bool generatePragmas,
            bool designTimeMode,
            IList <LineMapping> expectedDesignTimePragmas,
            TestSpan[] spans,
            bool withTabs,
            Func <RazorEngineHost, RazorEngineHost> hostConfig,
            Func <RazorTemplateEngine, RazorTemplateEngine> templateEngineConfig,
            Action <GeneratorResults> onResults = null)
        {
            // Load the test files
            if (baselineName == null)
            {
                baselineName = name;
            }

            var sourceLocation = string.Format("TestFiles/CodeGenerator/Source/{0}.{1}", name, FileExtension);
            var testFile       = TestFile
                                 .Create(string.Format("TestFiles/CodeGenerator/Output/{0}.{1}", baselineName, BaselineExtension));

            string expectedOutput;

#if GENERATE_BASELINES
            if (testFile.Exists())
            {
                expectedOutput = testFile.ReadAllText();
            }
            else
            {
                expectedOutput = null;
            }
#else
            expectedOutput = testFile.ReadAllText();
#endif

            // Set up the host and engine
            var host = CreateHost();
            host.NamespaceImports.Add("System");
            host.DesignTimeMode   = designTimeMode;
            host.StaticHelpers    = true;
            host.DefaultClassName = name;

            // Add support for templates, etc.
            host.GeneratedClassContext = new GeneratedClassContext(
                GeneratedClassContext.DefaultExecuteMethodName,
                GeneratedClassContext.DefaultWriteMethodName,
                GeneratedClassContext.DefaultWriteLiteralMethodName,
                "WriteTo",
                "WriteLiteralTo",
                "Template",
                "DefineSection",
                "Instrumentation.BeginContext",
                "Instrumentation.EndContext",
                new GeneratedTagHelperContext());
            if (hostConfig != null)
            {
                host = hostConfig(host);
            }

            host.IsIndentingWithTabs   = withTabs;
            host.EnableInstrumentation = true;

            var engine = new RazorTemplateEngine(host);

            if (templateEngineConfig != null)
            {
                engine = templateEngineConfig(engine);
            }

            // Generate code for the file
            GeneratorResults results = null;
            using (var source = TestFile.Create(sourceLocation).OpenRead())
            {
                var sourceFile     = NormalizeNewLines(source);
                var sourceFileName = generatePragmas ? string.Format("{0}.{1}", name, FileExtension) : null;
                results = engine.GenerateCode(
                    sourceFile,
                    className: name,
                    rootNamespace: TestRootNamespaceName,
                    sourceFileName: sourceFileName);
            }

            var textOutput = results.GeneratedCode;
#if GENERATE_BASELINES
            var outputFile = string.Format(
                @"test\Microsoft.AspNet.Razor.Test\TestFiles\CodeGenerator\Output\{0}.{1}",
                baselineName,
                BaselineExtension);

            // Update baseline files if files do not already match.
            if (!string.Equals(expectedOutput, textOutput, StringComparison.Ordinal))
            {
                BaselineWriter.WriteBaseline(outputFile, textOutput);
            }
#else
            if (onResults != null)
            {
                onResults(results);
            }

            // Verify code against baseline
            Assert.Equal(expectedOutput, textOutput);
#endif

            var generatedSpans = results.Document.Flatten();

            foreach (var span in generatedSpans)
            {
                VerifyNoBrokenEndOfLines(span.Content);
            }

            // Verify design-time pragmas
            if (designTimeMode)
            {
                if (spans != null)
                {
                    Assert.Equal(spans, generatedSpans.Select(span => new TestSpan(span)).ToArray());
                }

                if (expectedDesignTimePragmas != null)
                {
                    Assert.NotNull(results.DesignTimeLineMappings); // Guard
#if GENERATE_BASELINES
                    if (expectedDesignTimePragmas == null ||
                        !Enumerable.SequenceEqual(expectedDesignTimePragmas, results.DesignTimeLineMappings))
                    {
                        var lineMappingFile = Path.ChangeExtension(outputFile, "lineMappings.cs");
                        var lineMappingCode = GetDesignTimeLineMappingsCode(results.DesignTimeLineMappings);
                        BaselineWriter.WriteBaseline(lineMappingFile, lineMappingCode);
                    }
#else
                    for (var i = 0; i < expectedDesignTimePragmas.Count && i < results.DesignTimeLineMappings.Count; i++)
                    {
                        Assert.Equal(expectedDesignTimePragmas[i], results.DesignTimeLineMappings[i]);
                    }

                    Assert.Equal(expectedDesignTimePragmas.Count, results.DesignTimeLineMappings.Count);
#endif
                }
            }
        }