Skip to content

Commit

Permalink
dotnet#3360 using file-scoped namespaces in classlib template
Browse files Browse the repository at this point in the history
changed SDK version
  • Loading branch information
vlada-shubina committed Jul 15, 2021
1 parent 871b9c0 commit a0cd862
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 1 deletion.
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"tools": {
"dotnet": "6.0.100-preview.7.21362.12",
"dotnet": "6.0.100-preview.7.21364.4",
"runtimes": {
"dotnet": [
"$(MicrosoftNETCoreAppRefPackageVersion)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@
using System;

#endif
#if (csharpFeature_FileScopedNamespaces)
namespace Company.ClassLibrary1;
public class Class1
{

}
#else
namespace Company.ClassLibrary1
{
public class Class1
{

}
}
#endif
101 changes: 101 additions & 0 deletions test/dotnet-new3.UnitTests/CommonTemplatesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,107 @@ public void ImplicitUsingsSupport(string name, bool buildPass, string? framework
Assert.Equal("true", projectXml.Root?.Element(ns + "PropertyGroup")?.Element(ns + "DisableImplicitNamespaceImports")?.Value);
}
}

public static IEnumerable<object?[]> FileScopedNamespacesSupport_Data()
{
var templatesToTest = new[]
{
new { Template = "classlib", Frameworks = new[] { null, "net6.0", "netstandard2.0", "netstandard2.1" } }
};
string[] unsupportedLanguageVersions = { "1", "ISO-1" };
string?[] supportedLanguageVersions = { null, "ISO-2", "2", "3", "4", "5", "6", "7", "7.1", "7.2", "7.3", "8.0", "9.0", "10.0", "latest", "latestMajor", "default", "preview" };

string?[] supportedFrameworks = { null, "net6.0" };
string?[] fileScopedNamespacesSupportedLanguages = { "10.0", "latest", "latestMajor", "default", "preview" };

foreach (var template in templatesToTest)
{
foreach (var langVersion in unsupportedLanguageVersions)
{
foreach (var framework in template.Frameworks)
{
yield return new object?[] { template.Template, false, framework, langVersion, fileScopedNamespacesSupportedLanguages.Contains(langVersion) || langVersion == null && supportedFrameworks.Contains(framework) };
}
}
foreach (var langVersion in supportedLanguageVersions)
{
foreach (var framework in template.Frameworks)
{
yield return new object?[] { template.Template, true, framework, langVersion, fileScopedNamespacesSupportedLanguages.Contains(langVersion) || langVersion == null && supportedFrameworks.Contains(framework) };
}
}
}
}

[Theory]
//creates all possible combinations for supported templates, language versions and frameworks
[MemberData(nameof(FileScopedNamespacesSupport_Data))]
public void FileScopedNamespacesSupport(string name, bool pass, string? framework, string? langVersion, bool supportsFeature)
{
string workingDir = TestUtils.CreateTemporaryFolder();

List<string> args = new List<string>() { name, "-o", "MyProject" };
if (!string.IsNullOrWhiteSpace(framework))
{
args.Add("--framework");
args.Add(framework);
}
if (!string.IsNullOrWhiteSpace(langVersion))
{
args.Add("--langVersion");
args.Add(langVersion);
}

new DotnetNewCommand(_log, args.ToArray())
.WithCustomHive(_fixture.HomeDirectory)
.WithWorkingDirectory(workingDir)
.Execute()
.Should()
.ExitWith(0)
.And.NotHaveStdErr();

var buildResult = new DotnetCommand(_log, "build", "MyProject")
.WithWorkingDirectory(workingDir)
.Execute();

if (pass)
{
buildResult.Should().ExitWith(0).And.NotHaveStdErr();
}
else
{
buildResult.Should().Fail();
return;
}
string codeFileName = name == "console" ? "Program.cs" : "Class1.cs";
string programFileContent = File.ReadAllText(Path.Combine(workingDir, "MyProject", codeFileName));

string supportedContent =
@"namespace MyProject;
public class Class1
{
}";
string unsupportedContent =
@"namespace MyProject
{
public class Class1
{
}
}";

if (supportsFeature)
{
Assert.DoesNotContain(unsupportedContent, programFileContent);
Assert.Contains(supportedContent, programFileContent);
}
else
{
Assert.DoesNotContain(supportedContent, programFileContent);
Assert.Contains(unsupportedContent, programFileContent);
}
}
#endregion

[Theory]
Expand Down

0 comments on commit a0cd862

Please sign in to comment.