| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- package dialog
- import (
- "testing"
- "regexp"
- )
- func TestNamedArgPattern(t *testing.T) {
- testCases := []struct {
- input string
- expected []string
- }{
- {
- input: "This is a test with $ARGUMENTS placeholder",
- expected: []string{"ARGUMENTS"},
- },
- {
- input: "This is a test with $FOO and $BAR placeholders",
- expected: []string{"FOO", "BAR"},
- },
- {
- input: "This is a test with $FOO_BAR and $BAZ123 placeholders",
- expected: []string{"FOO_BAR", "BAZ123"},
- },
- {
- input: "This is a test with no placeholders",
- expected: []string{},
- },
- {
- input: "This is a test with $FOO appearing twice: $FOO",
- expected: []string{"FOO"},
- },
- {
- input: "This is a test with $1INVALID placeholder",
- expected: []string{},
- },
- }
- for _, tc := range testCases {
- matches := namedArgPattern.FindAllStringSubmatch(tc.input, -1)
-
- // Extract unique argument names
- argNames := make([]string, 0)
- argMap := make(map[string]bool)
-
- for _, match := range matches {
- argName := match[1] // Group 1 is the name without $
- if !argMap[argName] {
- argMap[argName] = true
- argNames = append(argNames, argName)
- }
- }
-
- // Check if we got the expected number of arguments
- if len(argNames) != len(tc.expected) {
- t.Errorf("Expected %d arguments, got %d for input: %s", len(tc.expected), len(argNames), tc.input)
- continue
- }
-
- // Check if we got the expected argument names
- for _, expectedArg := range tc.expected {
- found := false
- for _, actualArg := range argNames {
- if actualArg == expectedArg {
- found = true
- break
- }
- }
- if !found {
- t.Errorf("Expected argument %s not found in %v for input: %s", expectedArg, argNames, tc.input)
- }
- }
- }
- }
- func TestRegexPattern(t *testing.T) {
- pattern := regexp.MustCompile(`\$([A-Z][A-Z0-9_]*)`)
-
- validMatches := []string{
- "$FOO",
- "$BAR",
- "$FOO_BAR",
- "$BAZ123",
- "$ARGUMENTS",
- }
-
- invalidMatches := []string{
- "$foo",
- "$1BAR",
- "$_FOO",
- "FOO",
- "$",
- }
-
- for _, valid := range validMatches {
- if !pattern.MatchString(valid) {
- t.Errorf("Expected %s to match, but it didn't", valid)
- }
- }
-
- for _, invalid := range invalidMatches {
- if pattern.MatchString(invalid) {
- t.Errorf("Expected %s not to match, but it did", invalid)
- }
- }
- }
|