Skip to content

Commit 803c71a

Browse files
committed
Fix examples where feasible
Towards dotnet#646
1 parent 4bba1b8 commit 803c71a

File tree

10 files changed

+46
-27
lines changed

10 files changed

+46
-27
lines changed

standard/classes.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5114,8 +5114,7 @@ Finalizers are invoked automatically, and cannot be invoked explicitly. An insta
51145114

51155115
> *Example*: The output of the example
51165116
>
5117-
> <!-- FIX: This works when I run it at the command-line, but when run under the tester tools, no output is produced. Perhaps the program terminates *before* the thread doing the cleanup runs! That is, the Wait doesn't really wait. -->
5118-
> <!-- Incomplete$Example: {template:"standalone-console", name:"Finalizers1", inferOutput: true} -->
5117+
> <!-- Example: {template:"standalone-console", name:"Finalizers1", inferOutput: true} -->
51195118
> ```csharp
51205119
> class A
51215120
> {

standard/lexical-structure.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ If the following token is among this list, or an identifier in such a context, t
8787
>
8888
> will, according to this rule, be interpreted as a call to `F` with one argument, which is a call to a generic method `G` with two type arguments and one regular argument. The statements
8989
>
90-
> <!-- IncompleteExample: {template:"standalone-lib", name:"GrammarAmbiguities3"} -->
90+
> <!-- Incomplete$Example: {template:"standalone-lib", name:"GrammarAmbiguities3"} -->
9191
> ```csharp
9292
> F(G<A, B>7);
9393
> F(G<A, B>>7);
@@ -1311,8 +1311,7 @@ Any remaining conditional sections are skipped and no tokens, except those for p
13111311
<!-- markdownlint-enable MD028 -->
13121312
> *Example*: The following example illustrates how conditional compilation directives can nest:
13131313
>
1314-
> <!-- FIX: my thinking was to make CheckConsistency, WriteToLog, CommitHelper extension methods, but I could not get that to work. -->
1315-
> <!-- Incomplete$Example: {template:"standalone-lib-without-using", name:"PreproConditionalCompilation", replaceEllipsis:true} -->
1314+
> <!-- Example: {template:"standalone-lib-without-using", name:"PreproConditionalCompilation", replaceEllipsis:true, customEllipsisReplacements:["void CheckConsistency(){} void CommitHelper(){}"]} -->
13161315
> ```csharp
13171316
> #define Debug // Debugging on
13181317
> #undef Trace // Tracing off
@@ -1335,7 +1334,7 @@ Any remaining conditional sections are skipped and no tokens, except those for p
13351334
> Except for pre-processing directives, skipped source code is not subject to lexical analysis. For example, the following is valid despite the unterminated comment in the `#else` section:
13361335
>
13371336
> <!-- FIX: my thinking was to make CheckConsistency an extension method, but I could not get that to work. -->
1338-
> <!-- Incomplete$Example: {template:"standalone-lib-without-using", name:"PreproInvalidSkippedSource", replaceEllipsis:true} -->
1337+
> <!-- Example: {template:"standalone-lib-without-using", name:"PreproInvalidSkippedSource", replaceEllipsis:true, customEllipsisReplacements:["void CheckConsistency(){}"]} -->
13391338
> ```csharp
13401339
> #define Debug // Debugging on
13411340
> class PurchaseTransaction

standard/statements.md

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -663,16 +663,14 @@ An `else` part is associated with the lexically nearest preceding `if` that is a
663663

664664
> *Example*: Thus, an `if` statement of the form
665665
>
666-
> <!-- Incomplete$Example: {template:"standalone-console", name:"IfStatement1", expectedErrors:["x","x"], expectedWarnings:["x","x"]} -->
667-
> <!-- FIX: needs defs for x, y, F and G. -->
666+
> <!-- Example: {template:"code-in-main-without-using", name:"IfStatement1", additionalFiles:["PartialProgramWithFGxy.cs"]} -->
668667
> ```csharp
669668
> if (x) if (y) F(); else G();
670669
> ```
671670
>
672671
> is equivalent to
673672
>
674-
> <!-- Incomplete$Example: {template:"standalone-console", name:"IfStatement2", expectedErrors:["x","x"], expectedWarnings:["x","x"]} -->
675-
> <!-- FIX: needs defs for x, y, F and G. -->
673+
> <!-- Example: {template:"code-in-main-without-using", name:"IfStatement2", additionalFiles:["PartialProgramWithFGxy.cs"]} -->
676674
> ```csharp
677675
> if (x)
678676
> {
@@ -773,8 +771,7 @@ If the end point of the statement list of a switch section is reachable, a compi
773771
774772
> *Example*: The example
775773
>
776-
> <!-- Incomplete$Example: {template:"standalone-console", name:"SwitchStatement1", expectedErrors:["x","x"], expectedWarnings:["x","x"]} -->
777-
> <!-- FIX: needs defs for i, CaseZero, CaseOne, and CaseOthers. -->
774+
> <!-- Example: {template:"code-in-main-without-using", name:"SwitchStatement1", additionalFiles:["PartialProgramForSwitch.cs"]} -->
778775
> ```csharp
779776
> switch (i)
780777
> {
@@ -792,8 +789,7 @@ If the end point of the statement list of a switch section is reachable, a compi
792789
>
793790
> is valid because no switch section has a reachable end point. Unlike C and C++, execution of a switch section is not permitted to “fall through” to the next switch section, and the example
794791
>
795-
> <!-- Incomplete$Example: {template:"standalone-console", name:"SwitchStatement2", expectedErrors:["CS0163","CS0163","CS8070"]} -->
796-
> <!-- FIX: needs defs for i, CaseZero, CaseZeroOrOne, and CaseAny. -->
792+
> <!-- Example: {template:"code-in-main-without-using", name:"SwitchStatement2", additionalFiles:["PartialProgramForSwitch.cs"], expectedErrors:["CS0163","CS0163","CS8070"]} -->
797793
> ```csharp
798794
> switch (i)
799795
> {
@@ -808,8 +804,7 @@ If the end point of the statement list of a switch section is reachable, a compi
808804
>
809805
> results in a compile-time error. When execution of a switch section is to be followed by execution of another switch section, an explicit `goto case` or `goto default` statement shall be used:
810806
>
811-
> <!-- Incomplete$Example: {template:"standalone-console", name:"SwitchStatement3"} -->
812-
> <!-- FIX: needs defs for i, CaseZero, CaseZeroOrOne, and CaseAny. -->
807+
> <!-- Example: {template:"code-in-main-without-using", name:"SwitchStatement3", additionalFiles:["PartialProgramForSwitch.cs"]} -->
813808
> ```csharp
814809
> switch (i)
815810
> {
@@ -831,8 +826,7 @@ Multiple labels are permitted in a *switch_section*.
831826
832827
> *Example*: The example
833828
>
834-
> <!-- Incomplete$Example: {template:"standalone-console", name:"SwitchStatement4"} -->
835-
> <!-- FIX: needs defs for i, CaseZero, CaseOne, and CaseTwo. -->
829+
> <!-- Example: {template:"standalone-console", name:"SwitchStatement4", additionalFiles:["PartialProgramForSwitch.cs"]} -->
836830
> ```csharp
837831
> switch (i)
838832
> {
@@ -857,8 +851,7 @@ Multiple labels are permitted in a *switch_section*.
857851
<!-- markdownlint-enable MD028 -->
858852
> *Note*: Theno fall throughrule prevents a common class of bugs that occur in C and C++ when `break` statements are accidentally omitted. For example, the sections of the `switch` statement above can be reversed without affecting the behavior of the statement:
859853
>
860-
> <!-- Incomplete$Example: {template:"standalone-console", name:"SwitchStatement5"]} -->
861-
> <!-- FIX: needs defs for i, CaseZero, CaseZeroOrOne, and CaseAny. -->
854+
> <!-- Example: {template:"standalone-console", name:"SwitchStatement5", additionalFiles:["PartialProgramForSwitch.cs"]} -->
862855
> ```csharp
863856
> switch (i)
864857
> {
@@ -880,8 +873,7 @@ Multiple labels are permitted in a *switch_section*.
880873
<!-- markdownlint-enable MD028 -->
881874
> *Note*: The statement list of a switch section typically ends in a `break`, `goto case`, or `goto default` statement, but any construct that renders the end point of the statement list unreachable is permitted. For example, a `while` statement controlled by the Boolean expression `true` is known to never reach its end point. Likewise, a `throw` or `return` statement always transfers control elsewhere and never reaches its end point. Thus, the following example is valid:
882875
>
883-
> <!-- Incomplete$Example: {template:"standalone-console", name:"SwitchStatement6"} -->
884-
> <!-- FIX: needs defs for i, F. -->
876+
> <!-- Example: {template:"code-in-main", name:"SwitchStatement6", additionalFiles:["PartialProgramForSwitch.cs"]} -->
885877
> ```csharp
886878
> switch (i)
887879
> {
@@ -903,8 +895,7 @@ Multiple labels are permitted in a *switch_section*.
903895
<!-- markdownlint-enable MD028 -->
904896
> *Example*: The governing type of a `switch` statement can be the type `string`. For example:
905897
>
906-
> <!-- Incomplete$Example: {template:"standalone-console", name:"SwitchStatement7", expectedWarnings:["CS8321"]} -->
907-
> <!-- FIX: top-level must be a console, but then it has no named parent class type, so can't implement Do* as extension methods. -->
898+
> <!-- Example: {template:"code-in-class-lib", name:"SwitchStatement7", additionalFiles:["PartialClass1ForSwitch.cs"]} -->
908899
> ```csharp
909900
> void DoCommand(string command)
910901
> {

tools/ExampleTester/GeneratedExample.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,11 @@ bool ValidateOutput()
152152
{
153153
task.GetAwaiter().GetResult();
154154
}
155+
// For some reason, we don't *actually* get the result of all finalizers
156+
// without this. We shouldn't need it (as relevant examples already have it) but
157+
// code that works outside the test harness doesn't work inside it.
158+
GC.Collect();
159+
GC.WaitForPendingFinalizers();
155160
}
156161
catch (TargetInvocationException outer)
157162
{
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
partial class Class1
2+
{
3+
void DoRun() {}
4+
void DoSave() {}
5+
void DoQuit() {}
6+
void InvalidCommand(string command) {}
7+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
partial class Program
2+
{
3+
static int i = -1;
4+
static void CaseZero() {}
5+
static void CaseOne() {}
6+
static void CaseTwo() {}
7+
static void CaseZeroOrOne() {}
8+
static void CaseAny() {}
9+
static void CaseOthers() {}
10+
static void F() {}
11+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
partial class Program
2+
{
3+
static bool x = true;
4+
static bool y = true;
5+
static void F(){}
6+
static void G(){}
7+
}

tools/example-templates/code-in-class-lib/Library.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
using System.Text;
1212
using System.Threading;
1313

14-
class Class1
14+
partial class Class1
1515
{
1616
$example-code
1717
}

tools/example-templates/code-in-main-without-using/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
class Program
1+
partial class Program
22
{
33
static void Main()
44
{

tools/example-templates/code-in-main/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
using System.Text;
1212
using System.Threading;
1313

14-
class Program
14+
partial class Program
1515
{
1616
static void Main()
1717
{

0 commit comments

Comments
 (0)