| 1 | package wordsscip-go gomod github.com/betterleaks/betterleaks . `github.com/betterleaks/betterleaks/internal/words`/ |
| 2 | |
| 3 | import ( |
| 4 | "sortscip-go gomod github.com/golang/go/src go1.25.0 sort/" |
| 5 | "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/" |
| 6 | ) |
| 7 | |
| 8 | func TestHasMatchInListscip-go gomod github.com/betterleaks/betterleaks . `github.com/betterleaks/betterleaks/internal/words`/TestHasMatchInList().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 9 | tests := []struct { |
| 10 | name string |
| 11 | word string |
| 12 | minLen int |
| 13 | // want nil result |
| 14 | wantNil bool |
| 15 | // otherwise: expected unique words (order-independent) |
| 16 | wantUniqueWords []string |
| 17 | // and minimum number of total matches |
| 18 | wantMinMatchCount int |
| 19 | }{ |
| 20 | { |
| 21 | name: "empty string", |
| 22 | word: "", |
| 23 | minLen: 3, |
| 24 | wantNil: true, |
| 25 | }, |
| 26 | { |
| 27 | name: "shorter than minLen", |
| 28 | word: "ab", |
| 29 | minLen: 3, |
| 30 | wantNil: true, |
| 31 | }, |
| 32 | { |
| 33 | name: "no dictionary substring", |
| 34 | word: "xyzabc", |
| 35 | minLen: 3, |
| 36 | wantNil: true, |
| 37 | }, |
| 38 | { |
| 39 | name: "exact word", |
| 40 | word: "pass", |
| 41 | minLen: 3, |
| 42 | wantUniqueWords: []string{"pass"}, |
| 43 | wantMinMatchCount: 1, |
| 44 | }, |
| 45 | { |
| 46 | name: "prefix and middle matches", |
| 47 | word: "password", |
| 48 | minLen: 3, |
| 49 | wantUniqueWords: []string{"pass", "password", "sword", "word"}, |
| 50 | wantMinMatchCount: 4, |
| 51 | }, |
| 52 | { |
| 53 | name: "match in middle", |
| 54 | word: "xxwordxx", |
| 55 | minLen: 3, |
| 56 | wantUniqueWords: []string{"word"}, |
| 57 | wantMinMatchCount: 1, |
| 58 | }, |
| 59 | { |
| 60 | name: "minLen filters shorter", |
| 61 | word: "word", |
| 62 | minLen: 4, |
| 63 | wantUniqueWords: []string{"word"}, |
| 64 | wantMinMatchCount: 1, |
| 65 | }, |
| 66 | { |
| 67 | name: "minLen 4 excludes 3-char match", |
| 68 | word: "aba", |
| 69 | minLen: 4, |
| 70 | wantNil: true, |
| 71 | }, |
| 72 | } |
| 73 | for _, tt := range tests { |
| 74 | t.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Run().(tt.name, func(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 75 | got := HasMatchInListscip-go gomod github.com/betterleaks/betterleaks . `github.com/betterleaks/betterleaks/internal/words`/HasMatchInList().(tt.word, tt.minLen) |
| 76 | if tt.wantNil { |
| 77 | if got != nil { |
| 78 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("HasMatchInList() = %v, want nil", got) |
| 79 | } |
| 80 | return |
| 81 | } |
| 82 | if got == nil || len(got) == 0 { |
| 83 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("HasMatchInList() = nil, want result with UniqueWords %v", tt.wantUniqueWords) |
| 84 | } |
| 85 | r := got[0] |
| 86 | if r.WordCountscip-go gomod github.com/betterleaks/betterleaks . `github.com/betterleaks/betterleaks/internal/words`/Result#WordCount. < tt.wantMinMatchCount { |
| 87 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("WordCount = %d, want at least %d", r.WordCountscip-go gomod github.com/betterleaks/betterleaks . `github.com/betterleaks/betterleaks/internal/words`/Result#WordCount., tt.wantMinMatchCount) |
| 88 | } |
| 89 | sortscip-go gomod github.com/golang/go/src go1.25.0 sort/.Stringsscip-go gomod github.com/golang/go/src go1.25.0 sort/Strings().(r.UniqueWordsscip-go gomod github.com/betterleaks/betterleaks . `github.com/betterleaks/betterleaks/internal/words`/Result#UniqueWords.) |
| 90 | wantSorted := make([]string, len(tt.wantUniqueWords)) |
| 91 | copy(wantSorted, tt.wantUniqueWords) |
| 92 | sortscip-go gomod github.com/golang/go/src go1.25.0 sort/.Stringsscip-go gomod github.com/golang/go/src go1.25.0 sort/Strings().(wantSorted) |
| 93 | if len(r.UniqueWordsscip-go gomod github.com/betterleaks/betterleaks . `github.com/betterleaks/betterleaks/internal/words`/Result#UniqueWords.) != len(wantSorted) { |
| 94 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("UniqueWords = %v, want %v", r.UniqueWordsscip-go gomod github.com/betterleaks/betterleaks . `github.com/betterleaks/betterleaks/internal/words`/Result#UniqueWords., wantSorted) |
| 95 | } else { |
| 96 | for i := range wantSorted { |
| 97 | if r.UniqueWordsscip-go gomod github.com/betterleaks/betterleaks . `github.com/betterleaks/betterleaks/internal/words`/Result#UniqueWords.[i] != wantSorted[i] { |
| 98 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("UniqueWords = %v, want %v", r.UniqueWordsscip-go gomod github.com/betterleaks/betterleaks . `github.com/betterleaks/betterleaks/internal/words`/Result#UniqueWords., wantSorted) |
| 99 | break |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | }) |
| 104 | } |
| 105 | } |