github.com/blend/go-sdk@v1.20220411.3/profanity/contents_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package profanity
     9  
    10  import (
    11  	"testing"
    12  
    13  	"github.com/blend/go-sdk/assert"
    14  )
    15  
    16  func Test_Contents_Contains_NoMatch(t *testing.T) {
    17  	its := assert.New(t)
    18  
    19  	contents := `
    20  foo bar baz
    21  bar foo baz
    22  baz bar foo
    23  `
    24  	rule := Contents{
    25  		Contains: &ContainsFilter{
    26  			Filter: Filter{
    27  				Include: []string{"fuzzy wuzzy"},
    28  			},
    29  		},
    30  	}
    31  
    32  	res := rule.Check("test.go", []byte(contents))
    33  	its.True(res.OK)
    34  }
    35  
    36  func Test_Contents_Contains_Include(t *testing.T) {
    37  	its := assert.New(t)
    38  
    39  	contents := `
    40  foo bar baz
    41  bar foo baz
    42  baz bar foo
    43  `
    44  	rule := Contents{
    45  		Contains: &ContainsFilter{
    46  			Filter: Filter{
    47  				Include: []string{"foo baz"},
    48  			},
    49  		},
    50  	}
    51  
    52  	res := rule.Check("test.go", []byte(contents))
    53  	its.False(res.OK)
    54  	its.Equal("test.go", res.File)
    55  	its.Equal(3, res.Line)
    56  }
    57  
    58  func Test_Contents_Contains_IncludeExclude(t *testing.T) {
    59  	its := assert.New(t)
    60  
    61  	contents := `
    62  foo bar baz
    63  bar foo moo
    64  baz bar foo
    65  buzz foo baz
    66  `
    67  	rule := Contents{
    68  		Contains: &ContainsFilter{
    69  			Filter: Filter{
    70  				Include: []string{"foo baz"},
    71  				Exclude: []string{"buzz"},
    72  			},
    73  		},
    74  	}
    75  
    76  	res := rule.Check("test.go", []byte(contents))
    77  	its.True(res.OK)
    78  }
    79  
    80  func Test_Contents_Glob(t *testing.T) {
    81  	its := assert.New(t)
    82  
    83  	contents := `
    84  foo bar baz
    85  bar foo baz
    86  baz bar foo
    87  `
    88  	rule := Contents{
    89  		Glob: &GlobFilter{
    90  			Filter: Filter{
    91  				Include: []string{"bar*"},
    92  			},
    93  		},
    94  	}
    95  
    96  	res := rule.Check("test.go", []byte(contents))
    97  	its.False(res.OK)
    98  	its.Equal("test.go", res.File)
    99  	its.Equal(3, res.Line)
   100  }
   101  
   102  func Test_Contents_Regex(t *testing.T) {
   103  	its := assert.New(t)
   104  
   105  	contents := `
   106  foo bar baz
   107  bar foo baz
   108  baz bar foo
   109  `
   110  	rule := Contents{
   111  		Regex: &RegexFilter{
   112  			Filter: Filter{
   113  				Include: []string{"^bar"},
   114  			},
   115  		},
   116  	}
   117  
   118  	res := rule.Check("test.go", []byte(contents))
   119  	its.False(res.OK)
   120  	its.Equal("test.go", res.File)
   121  	its.Equal(3, res.Line)
   122  }