From bfd08e754ef38cbfdba3ebb23d227afce9611e4b Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Fri, 28 Mar 2025 00:38:05 +0100 Subject: [PATCH] Replace old tests with asserts --- processor/regex_test.go | 1028 +++++++++++++-------------------------- 1 file changed, 345 insertions(+), 683 deletions(-) diff --git a/processor/regex_test.go b/processor/regex_test.go index 8af9f35..3924f22 100644 --- a/processor/regex_test.go +++ b/processor/regex_test.go @@ -63,9 +63,7 @@ func TestBuildLuaScript(t *testing.T) { for _, c := range cases { result := PrependLuaAssignment(c.input) - if result != c.expected { - t.Errorf("BuildLuaScript(%q): expected %q, got %q", c.input, c.expected, result) - } + assert.Equal(t, c.expected, result, "BuildLuaScript(%q): expected %q, got %q", c.input, c.expected, result) } } @@ -84,21 +82,10 @@ func TestSimpleValueMultiplication(t *testing.T) { result, mods, matches, err := ApiAdaptor(content, `(?s)(\d+)`, "v1 = v1*1.5") - if err != nil { - t.Fatalf("Error processing content: %v", err) - } - - if matches != 1 { - t.Errorf("Expected 1 match, got %d", matches) - } - - if mods != 1 { - t.Errorf("Expected 1 modification, got %d", mods) - } - - if result != expected { - t.Errorf("Expected content to be:\n%s\n\nGot:\n%s", expected, result) - } + assert.NoError(t, err, "Error processing content: %v", err) + assert.Equal(t, 1, matches, "Expected 1 match, got %d", matches) + assert.Equal(t, 1, mods, "Expected 1 modification, got %d", mods) + assert.Equal(t, expected, result, "Expected content to be different") } func TestShorthandNotation(t *testing.T) { @@ -116,21 +103,10 @@ func TestShorthandNotation(t *testing.T) { result, mods, matches, err := ApiAdaptor(content, `(?s)(\d+)`, "v1*1.5") - if err != nil { - t.Fatalf("Error processing content: %v", err) - } - - if matches != 1 { - t.Errorf("Expected 1 match, got %d", matches) - } - - if mods != 1 { - t.Errorf("Expected 1 modification, got %d", mods) - } - - if result != expected { - t.Errorf("Expected content to be:\n%s\n\nGot:\n%s", expected, result) - } + assert.NoError(t, err, "Error processing content: %v", err) + assert.Equal(t, 1, matches, "Expected 1 match, got %d", matches) + assert.Equal(t, 1, mods, "Expected 1 modification, got %d", mods) + assert.Equal(t, expected, result, "Expected content to be different") } func TestShorthandNotationFloats(t *testing.T) { @@ -148,21 +124,10 @@ func TestShorthandNotationFloats(t *testing.T) { result, mods, matches, err := ApiAdaptor(content, `(?s)(\d+\.\d+)`, "v1*1.5") - if err != nil { - t.Fatalf("Error processing content: %v", err) - } - - if matches != 1 { - t.Errorf("Expected 1 match, got %d", matches) - } - - if mods != 1 { - t.Errorf("Expected 1 modification, got %d", mods) - } - - if result != expected { - t.Errorf("Expected content to be:\n%s\n\nGot:\n%s", expected, result) - } + assert.NoError(t, err, "Error processing content: %v", err) + assert.Equal(t, 1, matches, "Expected 1 match, got %d", matches) + assert.Equal(t, 1, mods, "Expected 1 modification, got %d", mods) + assert.Equal(t, expected, result, "Expected content to be different") } func TestArrayNotation(t *testing.T) { @@ -184,21 +149,10 @@ func TestArrayNotation(t *testing.T) { result, mods, matches, err := ApiAdaptor(content, `(?s)(\d+)`, "v1*2") - if err != nil { - t.Fatalf("Error processing content: %v", err) - } - - if matches != 3 { - t.Errorf("Expected 3 matches, got %d", matches) - } - - if mods != 3 { - t.Errorf("Expected 3 modifications, got %d", mods) - } - - if result != expected { - t.Errorf("Expected content to be:\n%s\n\nGot:\n%s", expected, result) - } + assert.NoError(t, err, "Error processing content: %v", err) + assert.Equal(t, 3, matches, "Expected 3 matches, got %d", matches) + assert.Equal(t, 3, mods, "Expected 3 modifications, got %d", mods) + assert.Equal(t, expected, result, "Expected content to be different") } func TestMultipleNumericMatches(t *testing.T) { @@ -216,21 +170,10 @@ func TestMultipleNumericMatches(t *testing.T) { result, mods, matches, err := ApiAdaptor(content, `(\d+)`, "v1*2") - if err != nil { - t.Fatalf("Error processing content: %v", err) - } - - if matches != 3 { - t.Errorf("Expected 3 matches, got %d", matches) - } - - if mods != 3 { - t.Errorf("Expected 3 modifications, got %d", mods) - } - - if result != expected { - t.Errorf("Expected content to be:\n%s\n\nGot:\n%s", expected, result) - } + assert.NoError(t, err, "Error processing content: %v", err) + assert.Equal(t, 3, matches, "Expected 3 matches, got %d", matches) + assert.Equal(t, 3, mods, "Expected 3 modifications, got %d", mods) + assert.Equal(t, expected, result, "Expected content to be different") } func TestMultipleStringMatches(t *testing.T) { @@ -246,21 +189,10 @@ func TestMultipleStringMatches(t *testing.T) { result, mods, matches, err := ApiAdaptor(content, `([A-Za-z]+)`, `s1 = s1 .. "_modified"`) - if err != nil { - t.Fatalf("Error processing content: %v", err) - } - - if matches != 2 { - t.Errorf("Expected 2 matches, got %d", matches) - } - - if mods != 2 { - t.Errorf("Expected 2 modifications, got %d", mods) - } - - if result != expected { - t.Errorf("Expected content to be:\n%s\n\nGot:\n%s", expected, result) - } + assert.NoError(t, err, "Error processing content: %v", err) + assert.Equal(t, 2, matches, "Expected 2 matches, got %d", matches) + assert.Equal(t, 2, mods, "Expected 2 modifications, got %d", mods) + assert.Equal(t, expected, result, "Expected content to be different") } func TestStringUpperCase(t *testing.T) { @@ -276,21 +208,10 @@ func TestStringUpperCase(t *testing.T) { result, mods, matches, err := ApiAdaptor(content, `([A-Za-z]+)`, `s1 = string.upper(s1)`) - if err != nil { - t.Fatalf("Error processing content: %v", err) - } - - if matches != 2 { - t.Errorf("Expected 2 matches, got %d", matches) - } - - if mods != 2 { - t.Errorf("Expected 2 modifications, got %d", mods) - } - - if result != expected { - t.Errorf("Expected content to be:\n%s\n\nGot:\n%s", expected, result) - } + assert.NoError(t, err, "Error processing content: %v", err) + assert.Equal(t, 2, matches, "Expected 2 matches, got %d", matches) + assert.Equal(t, 2, mods, "Expected 2 modifications, got %d", mods) + assert.Equal(t, expected, result, "Expected content to be different") } func TestStringConcatenation(t *testing.T) { @@ -306,21 +227,10 @@ func TestStringConcatenation(t *testing.T) { result, mods, matches, err := ApiAdaptor(content, `([A-Za-z]+)`, `s1 = s1 .. "_fruit"`) - if err != nil { - t.Fatalf("Error processing content: %v", err) - } - - if matches != 2 { - t.Errorf("Expected 2 matches, got %d", matches) - } - - if mods != 2 { - t.Errorf("Expected 2 modifications, got %d", mods) - } - - if result != expected { - t.Errorf("Expected content to be:\n%s\n\nGot:\n%s", expected, result) - } + assert.NoError(t, err, "Error processing content: %v", err) + assert.Equal(t, 2, matches, "Expected 2 matches, got %d", matches) + assert.Equal(t, 2, mods, "Expected 2 modifications, got %d", mods) + assert.Equal(t, expected, result, "Expected content to be different") } // Added from main_test.go @@ -346,15 +256,11 @@ func TestDecimalValues(t *testing.T) { luaExpr := BuildLuaScript("v1 = v1 * v2") result, _, _, err := ApiAdaptor(content, regex.String(), luaExpr) - if err != nil { - t.Fatalf("Error processing content: %v", err) - } + assert.NoError(t, err, "Error processing content: %v", err) normalizedModified := normalizeWhitespace(result) normalizedExpected := normalizeWhitespace(expected) - if normalizedModified != normalizedExpected { - t.Fatalf("Expected modified content to be %q, but got %q", normalizedExpected, normalizedModified) - } + assert.Equal(t, normalizedExpected, normalizedModified, "Expected modified content to be %q, but got %q", normalizedExpected, normalizedModified) } // Added from main_test.go @@ -378,15 +284,11 @@ func TestLuaMathFunctions(t *testing.T) { luaExpr := BuildLuaScript("v1 = math.sqrt(v1)") modifiedContent, _, _, err := ApiAdaptor(content, regex.String(), luaExpr) - if err != nil { - t.Fatalf("Error processing content: %v", err) - } + assert.NoError(t, err, "Error processing content: %v", err) normalizedModified := normalizeWhitespace(modifiedContent) normalizedExpected := normalizeWhitespace(expected) - if normalizedModified != normalizedExpected { - t.Fatalf("Expected modified content to be %q, but got %q", normalizedExpected, normalizedModified) - } + assert.Equal(t, normalizedExpected, normalizedModified, "Expected modified content to be %q, but got %q", normalizedExpected, normalizedModified) } // Added from main_test.go @@ -410,15 +312,11 @@ func TestDirectAssignment(t *testing.T) { luaExpr := BuildLuaScript("=0") modifiedContent, _, _, err := ApiAdaptor(content, regex.String(), luaExpr) - if err != nil { - t.Fatalf("Error processing content: %v", err) - } + assert.NoError(t, err, "Error processing content: %v", err) normalizedModified := normalizeWhitespace(modifiedContent) normalizedExpected := normalizeWhitespace(expected) - if normalizedModified != normalizedExpected { - t.Fatalf("Expected modified content to be %q, but got %q", normalizedExpected, normalizedModified) - } + assert.Equal(t, normalizedExpected, normalizedModified, "Expected modified content to be %q, but got %q", normalizedExpected, normalizedModified) } // Added from main_test.go @@ -473,18 +371,11 @@ func TestStringAndNumericOperations(t *testing.T) { // Process with our function result, modCount, _, err := ApiAdaptor(tt.input, pattern, luaExpr) - if err != nil { - t.Fatalf("Process function failed: %v", err) - } + assert.NoError(t, err, "Process function failed: %v", err) // Check results - if result != tt.expectedOutput { - t.Errorf("Expected output: %s, got: %s", tt.expectedOutput, result) - } - - if modCount != tt.expectedMods { - t.Errorf("Expected %d modifications, got %d", tt.expectedMods, modCount) - } + assert.Equal(t, tt.expectedOutput, result, "Expected output: %s, got: %s", tt.expectedOutput, result) + assert.Equal(t, tt.expectedMods, modCount, "Expected %d modifications, got %d", tt.expectedMods, modCount) }) } } @@ -541,18 +432,11 @@ func TestEdgeCases(t *testing.T) { // Process with our function result, modCount, _, err := ApiAdaptor(tt.input, pattern, luaExpr) - if err != nil { - t.Fatalf("Process function failed: %v", err) - } + assert.NoError(t, err, "Process function failed: %v", err) // Check results - if result != tt.expectedOutput { - t.Errorf("Expected output: %s, got: %s", tt.expectedOutput, result) - } - - if modCount != tt.expectedMods { - t.Errorf("Expected %d modifications, got %d", tt.expectedMods, modCount) - } + assert.Equal(t, tt.expectedOutput, result, "Expected output: %s, got: %s", tt.expectedOutput, result) + assert.Equal(t, tt.expectedMods, modCount, "Expected %d modifications, got %d", tt.expectedMods, modCount) }) } } @@ -572,21 +456,10 @@ func TestNamedCaptureGroups(t *testing.T) { result, mods, matches, err := ApiAdaptor(content, `(?s)(?\d+)`, "amount = amount * 2") - if err != nil { - t.Fatalf("Error processing content: %v", err) - } - - if matches != 1 { - t.Errorf("Expected 1 match, got %d", matches) - } - - if mods != 1 { - t.Errorf("Expected 1 modification, got %d", mods) - } - - if result != expected { - t.Errorf("Expected content to be:\n%s\n\nGot:\n%s", expected, result) - } + assert.NoError(t, err, "Error processing content: %v", err) + assert.Equal(t, 1, matches, "Expected 1 match, got %d", matches) + assert.Equal(t, 1, mods, "Expected 1 modification, got %d", mods) + assert.Equal(t, expected, result, "Expected content to be different") } func TestNamedCaptureGroupsNum(t *testing.T) { @@ -604,21 +477,10 @@ func TestNamedCaptureGroupsNum(t *testing.T) { result, mods, matches, err := ApiAdaptor(content, `(?s)(?!num)`, "amount = amount * 2") - if err != nil { - t.Fatalf("Error processing content: %v", err) - } - - if matches != 1 { - t.Errorf("Expected 1 match, got %d", matches) - } - - if mods != 1 { - t.Errorf("Expected 1 modification, got %d", mods) - } - - if result != expected { - t.Errorf("Expected content to be:\n%s\n\nGot:\n%s", expected, result) - } + assert.NoError(t, err, "Error processing content: %v", err) + assert.Equal(t, 1, matches, "Expected 1 match, got %d", matches) + assert.Equal(t, 1, mods, "Expected 1 modification, got %d", mods) + assert.Equal(t, expected, result, "Expected content to be different") } func TestMultipleNamedCaptureGroups(t *testing.T) { @@ -640,21 +502,10 @@ func TestMultipleNamedCaptureGroups(t *testing.T) { prodPrice = round(prodPrice + 8, 2) prodQty = prodQty + 5`) - if err != nil { - t.Fatalf("Error processing content: %v", err) - } - - if matches != 3 { - t.Errorf("Expected 3 matches, got %d", matches) - } - - if mods != 3 { - t.Errorf("Expected 3 modifications, got %d", mods) - } - - if result != expected { - t.Errorf("Expected content to be:\n%s\n\nGot:\n%s", expected, result) - } + assert.NoError(t, err, "Error processing content: %v", err) + assert.Equal(t, 3, matches, "Expected 3 matches, got %d", matches) + assert.Equal(t, 3, mods, "Expected 3 modifications, got %d", mods) + assert.Equal(t, expected, result, "Expected content to be different") } func TestMixedIndexedAndNamedCaptures(t *testing.T) { @@ -673,21 +524,10 @@ func TestMixedIndexedAndNamedCaptures(t *testing.T) { `v1 = v1 * 2 dataField = string.upper(dataField)`) - if err != nil { - t.Fatalf("Error processing content: %v", err) - } - - if matches != 2 { - t.Errorf("Expected 2 matches, got %d", matches) - } - - if mods != 2 { - t.Errorf("Expected 2 modifications, got %d", mods) - } - - if result != expected { - t.Errorf("Expected content to be:\n%s\n\nGot:\n%s", expected, result) - } + assert.NoError(t, err, "Error processing content: %v", err) + assert.Equal(t, 2, matches, "Expected 2 matches, got %d", matches) + assert.Equal(t, 2, mods, "Expected 2 modifications, got %d", mods) + assert.Equal(t, expected, result, "Expected content to be different") } func TestComplexNestedNamedCaptures(t *testing.T) { @@ -715,21 +555,10 @@ func TestComplexNestedNamedCaptures(t *testing.T) { `(?s)
.*?(?[^<]+).*?(?\d+)`, `fullName = string.upper(fullName) .. " (" .. age .. ")"`) - if err != nil { - t.Fatalf("Error processing content: %v", err) - } - - if matches != 1 { - t.Errorf("Expected 1 match, got %d", matches) - } - - if mods != 1 { - t.Errorf("Expected 1 modification, got %d", mods) - } - - if result != expected { - t.Errorf("Expected content to be:\n%s\n\nGot:\n%s", expected, result) - } + assert.NoError(t, err, "Error processing content: %v", err) + assert.Equal(t, 1, matches, "Expected 1 match, got %d", matches) + assert.Equal(t, 1, mods, "Expected 1 modification, got %d", mods) + assert.Equal(t, expected, result, "Expected content to be different") } func TestNamedCaptureWithVariableReadback(t *testing.T) { @@ -748,21 +577,10 @@ func TestNamedCaptureWithVariableReadback(t *testing.T) { `hp = hp * 1.5 mp = mp * 1.5`) - if err != nil { - t.Fatalf("Error processing content: %v", err) - } - - if matches != 2 { - t.Errorf("Expected 2 matches, got %d", matches) - } - - if mods != 2 { - t.Errorf("Expected 2 modifications, got %d", mods) - } - - if result != expected { - t.Errorf("Expected content to be:\n%s\n\nGot:\n%s", expected, result) - } + assert.NoError(t, err, "Error processing content: %v", err) + assert.Equal(t, 2, matches, "Expected 2 matches, got %d", matches) + assert.Equal(t, 2, mods, "Expected 2 modifications, got %d", mods) + assert.Equal(t, expected, result, "Expected content to be different") } func TestNamedCaptureWithSpecialCharsInName(t *testing.T) { @@ -774,21 +592,10 @@ func TestNamedCaptureWithSpecialCharsInName(t *testing.T) { `', price, qty, price * qty)`) - if err != nil { - t.Fatalf("Error processing content: %v", err) - } - - if matches != 1 { - t.Errorf("Expected 1 match, got %d", matches) - } - - if mods != 1 { - t.Errorf("Expected 1 modification, got %d", mods) - } + assert.NoError(t, err, "Error processing content: %v", err) + assert.Equal(t, 1, matches, "Expected 1 match, got %d", matches) + assert.Equal(t, 1, mods, "Expected 1 modification, got %d", mods) result = normalizeWhitespace(result) expected = normalizeWhitespace(expected) - - if result != expected { - t.Errorf("Expected content to be:\n%s\n\nGot:\n%s", expected, result) - } + assert.Equal(t, expected, result, "Expected content to be different") } func TestNamedCaptureWithGlobals(t *testing.T) { @@ -970,21 +723,10 @@ func TestNamedCaptureWithGlobals(t *testing.T) { unit = "C" end`) - if err != nil { - t.Fatalf("Error processing content: %v", err) - } - - if matches != 2 { - t.Errorf("Expected 2 matches, got %d", matches) - } - - if mods != 2 { - t.Errorf("Expected 2 modifications, got %d", mods) - } - - if result != expected { - t.Errorf("Expected content to be:\n%s\n\nGot:\n%s", expected, result) - } + assert.NoError(t, err, "Error processing content: %v", err) + assert.Equal(t, 2, matches, "Expected 2 matches, got %d", matches) + assert.Equal(t, 2, mods, "Expected 2 modifications, got %d", mods) + assert.Equal(t, expected, result, "Expected content to be different") } func TestMixedDynamicAndNamedCaptures(t *testing.T) { @@ -1010,24 +752,13 @@ func TestMixedDynamicAndNamedCaptures(t *testing.T) { replacement = string.format('', r, g, b, colorName, hex)`) - if err != nil { - t.Fatalf("Error processing content: %v", err) - } - - if matches != 2 { - t.Errorf("Expected 2 matches, got %d", matches) - } - - if mods != 2 { - t.Errorf("Expected 2 modifications, got %d", mods) - } + assert.NoError(t, err, "Error processing content: %v", err) + assert.Equal(t, 2, matches, "Expected 2 matches, got %d", matches) + assert.Equal(t, 2, mods, "Expected 2 modifications, got %d", mods) result = normalizeWhitespace(result) expected = normalizeWhitespace(expected) - - if result != expected { - t.Errorf("Expected content to be:\n%s\n\nGot:\n%s", expected, result) - } + assert.Equal(t, expected, result, "Expected content to be different") } func TestNamedCapturesWithMultipleReferences(t *testing.T) { @@ -1042,21 +773,10 @@ func TestNamedCapturesWithMultipleReferences(t *testing.T) { replacement = string.format('%s', contentLength, uppercaseContent)`) - if err != nil { - t.Fatalf("Error processing content: %v", err) - } - - if matches != 1 { - t.Errorf("Expected 1 match, got %d", matches) - } - - if mods != 1 { - t.Errorf("Expected 1 modification, got %d", mods) - } - - if result != expected { - t.Errorf("Expected content to be:\n%s\n\nGot:\n%s", expected, result) - } + assert.NoError(t, err, "Error processing content: %v", err) + assert.Equal(t, 1, matches, "Expected 1 match, got %d", matches) + assert.Equal(t, 1, mods, "Expected 1 modification, got %d", mods) + assert.Equal(t, expected, result, "Expected content to be different") } func TestNamedCaptureWithJsonData(t *testing.T) { @@ -1071,21 +791,10 @@ func TestNamedCaptureWithJsonData(t *testing.T) { local upperName = string.upper(name) json = json:gsub('"name":"([^"]+)"', '"name":"' .. upperName .. '"')`) - if err != nil { - t.Fatalf("Error processing content: %v", err) - } - - if matches != 1 { - t.Errorf("Expected 1 match, got %d", matches) - } - - if mods != 1 { - t.Errorf("Expected 1 modification, got %d", mods) - } - - if result != expected { - t.Errorf("Expected content to be:\n%s\n\nGot:\n%s", expected, result) - } + assert.NoError(t, err, "Error processing content: %v", err) + assert.Equal(t, 1, matches, "Expected 1 match, got %d", matches) + assert.Equal(t, 1, mods, "Expected 1 modification, got %d", mods) + assert.Equal(t, expected, result, "Expected content to be different") } func TestNamedCaptureInXML(t *testing.T) { @@ -1115,21 +824,10 @@ func TestNamedCaptureInXML(t *testing.T) { -- Reduce stock by 5 stock = stock - 5`) - if err != nil { - t.Fatalf("Error processing content: %v", err) - } - - if matches != 2 { - t.Errorf("Expected 2 matches, got %d", matches) - } - - if mods != 2 { - t.Errorf("Expected 2 modifications, got %d", mods) - } - - if result != expected { - t.Errorf("Expected content to be:\n%s\n\nGot:\n%s", expected, result) - } + assert.NoError(t, err, "Error processing content: %v", err) + assert.Equal(t, 2, matches, "Expected 2 matches, got %d", matches) + assert.Equal(t, 2, mods, "Expected 2 modifications, got %d", mods) + assert.Equal(t, expected, result, "Expected content to be different") } func TestComprehensiveNamedCaptures(t *testing.T) { @@ -1200,25 +898,14 @@ func TestComprehensiveNamedCaptures(t *testing.T) { sku, status, product_name, currency, price, qty) end`) - if err != nil { - t.Fatalf("Error processing content: %v", err) - } - - if matches != 3 { - t.Errorf("Expected 3 matches, got %d", matches) - } - - if mods != 3 { - t.Errorf("Expected 3 modifications, got %d", mods) - } + assert.NoError(t, err, "Error processing content: %v", err) + assert.Equal(t, 3, matches, "Expected 3 matches, got %d", matches) + assert.Equal(t, 3, mods, "Expected 3 modifications, got %d", mods) // Normalize whitespace for comparison normalizedResult := normalizeWhitespace(result) normalizedExpected := normalizeWhitespace(expected) - - if normalizedResult != normalizedExpected { - t.Errorf("Expected content to be:\n%s\n\nGot:\n%s", expected, result) - } + assert.Equal(t, normalizedExpected, normalizedResult, "Expected content to be different") } func TestVariousNamedCaptureFormats(t *testing.T) { @@ -1263,24 +950,13 @@ func TestVariousNamedCaptureFormats(t *testing.T) { replacement = string.format('', id_num, val) end`) - if err != nil { - t.Fatalf("Error processing content: %v", err) - } - - if matches != 3 { - t.Errorf("Expected 3 matches, got %d", matches) - } - - if mods != 3 { - t.Errorf("Expected 3 modifications, got %d", mods) - } + assert.NoError(t, err, "Error processing content: %v", err) + assert.Equal(t, 3, matches, "Expected 3 matches, got %d", matches) + assert.Equal(t, 3, mods, "Expected 3 modifications, got %d", mods) normalizedResult := normalizeWhitespace(result) normalizedExpected := normalizeWhitespace(expected) - - if normalizedResult != normalizedExpected { - t.Errorf("Expected content to be:\n%s\n\nGot:\n%s", expected, result) - } + assert.Equal(t, normalizedExpected, normalizedResult, "Expected content to be different") } func TestSimpleNamedCapture(t *testing.T) { @@ -1292,21 +968,225 @@ func TestSimpleNamedCapture(t *testing.T) { `name="(?[^"]+)"`, `product_name = string.upper(product_name)`) - if err != nil { - t.Fatalf("Error processing content: %v", err) - } + assert.NoError(t, err, "Error processing content: %v", err) + assert.Equal(t, 1, matches, "Expected 1 match, got %d", matches) + assert.Equal(t, 1, mods, "Expected 1 modification, got %d", mods) + assert.Equal(t, expected, result, "Expected content to be different") +} - if matches != 1 { - t.Errorf("Expected 1 match, got %d", matches) - } +// Pattern without "(?s)" prefix gets modified to include it +func TestPatternWithoutPrefixGetsModified(t *testing.T) { + // Setup + pattern := "some.*pattern" - if mods != 1 { - t.Errorf("Expected 1 modification, got %d", mods) - } + // Redirect stdout to capture fmt.Printf output + oldStdout := os.Stdout + r, w, _ := os.Pipe() + os.Stdout = w - if result != expected { - t.Errorf("Expected content to be:\n%s\n\nGot:\n%s", expected, result) - } + // Execute function + result := resolveRegexPlaceholders(pattern) + + // Restore stdout + w.Close() + os.Stdout = oldStdout + + // Read captured output + var buf bytes.Buffer + io.Copy(&buf, r) + output := buf.String() + + // Verify results + expectedPattern := "(?s)some.*pattern" + assert.Equal(t, expectedPattern, result, "Expected pattern to be %q, got %q", expectedPattern, result) + + expectedOutput := fmt.Sprintf("Pattern modified to include (?s): %s\n", expectedPattern) + assert.Equal(t, expectedOutput, output, "Expected output message %q, got %q", expectedOutput, output) +} + +// Empty string input returns "(?s)" +func TestEmptyStringReturnsWithPrefix(t *testing.T) { + // Setup + pattern := "" + + // Redirect stdout to capture fmt.Printf output + oldStdout := os.Stdout + r, w, _ := os.Pipe() + os.Stdout = w + + // Execute function + result := resolveRegexPlaceholders(pattern) + + // Restore stdout + w.Close() + os.Stdout = oldStdout + + // Read captured output + var buf bytes.Buffer + io.Copy(&buf, r) + output := buf.String() + + // Verify results + expectedPattern := "(?s)" + assert.Equal(t, expectedPattern, result, "Expected pattern to be %q, got %q", expectedPattern, result) + + expectedOutput := fmt.Sprintf("Pattern modified to include (?s): %s\n", expectedPattern) + assert.Equal(t, expectedOutput, output, "Expected output message %q, got %q", expectedOutput, output) +} + +// Named group with "!num" pattern gets replaced with proper regex for numbers +func TestNamedGroupNumPatternReplacement(t *testing.T) { + // Setup + pattern := "(?!num)" + + // Execute function + result := resolveRegexPlaceholders(pattern) + + // Verify results + expectedPattern := "(?s)(?-?\\d*\\.?\\d+)" + assert.Equal(t, expectedPattern, result, "Expected pattern to be %q, got %q", expectedPattern, result) +} + +// "!any" placeholder gets replaced with ".*?" pattern +func TestAnyPlaceholderReplacement(t *testing.T) { + // Setup + pattern := "start!anyend" + + // Execute function + result := resolveRegexPlaceholders(pattern) + + // Verify results + expectedPattern := "(?s)start.*?end" + assert.Equal(t, expectedPattern, result, "Expected pattern to be %q, got %q", expectedPattern, result) +} + +// "!num" placeholder gets replaced with quoted or unquoted number pattern +func TestNumPlaceholderReplacement(t *testing.T) { + // Setup + pattern := "value: !num" + + // Execute function + result := resolveRegexPlaceholders(pattern) + + // Verify results + expectedPattern := `value: "?(-?\d*\.?\d+)"?` + assert.Equal(t, expectedPattern, result, "Expected pattern to be %q, got %q", expectedPattern, result) +} + +// "!rep(pattern, count)" correctly repeats the pattern with separators +func TestRepPatternRepetition(t *testing.T) { + // Setup + pattern := "!rep(a, 3)" + + // Execute function + result := resolveRegexPlaceholders(pattern) + + // Verify results + expectedPattern := "a.*?a.*?a" + assert.Equal(t, expectedPattern, result, "Expected pattern to be %q, got %q", expectedPattern, result) +} + +// Multiple different placeholders in the same pattern are all correctly replaced +func TestMultiplePlaceholdersReplacedCorrectly(t *testing.T) { + // Setup + pattern := "(?s)some(?!num)text!anymore!rep(!num, 3)" + + // Redirect stdout to capture fmt.Printf output + oldStdout := os.Stdout + r, w, _ := os.Pipe() + os.Stdout = w + + // Execute function + result := resolveRegexPlaceholders(pattern) + + // Restore stdout + w.Close() + os.Stdout = oldStdout + + // Read captured output + var buf bytes.Buffer + io.Copy(&buf, r) + output := buf.String() + + // Verify results + expectedPattern := "(?s)some(?-?\\d*\\.?\\d+)text.*?more-?\\d*\\.?\\d+.*?-?\\d*\\.?\\d+.*?-?\\d*\\.?\\d+" + assert.Equal(t, expectedPattern, result, "Expected pattern to be %q, got %q", expectedPattern, result) + + expectedOutput := fmt.Sprintf("Pattern modified to include (?s): %s\n", pattern) + assert.Equal(t, expectedOutput, output, "Expected output message %q, got %q", expectedOutput, output) +} + +// Pattern already containing "(?s)" prefix remains unchanged +func TestPatternWithPrefixRemainsUnchanged(t *testing.T) { + // Setup + pattern := "(?s)some.*pattern" + + // Redirect stdout to capture fmt.Printf output + oldStdout := os.Stdout + r, w, _ := os.Pipe() + os.Stdout = w + + // Execute function + result := resolveRegexPlaceholders(pattern) + + // Restore stdout + w.Close() + os.Stdout = oldStdout + + // Read captured output + var buf bytes.Buffer + io.Copy(&buf, r) + output := buf.String() + + // Verify results + expectedPattern := "(?s)some.*pattern" + assert.Equal(t, expectedPattern, result, "Expected pattern to remain %q, got %q", expectedPattern, result) + + expectedOutput := "" + assert.Equal(t, expectedOutput, output, "Expected no output message, got %q", output) +} + +// Malformed "!rep" pattern without proper parameters returns unchanged match +func TestMalformedRepPatternReturnsUnchanged(t *testing.T) { + // Setup + pattern := "!rep(somepattern)" // Malformed !rep pattern without count + + // Execute function + result := resolveRegexPlaceholders(pattern) + + // Verify results + expectedPattern := "!rep(somepattern)" + assert.Equal(t, expectedPattern, result, "Expected pattern to be %q, got %q", expectedPattern, result) +} + +// Nested placeholder patterns (e.g., "!rep(!num, 3)") +func TestNestedPlaceholderPatterns(t *testing.T) { + // Setup + pattern := "!rep(!num, 3)" + + // Redirect stdout to capture fmt.Printf output + oldStdout := os.Stdout + r, w, _ := os.Pipe() + os.Stdout = w + + // Execute function + result := resolveRegexPlaceholders(pattern) + + // Restore stdout + w.Close() + os.Stdout = oldStdout + + // Read captured output + var buf bytes.Buffer + io.Copy(&buf, r) + output := buf.String() + + // Verify results + expectedPattern := `"?(-?\d*\.?\d+)".*?"?(-?\d*\.?\d+)".*?"?(-?\d*\.?\d+)"` + assert.Equal(t, expectedPattern, result, "Expected pattern to be %q, got %q", expectedPattern, result) + + expectedOutput := fmt.Sprintf("Pattern modified to include (?s): %s\n", expectedPattern) + assert.Equal(t, expectedOutput, output, "Expected output message %q, got %q", expectedOutput, output) } // Returns empty slice when input is empty @@ -1438,232 +1318,14 @@ func TestDeduplicateGroupsWithAdjacentNonOverlappingRanges(t *testing.T) { assert.Equal(t, "Group3", result[2].Name, "Expected Group3 to be the third in the result") } -// Pattern without "(?s)" prefix gets modified to include it -func TestPatternWithoutPrefixGetsModified(t *testing.T) { - // Setup - pattern := "some.*pattern" +// Handles nil groups in the input array +func TestDeduplicateGroupsWithNilInput(t *testing.T) { + // Arrange + captureGroups := []*CaptureGroup{nil, nil} - // Redirect stdout to capture fmt.Printf output - oldStdout := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w + // Act + result := deduplicateGroups(captureGroups) - // Execute function - result := resolveRegexPlaceholders(pattern) - - // Restore stdout - w.Close() - os.Stdout = oldStdout - - // Read captured output - var buf bytes.Buffer - io.Copy(&buf, r) - output := buf.String() - - // Verify results - expectedPattern := "(?s)some.*pattern" - if result != expectedPattern { - t.Errorf("Expected pattern to be %q, got %q", expectedPattern, result) - } - - expectedOutput := fmt.Sprintf("Pattern modified to include (?s): %s\n", expectedPattern) - if output != expectedOutput { - t.Errorf("Expected output message %q, got %q", expectedOutput, output) - } -} - -// Empty string input returns "(?s)" -func TestEmptyStringReturnsWithPrefix(t *testing.T) { - // Setup - pattern := "" - - // Redirect stdout to capture fmt.Printf output - oldStdout := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w - - // Execute function - result := resolveRegexPlaceholders(pattern) - - // Restore stdout - w.Close() - os.Stdout = oldStdout - - // Read captured output - var buf bytes.Buffer - io.Copy(&buf, r) - output := buf.String() - - // Verify results - expectedPattern := "(?s)" - if result != expectedPattern { - t.Errorf("Expected pattern to be %q, got %q", expectedPattern, result) - } - - expectedOutput := fmt.Sprintf("Pattern modified to include (?s): %s\n", expectedPattern) - if output != expectedOutput { - t.Errorf("Expected output message %q, got %q", expectedOutput, output) - } -} - -// Named group with "!num" pattern gets replaced with proper regex for numbers -func TestNamedGroupNumPatternReplacement(t *testing.T) { - // Setup - pattern := "(?!num)" - - // Execute function - result := resolveRegexPlaceholders(pattern) - - // Verify results - expectedPattern := "(?s)(?-?\\d*\\.?\\d+)" - if result != expectedPattern { - t.Errorf("Expected pattern to be %q, got %q", expectedPattern, result) - } -} - -// "!any" placeholder gets replaced with ".*?" pattern -func TestAnyPlaceholderReplacement(t *testing.T) { - // Setup - pattern := "start!anyend" - - // Execute function - result := resolveRegexPlaceholders(pattern) - - // Verify results - expectedPattern := "(?s)start.*?end" - if result != expectedPattern { - t.Errorf("Expected pattern to be %q, got %q", expectedPattern, result) - } -} - -// "!rep(pattern, count)" correctly repeats the pattern with separators -func TestRepPatternRepetition(t *testing.T) { - // Setup - pattern := "!rep(a, 3)" - - // Execute function - result := resolveRegexPlaceholders(pattern) - - // Verify results - expectedPattern := "a.*?a.*?a" - if result != expectedPattern { - t.Errorf("Expected pattern to be %q, got %q", expectedPattern, result) - } -} - -// Multiple different placeholders in the same pattern are all correctly replaced -func TestMultiplePlaceholdersReplacedCorrectly(t *testing.T) { - // Setup - pattern := "(?s)some(?!num)text!anymore!rep(!num, 3)" - - // Redirect stdout to capture fmt.Printf output - oldStdout := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w - - // Execute function - result := resolveRegexPlaceholders(pattern) - - // Restore stdout - w.Close() - os.Stdout = oldStdout - - // Read captured output - var buf bytes.Buffer - io.Copy(&buf, r) - output := buf.String() - - // Verify results - expectedPattern := "(?s)some(?-?\\d*\\.?\\d+)text.*?more-?\\d*\\.?\\d+.*?-?\\d*\\.?\\d+.*?-?\\d*\\.?\\d+" - if result != expectedPattern { - t.Errorf("Expected pattern to be %q, got %q", expectedPattern, result) - } - - expectedOutput := fmt.Sprintf("Pattern modified to include (?s): %s\n", pattern) - if output != expectedOutput { - t.Errorf("Expected output message %q, got %q", expectedOutput, output) - } -} - -// Pattern already containing "(?s)" prefix remains unchanged -func TestPatternWithPrefixRemainsUnchanged(t *testing.T) { - // Setup - pattern := "(?s)some.*pattern" - - // Redirect stdout to capture fmt.Printf output - oldStdout := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w - - // Execute function - result := resolveRegexPlaceholders(pattern) - - // Restore stdout - w.Close() - os.Stdout = oldStdout - - // Read captured output - var buf bytes.Buffer - io.Copy(&buf, r) - output := buf.String() - - // Verify results - expectedPattern := "(?s)some.*pattern" - if result != expectedPattern { - t.Errorf("Expected pattern to remain %q, got %q", expectedPattern, result) - } - - expectedOutput := "" - if output != expectedOutput { - t.Errorf("Expected no output message, got %q", output) - } -} - -// Malformed "!rep" pattern without proper parameters returns unchanged match -func TestMalformedRepPatternReturnsUnchanged(t *testing.T) { - // Setup - pattern := "!rep(somepattern)" // Malformed !rep pattern without count - - // Execute function - result := resolveRegexPlaceholders(pattern) - - // Verify results - expectedPattern := "!rep(somepattern)" - if result != expectedPattern { - t.Errorf("Expected pattern to be %q, got %q", expectedPattern, result) - } -} - -// Nested placeholder patterns (e.g., "!rep(!num, 3)") -func TestNestedPlaceholderPatterns(t *testing.T) { - // Setup - pattern := "!rep(!num, 3)" - - // Redirect stdout to capture fmt.Printf output - oldStdout := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w - - // Execute function - result := resolveRegexPlaceholders(pattern) - - // Restore stdout - w.Close() - os.Stdout = oldStdout - - // Read captured output - var buf bytes.Buffer - io.Copy(&buf, r) - output := buf.String() - - // Verify results - expectedPattern := `"?(-?\d*\.?\d+)".*?"?(-?\d*\.?\d+)".*?"?(-?\d*\.?\d+)"` - if result != expectedPattern { - t.Errorf("Expected pattern to be %q, got %q", expectedPattern, result) - } - - expectedOutput := fmt.Sprintf("Pattern modified to include (?s): %s\n", expectedPattern) - if output != expectedOutput { - t.Errorf("Expected output message %q, got %q", expectedOutput, output) - } + // Assert + assert.Empty(t, result, "Expected empty slice when input contains only nil groups") }