custom_commands_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package dialog
  2. import (
  3. "testing"
  4. "regexp"
  5. )
  6. func TestNamedArgPattern(t *testing.T) {
  7. testCases := []struct {
  8. input string
  9. expected []string
  10. }{
  11. {
  12. input: "This is a test with $ARGUMENTS placeholder",
  13. expected: []string{"ARGUMENTS"},
  14. },
  15. {
  16. input: "This is a test with $FOO and $BAR placeholders",
  17. expected: []string{"FOO", "BAR"},
  18. },
  19. {
  20. input: "This is a test with $FOO_BAR and $BAZ123 placeholders",
  21. expected: []string{"FOO_BAR", "BAZ123"},
  22. },
  23. {
  24. input: "This is a test with no placeholders",
  25. expected: []string{},
  26. },
  27. {
  28. input: "This is a test with $FOO appearing twice: $FOO",
  29. expected: []string{"FOO"},
  30. },
  31. {
  32. input: "This is a test with $1INVALID placeholder",
  33. expected: []string{},
  34. },
  35. }
  36. for _, tc := range testCases {
  37. matches := namedArgPattern.FindAllStringSubmatch(tc.input, -1)
  38. // Extract unique argument names
  39. argNames := make([]string, 0)
  40. argMap := make(map[string]bool)
  41. for _, match := range matches {
  42. argName := match[1] // Group 1 is the name without $
  43. if !argMap[argName] {
  44. argMap[argName] = true
  45. argNames = append(argNames, argName)
  46. }
  47. }
  48. // Check if we got the expected number of arguments
  49. if len(argNames) != len(tc.expected) {
  50. t.Errorf("Expected %d arguments, got %d for input: %s", len(tc.expected), len(argNames), tc.input)
  51. continue
  52. }
  53. // Check if we got the expected argument names
  54. for _, expectedArg := range tc.expected {
  55. found := false
  56. for _, actualArg := range argNames {
  57. if actualArg == expectedArg {
  58. found = true
  59. break
  60. }
  61. }
  62. if !found {
  63. t.Errorf("Expected argument %s not found in %v for input: %s", expectedArg, argNames, tc.input)
  64. }
  65. }
  66. }
  67. }
  68. func TestRegexPattern(t *testing.T) {
  69. pattern := regexp.MustCompile(`\$([A-Z][A-Z0-9_]*)`)
  70. validMatches := []string{
  71. "$FOO",
  72. "$BAR",
  73. "$FOO_BAR",
  74. "$BAZ123",
  75. "$ARGUMENTS",
  76. }
  77. invalidMatches := []string{
  78. "$foo",
  79. "$1BAR",
  80. "$_FOO",
  81. "FOO",
  82. "$",
  83. }
  84. for _, valid := range validMatches {
  85. if !pattern.MatchString(valid) {
  86. t.Errorf("Expected %s to match, but it didn't", valid)
  87. }
  88. }
  89. for _, invalid := range invalidMatches {
  90. if pattern.MatchString(invalid) {
  91. t.Errorf("Expected %s not to match, but it did", invalid)
  92. }
  93. }
  94. }