github.com/Cloud-Foundations/Dominator@v0.3.4/lib/filter/match_test.go (about) 1 package filter 2 3 import ( 4 "testing" 5 ) 6 7 var ( 8 excludeFilterLines = []string{ 9 "/etc/fstab", 10 "/tmp(|.*)", 11 } 12 13 includeFilterLines = []string{ 14 "!", 15 "/bin(|/.*)$", 16 } 17 ) 18 19 func TestExclude(t *testing.T) { 20 filt, err := New(excludeFilterLines) 21 if err != nil { 22 t.Error(err) 23 } 24 expectedNonMatches := []string{ 25 "/bin", 26 "/etc", 27 "/etc/passwd", 28 } 29 for _, line := range expectedNonMatches { 30 if filt.Match(line) { 31 t.Errorf("\"%s\" should not have matched", line) 32 } 33 } 34 expectedMatches := []string{ 35 "/etc/fstab", 36 "/tmp", 37 "/tmp/file", 38 } 39 for _, line := range expectedMatches { 40 if !filt.Match(line) { 41 t.Errorf("\"%s\" should have matched", line) 42 } 43 } 44 } 45 46 func TestInverted(t *testing.T) { 47 filt, err := New(includeFilterLines) 48 if err != nil { 49 t.Error(err) 50 } 51 expectedNonMatches := []string{ 52 "/bin", 53 "/bin/ls", 54 } 55 for _, line := range expectedNonMatches { 56 if filt.Match(line) { 57 t.Errorf("\"%s\" should not have matched", line) 58 } 59 } 60 expectedMatches := []string{ 61 "/bingo", 62 "/etc/fstab", 63 "/tmp", 64 "/tmp/file", 65 } 66 for _, line := range expectedMatches { 67 if !filt.Match(line) { 68 t.Errorf("\"%s\" should have matched", line) 69 } 70 } 71 }