Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# EssentialCSharp - C#本质论
# EssentialCSharp - C#本质论

本项目包含《C# 12.0本质论》的源代码,2024年清华大学出版社出版。

Expand Down Expand Up @@ -58,7 +58,9 @@

## 测试

注:中文版请忽略。
测试断言按中文版译稿及本仓库的配套示例维护,覆盖现有的 net6.0、net7.0 和 net8.0 目标框架。

部分清单刻意演示编译错误或运行时异常;对应测试会核对编译诊断或预期异常,不应为了通过测试而消除这些教学示例中的错误。

许多代码清单都有相应的单元测试。

Expand Down
4 changes: 2 additions & 2 deletions src/Chapter01.Tests/Listing01.01.Tests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using AddisonWesley.Michaelis.EssentialCSharp.Shared.Tests;
using AddisonWesley.Michaelis.EssentialCSharp.Shared.Tests;

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter01.Listing01_01.Tests;

Expand Down Expand Up @@ -27,7 +27,7 @@ public void CreateAndRunNewConsoleProject()
// produced the expected output.
Assert.IsTrue(
psOutput.Contains(
$"{Environment.NewLine}Hello. My name is Inigo Montoya.{Environment.NewLine}"),
$"{Environment.NewLine}你好,我叫Inigo Montoya。{Environment.NewLine}"),
"The expected HelloWorld output was not found.");

// If the output is in English
Expand Down
6 changes: 3 additions & 3 deletions src/Chapter01.Tests/Listing01.03.Tests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter01.Listing01_03.Tests;

