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