Skip to content

Commit 5aed200

Browse files
authored
Convert solution to file-scoped namespaces (#168)
1 parent a32b806 commit 5aed200

File tree

123 files changed

+11973
-12092
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

123 files changed

+11973
-12092
lines changed

.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,9 @@ csharp_space_between_square_brackets = false
194194
csharp_preserve_single_line_blocks = true
195195
csharp_preserve_single_line_statements = false
196196

197+
# C# formatting settings - Namespace options
198+
csharp_style_namespace_declarations = file_scoped:warning
199+
197200
########## name all private fields using camelCase with underscore prefix ##########
198201
# https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-naming-conventions?view=vs-2019
199202
# dotnet_naming_rule.<namingRuleTitle>.symbols = <symbolTitle>

src/GraphQLParser.ApiTests/ApiApprovalTests.cs

Lines changed: 54 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -8,74 +8,73 @@
88
using Shouldly;
99
using Xunit;
1010

11-
namespace GraphQLParser.ApiTests
11+
namespace GraphQLParser.ApiTests;
12+
13+
/// <summary>
14+
/// Tests to verify public API surface.
15+
/// </summary>
16+
public class ApiApprovalTests
1217
{
13-
/// <summary>
14-
/// Tests to verify public API surface.
15-
/// </summary>
16-
public class ApiApprovalTests
18+
[Theory]
19+
[InlineData(typeof(Lexer))]
20+
public void Public_Api_Should_Not_Change_Inadvertently(Type type)
1721
{
18-
[Theory]
19-
[InlineData(typeof(Lexer))]
20-
public void Public_Api_Should_Not_Change_Inadvertently(Type type)
21-
{
22-
string baseDir = AppDomain.CurrentDomain.BaseDirectory;
23-
string projectName = type.Assembly.GetName().Name!;
24-
string testDir = Path.Combine(baseDir, $"..{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}..");
25-
string projectDir = Path.Combine(testDir, "..");
26-
string buildDir = Path.Combine(projectDir, projectName, "bin", "Debug");
27-
Debug.Assert(Directory.Exists(buildDir), $"Directory '{buildDir}' doesn't exist");
28-
string csProject = Path.Combine(projectDir, projectName, projectName + ".csproj");
29-
var project = XDocument.Load(csProject);
30-
string[] tfms = project.Descendants("TargetFrameworks").Union(project.Descendants("TargetFramework")).First().Value.Split(";", StringSplitOptions.RemoveEmptyEntries);
22+
string baseDir = AppDomain.CurrentDomain.BaseDirectory;
23+
string projectName = type.Assembly.GetName().Name!;
24+
string testDir = Path.Combine(baseDir, $"..{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}..");
25+
string projectDir = Path.Combine(testDir, "..");
26+
string buildDir = Path.Combine(projectDir, projectName, "bin", "Debug");
27+
Debug.Assert(Directory.Exists(buildDir), $"Directory '{buildDir}' doesn't exist");
28+
string csProject = Path.Combine(projectDir, projectName, projectName + ".csproj");
29+
var project = XDocument.Load(csProject);
30+
string[] tfms = project.Descendants("TargetFrameworks").Union(project.Descendants("TargetFramework")).First().Value.Split(";", StringSplitOptions.RemoveEmptyEntries);
3131

32-
// There may be old stuff from earlier builds like net45, netcoreapp3.0, etc. so filter it out
33-
string[] actualTfmDirs = Directory.GetDirectories(buildDir).Where(dir => tfms.Any(tfm => dir.EndsWith(tfm))).ToArray();
34-
Debug.Assert(actualTfmDirs.Length > 0, $"Directory '{buildDir}' doesn't contain subdirectories matching {string.Join(";", tfms)}");
32+
// There may be old stuff from earlier builds like net45, netcoreapp3.0, etc. so filter it out
33+
string[] actualTfmDirs = Directory.GetDirectories(buildDir).Where(dir => tfms.Any(tfm => dir.EndsWith(tfm))).ToArray();
34+
Debug.Assert(actualTfmDirs.Length > 0, $"Directory '{buildDir}' doesn't contain subdirectories matching {string.Join(";", tfms)}");
3535

36-
(string tfm, string content)[] publicApi = actualTfmDirs.Select(tfmDir => (new DirectoryInfo(tfmDir).Name.Replace(".", ""), Assembly.LoadFile(Path.Combine(tfmDir, projectName + ".dll")).GeneratePublicApi(new ApiGeneratorOptions
37-
{
38-
IncludeAssemblyAttributes = false,
39-
//WhitelistedNamespacePrefixes = new[] { "Microsoft.Extensions.DependencyInjection" },
40-
ExcludeAttributes = new[] { "System.Diagnostics.DebuggerDisplayAttribute", "System.Diagnostics.CodeAnalysis.AllowNullAttribute" }
41-
}))).ToArray();
36+
(string tfm, string content)[] publicApi = actualTfmDirs.Select(tfmDir => (new DirectoryInfo(tfmDir).Name.Replace(".", ""), Assembly.LoadFile(Path.Combine(tfmDir, projectName + ".dll")).GeneratePublicApi(new ApiGeneratorOptions
37+
{
38+
IncludeAssemblyAttributes = false,
39+
//WhitelistedNamespacePrefixes = new[] { "Microsoft.Extensions.DependencyInjection" },
40+
ExcludeAttributes = new[] { "System.Diagnostics.DebuggerDisplayAttribute", "System.Diagnostics.CodeAnalysis.AllowNullAttribute" }
41+
}))).ToArray();
4242

43-
if (publicApi.DistinctBy(item => item.content).Count() == 1)
43+
if (publicApi.DistinctBy(item => item.content).Count() == 1)
44+
{
45+
AutoApproveOrFail(publicApi[0].content, "");
46+
}
47+
else
48+
{
49+
foreach (var item in publicApi.ToLookup(item => item.content))
4450
{
45-
AutoApproveOrFail(publicApi[0].content, "");
51+
AutoApproveOrFail(item.Key, string.Join("+", item.Select(x => x.tfm).OrderBy(x => x)));
4652
}
47-
else
53+
}
54+
55+
// Approval test should (re)generate approved.txt files locally if needed.
56+
// Approval test should fail on CI.
57+
// https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables
58+
void AutoApproveOrFail(string publicApi, string folder)
59+
{
60+
string file = null!;
61+
62+
try
4863
{
49-
foreach (var item in publicApi.ToLookup(item => item.content))
50-
{
51-
AutoApproveOrFail(item.Key, string.Join("+", item.Select(x => x.tfm).OrderBy(x => x)));
52-
}
64+
publicApi.ShouldMatchApproved(options => options.SubFolder(folder).NoDiff().WithFilenameGenerator((testMethodInfo, discriminator, fileType, fileExtension) => file = $"{type.Assembly.GetName().Name}.{fileType}.{fileExtension}"));
5365
}
54-
55-
// Approval test should (re)generate approved.txt files locally if needed.
56-
// Approval test should fail on CI.
57-
// https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables
58-
void AutoApproveOrFail(string publicApi, string folder)
66+
catch (ShouldMatchApprovedException) when (Environment.GetEnvironmentVariable("CI") == null)
5967
{
60-
string file = null!;
61-
62-
try
68+
string? received = Path.Combine(testDir, folder, file);
69+
string? approved = received.Replace(".received.txt", ".approved.txt");
70+
if (File.Exists(received) && File.Exists(approved))
6371
{
64-
publicApi.ShouldMatchApproved(options => options.SubFolder(folder).NoDiff().WithFilenameGenerator((testMethodInfo, discriminator, fileType, fileExtension) => file = $"{type.Assembly.GetName().Name}.{fileType}.{fileExtension}"));
72+
File.Copy(received, approved, overwrite: true);
73+
File.Delete(received);
6574
}
66-
catch (ShouldMatchApprovedException) when (Environment.GetEnvironmentVariable("CI") == null)
75+
else
6776
{
68-
string? received = Path.Combine(testDir, folder, file);
69-
string? approved = received.Replace(".received.txt", ".approved.txt");
70-
if (File.Exists(received) && File.Exists(approved))
71-
{
72-
File.Copy(received, approved, overwrite: true);
73-
File.Delete(received);
74-
}
75-
else
76-
{
77-
throw;
78-
}
77+
throw;
7978
}
8079
}
8180
}
Lines changed: 45 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,55 @@
11
using System.Collections.Generic;
22
using BenchmarkDotNet.Attributes;
33

4-
namespace GraphQLParser.Benchmarks
5-
{
6-
public abstract class BenchmarkBase : IBenchmark
7-
{
8-
private string _hero = null!;
9-
private string _escapes = null!;
10-
private string _kitchen = null!;
11-
private string _introspection = null!;
12-
private string _params = null!;
13-
private string _variables = null!;
14-
private string _github = null!;
4+
namespace GraphQLParser.Benchmarks;
155

16-
[GlobalSetup]
17-
public virtual void GlobalSetup()
18-
{
19-
_hero = "hero".ReadGraphQLFile();
20-
_escapes = "query_with_many_escape_symbols".ReadGraphQLFile();
21-
_kitchen = "kitchenSink".ReadGraphQLFile();
22-
_introspection = "introspectionQuery".ReadGraphQLFile();
23-
_params = "params".ReadGraphQLFile();
24-
_variables = "variables".ReadGraphQLFile();
25-
_github = "github".ReadGraphQLFile();
26-
}
6+
public abstract class BenchmarkBase : IBenchmark
7+
{
8+
private string _hero = null!;
9+
private string _escapes = null!;
10+
private string _kitchen = null!;
11+
private string _introspection = null!;
12+
private string _params = null!;
13+
private string _variables = null!;
14+
private string _github = null!;
2715

28-
public string GetQueryByName(string name)
29-
{
30-
return name switch
31-
{
32-
"hero" => _hero,
33-
"escapes" => _escapes,
34-
"kitchen" => _kitchen,
35-
"introspection" => _introspection,
36-
"params" => _params,
37-
"variables" => _variables,
38-
"github" => _github,
39-
_ => throw new System.Exception(name)
40-
};
41-
}
16+
[GlobalSetup]
17+
public virtual void GlobalSetup()
18+
{
19+
_hero = "hero".ReadGraphQLFile();
20+
_escapes = "query_with_many_escape_symbols".ReadGraphQLFile();
21+
_kitchen = "kitchenSink".ReadGraphQLFile();
22+
_introspection = "introspectionQuery".ReadGraphQLFile();
23+
_params = "params".ReadGraphQLFile();
24+
_variables = "variables".ReadGraphQLFile();
25+
_github = "github".ReadGraphQLFile();
26+
}
4227

43-
public IEnumerable<string> Names()
28+
public string GetQueryByName(string name)
29+
{
30+
return name switch
4431
{
45-
yield return "hero";
46-
yield return "escapes";
47-
yield return "kitchen";
48-
yield return "introspection";
49-
yield return "params";
50-
yield return "variables";
51-
yield return "github";
52-
}
32+
"hero" => _hero,
33+
"escapes" => _escapes,
34+
"kitchen" => _kitchen,
35+
"introspection" => _introspection,
36+
"params" => _params,
37+
"variables" => _variables,
38+
"github" => _github,
39+
_ => throw new System.Exception(name)
40+
};
41+
}
5342

54-
public abstract void Run();
43+
public IEnumerable<string> Names()
44+
{
45+
yield return "hero";
46+
yield return "escapes";
47+
yield return "kitchen";
48+
yield return "introspection";
49+
yield return "params";
50+
yield return "variables";
51+
yield return "github";
5552
}
53+
54+
public abstract void Run();
5655
}
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
namespace GraphQLParser.Benchmarks
1+
namespace GraphQLParser.Benchmarks;
2+
3+
internal interface IBenchmark
24
{
3-
internal interface IBenchmark
4-
{
5-
void GlobalSetup();
5+
void GlobalSetup();
66

7-
void Run();
8-
}
7+
void Run();
98
}
Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
using BenchmarkDotNet.Attributes;
22

3-
namespace GraphQLParser.Benchmarks
3+
namespace GraphQLParser.Benchmarks;
4+
5+
[MemoryDiagnoser]
6+
public class LexerBenchmark : BenchmarkBase
47
{
5-
[MemoryDiagnoser]
6-
public class LexerBenchmark : BenchmarkBase
8+
[Benchmark]
9+
[ArgumentsSource(nameof(Names))]
10+
public void Lex(string name)
711
{
8-
[Benchmark]
9-
[ArgumentsSource(nameof(Names))]
10-
public void Lex(string name)
12+
var source = GetQueryByName(name);
13+
int resetPosition = 0;
14+
Token token;
15+
while ((token = Lexer.Lex(source, resetPosition)).Kind != TokenKind.EOF)
1116
{
12-
var source = GetQueryByName(name);
13-
int resetPosition = 0;
14-
Token token;
15-
while ((token = Lexer.Lex(source, resetPosition)).Kind != TokenKind.EOF)
16-
{
17-
resetPosition = token.End;
18-
}
17+
resetPosition = token.End;
1918
}
20-
21-
public override void Run() => Lex("github");
2219
}
20+
21+
public override void Run() => Lex("github");
2322
}

0 commit comments

Comments
 (0)