-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Table 13.01 Code listings and tests written
- Loading branch information
Showing
11 changed files
with
192 additions
and
51 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using AddisonWesley.Michaelis.EssentialCSharp.Shared.Tests; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01.Tests; | ||
|
||
[TestClass] | ||
public class LambdaHighlightTests | ||
{ | ||
public TestContext TestContext { get; set; } = null!; // Set by MSTest; | ||
|
||
[TestMethod] | ||
/* 1. */ [DataRow(".a", "CS0023")] | ||
/* 2. */ [DataRow(".b", "CS0837")] | ||
/* 3. */ [DataRow(".c", "CS0029", "CS1662")] | ||
/* 5. */ [DataRow(".e", "CS8070", "CS1632")] | ||
/* 6. */ [DataRow(".f", "CS0103")] | ||
/* 7. */ [DataRow(".g", "CS0165")] | ||
/* 8. */ [DataRow(".h", "CS0165")] | ||
public async Task ParseAndCompile(string fileNameSuffix, params string[] errorIds) | ||
{ | ||
await CompilerAssert.CompileAsync($"Table13.01{fileNameSuffix}.LambdaExpressionNotesAndExamples.cs", errorIds); | ||
} | ||
|
||
[TestMethod] | ||
public void PatternMatchingOnTypeTest(){ | ||
LambdaExpressionNotesAndExamples.PatternMatchingOnType(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/Chapter13/Table13.01.b.LambdaExpressionNotesAndExamples.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Justification: Only snippets of source code shown for elucidation. | ||
#pragma warning disable CS0168 // Variable is declared but never used | ||
|
||
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01; | ||
|
||
public partial class LambdaExpressionNotesAndExamples | ||
{ | ||
// 2. | ||
static public void PatternMatchingOnType() | ||
{ | ||
//#if COMPILEERROR | ||
#if !NET6_0_OR_GREATER | ||
//ERROR: The first operand of an "is" or "as" | ||
//operator may not be a lambda expression or | ||
//anonymous method | ||
bool b = ((int x) => x) is Func<int,int>; | ||
#endif | ||
//#endif // COMPILEERROR | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/Chapter13/Table13.01.c.LambdaExpressionNotesAndExamples.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Justification: Only snippets of source code shown for elucidation. | ||
#pragma warning disable CS0168 // Variable is declared but never used | ||
|
||
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01; | ||
|
||
public partial class LambdaExpressionNotesAndExamples | ||
{ | ||
// 3. | ||
static public void ConvertingToImproperDelegate() | ||
{ | ||
//#if COMPILEERROR | ||
#if !NET6_0_OR_GREATER | ||
//ERROR: Lambda expression is not compatible | ||
//with Func<int, bool> type | ||
Func<int, bool> f = (int x) => x; | ||
#endif | ||
//#endif // COMPILEERROR | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/Chapter13/Table13.01.d.LambdaExpressionNotesAndExamples.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Justification: Only snippets of source code shown for elucidation. | ||
#pragma warning disable CS0168 // Variable is declared but never used | ||
|
||
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01; | ||
|
||
public partial class LambdaExpressionNotesAndExamples | ||
{ | ||
// 4. | ||
static public void TypeInferenceOfExpression() | ||
{ | ||
//#if COMPILEERROR | ||
#if !NET6_0_OR_GREATER | ||
//ERROR: You cannot assign lambda | ||
//expression to an implicitly | ||
//typed local variable prior C#10 | ||
var v = (int x) => x; | ||
#endif | ||
//#endif // COMPILEERROR | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/Chapter13/Table13.01.e.LambdaExpressionNotesAndExamples.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Justification: Only snippets of source code shown for elucidation. | ||
#pragma warning disable CS0168 // Variable is declared but never used | ||
|
||
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01; | ||
|
||
public partial class LambdaExpressionNotesAndExamples | ||
{ | ||
// 5. | ||
static public void JumpStatementsToOutOfScopeDestinations() | ||
{ | ||
//#if COMPILEERROR | ||
#if !NET6_0_OR_GREATER | ||
//ERROR: Control cannot leave the body of an | ||
//anonymous method or lambda expression | ||
string[] args = {"/File", "fileThatMostCertainlyDoesNotExist"}; | ||
Func<string> f; | ||
switch(args[0]) | ||
{ | ||
case "/File": | ||
f = () => | ||
{ | ||
if (!File.Exists(args[1])) | ||
break; | ||
return args[1]; | ||
}; | ||
} | ||
#endif | ||
//#endif // COMPILEERROR | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/Chapter13/Table13.01.f.LambdaExpressionNotesAndExamples.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Justification: Only snippets of source code shown for elucidation. | ||
#pragma warning disable CS0168 // Variable is declared but never used | ||
|
||
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01; | ||
|
||
public partial class LambdaExpressionNotesAndExamples | ||
{ | ||
// 6. | ||
static public void AccessingParametersAndLocalsOutOfBody() | ||
{ | ||
//#if COMPILEERROR | ||
#if !NET6_0_OR_GREATER | ||
//ERROR: The name "first" does not | ||
//exist in the current context | ||
Func <int, int, bool> expression = | ||
(first, secont) => first > second; | ||
first++; | ||
#endif | ||
//#endif // COMPILEERROR | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/Chapter13/Table13.01.g.LambdaExpressionNotesAndExamples.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Justification: Only snippets of source code shown for elucidation. | ||
#pragma warning disable CS0168 // Variable is declared but never used | ||
|
||
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01; | ||
|
||
public partial class LambdaExpressionNotesAndExamples | ||
{ | ||
// 7. | ||
static public void UsingOutParameters() | ||
{ | ||
//#if COMPILEERROR | ||
#if !NET6_0_OR_GREATER | ||
int number; | ||
Func <string, bool> f = | ||
text => int.TryParse(text, out number); | ||
if (f("1")) | ||
{ | ||
//ERROR: Use of unassigned local variable | ||
System.Console.Write(number) | ||
} | ||
#endif | ||
//#endif // COMPILEERROR | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/Chapter13/Table13.01.h.LambdaExpressionNotesAndExamples.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Justification: Only snippets of source code shown for elucidation. | ||
#pragma warning disable CS0168 // Variable is declared but never used | ||
|
||
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01; | ||
|
||
public partial class LambdaExpressionNotesAndExamples | ||
{ | ||
// 8. | ||
static public void CompilerWillNotDetectInLambdaInitialization() | ||
{ | ||
//#if COMPILEERROR | ||
#if !NET6_0_OR_GREATER | ||
int number; | ||
Func<int, bool> isFortyTwo = | ||
x => 42 == (number = x); | ||
if(isFortyTwo(42)) | ||
{ | ||
//ERROR: Use of unassigned local variable | ||
System.Console.Write(number); | ||
} | ||
#endif | ||
//#endif // COMPILEERROR | ||
} | ||
} |