Browse Source

Adjust Loops test.

pull/960/head
Daniel Grunwald 8 years ago
parent
commit
e813ebcc03
  1. 31
      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

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

@ -3,6 +3,8 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using DiffLib; using DiffLib;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using NUnit.Framework; using NUnit.Framework;
namespace ICSharpCode.Decompiler.Tests.Helpers namespace ICSharpCode.Decompiler.Tests.Helpers
@ -115,24 +117,23 @@ namespace ICSharpCode.Decompiler.Tests.Helpers
return NormalizeLine(line) == string.Empty; return NormalizeLine(line) == string.Empty;
} }
private static IEnumerable<string> NormalizeAndSplitCode(string input, string[] definedSymbols) class DeleteDisabledTextRewriter : CSharpSyntaxRewriter
{ {
bool include = true; public override SyntaxTrivia VisitTrivia(SyntaxTrivia trivia)
foreach (string line in input.Split(new[] { "\r\n", "\n", "\r" }, StringSplitOptions.RemoveEmptyEntries)) { {
var temp = line.Trim(); if (trivia.IsKind(SyntaxKind.DisabledTextTrivia)) {
if (temp.StartsWith("#if !", StringComparison.Ordinal)) { return default(SyntaxTrivia); // delete
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; 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
}; };
}); });
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) public static CompilerResults CompileCSharp(string sourceFileName, CompilerOptions flags = CompilerOptions.UseDebug, string outputFileName = null)
{ {
List<string> sourceFileNames = new List<string> { sourceFileName }; List<string> sourceFileNames = new List<string> { sourceFileName };
@ -165,10 +183,7 @@ namespace ICSharpCode.Decompiler.Tests.Helpers
sourceFileNames.Add(Path.GetFullPath(Path.Combine(Path.GetDirectoryName(sourceFileName), match.Groups[1].Value))); sourceFileNames.Add(Path.GetFullPath(Path.Combine(Path.GetDirectoryName(sourceFileName), match.Groups[1].Value)));
} }
var preprocessorSymbols = new List<string>(); var preprocessorSymbols = GetPreprocessorSymbols(flags);
if (flags.HasFlag(CompilerOptions.UseDebug)) {
preprocessorSymbols.Add("DEBUG");
}
if (flags.HasFlag(CompilerOptions.UseRoslyn)) { if (flags.HasFlag(CompilerOptions.UseRoslyn)) {
var parseOptions = new CSharpParseOptions(preprocessorSymbols: preprocessorSymbols.ToArray()); var parseOptions = new CSharpParseOptions(preprocessorSymbols: preprocessorSymbols.ToArray());
@ -194,7 +209,6 @@ namespace ICSharpCode.Decompiler.Tests.Helpers
} }
return results; return results;
} else { } else {
preprocessorSymbols.Add("LEGACY_CSC");
var provider = new CSharpCodeProvider(new Dictionary<string, string> { { "CompilerVersion", "v4.0" } }); var provider = new CSharpCodeProvider(new Dictionary<string, string> { { "CompilerVersion", "v4.0" } });
CompilerParameters options = new CompilerParameters(); CompilerParameters options = new CompilerParameters();
options.GenerateExecutable = !flags.HasFlag(CompilerOptions.Library); options.GenerateExecutable = !flags.HasFlag(CompilerOptions.Library);

2
ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs

@ -226,7 +226,7 @@ namespace ICSharpCode.Decompiler.Tests
var executable = Tester.AssembleIL(ilFile, asmOptions | AssemblerOptions.Library); var executable = Tester.AssembleIL(ilFile, asmOptions | AssemblerOptions.Library);
var decompiled = Tester.DecompileCSharp(executable); 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
public static void ForeachWithRefUsage(List<int> items) public static void ForeachWithRefUsage(List<int> items)
{ {
foreach (int item in 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; int current = item;
Loops.Operation(ref current); Loops.Operation(ref current);
#else
int num = item;
Loops.Operation(ref num);
#endif
} }
} }

Loading…
Cancel
Save