[TestClass]
Expand All @@ -8,8 +8,8 @@ public class HelloWorldTests
public void Main_UpDown()
{
const string expected =
@"Up
Down";
@"上
下";

IntelliTect.TestTools.Console.ConsoleAssert.Expect(
expected, HelloWorld.Main);
Expand Down
8 changes: 4 additions & 4 deletions src/Chapter01.Tests/Listing01.04.Tests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter01.Listing01_04.Tests;

[TestClass]
Expand All @@ -8,9 +8,9 @@ public class HelloWorldTests
public void Main_InigoHello()
{
const string expected =
@"Down
Side
Up";
@"上
中
下";

IntelliTect.TestTools.Console.ConsoleAssert.Expect(
expected, HelloWorld.Main);
Expand Down
4 changes: 2 additions & 2 deletions src/Chapter01.Tests/Listing01.05.Tests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter01.Listing01_05.Tests;

[TestClass]
Expand All @@ -8,7 +8,7 @@ public class HelloWorldTests
public void Main_InigoHello()
{
const string expected =
@"Hello. My name is Inigo Montoya.";
@"你好,我叫Inigo Montoya。";

IntelliTect.TestTools.Console.ConsoleAssert.Expect(
expected, HelloWorld.Main);
Expand Down
4 changes: 2 additions & 2 deletions src/Chapter01.Tests/Listing01.06.Tests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter01.Listing01_06.Tests;

[TestClass]
Expand All @@ -8,7 +8,7 @@ public class HelloWorldTests
public void Main_InigoHello()
{
const string expected =
@"Hello. My name is Inigo Montoya.";
@"你好,我叫Inigo Montoya。";

IntelliTect.TestTools.Console.ConsoleAssert.Expect(
expected, Program.Main);
Expand Down
4 changes: 2 additions & 2 deletions src/Chapter01.Tests/Listing01.08.Tests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter01.Listing01_08.Tests;

[TestClass]
Expand All @@ -7,7 +7,7 @@ public class ProgramTests
[TestMethod]
public void Main_InigoHello()
{
const string expected = @"Hello, My name is Inigo Montoya";
const string expected = @"你好,我叫Inigo Montoya。";

IntelliTect.TestTools.Console.ConsoleAssert.Expect(
expected, Program.Main);
Expand Down
4 changes: 2 additions & 2 deletions src/Chapter01.Tests/Listing01.10.Tests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter01.Listing01_10.Tests;

[TestClass]
Expand All @@ -7,7 +7,7 @@ public class HelloWorldTests
[TestMethod]
public void Main_HelloToInigo()
{
const string expected = @"Hello Inigo Montoya";
const string expected = @"你好,Inigo Montoya。";

IntelliTect.TestTools.Console.ConsoleAssert.Expect(
expected, HelloWorld.Main);
Expand Down
4 changes: 2 additions & 2 deletions src/Chapter01.Tests/Listing01.11.Tests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter01.Listing01_11.Tests;

[TestClass]
Expand All @@ -7,7 +7,7 @@ public class ProgramTests
[TestMethod]
public void Main_HelloToInigo()
{
const string expected = @"Hello Inigo Montoya";
const string expected = @"你好,Inigo Montoya。";

IntelliTect.TestTools.Console.ConsoleAssert.Expect(
expected, HelloWorld.Main);
Expand Down
4 changes: 2 additions & 2 deletions src/Chapter01.Tests/Listing01.12.Tests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter01.Listing01_12.Tests;

[TestClass]
Expand All @@ -7,7 +7,7 @@ public class MiracleMaxTests
[TestMethod]
public void Main_StormCastleShort()
{
const string expected = @"Have fun storming the castle!";
const string expected = @"愉快地袭击古堡吧!";

IntelliTect.TestTools.Console.ConsoleAssert.Expect(
expected, MiracleMax.Main);
Expand Down
6 changes: 3 additions & 3 deletions src/Chapter01.Tests/Listing01.13.Tests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter01.Listing01_13.Tests;

[TestClass]
Expand All @@ -8,8 +8,8 @@ public class HelloWorldTests
public void Main_InigoMantra_ProperOutput()
{
const string expected =
@"My name is Inigo Montoya.
You killed my father....";
@"你好,我叫Inigo Montoya。
你杀死了我的父亲....";

IntelliTect.TestTools.Console.ConsoleAssert.Expect(expected, HelloWorld.Main);
}
Expand Down
8 changes: 4 additions & 4 deletions src/Chapter01.Tests/Listing01.14.Tests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter01.Listing01_14.Tests;

[TestClass]
Expand All @@ -8,9 +8,9 @@ public class MiracleMaxTests
public void Main_StormCastleLong()
{
const string expected =
@"Have fun storming the castle!
Think it will work?
It would take a miracle.";
@"愉快地袭击古堡吧!
你觉得可以吗?
这需要奇迹。";

IntelliTect.TestTools.Console.ConsoleAssert.Expect(
expected, StormingTheCastle.Main);
Expand Down
4 changes: 2 additions & 2 deletions src/Chapter01.Tests/Listing01.15.Tests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter01.Listing01_15.Tests;

[TestClass]
Expand All @@ -8,7 +8,7 @@ public class Listing01_13_Tests
public void Main_ItWouldTakeAMiracle_OutputWrittenToConsole()
{
const string expected =
@"It would take a miracle.";
@"这需要奇迹。";

IntelliTect.TestTools.Console.ConsoleAssert.Expect(
expected, StormingTheCastle.Main);
Expand Down
8 changes: 4 additions & 4 deletions src/Chapter01.Tests/Listing01.16.Tests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter01.Listing01_16.Tests;

[TestClass]
Expand All @@ -8,9 +8,9 @@ public class HeyYouTests
public void Main_InputInigoMontoya_WriteNothing()
{
const string expected =
@"Hey you!
Enter your first name: <<Inigo
>>Enter your last name: <<Montoya
@"嘿,你!
请输入你的名字: <<Inigo
>>请输入你的姓氏: <<Montoya
>>";

IntelliTect.TestTools.Console.ConsoleAssert.Expect(
Expand Down
4 changes: 2 additions & 2 deletions src/Chapter01.Tests/Listing01.17.Tests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter01.Listing01_17.Tests;

[TestClass]
Expand All @@ -7,7 +7,7 @@ public class ProgramTests
[TestMethod]
public void Main_InputOne_WriteOne()
{
const string expected = @"1<<1>>";
const string expected = "随意输入,按Enter键结束:<<1\r>>1";

IntelliTect.TestTools.Console.ConsoleAssert.Expect(
expected, Program.Main);
Expand Down
10 changes: 5 additions & 5 deletions src/Chapter01.Tests/Listing01.18.Tests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter01.Listing01_18.Tests;

[TestClass]
Expand All @@ -8,10 +8,10 @@ public class ProgramTests
public void Main_InputInigoMontoya_WriteFirstLast()
{
const string expected =
@"Hey you!
Enter your first name: <<Inigo
>>Enter your last name: <<Montoya
>>Your full name is Inigo Montoya.";
@"嘿,你!
请输入你的名字: <<Inigo
>>请输入你的姓氏: <<Montoya
>>你的全名是Inigo Montoya。";

IntelliTect.TestTools.Console.ConsoleAssert.Expect(
expected, HeyYou.Main);
Expand Down
10 changes: 5 additions & 5 deletions src/Chapter01.Tests/Listing01.19.Tests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter01.Listing01_19.Tests;

[TestClass]
Expand All @@ -8,10 +8,10 @@ public class ProgramTests
public void Main_InputInigoMontoya_WriteFirstLast()
{
const string expected =
@"Hey you!
Enter your first name: <<Inigo
>>Enter your last name: <<Montoya
>>Your full name is Inigo Montoya.";
@"嘿,你!
请输入你的名字: <<Inigo
>>请输入你的姓氏: <<Montoya
>>你的全名是Inigo Montoya。";

IntelliTect.TestTools.Console.ConsoleAssert.Expect(
expected, HeyYou.Main);
Expand Down
10 changes: 5 additions & 5 deletions src/Chapter01.Tests/Listing01.20.Tests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter01.Listing01_20.Tests;

[TestClass]
Expand All @@ -8,10 +8,10 @@ public class ProgramTests
public void Main_InputInigoMontoya_WriteLastFirst()
{
const string expected =
@"Hey you!
Enter your first name: <<Inigo
>>Enter your last name: <<Montoya
>>Your full name is Montoya, Inigo.";
@"嘿,你!
请输入你的名字: <<Inigo
>>请输入你的姓氏: <<Montoya
>>你的全名是Montoya, Inigo。";

IntelliTect.TestTools.Console.ConsoleAssert.Expect(
expected, Program.Main);
Expand Down
12 changes: 6 additions & 6 deletions src/Chapter01.Tests/Listing01.21.Tests.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@


namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter01.Listing01_21.Tests;

[TestClass]
public class ProgramTests
{
[TestMethod]
public void Main_InputInigoMontoya_WriteLastFirst()
public void Main_InputInigoMontoya_WriteFirstLast()
{
const string expected =
@"Hey you!
Enter your first name: <<Inigo
>>Enter your last name: <<Montoya
>>Your full name is Montoya, Inigo.";
@"嘿,你!
请输入你的名字: <<Inigo
>>请输入你的姓氏: <<Montoya
>>你的全名是Inigo Montoya。";

IntelliTect.TestTools.Console.ConsoleAssert.Expect(
expected, CommentSamples.Main);
Expand Down
12 changes: 7 additions & 5 deletions src/Chapter01/Listing01.01.HelloWorldInC#.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[CmdletBinding()]
[CmdletBinding()]
param(
[int]$traceLevel
)
Expand All @@ -10,33 +10,35 @@ if('traceLevel' -notin $PSBoundParameters.Keys) {
- 1: Trace script lines as they run.
- 2: Trace script lines, variable assignments, function calls, and scripts.

"@
"@
}

try {
Get-Item .\HelloWorld -ErrorAction Ignore | ForEach-Object { Start-Sleep 10;Remove-Item -Recurse $_ }
$startLocation = Get-Location
$startLocation = Get-Location
Set-PSDebug -Trace $traceLevel


New-Item -ItemType Directory ./HelloWorld
Set-Location ./HelloWorld/
dotnet new console
if ($LASTEXITCODE -ne 0) { throw "dotnet new failed with exit code $LASTEXITCODE" }
Get-Content Program.cs
$codeListing =
$codeListing =
@"
class HelloWorld
{
static void Main()
{
Console.WriteLine(""ÄãºÃ£¬ÎÒ½ÐInigo Montoya¡£"");
Console.WriteLine("你好,我叫Inigo Montoya。");
}
}
"@

$codeListing > Program.cs
Get-Content Program.cs
dotnet run
if ($LASTEXITCODE -ne 0) { throw "dotnet run failed with exit code $LASTEXITCODE" }
}
finally {
Set-PSDebug -Off
Expand Down
4 changes: 2 additions & 2 deletions src/Chapter01/Listing01.03.MultipleStatementsOneOneLine.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter01.Listing01_03;

Expand All @@ -7,7 +7,7 @@ public class HelloWorld
public static void Main()
{
#region INCLUDE
Console.WriteLine("ÉÏ"); Console.WriteLine("ÏÂ");
Console.WriteLine("上"); Console.WriteLine("下");
#endregion INCLUDE
}
}
Loading
Loading