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 80c10e6 commit bc36ba6
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 5 deletions.
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 @@ -109,7 +109,7 @@
},
"csharpFeature_FileScopedNamespaces": {
"type": "computed",
"value": "Framework != \"netstandard2.0\" && Framework != \"netstandard2.1\" && csharp10orLater == \"true\""
"value": "(Framework == \"net6.0\" || ((Framework == \"netstandard2.1\" || Framework = \"netstandard2.0\") && langVersion != \"\")) && csharp10orLater == \"true\""
},
"csharpFeature_Nullable": {
"type": "computed",
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
95 changes: 92 additions & 3 deletions test/dotnet-new3.UnitTests/CommonTemplatesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ Determining projects to restore\.\.\.
}

[Theory]
//creates all possible combinations for supported templatees, languages versions and frameworks
//creates all possible combinations for supported templates, language versions and frameworks
[MemberData(nameof(TopLevelProgramSupport_Data))]
public void TopLevelProgramSupport(string name, bool pass, string? framework, string? langVersion, bool hasTopLevelProgram)
{
Expand Down Expand Up @@ -263,7 +263,7 @@ public void TopLevelProgramSupport(string name, bool pass, string? framework, st
}

[Theory]
//creates all possible combinations for supported languages and frameworks
//creates all possible combinations for supported templates, language versions and frameworks
[MemberData(nameof(NullableSupport_Data))]
public void NullableSupport(string name, bool pass, string? framework, string? langVersion, bool supportsNullable)
{
Expand Down Expand Up @@ -348,7 +348,7 @@ public void NullableSupport(string name, bool pass, string? framework, string? l
}

[Theory]
//creates all possible combinations for supported languages and frameworks
//creates all possible combinations for supported templates, language versions and frameworks
[MemberData(nameof(ImplicitUsingsSupport_Data))]
public void ImplicitUsingsSupport(string name, bool pass, string? framework, string? langVersion, bool supportsImplicitUsings)
{
Expand Down Expand Up @@ -403,6 +403,95 @@ public void ImplicitUsingsSupport(string name, bool pass, string? framework, str
}
}

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 = { null, "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;\npublic class Class1\n{\n\n}";
string unsupportedContent = "namespace MyProject\n{\n public class Class1\n {\n\n }\n}";

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

[Theory]
[InlineData("Console Application", "console")]
[InlineData("Console Application", "console", "C#")]
Expand Down

0 comments on commit bc36ba6

Please sign in to comment.