comma_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package parser
  2. import (
  3. "testing"
  4. )
  5. func TestCommaSeparatedValues(t *testing.T) {
  6. type testStruct struct {
  7. A int `comma:"a"`
  8. B string `comma:"b"`
  9. C bool `comma:"c"`
  10. D float64 `comma:"d"`
  11. E string // no tag
  12. Endpoint string `comma:"endpoint"`
  13. Name string `comma:"name"`
  14. ID string `comma:"id"`
  15. }
  16. tests := []struct {
  17. name string
  18. input []byte
  19. want testStruct
  20. wantErr bool
  21. }{
  22. {
  23. name: "basic test",
  24. input: []byte("a=1,b=hello,c=true,d=3.14,E=world"),
  25. want: testStruct{
  26. A: 1,
  27. B: "hello",
  28. C: true,
  29. D: 3.14,
  30. E: "world",
  31. },
  32. wantErr: false,
  33. },
  34. {
  35. name: "empty input",
  36. input: []byte(""),
  37. want: testStruct{},
  38. wantErr: false,
  39. },
  40. {
  41. name: "partial fields",
  42. input: []byte("a=42,c=false"),
  43. want: testStruct{
  44. A: 42,
  45. C: false,
  46. },
  47. wantErr: false,
  48. },
  49. {
  50. name: "extra fields ignored",
  51. input: []byte("a=1,b=test,extra=ignored"),
  52. want: testStruct{
  53. A: 1,
  54. B: "test",
  55. },
  56. wantErr: false,
  57. },
  58. {
  59. name: "invalid format - missing value",
  60. input: []byte("a=1,b"),
  61. wantErr: true,
  62. },
  63. {
  64. name: "invalid format - missing equals",
  65. input: []byte("a=1,b:2"),
  66. wantErr: true,
  67. },
  68. {
  69. name: "whitespace handling",
  70. input: []byte("a = 1, b = hello "),
  71. want: testStruct{
  72. A: 1,
  73. B: "hello",
  74. },
  75. wantErr: false,
  76. },
  77. {
  78. name: "type conversion - invalid int",
  79. input: []byte("a=notanumber,b=test"),
  80. want: testStruct{
  81. B: "test",
  82. },
  83. wantErr: false,
  84. },
  85. {
  86. name: "type conversion - invalid bool",
  87. input: []byte("c=notabool,b=test"),
  88. want: testStruct{
  89. B: "test",
  90. },
  91. wantErr: false,
  92. },
  93. {
  94. name: "type conversion - invalid float",
  95. input: []byte("d=notafloat,b=test"),
  96. want: testStruct{
  97. B: "test",
  98. },
  99. wantErr: false,
  100. },
  101. {
  102. name: "actual example",
  103. input: []byte("endpoint=test-plugin.dify.uwu,name=c31a98df2ef139d6532d8da8caa2bb63,id=c31a98df2ef139d6532d8da8caa2bb63"),
  104. want: testStruct{
  105. Endpoint: "test-plugin.dify.uwu",
  106. Name: "c31a98df2ef139d6532d8da8caa2bb63",
  107. ID: "c31a98df2ef139d6532d8da8caa2bb63",
  108. },
  109. wantErr: false,
  110. },
  111. }
  112. for _, tt := range tests {
  113. t.Run(tt.name, func(t *testing.T) {
  114. got, err := ParserCommaSeparatedValues[testStruct](tt.input)
  115. if (err != nil) != tt.wantErr {
  116. t.Errorf("ParserCommaSeparatedValues() error = %v, wantErr %v", err, tt.wantErr)
  117. return
  118. }
  119. if !tt.wantErr && got != tt.want {
  120. t.Errorf("ParserCommaSeparatedValues() = %v, want %v", got, tt.want)
  121. }
  122. })
  123. }
  124. }