From 6a368b01f3aee0cf660742c9c477611a329acef6 Mon Sep 17 00:00:00 2001 From: Mark Vanderwiel Date: Tue, 21 Jan 2025 15:14:32 -0600 Subject: [PATCH] Fix loop var --- rules/matcher.go | 3 +-- rules/matcher_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/rules/matcher.go b/rules/matcher.go index 9666295..07400f2 100644 --- a/rules/matcher.go +++ b/rules/matcher.go @@ -134,8 +134,7 @@ func formatPath(pattern string, m Attributes) (string, bool) { if strings.HasPrefix(path, ":") { attr := m.GetAttribute(path[1:]) if attr == nil { - s := path - attr = &s + attr = &path allFound = false } result.WriteString(*attr) diff --git a/rules/matcher_test.go b/rules/matcher_test.go index 3998f74..db4040f 100644 --- a/rules/matcher_test.go +++ b/rules/matcher_test.go @@ -72,3 +72,43 @@ func TestNoRegex(t *testing.T) { t.Fail() } } + +func TestFormatPath(t *testing.T) { + testCases := []struct { + name string + pattern string + atttributes Attributes + result string + allFound bool + }{ + { + "Happy path", + "/:region/test", + NewAttributes(map[string]string{"region": "region"}), + "/region/test", + true, + }, + { + "Empty path", + "", + NewAttributes(map[string]string{"region": "region"}), + "", + true, + }, + { + "Missing attribute", + "/:region/test", + NewAttributes(map[string]string{}), + "/:region/test", + false, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + result, allFound := formatPath(tc.pattern, tc.atttributes) + assert.Equal(t, tc.result, result) + assert.Equal(t, tc.allFound, allFound) + }) + } +}