github.com/go-enry/go-enry@v1.7.3/utils_test.go (about) 1 package enry 2 3 import ( 4 "bytes" 5 "fmt" 6 "testing" 7 8 "github.com/stretchr/testify/assert" 9 ) 10 11 func TestIsVendor(t *testing.T) { 12 tests := []struct { 13 name string 14 path string 15 expected bool 16 }{ 17 {name: "TestIsVendor_1", path: "foo/bar", expected: false}, 18 {name: "TestIsVendor_2", path: "foo/vendor/foo", expected: true}, 19 {name: "TestIsVendor_3", path: ".sublime-project", expected: true}, 20 {name: "TestIsVendor_4", path: "leaflet.draw-src.js", expected: true}, 21 {name: "TestIsVendor_5", path: "foo/bar/MochiKit.js", expected: true}, 22 {name: "TestIsVendor_6", path: "foo/bar/dojo.js", expected: true}, 23 {name: "TestIsVendor_7", path: "foo/env/whatever", expected: true}, 24 {name: "TestIsVendor_8", path: "foo/.imageset/bar", expected: true}, 25 {name: "TestIsVendor_9", path: "Vagrantfile", expected: true}, 26 } 27 28 for _, test := range tests { 29 is := IsVendor(test.path) 30 assert.Equal(t, is, test.expected, fmt.Sprintf("%v: is = %v, expected: %v", test.name, is, test.expected)) 31 } 32 } 33 34 func TestIsDocumentation(t *testing.T) { 35 tests := []struct { 36 name string 37 path string 38 expected bool 39 }{ 40 {name: "TestIsDocumentation_1", path: "foo", expected: false}, 41 {name: "TestIsDocumentation_2", path: "README", expected: true}, 42 } 43 44 for _, test := range tests { 45 is := IsDocumentation(test.path) 46 assert.Equal(t, is, test.expected, fmt.Sprintf("%v: is = %v, expected: %v", test.name, is, test.expected)) 47 } 48 } 49 50 func TestIsImage(t *testing.T) { 51 tests := []struct { 52 name string 53 path string 54 expected bool 55 }{ 56 {name: "TestIsImage_1", path: "invalid.txt", expected: false}, 57 {name: "TestIsImage_2", path: "image.png", expected: true}, 58 {name: "TestIsImage_3", path: "image.jpg", expected: true}, 59 {name: "TestIsImage_4", path: "image.jpeg", expected: true}, 60 {name: "TestIsImage_5", path: "image.gif", expected: true}, 61 } 62 63 for _, test := range tests { 64 is := IsImage(test.path) 65 assert.Equal(t, is, test.expected, fmt.Sprintf("%v: is = %v, expected: %v", test.name, is, test.expected)) 66 } 67 } 68 69 func TestGetMimeType(t *testing.T) { 70 tests := []struct { 71 name string 72 path string 73 lang string 74 expected string 75 }{ 76 {name: "TestGetMimeType_1", path: "text.txt", lang: "", expected: "text/plain"}, 77 {name: "TestGetMimeType_2", path: "file.go", lang: "Go", expected: "text/x-go"}, 78 {name: "TestGetMimeType_3", path: "image.png", lang: "", expected: "image/png"}, 79 } 80 81 for _, test := range tests { 82 is := GetMIMEType(test.path, test.lang) 83 assert.Equal(t, is, test.expected, fmt.Sprintf("%v: is = %v, expected: %v", test.name, is, test.expected)) 84 } 85 } 86 87 func TestIsConfiguration(t *testing.T) { 88 tests := []struct { 89 name string 90 path string 91 expected bool 92 }{ 93 {name: "TestIsConfiguration_1", path: "foo", expected: false}, 94 {name: "TestIsConfiguration_2", path: "foo.ini", expected: true}, 95 {name: "TestIsConfiguration_3", path: "/test/path/foo.json", expected: true}, 96 } 97 98 for _, test := range tests { 99 is := IsConfiguration(test.path) 100 assert.Equal(t, is, test.expected, fmt.Sprintf("%v: is = %v, expected: %v", test.name, is, test.expected)) 101 } 102 } 103 104 func TestIsBinary(t *testing.T) { 105 tests := []struct { 106 name string 107 data []byte 108 expected bool 109 }{ 110 {name: "TestIsBinary_1", data: []byte("foo"), expected: false}, 111 {name: "TestIsBinary_2", data: []byte{0}, expected: true}, 112 {name: "TestIsBinary_3", data: bytes.Repeat([]byte{'o'}, 8000), expected: false}, 113 } 114 115 for _, test := range tests { 116 is := IsBinary(test.data) 117 assert.Equal(t, is, test.expected, fmt.Sprintf("%v: is = %v, expected: %v", test.name, is, test.expected)) 118 } 119 } 120 121 func TestIsDotFile(t *testing.T) { 122 tests := []struct { 123 name string 124 path string 125 expected bool 126 }{ 127 {name: "TestIsDotFile_1", path: "foo/bar/./", expected: false}, 128 {name: "TestIsDotFile_2", path: "./", expected: false}, 129 } 130 131 for _, test := range tests { 132 is := IsDotFile(test.path) 133 assert.Equal(t, test.expected, is, fmt.Sprintf("%v: is = %v, expected: %v", test.name, is, test.expected)) 134 } 135 }