You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
4.6 KiB

  1. // Copyright (c) AlphaSierraPapa for the SharpDevelop Team
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4. // software and associated documentation files (the "Software"), to deal in the Software
  5. // without restriction, including without limitation the rights to use, copy, modify, merge,
  6. // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7. // to whom the Software is furnished to do so, subject to the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be included in all copies or
  10. // substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  13. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  14. // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  15. // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  16. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  17. // DEALINGS IN THE SOFTWARE.
  18. using System;
  19. using System.CodeDom.Compiler;
  20. using System.IO;
  21. using System.Linq;
  22. using System.Runtime.CompilerServices;
  23. using ICSharpCode.Decompiler.Tests.Helpers;
  24. using NUnit.Framework;
  25. namespace ICSharpCode.Decompiler.Tests
  26. {
  27. public class PrettyTestRunner
  28. {
  29. const string TestCasePath = @"../../Tests/TestCases/Pretty";
  30. [Test]
  31. public void AllFilesHaveTests()
  32. {
  33. var testNames = typeof(PrettyTestRunner).GetMethods()
  34. .Where(m => m.GetCustomAttributes(typeof(TestAttribute), false).Any())
  35. .Select(m => m.Name)
  36. .ToArray();
  37. foreach (var file in new DirectoryInfo(TestCasePath).EnumerateFiles()) {
  38. if (file.Extension.Equals(".il", StringComparison.OrdinalIgnoreCase)
  39. || file.Extension.Equals(".cs", StringComparison.OrdinalIgnoreCase)) {
  40. var testName = file.Name.Split('.')[0];
  41. Assert.Contains(testName, testNames);
  42. }
  43. }
  44. }
  45. static readonly CompilerOptions[] noRoslynOptions =
  46. {
  47. CompilerOptions.None,
  48. CompilerOptions.Optimize
  49. };
  50. static readonly CompilerOptions[] defaultOptions =
  51. {
  52. CompilerOptions.None,
  53. CompilerOptions.Optimize,
  54. CompilerOptions.UseRoslyn,
  55. CompilerOptions.Optimize | CompilerOptions.UseRoslyn
  56. };
  57. [Test]
  58. public void HelloWorld()
  59. {
  60. Run();
  61. Run(asmOptions: AssemblerOptions.UseDebug);
  62. }
  63. [Test]
  64. public void InlineAssignmentTest([ValueSource("noRoslynOptions")] CompilerOptions cscOptions)
  65. {
  66. Run(cscOptions: cscOptions);
  67. }
  68. [Test]
  69. public void CompoundAssignmentTest([ValueSource("defaultOptions")] CompilerOptions cscOptions)
  70. {
  71. Run(cscOptions: cscOptions);
  72. }
  73. [Test]
  74. public void ShortCircuit([ValueSource("defaultOptions")] CompilerOptions cscOptions)
  75. {
  76. Run(cscOptions: cscOptions);
  77. }
  78. [Test]
  79. public void ExceptionHandling([Values(CompilerOptions.None, CompilerOptions.Optimize)] CompilerOptions cscOptions)
  80. {
  81. Run(cscOptions: cscOptions);
  82. }
  83. [Test]
  84. public void AnonymousTypes([Values(CompilerOptions.None, CompilerOptions.Optimize)] CompilerOptions cscOptions)
  85. {
  86. Run(cscOptions: cscOptions);
  87. }
  88. [Test, Ignore("Not implemented")]
  89. public void Async([ValueSource("defaultOptions")] CompilerOptions cscOptions)
  90. {
  91. Run(cscOptions: cscOptions);
  92. }
  93. [Test, Ignore("Bad variable names; some if-else misdetected")]
  94. public void Loops([ValueSource("defaultOptions")] CompilerOptions cscOptions)
  95. {
  96. Run(cscOptions: cscOptions);
  97. }
  98. void Run([CallerMemberName] string testName = null, AssemblerOptions asmOptions = AssemblerOptions.None, CompilerOptions cscOptions = CompilerOptions.None)
  99. {
  100. var ilFile = Path.Combine(TestCasePath, testName);
  101. if ((cscOptions & CompilerOptions.Optimize) != 0)
  102. ilFile += ".opt";
  103. if ((cscOptions & CompilerOptions.Force32Bit) != 0)
  104. ilFile += ".32";
  105. if ((cscOptions & CompilerOptions.UseDebug) != 0)
  106. ilFile += ".dbg";
  107. if ((cscOptions & CompilerOptions.UseRoslyn) != 0)
  108. ilFile += ".roslyn";
  109. ilFile += ".il";
  110. var csFile = Path.Combine(TestCasePath, testName + ".cs");
  111. if (!File.Exists(ilFile)) {
  112. // re-create .il file if necessary
  113. CompilerResults output = null;
  114. try {
  115. output = Tester.CompileCSharp(csFile, cscOptions | CompilerOptions.Library);
  116. Tester.Disassemble(output.PathToAssembly, ilFile);
  117. } finally {
  118. if (output != null)
  119. output.TempFiles.Delete();
  120. }
  121. }
  122. var executable = Tester.AssembleIL(ilFile, asmOptions | AssemblerOptions.Library);
  123. var decompiled = Tester.DecompileCSharp(executable);
  124. CodeAssert.FilesAreEqual(csFile, decompiled);
  125. }
  126. }
  127. }