Skip to content

Commit b43a102

Browse files
committed
Switched to file based namespaces
1 parent bb5bbb8 commit b43a102

32 files changed

+1109
-995
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using Microsoft.CodeAnalysis;
2+
using Microsoft.CodeAnalysis.CSharp;
3+
using Microsoft.CodeAnalysis.Diagnostics;
4+
using Microsoft.CodeAnalysis.Testing;
5+
using Microsoft.CodeAnalysis.Testing.Verifiers;
6+
using System.Collections.Immutable;
7+
8+
namespace AddisonWesley.Michaelis.EssentialCSharp.Shared;
9+
10+
public static class CompilerAssert2
11+
{
12+
public static async Task Compile2Async(string[] fileNames, string[] expectedErrorIds)
13+
{
14+
string code = string.Empty;
15+
foreach (string fileName in fileNames)
16+
{
17+
code += Environment.NewLine + await File.ReadAllTextAsync(fileName);
18+
}
19+
20+
var test = new Test()
21+
{
22+
TestCode = code
23+
};
24+
foreach (var errorId in expectedErrorIds)
25+
{
26+
test.ExpectedDiagnostics.Add(DiagnosticResult.CompilerError(errorId));
27+
}
28+
29+
await test.RunAsync();
30+
}
31+
32+
//Custom verifier to ignore diagnostic locations locations
33+
public class CustomMSTestVerifier : MSTestVerifier
34+
{
35+
public override void Equal<T>(T expected, T actual, string? message = null)
36+
{
37+
if (typeof(T) == typeof(Location))
38+
{
39+
return;
40+
}
41+
base.Equal(expected, actual, message);
42+
}
43+
}
44+
45+
private class Test : AnalyzerTest<CustomMSTestVerifier>
46+
{
47+
protected override CompilationOptions CreateCompilationOptions()
48+
{
49+
var compilationOptions = new CSharpCompilationOptions(
50+
OutputKind.DynamicallyLinkedLibrary,
51+
allowUnsafe: true,
52+
//Setup set #nullable enable
53+
nullableContextOptions: NullableContextOptions.Enable);
54+
55+
return compilationOptions
56+
.WithSpecificDiagnosticOptions(
57+
compilationOptions.SpecificDiagnosticOptions.SetItems(GetNullableWarningsFromCompiler()));
58+
}
59+
60+
public LanguageVersion LanguageVersion { get; set; } = LanguageVersion.Default;
61+
protected override string DefaultFileExt => "cs";
62+
public override string Language => LanguageNames.CSharp;
63+
64+
private static ImmutableDictionary<string, ReportDiagnostic> GetNullableWarningsFromCompiler()
65+
{
66+
string[] args = Array.Empty<string>();
67+
//TODO: if we care to elevate NRT warnings, you can do it like this:
68+
//string[] args = { "/warnaserror:nullable" };
69+
var commandLineArguments = CSharpCommandLineParser.Default.Parse(args, baseDirectory: Environment.CurrentDirectory, sdkDirectory: Environment.CurrentDirectory);
70+
var nullableWarnings = commandLineArguments.CompilationOptions.SpecificDiagnosticOptions;
71+
72+
return nullableWarnings;
73+
}
74+
75+
protected override ParseOptions CreateParseOptions()
76+
=> new CSharpParseOptions(LanguageVersion, DocumentationMode.Diagnose)
77+
.WithPreprocessorSymbols("INCLUDE");
78+
79+
protected override IEnumerable<DiagnosticAnalyzer> GetDiagnosticAnalyzers()
80+
=> Enumerable.Empty<DiagnosticAnalyzer>();
81+
}
82+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using AddisonWesley.Michaelis.EssentialCSharp.Shared;
2+
using AddisonWesley.Michaelis.EssentialCSharp.Shared.Tests;
3+
using Microsoft.VisualStudio.TestTools.UnitTesting;
4+
5+
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing07_06.Tests;
6+
7+
[TestClass]
8+
public class ProgramTests
9+
{
10+
[TestMethod]
11+
[Ignore]
12+
public async Task UnassignedVariableThrowsError()
13+
{
14+
await CompilerAssert2.Compile2Async(
15+
new string[] { "Listing07.06.PrivateMembersAreNotInherited.cs" },
16+
new string[] { "CS0122" });
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using AddisonWesley.Michaelis.EssentialCSharp.Shared;
2+
using AddisonWesley.Michaelis.EssentialCSharp.Shared.Tests;
3+
using Microsoft.VisualStudio.TestTools.UnitTesting;
4+
5+
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing07_07.Tests;
6+
7+
[TestClass]
8+
public class ProgramTests
9+
{
10+
[TestMethod]
11+
[Ignore]
12+
public async Task UnassignedVariableThrowsError()
13+
{
14+
await CompilerAssert2.Compile2Async(
15+
new string[] { "Listing07.08.PreventingDerivationWithSealedClasses.cs" },
16+
new string[] { "CS0122" });
17+
}
18+
}

src/Chapter07/Chapter07.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,18 @@
1010
</PropertyGroup>
1111
<Import Project="..\Chapter.props" />
1212
<ItemGroup>
13+
<Compile Remove="Listing07.06.PrivateMembersAreNotInherited.cs" />
14+
<Compile Remove="Listing07.07.ProtectedMembersAreAccessibleOnlyFromDerivedClasses.cs" />
1315
<Compile Remove="Table07.03.TypePatternMatchingWithIsOperator.cs" />
1416
</ItemGroup>
1517
<ItemGroup>
1618
<Compile Include="..\Shared\Program.cs">
1719
<Link>Program.cs</Link>
1820
</Compile>
21+
<None Include="Listing07.06.PrivateMembersAreNotInherited.cs">
22+
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
23+
</None>
24+
<None Include="Listing07.07.ProtectedMembersAreAccessibleOnlyFromDerivedClasses.cs" />
1925
<None Include="Table07.03.TypePatternMatchingWithIsOperator.cs" />
2026
</ItemGroup>
2127
</Project>
Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
1-
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter07.Listing07_01
2-
{
3-
using System;
4-
using System.Diagnostics.CodeAnalysis;
1+
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter07.Listing07_01;
2+
3+
using System;
4+
using System.Diagnostics.CodeAnalysis;
55

6-
#region INCLUDE
7-
public class PdaItem
8-
{
9-
[DisallowNull]
10-
public string? Name { get; set; }
11-
public DateTime LastUpdated { get; set; }
12-
}
13-
// Define the Contact class as inheriting the PdaItem class
14-
#region HIGHLIGHT
15-
public class Contact : PdaItem
16-
#endregion HIGHLIGHT
17-
{
18-
public string? Address { get; set; }
19-
public string? Phone { get; set; }
20-
}
21-
#endregion INCLUDE
6+
#region INCLUDE
7+
public class PdaItem
8+
{
9+
[DisallowNull]
10+
public string? Name { get; set; }
11+
public DateTime LastUpdated { get; set; }
12+
}
13+
// Define the Contact class as inheriting the PdaItem class
14+
#region HIGHLIGHT
15+
public class Contact : PdaItem
16+
#endregion HIGHLIGHT
17+
{
18+
public string? Address { get; set; }
19+
public string? Phone { get; set; }
2220
}
21+
#endregion INCLUDE
Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1-
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter07.Listing07_02
2-
{
3-
using Listing07_01;
1+
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter07.Listing07_02;
2+
3+
using Listing07_01;
44

5-
#region INCLUDE
6-
public class Program
5+
#region INCLUDE
6+
public class Program
7+
{
8+
public static void Main()
79
{
8-
public static void Main()
9-
{
10-
Contact contact = new Contact();
11-
#region HIGHLIGHT
12-
contact.Name = "Inigo Montoya";
13-
#endregion HIGHLIGHT
10+
Contact contact = new Contact();
11+
#region HIGHLIGHT
12+
contact.Name = "Inigo Montoya";
13+
#endregion HIGHLIGHT
1414

15-
// ...
16-
}
15+
// ...
1716
}
18-
#endregion INCLUDE
1917
}
18+
#endregion INCLUDE
Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1-
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter07.Listing07_03
1+
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter07.Listing07_03;
2+
3+
#region INCLUDE
4+
public class PdaItem : object
25
{
3-
#region INCLUDE
4-
public class PdaItem : object
5-
{
6-
// ...
7-
}
8-
public class Appointment : PdaItem
9-
{
10-
// ...
11-
}
12-
public class Contact : PdaItem
13-
{
14-
// ...
15-
}
16-
public class Customer : Contact
17-
{
18-
// ...
19-
}
20-
#endregion INCLUDE
6+
// ...
217
}
8+
public class Appointment : PdaItem
9+
{
10+
// ...
11+
}
12+
public class Contact : PdaItem
13+
{
14+
// ...
15+
}
16+
public class Customer : Contact
17+
{
18+
// ...
19+
}
20+
#endregion INCLUDE
Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
1-
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter07.Listing07_04
2-
{
3-
using Listing07_03;
1+
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter07.Listing07_04;
2+
3+
using Listing07_03;
44

5-
#region INCLUDE
6-
public class Program
5+
#region INCLUDE
6+
public class Program
7+
{
8+
public static void Main()
79
{
8-
public static void Main()
9-
{
10-
// Derived types can be implicitly converted to
11-
// base types
12-
Contact contact = new Contact();
13-
#region HIGHLIGHT
14-
PdaItem item = contact;
15-
#endregion HIGHLIGHT
16-
// ...
10+
// Derived types can be implicitly converted to
11+
// base types
12+
Contact contact = new Contact();
13+
#region HIGHLIGHT
14+
PdaItem item = contact;
15+
#endregion HIGHLIGHT
16+
// ...
1717

18-
// Base types must be cast explicitly to derived types
19-
#region HIGHLIGHT
20-
contact = (Contact)item;
21-
#endregion HIGHLIGHT
22-
// ...
23-
}
18+
// Base types must be cast explicitly to derived types
19+
#region HIGHLIGHT
20+
contact = (Contact)item;
21+
#endregion HIGHLIGHT
22+
// ...
2423
}
25-
#endregion INCLUDE
2624
}
25+
#endregion INCLUDE
Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
#pragma warning disable IDE0060 // Remove unused parameter
22

3-
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter07.Listing07_05
3+
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter07.Listing07_05;
4+
5+
#region INCLUDE
6+
class GPSCoordinates
47
{
5-
#region INCLUDE
6-
class GPSCoordinates
7-
{
8-
// ...
8+
// ...
99

10-
public static implicit operator UTMCoordinates(
11-
GPSCoordinates coordinates)
12-
{
13-
#region EXCLUDE
14-
return null!; //return the new UTMCoordinates object
15-
#endregion EXCLUDE
16-
}
10+
public static implicit operator UTMCoordinates(
11+
GPSCoordinates coordinates)
12+
{
13+
#region EXCLUDE
14+
return null!; //return the new UTMCoordinates object
15+
#endregion EXCLUDE
1716
}
18-
#endregion INCLUDE
17+
}
18+
#endregion INCLUDE
1919

20-
class UTMCoordinates
21-
{
20+
class UTMCoordinates
21+
{
2222

23-
}
2423
}

0 commit comments

Comments
 (0)