Skip to content

Commit

Permalink
Merge branch 'v12.0' into Re-enableIntelliTectWebDownloadTests
Browse files Browse the repository at this point in the history
  • Loading branch information
BenjaminMichaelis authored Sep 21, 2023
2 parents b0e3df0 + 74939e6 commit 164ae13
Show file tree
Hide file tree
Showing 26 changed files with 444 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/Chapter06/Listing06.27.CallingAConstructor.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_27;

using Listing06_28;
using Listing06_30;

#region INCLUDE
public class Program
Expand Down
25 changes: 25 additions & 0 deletions src/Chapter13.Tests/Table13.01.Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using AddisonWesley.Michaelis.EssentialCSharp.Shared.Tests;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01.Tests;

[TestClass]
public class LambdaHighlightTests
{

[TestMethod]
/* 1. */ [DataRow(".a")]
/* 2. */ [DataRow(".b")]
/* 3. */ [DataRow(".c")]
/* 4. */ [DataRow(".d", "CS0023")]
/* 5. */ [DataRow(".e", "CS0837")]
/* 6. */ [DataRow(".f", "CS0029", "CS1662")]
/* 7. */ [DataRow(".g", "CS8070", "CS1632")]
/* 8. */ [DataRow(".h", "CS0103")]
/* 9. */ [DataRow(".i", "CS0165")]
/* 10. */ [DataRow(".j", "CS0165")]
public async Task ParseAndCompile(string fileNameSuffix, params string[] errorIds)
{
await CompilerAssert.CompileAsync($"Table13.01{fileNameSuffix}.LambdaExpressionNotesAndExamples.cs", errorIds);
}
}
16 changes: 16 additions & 0 deletions src/Chapter13/Table13.01.a.LambdaExpressionNotesAndExamples.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// 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
{
// 1.
public static void DiscardParameters()
{
#if !NET6_0_OR_GREATER
Action<int, int> x = (_, _)=>
Console.WriteLine("This is a test.");
#endif
}
}
18 changes: 18 additions & 0 deletions src/Chapter13/Table13.01.b.LambdaExpressionNotesAndExamples.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// 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 TypeInferenceOfExpression()
{
#if !NET6_0_OR_GREATER
//You can assign lambda
//expression to an implicitly
//typed local variable starting C#10
var v = (int x) => x;
#endif
}
}
17 changes: 17 additions & 0 deletions src/Chapter13/Table13.01.c.LambdaExpressionNotesAndExamples.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// 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 ExpressionsCanHaveReturnTypes()
{
#if !NET6_0_OR_GREATER
Action action = void () => { };
var func = short?(long number) =>
number <= short.MaxValue ? (short)number : null;
#endif
}
}
19 changes: 19 additions & 0 deletions src/Chapter13/Table13.01.d.LambdaExpressionNotesAndExamples.cs
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
{
// 4.
public static void MemberMethodsOnExpressions()
{
//#if COMPILEERROR
#if !NET6_0_OR_GREATER
//ERROR: Operator "." cannot be applied to
//operand of type "lambda expression"
string s = ((int x) => x).ToString();
#endif
//#endif // COMPILEERROR
}
}
20 changes: 20 additions & 0 deletions src/Chapter13/Table13.01.e.LambdaExpressionNotesAndExamples.cs
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
{
// 5.
public static 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 src/Chapter13/Table13.01.f.LambdaExpressionNotesAndExamples.cs
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
{
// 6.
public static 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
}
}
30 changes: 30 additions & 0 deletions src/Chapter13/Table13.01.g.LambdaExpressionNotesAndExamples.cs
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
{
// 7.
public static 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 src/Chapter13/Table13.01.h.LambdaExpressionNotesAndExamples.cs
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
{
// 8.
public static 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, second) => first > second;
first++;
#endif
//#endif // COMPILEERROR
}
}
24 changes: 24 additions & 0 deletions src/Chapter13/Table13.01.i.LambdaExpressionNotesAndExamples.cs
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
{
// 9.
public static 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
}
}
25 changes: 25 additions & 0 deletions src/Chapter13/Table13.01.j.LambdaExpressionNotesAndExamples.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// 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
{
// 10.
public static void CompilerWillNotDetectInLambdaAssignment()
{
//#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
}
}
24 changes: 24 additions & 0 deletions src/Chapter18.Tests/Listing18.26.GenericAttributes.Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_26.Tests;

#if NET7_0_OR_GREATER
[TestClass]
public class GenericExceptionTests
{
[TestMethod]
public void ExpectedExceptionIsThrown()
{
ExpectedException<DivideByZeroException>.AssertExceptionThrown(
SampleTests.ThrowDivideByZeroExceptionTest);
}

[TestMethod]
[ExpectedException(typeof(InvalidOperationException))]
public void ExpectedExceptionIsNotThrown()
{
ExpectedException<DivideByZeroException>.AssertExceptionThrown(
() => { });
}
}
#endif // NET7_0_OR_GREATER
61 changes: 61 additions & 0 deletions src/Chapter18.Tests/Listing18.27.CallerArgumentExpression.Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_27.Tests;

#if NET7_0_OR_GREATER
[TestClass]
public class GenericExceptionTests
{
[TestMethod]
public void ExpectedExceptionIsThrown()
{
ExpectedException<DivideByZeroException>.AssertExceptionThrown(
SampleTests.ThrowArgumentNullExceptionTest);
}

[TestMethod]
public void VerifyExpectedExceptionMessage()
{
try
{
ExpectedException<InvalidOperationException>.AssertExceptionThrown(
() => { });
}
catch(InvalidOperationException exception)
{
Assert.IsTrue(exception.Message.Contains("'() => { }'"));
}
}

[TestMethod]
public void Method()
{
try
{
SampleTests.Method();
}
catch (InvalidOperationException exception)
{
Assert.IsTrue(
exception.Message.Contains("'() => { }'") &&
exception.Message.Contains("'Method'") &&
exception.Message.Contains("'./FileName.cs'"));
// The expected exception, System.DivideByZeroException,
// was not thrown by the expression, 'Method' in the method, './FileName.cs', and file 'C:\Git\EssentialCSharp\src\Chapter18\Listing18.25b.CallerArgumentExpression.cs'.
}
}

private object PassingMethodNameAndFileName()
{
throw new NotImplementedException();
}

[TestMethod]
[ExpectedException(typeof(InvalidOperationException))]
public void ExpectedExceptionIsNotThrown()
{
ExpectedException<DivideByZeroException>.AssertExceptionThrown(
() => { });
}
}
#endif // NET7_0_OR_GREATER
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_26.Tests;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_28.Tests;

[TestClass]
public class ProgramTests
Expand All @@ -14,4 +14,4 @@ public void MainTest()
IntelliTect.TestTools.Console.ConsoleAssert.Expect(
expected, Program.Main);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_27.Tests;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_29.Tests;

[TestClass]
public class ProgramTests
Expand All @@ -13,4 +13,4 @@ public void MainTest()
IntelliTect.TestTools.Console.ConsoleAssert.Expect(
expected, Program.Main);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_28.Tests;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_30.Tests;

[TestClass]
public class ProgramTests
Expand All @@ -13,4 +13,4 @@ public void MainTest()
IntelliTect.TestTools.Console.ConsoleAssert.Expect(
expected, Program.Main);
}
}
}
Loading

0 comments on commit 164ae13

Please sign in to comment.