You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

FunctionCallTemplateEncodingTests.cs 3.3 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. // FunctionCallTemplateEncodingTests.cs
  3. using System.Text.Json; // Needed for JsonSerializer
  4. using AutoGen.SourceGenerator.Template; // Needed for FunctionCallTemplate
  5. using Xunit; // Needed for Fact and Assert
  6. namespace AutoGen.SourceGenerator.Tests
  7. {
  8. public class FunctionCallTemplateEncodingTests
  9. {
  10. private readonly JsonSerializerOptions jsonSerializerOptions = new JsonSerializerOptions
  11. {
  12. WriteIndented = true,
  13. };
  14. [Fact]
  15. public void FunctionDescription_Should_Encode_DoubleQuotes()
  16. {
  17. // Arrange
  18. var functionContracts = new List<SourceGeneratorFunctionContract>
  19. {
  20. new SourceGeneratorFunctionContract
  21. {
  22. Name = "TestFunction",
  23. Description = "This is a \"test\" function",
  24. Parameters = new SourceGeneratorParameterContract[]
  25. {
  26. new SourceGeneratorParameterContract
  27. {
  28. Name = "param1",
  29. Description = "This is a \"parameter\" description",
  30. Type = "string",
  31. IsOptional = false
  32. }
  33. },
  34. ReturnType = "void"
  35. }
  36. };
  37. var template = new FunctionCallTemplate
  38. {
  39. NameSpace = "TestNamespace",
  40. ClassName = "TestClass",
  41. FunctionContracts = functionContracts
  42. };
  43. // Act
  44. var result = template.TransformText();
  45. // Assert
  46. Assert.Contains("Description = @\"This is a \"\"test\"\" function\"", result);
  47. Assert.Contains("Description = @\"This is a \"\"parameter\"\" description\"", result);
  48. }
  49. [Fact]
  50. public void ParameterDescription_Should_Encode_DoubleQuotes()
  51. {
  52. // Arrange
  53. var functionContracts = new List<SourceGeneratorFunctionContract>
  54. {
  55. new SourceGeneratorFunctionContract
  56. {
  57. Name = "TestFunction",
  58. Description = "This is a test function",
  59. Parameters = new SourceGeneratorParameterContract[]
  60. {
  61. new SourceGeneratorParameterContract
  62. {
  63. Name = "param1",
  64. Description = "This is a \"parameter\" description",
  65. Type = "string",
  66. IsOptional = false
  67. }
  68. },
  69. ReturnType = "void"
  70. }
  71. };
  72. var template = new FunctionCallTemplate
  73. {
  74. NameSpace = "TestNamespace",
  75. ClassName = "TestClass",
  76. FunctionContracts = functionContracts
  77. };
  78. // Act
  79. var result = template.TransformText();
  80. // Assert
  81. Assert.Contains("Description = @\"This is a \"\"parameter\"\" description\"", result);
  82. }
  83. }
  84. }