Browse Source

Adjust Loops test.

pull/960/head
Daniel Grunwald 8 years ago
parent
commit
e813ebcc03
  1. 33
      ICSharpCode.Decompiler.Tests/Helpers/CodeAssert.cs
  2. 24
      ICSharpCode.Decompiler.Tests/Helpers/Tester.cs
  3. 2
      ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs
  4. 6
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/Loops.cs

33
ICSharpCode.Decompiler.Tests/Helpers/CodeAssert.cs

@ -3,6 +3,8 @@ using System.Collections.Generic; @@ -3,6 +3,8 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
using DiffLib;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using NUnit.Framework;
namespace ICSharpCode.Decompiler.Tests.Helpers
@ -115,24 +117,23 @@ namespace ICSharpCode.Decompiler.Tests.Helpers @@ -115,24 +117,23 @@ namespace ICSharpCode.Decompiler.Tests.Helpers
return NormalizeLine(line) == string.Empty;
}
private static IEnumerable<string> NormalizeAndSplitCode(string input, string[] definedSymbols)
class DeleteDisabledTextRewriter : CSharpSyntaxRewriter
{
bool include = true;
foreach (string line in input.Split(new[] { "\r\n", "\n", "\r" }, StringSplitOptions.RemoveEmptyEntries)) {
var temp = line.Trim();
if (temp.StartsWith("#if !", StringComparison.Ordinal)) {
string symbol = temp.Substring(5);
if (definedSymbols.Contains(symbol))
include = false;
} else if (temp.StartsWith("#if ", StringComparison.Ordinal)) {
string symbol = temp.Substring(4);
if (!definedSymbols.Contains(symbol))
include = false;
} else if (temp.StartsWith("#endif", StringComparison.Ordinal)) {
include = true;
}
if (include) yield return line;
public override SyntaxTrivia VisitTrivia(SyntaxTrivia trivia)
{
if (trivia.IsKind(SyntaxKind.DisabledTextTrivia)) {
return default(SyntaxTrivia); // delete
}
return base.VisitTrivia(trivia);
}
}
private static IEnumerable<string> NormalizeAndSplitCode(string input, IEnumerable<string> definedSymbols)
{
var syntaxTree = CSharpSyntaxTree.ParseText(input, new CSharpParseOptions(preprocessorSymbols: definedSymbols));
var result = new DeleteDisabledTextRewriter().Visit(syntaxTree.GetRoot());
input = result.ToFullString();
return input.Split(new[] { "\r\n", "\n", "\r" }, StringSplitOptions.RemoveEmptyEntries);
}
}
}

24
ICSharpCode.Decompiler.Tests/Helpers/Tester.cs

@ -158,6 +158,24 @@ namespace ICSharpCode.Decompiler.Tests.Helpers @@ -158,6 +158,24 @@ namespace ICSharpCode.Decompiler.Tests.Helpers
};
});
public static List<string> GetPreprocessorSymbols(CompilerOptions flags)
{
var preprocessorSymbols = new List<string>();
if (flags.HasFlag(CompilerOptions.UseDebug)) {
preprocessorSymbols.Add("DEBUG");
}
if (flags.HasFlag(CompilerOptions.Optimize)) {
preprocessorSymbols.Add("OPT");
}
if (flags.HasFlag(CompilerOptions.UseRoslyn)) {
preprocessorSymbols.Add("ROSLYN");
} else {
preprocessorSymbols.Add("LEGACY_CSC");
}
return preprocessorSymbols;
}
public static CompilerResults CompileCSharp(string sourceFileName, CompilerOptions flags = CompilerOptions.UseDebug, string outputFileName = null)
{
List<string> sourceFileNames = new List<string> { sourceFileName };
@ -165,10 +183,7 @@ namespace ICSharpCode.Decompiler.Tests.Helpers @@ -165,10 +183,7 @@ namespace ICSharpCode.Decompiler.Tests.Helpers
sourceFileNames.Add(Path.GetFullPath(Path.Combine(Path.GetDirectoryName(sourceFileName), match.Groups[1].Value)));
}
var preprocessorSymbols = new List<string>();
if (flags.HasFlag(CompilerOptions.UseDebug)) {
preprocessorSymbols.Add("DEBUG");
}
var preprocessorSymbols = GetPreprocessorSymbols(flags);
if (flags.HasFlag(CompilerOptions.UseRoslyn)) {
var parseOptions = new CSharpParseOptions(preprocessorSymbols: preprocessorSymbols.ToArray());
@ -194,7 +209,6 @@ namespace ICSharpCode.Decompiler.Tests.Helpers @@ -194,7 +209,6 @@ namespace ICSharpCode.Decompiler.Tests.Helpers
}
return results;
} else {
preprocessorSymbols.Add("LEGACY_CSC");
var provider = new CSharpCodeProvider(new Dictionary<string, string> { { "CompilerVersion", "v4.0" } });
CompilerParameters options = new CompilerParameters();
options.GenerateExecutable = !flags.HasFlag(CompilerOptions.Library);

2
ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs

@ -226,7 +226,7 @@ namespace ICSharpCode.Decompiler.Tests @@ -226,7 +226,7 @@ namespace ICSharpCode.Decompiler.Tests
var executable = Tester.AssembleIL(ilFile, asmOptions | AssemblerOptions.Library);
var decompiled = Tester.DecompileCSharp(executable);
CodeAssert.FilesAreEqual(csFile, decompiled, cscOptions.HasFlag(CompilerOptions.UseRoslyn) ? null : new[] { "LEGACY_CSC" });
CodeAssert.FilesAreEqual(csFile, decompiled, Tester.GetPreprocessorSymbols(cscOptions).ToArray());
}
}
}

6
ICSharpCode.Decompiler.Tests/TestCases/Pretty/Loops.cs

@ -388,8 +388,14 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -388,8 +388,14 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
public static void ForeachWithRefUsage(List<int> items)
{
foreach (int item in items) {
#if ROSLYN && OPT
// The variable names differs based on whether roslyn optimizes out the 'item' variable
int current = item;
Loops.Operation(ref current);
#else
int num = item;
Loops.Operation(ref num);
#endif
}
}

Loading…
Cancel
Save