diff --git a/processor/processor.go b/processor/processor.go index 1076cd0..88384b0 100644 --- a/processor/processor.go +++ b/processor/processor.go @@ -487,8 +487,8 @@ func EvalRegex(L *lua.LState) int { evalRegexLogger := processorLogger.WithPrefix("evalRegex") evalRegexLogger.Debug("Lua evalRegex function called") - input := L.ToString(1) - pattern := L.ToString(2) + pattern := L.ToString(1) + input := L.ToString(2) evalRegexLogger.Debug("Pattern: %q, Input: %q", pattern, input) @@ -496,6 +496,7 @@ func EvalRegex(L *lua.LState) int { matches := re.FindStringSubmatch(input) evalRegexLogger.Debug("Go regex matches: %v (count: %d)", matches, len(matches)) + evalRegexLogger.Debug("Matches is nil: %t", matches == nil) if len(matches) > 0 { matchesTable := L.NewTable() diff --git a/processor/processor_test.go b/processor/processor_test.go index d4cbe67..8723b0d 100644 --- a/processor/processor_test.go +++ b/processor/processor_test.go @@ -21,7 +21,7 @@ func TestEvalRegex_CaptureGroupsReturned(t *testing.T) { result := processor.EvalRegex(L) - assert.Equal(t, 0, result, "Expected return value to be 0") + assert.Equal(t, 1, result, "Expected return value to be 1 (one value pushed to Lua stack)") out := L.Get(-1) tbl, ok := out.(*lua.LTable) @@ -35,26 +35,19 @@ func TestEvalRegex_CaptureGroupsReturned(t *testing.T) { } } -// Happy Path: Function returns an empty Lua table when regex pattern does not match input string. -func TestEvalRegex_NoMatchReturnsEmptyTable(t *testing.T) { +// Happy Path: Function returns nil when regex pattern does not match input string. +func TestEvalRegex_NoMatchReturnsNil(t *testing.T) { L := lua.NewState() defer L.Close() L.Push(lua.LString(`(foo)(bar)`)) L.Push(lua.LString("no-match-here")) result := processor.EvalRegex(L) - assert.Equal(t, 0, result) + assert.Equal(t, 1, result, "Expected return value to be 1 (one value pushed to Lua stack)") out := L.Get(-1) - tbl, ok := out.(*lua.LTable) - if !ok { - t.Fatalf("Expected Lua table, got %T", out) - } - count := 0 - tbl.ForEach(func(k, v lua.LValue) { - count++ - }) - assert.Zero(t, count, "Expected no items in the table for non-matching input") + // Should be nil when no matches found + assert.Equal(t, lua.LNil, out, "Expected nil when no matches found") } // Happy Path: Function handles patterns with no capture groups by returning the full match in the Lua table. @@ -67,7 +60,7 @@ func TestEvalRegex_NoCaptureGroups(t *testing.T) { L.Push(lua.LString(input)) result := processor.EvalRegex(L) - assert.Equal(t, 0, result) + assert.Equal(t, 1, result, "Expected return value to be 1 (one value pushed to Lua stack)") out := L.Get(-1) tbl, ok := out.(*lua.LTable) @@ -84,7 +77,7 @@ func TestEvalRegex_NoCaptureGroups(t *testing.T) { assert.Equal(t, 1, count) } -// Edge Case: Function panics or errors when given an invalid regex pattern. +// Edge Case: Function handles invalid regex pattern by letting regexp.MustCompile panic (which is expected behavior) func TestEvalRegex_InvalidPattern(t *testing.T) { L := lua.NewState() defer L.Close() @@ -92,15 +85,13 @@ func TestEvalRegex_InvalidPattern(t *testing.T) { L.Push(lua.LString(pattern)) L.Push(lua.LString("someinput")) - defer func() { - if r := recover(); r == nil { - t.Error("Expected panic for invalid regex pattern, but did not panic") - } - }() - processor.EvalRegex(L) + // This should panic due to invalid regex pattern + assert.Panics(t, func() { + processor.EvalRegex(L) + }, "Expected panic for invalid regex pattern") } -// Edge Case: Function returns an empty Lua table when input string is empty. +// Edge Case: Function returns nil when input string is empty and pattern doesn't match. func TestEvalRegex_EmptyInputString(t *testing.T) { L := lua.NewState() defer L.Close() @@ -108,19 +99,11 @@ func TestEvalRegex_EmptyInputString(t *testing.T) { L.Push(lua.LString("")) result := processor.EvalRegex(L) - assert.Equal(t, 0, result) + assert.Equal(t, 1, result, "Expected return value to be 1 (one value pushed to Lua stack)") out := L.Get(-1) - tbl, ok := out.(*lua.LTable) - if !ok { - t.Fatalf("Expected Lua table, got %T", out) - } - // Should be empty - count := 0 - tbl.ForEach(func(k, v lua.LValue) { - count++ - }) - assert.Zero(t, count, "Expected empty table when input is empty") + // Should be nil when no matches found + assert.Equal(t, lua.LNil, out, "Expected nil when input is empty and pattern doesn't match") } // Edge Case: Function handles nil or missing arguments gracefully without causing a runtime panic. @@ -138,7 +121,7 @@ func TestEvalRegex_MissingArguments(t *testing.T) { } func TestEvalComplexRegex(t *testing.T) { - // 23:47:35.567068 processor.go:369 [g:22 ] [LUA] Pistol_Round ^((Bulk_)?(Pistol|Rifle).*?Round.*?)$ + // Test complex regex pattern with multiple capture groups L := lua.NewState() defer L.Close() pattern := `^((Bulk_)?(Pistol|Rifle).*?Round.*?)$` @@ -153,10 +136,13 @@ func TestEvalComplexRegex(t *testing.T) { if !ok { t.Fatalf("Expected Lua table, got %T", out) } - count := 0 + + // Pattern should match: ["Pistol_Round", "Pistol_Round", "", "Pistol"] + // This creates 4 elements in the matches array, not 1 + expectedCount := 4 + actualCount := 0 tbl.ForEach(func(k, v lua.LValue) { - fmt.Println(k, v) - count++ + actualCount++ }) - assert.Equal(t, 1, count) + assert.Equal(t, expectedCount, actualCount, "Expected %d matches for pattern %q with input %q", expectedCount, pattern, input) }