PageRenderTime 23ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/wine-mono-0.0.4/mono/external/aspnetwebstack/test/System.Web.Razor.Test/Generator/RazorCodeGeneratorTest.cs

#
C# | 148 lines | 114 code | 20 blank | 14 comment | 18 complexity | a39be85a486592785bc650daf5c799b0 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, Unlicense, BSD-3-Clause, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, ISC, Apache-2.0, LGPL-2.0
  1. // Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
  2. //#define GENERATE_BASELINES
  3. using System.CodeDom;
  4. using System.CodeDom.Compiler;
  5. using System.Collections.Generic;
  6. using System.Diagnostics;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Web.Razor.Generator;
  11. using System.Web.Razor.Test.Utils;
  12. using System.Web.WebPages.TestUtils;
  13. using Xunit;
  14. namespace System.Web.Razor.Test.Generator
  15. {
  16. public abstract class RazorCodeGeneratorTest<TLanguage>
  17. where TLanguage : RazorCodeLanguage, new()
  18. {
  19. protected static readonly string TestRootNamespaceName = "TestOutput";
  20. protected abstract string FileExtension { get; }
  21. protected abstract string LanguageName { get; }
  22. protected abstract string BaselineExtension { get; }
  23. protected RazorEngineHost CreateHost()
  24. {
  25. return new RazorEngineHost(new TLanguage());
  26. }
  27. protected void RunTest(string name, string baselineName = null, bool generatePragmas = true, bool designTimeMode = false, IList<GeneratedCodeMapping> expectedDesignTimePragmas = null, Action<RazorEngineHost> hostConfig = null)
  28. {
  29. // Load the test files
  30. if (baselineName == null)
  31. {
  32. baselineName = name;
  33. }
  34. string source = TestFile.Create(String.Format("CodeGenerator.{1}.Source.{0}.{2}", name, LanguageName, FileExtension)).ReadAllText();
  35. string expectedOutput = TestFile.Create(String.Format("CodeGenerator.{1}.Output.{0}.{2}", baselineName, LanguageName, BaselineExtension)).ReadAllText();
  36. // Set up the host and engine
  37. RazorEngineHost host = CreateHost();
  38. host.NamespaceImports.Add("System");
  39. host.DesignTimeMode = designTimeMode;
  40. host.StaticHelpers = true;
  41. host.DefaultClassName = name;
  42. // Add support for templates, etc.
  43. host.GeneratedClassContext = new GeneratedClassContext(GeneratedClassContext.DefaultExecuteMethodName,
  44. GeneratedClassContext.DefaultWriteMethodName,
  45. GeneratedClassContext.DefaultWriteLiteralMethodName,
  46. "WriteTo",
  47. "WriteLiteralTo",
  48. "Template",
  49. "DefineSection",
  50. "BeginContext",
  51. "EndContext")
  52. {
  53. LayoutPropertyName = "Layout",
  54. ResolveUrlMethodName = "Href"
  55. };
  56. if (hostConfig != null)
  57. {
  58. hostConfig(host);
  59. }
  60. RazorTemplateEngine engine = new RazorTemplateEngine(host);
  61. // Generate code for the file
  62. GeneratorResults results = null;
  63. using (StringTextBuffer buffer = new StringTextBuffer(source))
  64. {
  65. results = engine.GenerateCode(buffer, className: name, rootNamespace: TestRootNamespaceName, sourceFileName: generatePragmas ? String.Format("{0}.{1}", name, FileExtension) : null);
  66. }
  67. // Generate code
  68. CodeCompileUnit ccu = results.GeneratedCode;
  69. CodeDomProvider codeProvider = (CodeDomProvider)Activator.CreateInstance(host.CodeLanguage.CodeDomProviderType);
  70. CodeGeneratorOptions options = new CodeGeneratorOptions();
  71. // Both run-time and design-time use these settings. See:
  72. // * $/Dev10/pu/SP_WebTools/venus/html/Razor/Impl/RazorCodeGenerator.cs:204
  73. // * $/Dev10/Releases/RTMRel/ndp/fx/src/xsp/System/Web/Compilation/BuildManagerHost.cs:373
  74. options.BlankLinesBetweenMembers = false;
  75. options.IndentString = String.Empty;
  76. StringBuilder output = new StringBuilder();
  77. using (StringWriter writer = new StringWriter(output))
  78. {
  79. codeProvider.GenerateCodeFromCompileUnit(ccu, writer, options);
  80. }
  81. WriteBaseline(String.Format(@"test\System.Web.Razor.Test\TestFiles\CodeGenerator\{0}\Output\{1}.{2}", LanguageName, baselineName, BaselineExtension), MiscUtils.StripRuntimeVersion(output.ToString()));
  82. // Verify code against baseline
  83. #if !GENERATE_BASELINES
  84. Assert.Equal(expectedOutput, MiscUtils.StripRuntimeVersion(output.ToString()));
  85. #endif
  86. // Verify design-time pragmas
  87. if (designTimeMode)
  88. {
  89. Assert.True(expectedDesignTimePragmas != null || results.DesignTimeLineMappings == null || results.DesignTimeLineMappings.Count == 0);
  90. Assert.True(expectedDesignTimePragmas == null || (results.DesignTimeLineMappings != null && results.DesignTimeLineMappings.Count > 0));
  91. if (expectedDesignTimePragmas != null)
  92. {
  93. Assert.Equal(
  94. expectedDesignTimePragmas.ToArray(),
  95. results.DesignTimeLineMappings
  96. .OrderBy(p => p.Key)
  97. .Select(p => p.Value)
  98. .ToArray());
  99. }
  100. }
  101. }
  102. [Conditional("GENERATE_BASELINES")]
  103. private void WriteBaseline(string baselineFile, string output)
  104. {
  105. string root = RecursiveFind("Runtime.sln", Path.GetFullPath("."));
  106. string baselinePath = Path.Combine(root, baselineFile);
  107. // Update baseline
  108. // IMPORTANT! Replace this path with the local path on your machine to the baseline files!
  109. if (File.Exists(baselinePath))
  110. {
  111. File.Delete(baselinePath);
  112. }
  113. File.WriteAllText(baselinePath, output.ToString());
  114. }
  115. private string RecursiveFind(string path, string start)
  116. {
  117. string test = Path.Combine(start, path);
  118. if (File.Exists(test))
  119. {
  120. return start;
  121. }
  122. else
  123. {
  124. return RecursiveFind(path, new DirectoryInfo(start).Parent.FullName);
  125. }
  126. }
  127. }
  128. }