package processor import ( "cook/utils" "regexp" "testing" "github.com/stretchr/testify/assert" ) // Test named capture group fallback when value is not in Lua func TestNamedCaptureGroupFallback(t *testing.T) { pattern := `value = (?P\d+)` input := `value = 42` // Don't set myvalue in Lua, but do something else so we get a match lua := `v1 = v1 * 2 -- Set v1 but not myvalue, test fallback` cmd := utils.ModifyCommand{ Name: "test_fallback", Regex: pattern, Lua: lua, } re := regexp.MustCompile(pattern) matches := re.FindStringSubmatchIndex(input) assert.NotNil(t, matches) replacements, err := ProcessRegex(input, cmd, "test.txt") // Should not error assert.NoError(t, err) // Since only v1 is set, myvalue should keep original // Should have 1 replacement for v1 if replacements != nil { assert.GreaterOrEqual(t, len(replacements), 0) } } // Test named capture groups with nil value in Lua func TestNamedCaptureGroupNilInLua(t *testing.T) { pattern := `value = (?P\d+)` input := `value = 123` // Set num to nil explicitly, and also set v1 to get a modification lua := `v1 = v1 .. "_test"; num = nil -- v1 modified, num set to nil` cmd := utils.ModifyCommand{ Name: "test_nil", Regex: pattern, Lua: lua, } replacements, err := ProcessRegex(input, cmd, "test.txt") // Should not error assert.NoError(t, err) // Should have replacements for v1, num should fallback to original if replacements != nil { assert.GreaterOrEqual(t, len(replacements), 0) } } // Test multiple named capture groups with some undefined func TestMixedNamedCaptureGroups(t *testing.T) { pattern := `(?P\w+) = (?P\d+)` input := `count = 100` lua := `key = key .. "_modified" -- Only modify key, leave value undefined` cmd := utils.ModifyCommand{ Name: "test_mixed", Regex: pattern, Lua: lua, } replacements, err := ProcessRegex(input, cmd, "test.txt") assert.NoError(t, err) assert.NotNil(t, replacements) // Apply replacements result, _ := utils.ExecuteModifications(replacements, input) // key should be modified, value should remain unchanged assert.Contains(t, result, "count_modified") assert.Contains(t, result, "100") }