Skip to content

Commit c7898da

Browse files
Copilotstephentoub
andcommitted
Add tests for ASP.NET Core OpenAPI generator improvements: string escaping, invalid XML handling, generic types, multiline comments
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
1 parent 5ade3c3 commit c7898da

1 file changed

Lines changed: 162 additions & 0 deletions

File tree

tests/ModelContextProtocol.SourceGenerators.Tests/XmlToDescriptionGeneratorTests.cs

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,168 @@ public static string TestMethod(string input)
262262
Assert.Empty(result.GeneratedSources);
263263
}
264264

265+
[Fact]
266+
public void Generator_WithSpecialCharacters_EscapesCorrectly()
267+
{
268+
var source = """
269+
using ModelContextProtocol.Server;
270+
using System.ComponentModel;
271+
272+
namespace Test;
273+
274+
[McpServerToolType]
275+
public partial class TestTools
276+
{
277+
/// <summary>
278+
/// Test with "quotes", \backslash, newline
279+
/// and tab characters.
280+
/// </summary>
281+
/// <param name="input">Parameter with "quotes"</param>
282+
[McpServerTool]
283+
public static partial string TestEscaping(string input)
284+
{
285+
return input;
286+
}
287+
}
288+
""";
289+
290+
var result = RunGenerator(source);
291+
292+
Assert.True(result.Success);
293+
Assert.Single(result.GeneratedSources);
294+
295+
var generatedSource = result.GeneratedSources[0].SourceText.ToString();
296+
// Verify quotes are escaped
297+
Assert.Contains("\\\"quotes\\\"", generatedSource);
298+
// Verify backslashes are escaped
299+
Assert.Contains("\\\\backslash", generatedSource);
300+
}
301+
302+
[Fact]
303+
public void Generator_WithInvalidXml_DoesNotThrow()
304+
{
305+
var source = """
306+
using ModelContextProtocol.Server;
307+
using System.ComponentModel;
308+
309+
namespace Test;
310+
311+
[McpServerToolType]
312+
public partial class TestTools
313+
{
314+
/// <summary>
315+
/// Test with <unclosed tag
316+
/// </summary>
317+
[McpServerTool]
318+
public static partial string TestInvalidXml(string input)
319+
{
320+
return input;
321+
}
322+
}
323+
""";
324+
325+
var result = RunGenerator(source);
326+
327+
// Should not throw, just skip generation
328+
Assert.True(result.Success);
329+
Assert.Empty(result.GeneratedSources);
330+
}
331+
332+
[Fact]
333+
public void Generator_WithGenericType_GeneratesCorrectFileName()
334+
{
335+
var source = """
336+
using ModelContextProtocol.Server;
337+
using System.ComponentModel;
338+
339+
namespace Test;
340+
341+
[McpServerToolType]
342+
public partial class TestTools<T>
343+
{
344+
/// <summary>
345+
/// Test generic
346+
/// </summary>
347+
[McpServerTool]
348+
public static partial string TestGeneric(string input)
349+
{
350+
return input;
351+
}
352+
}
353+
""";
354+
355+
var result = RunGenerator(source);
356+
357+
Assert.True(result.Success);
358+
Assert.Single(result.GeneratedSources);
359+
360+
var fileName = result.GeneratedSources[0].FilePath;
361+
// Should include arity for generic types
362+
Assert.Contains("`1", fileName);
363+
}
364+
365+
[Fact]
366+
public void Generator_WithEmptyXmlComments_DoesNotGenerate()
367+
{
368+
var source = """
369+
using ModelContextProtocol.Server;
370+
using System.ComponentModel;
371+
372+
namespace Test;
373+
374+
[McpServerToolType]
375+
public partial class TestTools
376+
{
377+
/// <summary>
378+
/// </summary>
379+
[McpServerTool]
380+
public static partial string TestEmpty(string input)
381+
{
382+
return input;
383+
}
384+
}
385+
""";
386+
387+
var result = RunGenerator(source);
388+
389+
Assert.True(result.Success);
390+
Assert.Empty(result.GeneratedSources);
391+
}
392+
393+
[Fact]
394+
public void Generator_WithMultilineComments_CombinesIntoSingleLine()
395+
{
396+
var source = """
397+
using ModelContextProtocol.Server;
398+
using System.ComponentModel;
399+
400+
namespace Test;
401+
402+
[McpServerToolType]
403+
public partial class TestTools
404+
{
405+
/// <summary>
406+
/// First line
407+
/// Second line
408+
/// Third line
409+
/// </summary>
410+
[McpServerTool]
411+
public static partial string TestMultiline(string input)
412+
{
413+
return input;
414+
}
415+
}
416+
""";
417+
418+
var result = RunGenerator(source);
419+
420+
Assert.True(result.Success);
421+
Assert.Single(result.GeneratedSources);
422+
423+
var generatedSource = result.GeneratedSources[0].SourceText.ToString();
424+
Assert.Contains("[Description(\"First line Second line Third line\")]", generatedSource);
425+
}
426+
265427
private GeneratorRunResult RunGenerator(string source)
266428
{
267429
var syntaxTree = CSharpSyntaxTree.ParseText(source);

0 commit comments

Comments
 (0)