Skip to content

Commit e996081

Browse files
DocSamples compares a website sample's output with the block its page says it prints (#2493)
A site sample was compiled and run but never had an expectation, so it could only be "unchecked". The block straight after an annotated sample is now its expected output when a paragraph that is only `Prints:` (or `Output:` and the others the wiki accepts) introduces it. Against the site's master: 61 outputs verified where 60 were, the 2.8.0 What's new example among them; with one line of that page's stated output changed, the run fails on `site:whatsnew:68`. Fixes #2492 Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 50e8d58 commit e996081

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

‎Sources/Tests/Harnesses/DocSamples/README.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ must say**, which is how the missing `using System.Numerics;` and
4545

4646
**What a page claims it prints** is the next bare (untagged) fence, and only when the line
4747
before that fence is `Output:` — or `Prints:`, `Should print:`, `Will print:`, `Returns:`.
48-
A bare fence used for anything else is never mistaken for an expectation. A sample that
48+
A bare fence used for anything else is never mistaken for an expectation. On a website page
49+
the same rule reads HTML: the `pre code` block straight after an annotated sample, when a
50+
paragraph that is only `Prints:` (or one of the others) introduces it. A sample that
4951
documents its output in trailing comments on its `Console.WriteLine` lines is read the same
5052
way, as long as *every* printing line carries one; a partly annotated sample is reported as
5153
unchecked rather than checked against half of its output.

‎Sources/Tests/Harnesses/DocSamples/Snippets.cs‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,19 @@ public static List<Snippet> FromFile(string path)
175175
@"<!--\s*amcheck:(?<lang>cs|fs)(?<rest>[^>]*?)-->\s*<pre><code>(?<body>.*?)</code></pre>",
176176
RegexOptions.Compiled | RegexOptions.Singleline);
177177
static readonly Regex AnyHtmlBlock = new(@"<pre><code>", RegexOptions.Compiled);
178+
/// What a site page says a sample prints: the `pre code` block straight after it, introduced
179+
/// by a paragraph that is only `Prints:` (or `Output:`, as on the wiki). The block may carry a
180+
/// class, which the site uses to stop highlighting it as code.
181+
static readonly Regex HtmlOutput = new(
182+
@"\G\s*<p>\s*(?<lead>[^<]*?)\s*</p>\s*<pre><code(?:\s[^>]*)?>(?<body>.*?)</code></pre>",
183+
RegexOptions.Compiled | RegexOptions.Singleline);
178184

179185
/// <summary>
180186
/// The website's pages are HTML, and a `pre code` block there could be C#, F#, shell or
181187
/// CMake with nothing to say which. So a block is checked only where the page says to,
182188
/// with a preceding `&lt;!-- amcheck:cs --&gt;` or `&lt;!-- amcheck:fs --&gt;`, and the count of
183-
/// unannotated blocks is reported rather than passed over.
189+
/// unannotated blocks is reported rather than passed over. What the sample prints is the block
190+
/// after it, when a paragraph saying `Prints:` introduces that block.
184191
/// </summary>
185192
public static List<Snippet> FromHtmlDirectory(string dir, out int unannotated)
186193
{
@@ -206,13 +213,21 @@ public static List<Snippet> FromHtmlDirectory(string dir, out int unannotated)
206213
Mode = m.Groups["rest"].Value.Contains("compile") ? Mode.CompileOnly : Mode.Run,
207214
Reason = m.Groups["rest"].Value.Trim(),
208215
Body = Decode(m.Groups["body"].Value).Split('\n').Select(l => l.TrimEnd()).ToList(),
216+
Expected = FollowingHtmlOutput(text, m.Index + m.Length),
209217
});
210218
}
211219
}
212220
unannotated = total - annotated;
213221
return all;
214222
}
215223

224+
static string FollowingHtmlOutput(string text, int afterSample)
225+
{
226+
var m = HtmlOutput.Match(text, afterSample);
227+
if (!m.Success || !OutputLead.IsMatch(m.Groups["lead"].Value)) return null;
228+
return string.Join("\n", Decode(m.Groups["body"].Value).Split('\n').Select(l => l.TrimEnd())).Trim('\n');
229+
}
230+
216231
/// The page as a reader reaches it: `quickstart/index.html` is `quickstart`.
217232
static string PageName(string root, string file)
218233
{

0 commit comments

Comments
 (0)