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.2 kB

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