Support multi-line code blocks in examples (#10776)

This commit is contained in:
Greg Smulko
2019-11-15 17:24:16 -08:00
committed by Aditya Patwardhan
parent ff37337f95
commit f32ae64b8b
2 changed files with 69 additions and 32 deletions
@@ -495,47 +495,25 @@ namespace System.Management.Automation
private static void GetExampleSections(string content, out string prompt_str, out string code_str, out string remarks_str)
{
prompt_str = code_str = string.Empty;
StringBuilder builder = new StringBuilder();
string default_prompt_str = "PS > ";
int collectingPart = 1;
foreach (char c in content)
var promptMatch = Regex.Match(content, "^.*?>");
prompt_str = promptMatch.Success ? promptMatch.Value : default_prompt_str;
if (promptMatch.Success)
{
if (c == '>' && collectingPart == 1)
{
builder.Append(c);
prompt_str = builder.ToString().Trim();
builder = new StringBuilder();
++collectingPart;
continue;
}
if (c == '\n' && collectingPart < 3)
{
if (collectingPart == 1)
{
prompt_str = default_prompt_str;
}
code_str = builder.ToString().Trim();
builder = new StringBuilder();
collectingPart = 3;
continue;
}
builder.Append(c);
content = content.Substring(prompt_str.Length);
}
if (collectingPart == 1)
var codeAndRemarksMatch = Regex.Match(content, "^(?<code>.*?)\r?\n\r?\n(?<remarks>.*)$", RegexOptions.Singleline);
if (codeAndRemarksMatch.Success)
{
prompt_str = default_prompt_str;
code_str = builder.ToString().Trim();
remarks_str = string.Empty;
code_str = codeAndRemarksMatch.Groups["code"].Value.Trim();
remarks_str = codeAndRemarksMatch.Groups["remarks"].Value;
}
else
{
remarks_str = builder.ToString();
code_str = content.Trim();
remarks_str = string.Empty;
}
}
@@ -627,4 +627,63 @@ Describe 'get-help other tests' -Tags "CI" {
(Get-Help foo -Examples).examples.example.introduction.Text | Should -BeExactly "PS > "
}
}
Context 'get-help -Examples multi-line code block should be handled' {
function foo {
<#
.EXAMPLE
$a = Get-Service
$a | group Status
.EXAMPLE
PS> $a = Get-Service
PS> $a | group Status
Explanation.
.EXAMPLE
PS> Get-Service
Output
.EXAMPLE
PS> Get-Service
Output
Explanation.
Second line.
.EXAMPLE
PS> Get-Service
Output
Explanation.
Next section.
#>
param()
}
$x = get-help foo
It '$x.examples.example[0].introduction[0].text' { $x.examples.example[0].introduction[0].text | Should -BeExactly "PS > " }
It '$x.examples.example[0].code' { $x.examples.example[0].code | Should -BeExactly "`$a = Get-Service`n`$a | group Status" }
It '$x.examples.example[0].remarks[0].text' { $x.examples.example[0].remarks[0].text | Should -BeNullOrEmpty }
It '$x.examples.example[0].remarks.length' { $x.examples.example[0].remarks.length | Should -Be 5 }
It '$x.examples.example[1].introduction[0].text' { $x.examples.example[1].introduction[0].text | Should -BeExactly "PS>" }
It '$x.examples.example[1].code' { $x.examples.example[1].code | Should -BeExactly "`$a = Get-Service`nPS> `$a | group Status" }
It '$x.examples.example[1].remarks[0].text' { $x.examples.example[1].remarks[0].text | Should -BeExactly "Explanation." }
It '$x.examples.example[1].remarks.length' { $x.examples.example[1].remarks.length | Should -Be 5 }
It '$x.examples.example[2].introduction[0].text' { $x.examples.example[2].introduction[0].text | Should -BeExactly "PS>" }
It '$x.examples.example[2].code' { $x.examples.example[2].code | Should -BeExactly "Get-Service`nOutput" }
It '$x.examples.example[2].remarks[0].text' { $x.examples.example[2].remarks[0].text | Should -BeNullOrEmpty }
It '$x.examples.example[2].remarks.length' { $x.examples.example[2].remarks.length | Should -Be 5 }
It '$x.examples.example[3].introduction[0].text' { $x.examples.example[3].introduction[0].text | Should -BeExactly "PS>" }
It '$x.examples.example[3].code' { $x.examples.example[3].code | Should -BeExactly "Get-Service`nOutput" }
It '$x.examples.example[3].remarks[0].text' { $x.examples.example[3].remarks[0].text | Should -BeExactly "Explanation.`nSecond line." }
It '$x.examples.example[3].remarks.length' { $x.examples.example[3].remarks.length | Should -Be 5 }
It '$x.examples.example[4].introduction[0].text' { $x.examples.example[4].introduction[0].text | Should -BeExactly "PS>" }
It '$x.examples.example[4].code' { $x.examples.example[4].code | Should -BeExactly "Get-Service`nOutput" }
It '$x.examples.example[4].remarks[0].text' { $x.examples.example[4].remarks[0].text | Should -BeExactly "Explanation.`n`nNext section." }
It '$x.examples.example[4].remarks.length' { $x.examples.example[4].remarks.length | Should -Be 5 }
}
}