Skip to content

Commit ec67fa2

Browse files
authored
fix generate name value for MethodInfo without Parameters (#1789)
* fix generate name value for MethodInfo without Parameters * add test for XmlDocReader, case: method without input params
1 parent 5618b2d commit ec67fa2

File tree

2 files changed

+46
-13
lines changed

2 files changed

+46
-13
lines changed

src/System.CommandLine.DragonFruit.Tests/XmlDocReaderTests.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ private class Program
1414
public static void Main(bool verbose = false, string flavor = null, int? count = 0)
1515
{
1616
}
17+
18+
public static void MainWithoutParam()
19+
{
20+
}
1721
}
1822

1923
[Fact]
@@ -46,5 +50,30 @@ public void It_finds_member_xml()
4650
helpMetadata.ParameterDescriptions["flavor"].Should().Be("Which flavor to use");
4751
helpMetadata.ParameterDescriptions["count"].Should().Be("How many smoothies?");
4852
}
53+
54+
[Fact]
55+
public void It_finds_member_without_param()
56+
{
57+
const string xml = @"<?xml version=""1.0""?>
58+
<doc>
59+
<assembly>
60+
<name>DragonFruit</name>
61+
</assembly>
62+
<members>
63+
<member name=""M:System.CommandLine.DragonFruit.Tests." + nameof(XmlDocReaderTests) + @".Program.MainWithoutParam"">
64+
<summary>
65+
Hello
66+
</summary>
67+
</member>
68+
</members>
69+
</doc>
70+
";
71+
Action action = Program.MainWithoutParam;
72+
var reader = new StringReader(xml);
73+
XmlDocReader.TryLoad(reader, out var docReader).Should().BeTrue();
74+
75+
docReader.TryGetMethodDescription(action.Method, out var helpMetadata).Should().BeTrue();
76+
helpMetadata.Description.Should().Be("Hello");
77+
}
4978
}
5079
}

src/System.CommandLine.DragonFruit/XmlDocReader.cs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,26 +55,30 @@ public bool TryGetMethodDescription(MethodInfo info, out CommandHelpMetadata com
5555
sb.Append("M:");
5656
AppendTypeName(sb, info.DeclaringType);
5757
sb.Append(".")
58-
.Append(info.Name)
59-
.Append("(");
58+
.Append(info.Name);
6059

61-
bool first = true;
62-
foreach (ParameterInfo param in info.GetParameters())
60+
var parameters = info.GetParameters();
61+
if (parameters.Length > 0)
6362
{
64-
if (first)
63+
sb.Append("(");
64+
bool first = true;
65+
foreach (ParameterInfo param in info.GetParameters())
6566
{
66-
first = false;
67-
}
68-
else
69-
{
70-
sb.Append(",");
67+
if (first)
68+
{
69+
first = false;
70+
}
71+
else
72+
{
73+
sb.Append(",");
74+
}
75+
76+
AppendTypeName(sb, param.ParameterType);
7177
}
7278

73-
AppendTypeName(sb, param.ParameterType);
79+
sb.Append(")");
7480
}
7581

76-
sb.Append(")");
77-
7882
string name = sb.ToString();
7983

8084
XElement member = _members.Elements("member")

0 commit comments

Comments
 (0)