amuz.es/src/go/misc@v1.0.1/strutil/format_test.go (about) 1 package strutil 2 3 import ( 4 "reflect" 5 "testing" 6 ) 7 8 func TestStrPad(t *testing.T) { 9 const input = "Codes" 10 type args struct { 11 input string 12 padLength int 13 padString string 14 rightPad bool 15 } 16 tests := []struct { 17 name string 18 args args 19 wantOutput string 20 }{ 21 { 22 "rpad test", 23 args{input, 10, " ", true}, 24 "Codes ", 25 }, 26 { 27 "lpad test", 28 args{input, 10, "-=", false}, 29 "=-=-=Codes", 30 }, 31 { 32 "rpad filled test", 33 args{input, len(input), "_*", true}, 34 input, 35 }, 36 { 37 "lpad filled test", 38 args{input, len(input), "*", false}, 39 input, 40 }, 41 { 42 "rpad overflow test", 43 args{input, 3, "_*", true}, 44 input, 45 }, 46 { 47 "lpad overflow test", 48 args{input, 3, "*", false}, 49 input, 50 }, 51 } 52 for _, tt := range tests { 53 t.Run(tt.name, func(t *testing.T) { 54 if gotOutput := StrPad(tt.args.input, tt.args.padLength, tt.args.padString, tt.args.rightPad); gotOutput != tt.wantOutput { 55 t.Errorf("StrPad() = %v, want %v", gotOutput, tt.wantOutput) 56 } 57 }) 58 } 59 } 60 61 func TestStrPadSingle(t *testing.T) { 62 const input = "Codes" 63 type args struct { 64 input string 65 padLength int 66 pad byte 67 rightPad bool 68 } 69 tests := []struct { 70 name string 71 args args 72 wantOutput string 73 }{ 74 { 75 "rpad test", 76 args{input, 10, ' ', true}, 77 "Codes ", 78 }, 79 { 80 "lpad test", 81 args{input, 10, '-', false}, 82 "-----Codes", 83 }, 84 { 85 "rpad filled test", 86 args{input, len(input), '*', true}, 87 input, 88 }, 89 { 90 "lpad filled test", 91 args{input, len(input), '*', false}, 92 input, 93 }, 94 { 95 "rpad overflow test", 96 args{input, 3, '*', true}, 97 input, 98 }, 99 { 100 "lpad overflow test", 101 args{input, 3, '*', false}, 102 input, 103 }, 104 } 105 for _, tt := range tests { 106 t.Run(tt.name, func(t *testing.T) { 107 if gotOutput := StrPadSingle(tt.args.input, tt.args.padLength, tt.args.pad, tt.args.rightPad); gotOutput != tt.wantOutput { 108 t.Errorf("StrPad() = %v, want %v", gotOutput, tt.wantOutput) 109 } 110 }) 111 } 112 } 113 func TestBytesPad(t *testing.T) { 114 const input = "Codes" 115 type args struct { 116 input []byte 117 padLength int 118 padData []byte 119 rightPad bool 120 } 121 tests := []struct { 122 name string 123 args args 124 wantOutput []byte 125 }{ 126 { 127 "rpad test", 128 args{[]byte(input), 10, []byte(" "), true}, 129 []byte("Codes "), 130 }, 131 { 132 "lpad test", 133 args{[]byte(input), 10, []byte("-="), false}, 134 []byte("=-=-=Codes"), 135 }, 136 { 137 "rpad filled test", 138 args{[]byte(input), len(input), []byte("_*"), true}, 139 []byte(input), 140 }, 141 { 142 "lpad filled test", 143 args{[]byte(input), len(input), []byte("*"), false}, 144 []byte(input), 145 }, 146 { 147 "rpad overflow test", 148 args{[]byte(input), 3, []byte("_*"), true}, 149 []byte(input), 150 }, 151 { 152 "lpad overflow test", 153 args{[]byte(input), 3, []byte("*"), false}, 154 []byte(input), 155 }, 156 } 157 for _, tt := range tests { 158 t.Run(tt.name, func(t *testing.T) { 159 if gotOutput := BytesPad(tt.args.input, tt.args.padLength, tt.args.padData, tt.args.rightPad); !reflect.DeepEqual(gotOutput, tt.wantOutput) { 160 t.Errorf("BytesPad() = %v, want %v", gotOutput, tt.wantOutput) 161 } 162 }) 163 } 164 } 165 166 func TestBytesPadSingle(t *testing.T) { 167 const input = "Codes" 168 type args struct { 169 input []byte 170 padLength int 171 padData byte 172 rightPad bool 173 } 174 tests := []struct { 175 name string 176 args args 177 wantOutput []byte 178 }{ 179 { 180 "rpad test", 181 args{[]byte(input), 10, ' ', true}, 182 []byte("Codes "), 183 }, 184 { 185 "lpad test", 186 args{[]byte(input), 10, '_', false}, 187 []byte("_____Codes"), 188 }, 189 { 190 "rpad filled test", 191 args{[]byte(input), len(input), '*', true}, 192 []byte(input), 193 }, 194 { 195 "lpad filled test", 196 args{[]byte(input), len(input), '*', false}, 197 []byte(input), 198 }, 199 { 200 "rpad overflow test", 201 args{[]byte(input), 3, '*', true}, 202 []byte(input), 203 }, 204 { 205 "lpad overflow test", 206 args{[]byte(input), 3, '*', false}, 207 []byte(input), 208 }, 209 } 210 for _, tt := range tests { 211 t.Run(tt.name, func(t *testing.T) { 212 if gotOutput := BytesPadSingle(tt.args.input, tt.args.padLength, tt.args.padData, tt.args.rightPad); !reflect.DeepEqual(gotOutput, tt.wantOutput) { 213 t.Errorf("BytesPad() = %v, want %v", gotOutput, tt.wantOutput) 214 } 215 }) 216 } 217 }