github.com/wtfutil/wtf@v0.43.0/utils/utils_test.go (about) 1 package utils 2 3 import ( 4 "os/exec" 5 "reflect" 6 "testing" 7 8 "github.com/stretchr/testify/assert" 9 ) 10 11 func Test_DoesNotInclude(t *testing.T) { 12 tests := []struct { 13 name string 14 strs []string 15 val string 16 expected bool 17 }{ 18 { 19 name: "when included", 20 strs: []string{"a", "b", "c"}, 21 val: "b", 22 expected: false, 23 }, 24 { 25 name: "when not included", 26 strs: []string{"a", "b", "c"}, 27 val: "f", 28 expected: true, 29 }, 30 } 31 32 for _, tt := range tests { 33 t.Run(tt.name, func(t *testing.T) { 34 actual := DoesNotInclude(tt.strs, tt.val) 35 36 if tt.expected != actual { 37 t.Errorf("\nexpected: %t\n got: %t", tt.expected, actual) 38 } 39 }) 40 } 41 } 42 43 func Test_ExecuteCommand(t *testing.T) { 44 tests := []struct { 45 name string 46 cmd *exec.Cmd 47 expected string 48 }{ 49 { 50 name: "with nil command", 51 cmd: nil, 52 expected: "", 53 }, 54 { 55 name: "with defined command", 56 cmd: exec.Command("echo", "cats"), 57 expected: "cats\n", 58 }, 59 } 60 61 for _, tt := range tests { 62 t.Run(tt.name, func(t *testing.T) { 63 actual := ExecuteCommand(tt.cmd) 64 65 if tt.expected != actual { 66 t.Errorf("\nexpected: %s\n got: %s", tt.expected, actual) 67 } 68 }) 69 } 70 } 71 72 func Test_FindMatch(t *testing.T) { 73 expected := [][]string{{"SSID: 7E5B5C", "7E5B5C"}} 74 result := FindMatch(`s*SSID: (.+)s*`, "SSID: 7E5B5C") 75 76 assert.Equal(t, expected, result) 77 } 78 79 func Test_Includes(t *testing.T) { 80 tests := []struct { 81 name string 82 strs []string 83 val string 84 expected bool 85 }{ 86 { 87 name: "when included", 88 strs: []string{"a", "b", "c"}, 89 val: "b", 90 expected: true, 91 }, 92 { 93 name: "when not included", 94 strs: []string{"a", "b", "c"}, 95 val: "f", 96 expected: false, 97 }, 98 } 99 100 for _, tt := range tests { 101 t.Run(tt.name, func(t *testing.T) { 102 actual := Includes(tt.strs, tt.val) 103 104 if tt.expected != actual { 105 t.Errorf("\nexpected: %t\n got: %t", tt.expected, actual) 106 } 107 }) 108 } 109 } 110 111 func Test_ReadFileBytes(t *testing.T) { 112 tests := []struct { 113 name string 114 file string 115 expected []byte 116 }{ 117 { 118 name: "with non-existent file", 119 file: "/tmp/junk-daa6bf613f4c.md", 120 expected: []byte{}, 121 }, 122 } 123 124 for _, tt := range tests { 125 t.Run(tt.name, func(t *testing.T) { 126 actual, _ := ReadFileBytes(tt.file) 127 128 if reflect.DeepEqual(tt.expected, actual) == false { 129 t.Errorf("\nexpected: %q\n got: %q", tt.expected, actual) 130 } 131 }) 132 } 133 } 134 135 func Test_MaxInt(t *testing.T) { 136 expected := 3 137 result := MaxInt(3, 2) 138 139 assert.Equal(t, expected, result) 140 141 expected = 3 142 result = MaxInt(2, 3) 143 144 assert.Equal(t, expected, result) 145 } 146 147 func Test_Clamp(t *testing.T) { 148 expected := 6 149 result := Clamp(6, 3, 8) 150 151 assert.Equal(t, expected, result) 152 153 expected = 3 154 result = Clamp(1, 3, 8) 155 156 assert.Equal(t, expected, result) 157 158 expected = 8 159 result = Clamp(9, 3, 8) 160 161 assert.Equal(t, expected, result) 162 }