|
| 1 | +using System.Xml.Linq; |
| 2 | +using Microsoft.CodeAnalysis; |
| 3 | +using Microsoft.CodeAnalysis.CSharp; |
| 4 | +using Shouldly; |
| 5 | + |
| 6 | +namespace ExampleTester.Tests; |
| 7 | + |
| 8 | +#pragma warning disable CS8509 // The switch expression does not handle all possible values of its input type (it is not exhaustive). |
| 9 | + |
| 10 | +public static class FastCsprojCompilationParserTests |
| 11 | +{ |
| 12 | + private static CsprojParseResult ParseCsproj(string csprojContents) |
| 13 | + { |
| 14 | + return FastCsprojCompilationParser.ParseCsproj(XDocument.Parse(csprojContents), "Test.csproj"); |
| 15 | + } |
| 16 | + |
| 17 | + [Test] |
| 18 | + public static void Defaults() |
| 19 | + { |
| 20 | + var result = ParseCsproj(""" |
| 21 | + <Project Sdk="Microsoft.NET.Sdk"> |
| 22 | +
|
| 23 | + <PropertyGroup> |
| 24 | + <TargetFramework>net6.0</TargetFramework> |
| 25 | + </PropertyGroup> |
| 26 | +
|
| 27 | + </Project> |
| 28 | + """); |
| 29 | + |
| 30 | + result.CompilationOptions.OutputKind.ShouldBe(OutputKind.DynamicallyLinkedLibrary); |
| 31 | + result.CompilationOptions.NullableContextOptions.ShouldBe(NullableContextOptions.Disable); |
| 32 | + result.AssemblyName.ShouldBeNull(); |
| 33 | + result.CompilationOptions.AllowUnsafe.ShouldBeFalse(); |
| 34 | + result.ParseOptions.LanguageVersion.ShouldBe(LanguageVersion.CSharp10); // Due to net6.0 |
| 35 | + result.CompilationOptions.WarningLevel.ShouldBe(6); // Due to net6.0 |
| 36 | + result.GeneratedSources.ShouldBeEmpty(); |
| 37 | + } |
| 38 | + |
| 39 | + [Test] |
| 40 | + public static void ParsesTargetFramework() |
| 41 | + { |
| 42 | + ParseCsproj(""" |
| 43 | + <Project Sdk="Microsoft.NET.Sdk"> |
| 44 | +
|
| 45 | + <PropertyGroup> |
| 46 | + <TargetFramework>net6.0</TargetFramework> |
| 47 | + </PropertyGroup> |
| 48 | +
|
| 49 | + </Project> |
| 50 | + """).TargetFramework.ShouldBe("net6.0"); |
| 51 | + } |
| 52 | + |
| 53 | + [Test] |
| 54 | + public static void ParsesOutputType([Values("Library", "Exe", "WinExe")] string outputType) |
| 55 | + { |
| 56 | + ParseCsproj($""" |
| 57 | + <Project Sdk="Microsoft.NET.Sdk"> |
| 58 | +
|
| 59 | + <PropertyGroup> |
| 60 | + <TargetFramework>net6.0</TargetFramework> |
| 61 | + <OutputType>{outputType}</OutputType> |
| 62 | + </PropertyGroup> |
| 63 | +
|
| 64 | + </Project> |
| 65 | + """).CompilationOptions.OutputKind.ShouldBe(outputType switch |
| 66 | + { |
| 67 | + "Library" => OutputKind.DynamicallyLinkedLibrary, |
| 68 | + "Exe" => OutputKind.ConsoleApplication, |
| 69 | + "WinExe" => OutputKind.WindowsApplication, |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + [Test] |
| 74 | + public static void ParsesNullable([Values("enable", "disable", "annotations", "warnings")] string nullable) |
| 75 | + { |
| 76 | + ParseCsproj($""" |
| 77 | + <Project Sdk="Microsoft.NET.Sdk"> |
| 78 | +
|
| 79 | + <PropertyGroup> |
| 80 | + <TargetFramework>net6.0</TargetFramework> |
| 81 | + <Nullable>{nullable}</Nullable> |
| 82 | + </PropertyGroup> |
| 83 | +
|
| 84 | + </Project> |
| 85 | + """).CompilationOptions.NullableContextOptions.ShouldBe(nullable switch |
| 86 | + { |
| 87 | + "enable" => NullableContextOptions.Enable, |
| 88 | + "disable" => NullableContextOptions.Disable, |
| 89 | + "annotations" => NullableContextOptions.Annotations, |
| 90 | + "warnings" => NullableContextOptions.Warnings, |
| 91 | + }); |
| 92 | + } |
| 93 | + |
| 94 | + [Test] |
| 95 | + public static void ParsesAssemblyName() |
| 96 | + { |
| 97 | + ParseCsproj($""" |
| 98 | + <Project Sdk="Microsoft.NET.Sdk"> |
| 99 | +
|
| 100 | + <PropertyGroup> |
| 101 | + <TargetFramework>net6.0</TargetFramework> |
| 102 | + <AssemblyName>Xyz</AssemblyName> |
| 103 | + </PropertyGroup> |
| 104 | +
|
| 105 | + </Project> |
| 106 | + """).AssemblyName.ShouldBe("Xyz"); |
| 107 | + } |
| 108 | + |
| 109 | + [Test] |
| 110 | + public static void ParsesAllowUnsafeBlocks([Values("true", "false")] string allowUnsafeBlocks) |
| 111 | + { |
| 112 | + ParseCsproj($""" |
| 113 | + <Project Sdk="Microsoft.NET.Sdk"> |
| 114 | +
|
| 115 | + <PropertyGroup> |
| 116 | + <TargetFramework>net6.0</TargetFramework> |
| 117 | + <AllowUnsafeBlocks>{allowUnsafeBlocks}</AllowUnsafeBlocks> |
| 118 | + </PropertyGroup> |
| 119 | +
|
| 120 | + </Project> |
| 121 | + """).CompilationOptions.AllowUnsafe.ShouldBe(allowUnsafeBlocks switch |
| 122 | + { |
| 123 | + "true" => true, |
| 124 | + "false" => false, |
| 125 | + }); |
| 126 | + } |
| 127 | + |
| 128 | + [Test] |
| 129 | + public static void ParsesImplicitUsings([Values("true", "enable", "false", "disable")] string implicitUsings) |
| 130 | + { |
| 131 | + var hasImplicitUsings = implicitUsings switch |
| 132 | + { |
| 133 | + "true" or "enable" => true, |
| 134 | + "false" or "disable" => false, |
| 135 | + }; |
| 136 | + |
| 137 | + var result = ParseCsproj($""" |
| 138 | + <Project Sdk="Microsoft.NET.Sdk"> |
| 139 | +
|
| 140 | + <PropertyGroup> |
| 141 | + <TargetFramework>net6.0</TargetFramework> |
| 142 | + <ImplicitUsings>{implicitUsings}</ImplicitUsings> |
| 143 | + </PropertyGroup> |
| 144 | +
|
| 145 | + </Project> |
| 146 | + """); |
| 147 | + |
| 148 | + if (hasImplicitUsings) |
| 149 | + { |
| 150 | + var source = result.GeneratedSources.ShouldHaveSingleItem(); |
| 151 | + source.Options.ShouldBe(result.ParseOptions); |
| 152 | + source.FilePath.ShouldBe("Test.GlobalUsings.g.cs"); |
| 153 | + source.GetText().ToString().ShouldBe(""" |
| 154 | + global using System; |
| 155 | + global using System.Collections.Generic; |
| 156 | + global using System.IO; |
| 157 | + global using System.Linq; |
| 158 | + global using System.Net.Http; |
| 159 | + global using System.Threading; |
| 160 | + global using System.Threading.Tasks; |
| 161 | + """); |
| 162 | + } |
| 163 | + else |
| 164 | + { |
| 165 | + result.GeneratedSources.ShouldBeEmpty(); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + [Test] |
| 170 | + public static void ThrowsNotImplementedExceptionForUnrecognizedProperty() |
| 171 | + { |
| 172 | + Should.Throw<NotImplementedException>(() => ParseCsproj(""" |
| 173 | + <Project Sdk="Microsoft.NET.Sdk"> |
| 174 | +
|
| 175 | + <PropertyGroup> |
| 176 | + <Xyz></Xyz> |
| 177 | + </PropertyGroup> |
| 178 | +
|
| 179 | + </Project> |
| 180 | + """)); |
| 181 | + } |
| 182 | + |
| 183 | + [Test] |
| 184 | + public static void ThrowsNotImplementedExceptionForUnrecognizedItem() |
| 185 | + { |
| 186 | + Should.Throw<NotImplementedException>(() => ParseCsproj(""" |
| 187 | + <Project Sdk="Microsoft.NET.Sdk"> |
| 188 | +
|
| 189 | + <ItemGroup> |
| 190 | + <Xyz Include="" /> |
| 191 | + </ItemGroup> |
| 192 | +
|
| 193 | + </Project> |
| 194 | + """)); |
| 195 | + } |
| 196 | + |
| 197 | + [Test] |
| 198 | + public static void ThrowsNotImplementedExceptionForUnrecognizedTopLevelElement() |
| 199 | + { |
| 200 | + Should.Throw<NotImplementedException>(() => ParseCsproj(""" |
| 201 | + <Project Sdk="Microsoft.NET.Sdk"> |
| 202 | +
|
| 203 | + <Xyz></Xyz> |
| 204 | +
|
| 205 | + </Project> |
| 206 | + """)); |
| 207 | + } |
| 208 | +} |
0 commit comments