|
| 1 | +// Copyright (c) 2025 Daniel Grunwald |
| 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 | + |
| 19 | +using System; |
| 20 | +using System.Collections.Generic; |
| 21 | +using System.IO; |
| 22 | + |
| 23 | +using ICSharpCode.Decompiler.CSharp.ProjectDecompiler; |
| 24 | +using ICSharpCode.Decompiler.Metadata; |
| 25 | + |
| 26 | +using NUnit.Framework; |
| 27 | + |
| 28 | +namespace ICSharpCode.Decompiler.Tests.ProjectDecompiler; |
| 29 | + |
| 30 | +[TestFixture] |
| 31 | +public sealed class WholeProjectDecompilerTests |
| 32 | +{ |
| 33 | + [Test] |
| 34 | + public void UseNestedDirectoriesForNamespacesTrueWorks() |
| 35 | + { |
| 36 | + string targetDirectory = Path.Combine(Environment.CurrentDirectory, Path.GetRandomFileName()); |
| 37 | + TestFriendlyProjectDecompiler decompiler = new(new UniversalAssemblyResolver(null, false, null)); |
| 38 | + decompiler.Settings.UseNestedDirectoriesForNamespaces = true; |
| 39 | + decompiler.DecompileProject(new PEFile("ICSharpCode.Decompiler.dll"), targetDirectory); |
| 40 | + AssertDirectoryDoesntExist(targetDirectory); |
| 41 | + |
| 42 | + string projectDecompilerDirectory = Path.Combine(targetDirectory, "ICSharpCode", "Decompiler", "CSharp", "ProjectDecompiler"); |
| 43 | + string projectDecompilerFile = Path.Combine(projectDecompilerDirectory, $"{nameof(WholeProjectDecompiler)}.cs"); |
| 44 | + |
| 45 | + using (Assert.EnterMultipleScope()) |
| 46 | + { |
| 47 | + Assert.That(decompiler.Files.ContainsKey(projectDecompilerFile), Is.True); |
| 48 | + Assert.That(decompiler.Directories.Contains(projectDecompilerDirectory), Is.True); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + [Test] |
| 53 | + public void UseNestedDirectoriesForNamespacesFalseWorks() |
| 54 | + { |
| 55 | + string targetDirectory = Path.Combine(Environment.CurrentDirectory, Path.GetRandomFileName()); |
| 56 | + TestFriendlyProjectDecompiler decompiler = new(new UniversalAssemblyResolver(null, false, null)); |
| 57 | + decompiler.Settings.UseNestedDirectoriesForNamespaces = false; |
| 58 | + decompiler.DecompileProject(new PEFile("ICSharpCode.Decompiler.dll"), targetDirectory); |
| 59 | + AssertDirectoryDoesntExist(targetDirectory); |
| 60 | + |
| 61 | + string projectDecompilerDirectory = Path.Combine(targetDirectory, "ICSharpCode.Decompiler.CSharp.ProjectDecompiler"); |
| 62 | + string projectDecompilerFile = Path.Combine(projectDecompilerDirectory, $"{nameof(WholeProjectDecompiler)}.cs"); |
| 63 | + |
| 64 | + using (Assert.EnterMultipleScope()) |
| 65 | + { |
| 66 | + Assert.That(decompiler.Files.ContainsKey(projectDecompilerFile), Is.True); |
| 67 | + Assert.That(decompiler.Directories.Contains(projectDecompilerDirectory), Is.True); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + static void AssertDirectoryDoesntExist(string directory) |
| 72 | + { |
| 73 | + if (Directory.Exists(directory)) |
| 74 | + { |
| 75 | + Directory.Delete(directory, recursive: true); |
| 76 | + Assert.Fail("Directory should not have been created."); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + sealed class TestFriendlyProjectDecompiler(IAssemblyResolver assemblyResolver) : WholeProjectDecompiler(assemblyResolver) |
| 81 | + { |
| 82 | + public Dictionary<string, StringWriter> Files { get; } = []; |
| 83 | + public HashSet<string> Directories { get; } = []; |
| 84 | + |
| 85 | + protected override TextWriter CreateFile(string path) |
| 86 | + { |
| 87 | + StringWriter writer = new(); |
| 88 | + Files[path] = writer; |
| 89 | + return writer; |
| 90 | + } |
| 91 | + |
| 92 | + protected override void CreateDirectory(string path) |
| 93 | + { |
| 94 | + Directories.Add(path); |
| 95 | + } |
| 96 | + |
| 97 | + protected override IEnumerable<ProjectItemInfo> WriteMiscellaneousFilesInProject(PEFile module) => []; |
| 98 | + |
| 99 | + protected override IEnumerable<ProjectItemInfo> WriteResourceFilesInProject(MetadataFile module) => []; |
| 100 | + } |
| 101 | +} |
0 commit comments