From 2ad4e601197a7a65f7ae94cdaf68c04fa50a0301 Mon Sep 17 00:00:00 2001 From: Jaz White Date: Thu, 5 Oct 2023 11:36:46 -0400 Subject: [PATCH] test: example with delimiters disabled (#395) Demonstrate delimiter behavior described in https://github.com/launchdarkly/ld-find-code-refs/issues/393 --- search/matcher_test.go | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/search/matcher_test.go b/search/matcher_test.go index 50260927..ddf46fcb 100644 --- a/search/matcher_test.go +++ b/search/matcher_test.go @@ -29,32 +29,48 @@ func TestElementMatcher_FindMatches(t *testing.T) { } func TestMatcher_MatchElement(t *testing.T) { - const FLAG_KEY = "testflag" - specs := []struct { name string expected bool line string matcher Matcher + flagKey string }{ { name: "match found", expected: true, line: "var flagKey = 'testflag'", matcher: Matcher{Elements: []ElementMatcher{NewElementMatcher("projKey", "", ",'\"", []string{"testflag"}, map[string][]string{"testflag": {"testFlag"}})}}, + flagKey: "testflag", }, { name: "no match found", expected: false, line: "var flagKey = 'testflag'", matcher: Matcher{Elements: []ElementMatcher{NewElementMatcher("projKey", "", ",'\"", []string{"anotherflag"}, map[string][]string{"anotherflag": {"anotherFlag"}})}}, + flagKey: "testflag", + }, + { + name: "doesn't match when delimiters aren't present", + expected: false, + line: "var TEST_FLAG", + matcher: Matcher{Elements: []ElementMatcher{NewElementMatcher("projKey", "", "'", []string{"TEST_FLAG"}, map[string][]string{"testflag": {}})}}, + flagKey: "TEST_FLAG", + }, + { + // This example is equivalent to setting `defaultsDisabled: true` in delimiters configuration + name: "matches without delimiters", + expected: true, + line: "var TEST_FLAG", + matcher: Matcher{Elements: []ElementMatcher{NewElementMatcher("projKey", "", "", []string{"TEST_FLAG"}, map[string][]string{"testflag": {}})}}, + flagKey: "TEST_FLAG", }, } for _, tt := range specs { tt := tt t.Run(tt.name, func(t *testing.T) { - require.Equal(t, tt.expected, tt.matcher.MatchElement(tt.line, FLAG_KEY)) + require.Equal(t, tt.expected, tt.matcher.MatchElement(tt.line, tt.flagKey)) }) } }