github.com/zkry/enry@v1.6.3/utils_test.go (about)

     1  package enry
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"sort"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func (s *EnryTestSuite) TestIsVendor() {
    13  	tests := []struct {
    14  		name     string
    15  		path     string
    16  		expected bool
    17  	}{
    18  		{name: "TestIsVendor_1", path: "foo/bar", expected: false},
    19  		{name: "TestIsVendor_2", path: "foo/vendor/foo", expected: true},
    20  		{name: "TestIsVendor_3", path: ".sublime-project", expected: true},
    21  		{name: "TestIsVendor_4", path: "leaflet.draw-src.js", expected: true},
    22  		{name: "TestIsVendor_5", path: "foo/bar/MochiKit.js", expected: true},
    23  		{name: "TestIsVendor_6", path: "foo/bar/dojo.js", expected: true},
    24  		{name: "TestIsVendor_7", path: "foo/env/whatever", expected: true},
    25  		{name: "TestIsVendor_8", path: "foo/.imageset/bar", expected: true},
    26  		{name: "TestIsVendor_9", path: "Vagrantfile", expected: true},
    27  	}
    28  
    29  	for _, test := range tests {
    30  		is := IsVendor(test.path)
    31  		assert.Equal(s.T(), is, test.expected, fmt.Sprintf("%v: is = %v, expected: %v", test.name, is, test.expected))
    32  	}
    33  }
    34  
    35  func (s *EnryTestSuite) TestIsDocumentation() {
    36  	tests := []struct {
    37  		name     string
    38  		path     string
    39  		expected bool
    40  	}{
    41  		{name: "TestIsDocumentation_1", path: "foo", expected: false},
    42  		{name: "TestIsDocumentation_2", path: "README", expected: true},
    43  	}
    44  
    45  	for _, test := range tests {
    46  		is := IsDocumentation(test.path)
    47  		assert.Equal(s.T(), is, test.expected, fmt.Sprintf("%v: is = %v, expected: %v", test.name, is, test.expected))
    48  	}
    49  }
    50  
    51  func (s *EnryTestSuite) TestIsConfiguration() {
    52  	tests := []struct {
    53  		name     string
    54  		path     string
    55  		expected bool
    56  	}{
    57  		{name: "TestIsConfiguration_1", path: "foo", expected: false},
    58  		{name: "TestIsConfiguration_2", path: "foo.ini", expected: true},
    59  		{name: "TestIsConfiguration_3", path: "/test/path/foo.json", expected: true},
    60  	}
    61  
    62  	for _, test := range tests {
    63  		is := IsConfiguration(test.path)
    64  		assert.Equal(s.T(), is, test.expected, fmt.Sprintf("%v: is = %v, expected: %v", test.name, is, test.expected))
    65  	}
    66  }
    67  
    68  func (s *EnryTestSuite) TestIsBinary() {
    69  	tests := []struct {
    70  		name     string
    71  		data     []byte
    72  		expected bool
    73  	}{
    74  		{name: "TestIsBinary_1", data: []byte("foo"), expected: false},
    75  		{name: "TestIsBinary_2", data: []byte{0}, expected: true},
    76  		{name: "TestIsBinary_3", data: bytes.Repeat([]byte{'o'}, 8000), expected: false},
    77  	}
    78  
    79  	for _, test := range tests {
    80  		is := IsBinary(test.data)
    81  		assert.Equal(s.T(), is, test.expected, fmt.Sprintf("%v: is = %v, expected: %v", test.name, is, test.expected))
    82  	}
    83  }
    84  
    85  func (s *EnryTestSuite) TestIsDotFile() {
    86  	tests := []struct {
    87  		name     string
    88  		path     string
    89  		expected bool
    90  	}{
    91  		{name: "TestIsDotFile_1", path: "foo/bar/./", expected: false},
    92  		{name: "TestIsDotFile_2", path: "./", expected: false},
    93  	}
    94  
    95  	for _, test := range tests {
    96  		is := IsDotFile(test.path)
    97  		assert.Equal(s.T(), test.expected, is, fmt.Sprintf("%v: is = %v, expected: %v", test.name, is, test.expected))
    98  	}
    99  }
   100  
   101  func TestFileCountListSort(t *testing.T) {
   102  	sampleData := FileCountList{{"a", 8}, {"b", 65}, {"c", 20}, {"d", 90}}
   103  	const ascending = "ASC"
   104  	const descending = "DESC"
   105  
   106  	tests := []struct {
   107  		name         string
   108  		data         FileCountList
   109  		order        string
   110  		expectedData FileCountList
   111  	}{
   112  		{
   113  			name:         "ascending order",
   114  			data:         sampleData,
   115  			order:        ascending,
   116  			expectedData: FileCountList{{"a", 8}, {"c", 20}, {"b", 65}, {"d", 90}},
   117  		},
   118  		{
   119  			name:         "descending order",
   120  			data:         sampleData,
   121  			order:        descending,
   122  			expectedData: FileCountList{{"d", 90}, {"b", 65}, {"c", 20}, {"a", 8}},
   123  		},
   124  	}
   125  
   126  	for _, test := range tests {
   127  		t.Run(test.name, func(t *testing.T) {
   128  			if test.order == descending {
   129  				sort.Sort(sort.Reverse(test.data))
   130  			} else {
   131  				sort.Sort(test.data)
   132  			}
   133  
   134  			for i := 0; i < len(test.data); i++ {
   135  				assert.Equal(t, test.data[i], test.expectedData[i], fmt.Sprintf("%v: FileCount at position %d = %v, expected: %v", test.name, i, test.data[i], test.expectedData[i]))
   136  			}
   137  		})
   138  	}
   139  }