| 1 | + | // Copyright 2018 Google Inc. |
| 2 | + | // |
| 3 | + | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + | // you may not use this file except in compliance with the License. |
| 5 | + | // You may obtain a copy of the License at |
| 6 | + | // |
| 7 | + | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + | // |
| 9 | + | // Unless required by applicable law or agreed to in writing, software |
| 10 | + | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + | // See the License for the specific language governing permissions and |
| 13 | + | // limitations under the License. |
| 14 | + | |
| 15 | + | package alignfor |
| 16 | + | |
| 17 | + | import ( |
| 18 | + | "image" |
| 19 | + | "testing" |
| 20 | + | |
| 21 | + | "github.com/kylelemons/godebug/pretty" |
| 22 | + | "github.com/mum4k/termdash/align" |
| 23 | + | ) |
| 24 | + | |
| 25 | + | func TestRectangle(t *testing.T) { |
| 26 | + | tests := []struct { |
| 27 | + | desc string |
| 28 | + | rect image.Rectangle |
| 29 | + | area image.Rectangle |
| 30 | + | hAlign align.Horizontal |
| 31 | + | vAlign align.Vertical |
| 32 | + | want image.Rectangle |
| 33 | + | wantErr bool |
| 34 | + | }{ |
| 35 | + | { |
| 36 | + | desc: "area falls outside of the rectangle", |
| 37 | + | rect: image.Rect(0, 0, 1, 1), |
| 38 | + | area: image.Rect(1, 1, 2, 2), |
| 39 | + | hAlign: align.HorizontalLeft, |
| 40 | + | vAlign: align.VerticalTop, |
| 41 | + | wantErr: true, |
| 42 | + | }, |
| 43 | + | { |
| 44 | + | desc: "unsupported horizontal alignment", |
| 45 | + | rect: image.Rect(0, 0, 2, 2), |
| 46 | + | area: image.Rect(0, 0, 1, 1), |
| 47 | + | hAlign: align.Horizontal(-1), |
| 48 | + | vAlign: align.VerticalTop, |
| 49 | + | wantErr: true, |
| 50 | + | }, |
| 51 | + | { |
| 52 | + | desc: "unsupported vertical alignment", |
| 53 | + | rect: image.Rect(0, 0, 2, 2), |
| 54 | + | area: image.Rect(0, 0, 1, 1), |
| 55 | + | hAlign: align.HorizontalLeft, |
| 56 | + | vAlign: align.Vertical(-1), |
| 57 | + | wantErr: true, |
| 58 | + | }, |
| 59 | + | { |
| 60 | + | desc: "nothing to align if the rectangles are equal", |
| 61 | + | rect: image.Rect(0, 0, 2, 2), |
| 62 | + | area: image.Rect(0, 0, 2, 2), |
| 63 | + | hAlign: align.HorizontalLeft, |
| 64 | + | vAlign: align.VerticalTop, |
| 65 | + | want: image.Rect(0, 0, 2, 2), |
| 66 | + | }, |
| 67 | + | { |
| 68 | + | desc: "aligns top and left, area is zero based", |
| 69 | + | rect: image.Rect(0, 0, 3, 3), |
| 70 | + | area: image.Rect(0, 0, 1, 1), |
| 71 | + | hAlign: align.HorizontalLeft, |
| 72 | + | vAlign: align.VerticalTop, |
| 73 | + | want: image.Rect(0, 0, 1, 1), |
| 74 | + | }, |
| 75 | + | { |
| 76 | + | desc: "aligns top and center, area is zero based", |
| 77 | + | rect: image.Rect(0, 0, 3, 3), |
| 78 | + | area: image.Rect(0, 0, 1, 1), |
| 79 | + | hAlign: align.HorizontalCenter, |
| 80 | + | vAlign: align.VerticalTop, |
| 81 | + | want: image.Rect(1, 0, 2, 1), |
| 82 | + | }, |
| 83 | + | { |
| 84 | + | desc: "aligns top and right, area is zero based", |
| 85 | + | rect: image.Rect(0, 0, 3, 3), |
| 86 | + | area: image.Rect(0, 0, 1, 1), |
| 87 | + | hAlign: align.HorizontalRight, |
| 88 | + | vAlign: align.VerticalTop, |
| 89 | + | want: image.Rect(2, 0, 3, 1), |
| 90 | + | }, |
| 91 | + | { |
| 92 | + | desc: "aligns middle and left, area is zero based", |
| 93 | + | rect: image.Rect(0, 0, 3, 3), |
| 94 | + | area: image.Rect(0, 0, 1, 1), |
| 95 | + | hAlign: align.HorizontalLeft, |
| 96 | + | vAlign: align.VerticalMiddle, |
| 97 | + | want: image.Rect(0, 1, 1, 2), |
| 98 | + | }, |
| 99 | + | { |
| 100 | + | desc: "aligns middle and center, area is zero based", |
| 101 | + | rect: image.Rect(0, 0, 3, 3), |
| 102 | + | area: image.Rect(0, 0, 1, 1), |
| 103 | + | hAlign: align.HorizontalCenter, |
| 104 | + | vAlign: align.VerticalMiddle, |
| 105 | + | want: image.Rect(1, 1, 2, 2), |
| 106 | + | }, |
| 107 | + | { |
| 108 | + | desc: "aligns middle and right, area is zero based", |
| 109 | + | rect: image.Rect(0, 0, 3, 3), |
| 110 | + | area: image.Rect(0, 0, 1, 1), |
| 111 | + | hAlign: align.HorizontalRight, |
| 112 | + | vAlign: align.VerticalMiddle, |
| 113 | + | want: image.Rect(2, 1, 3, 2), |
| 114 | + | }, |
| 115 | + | { |
| 116 | + | desc: "aligns bottom and left, area is zero based", |
| 117 | + | rect: image.Rect(0, 0, 3, 3), |
| 118 | + | area: image.Rect(0, 0, 1, 1), |
| 119 | + | hAlign: align.HorizontalLeft, |
| 120 | + | vAlign: align.VerticalBottom, |
| 121 | + | want: image.Rect(0, 2, 1, 3), |
| 122 | + | }, |
| 123 | + | { |
| 124 | + | desc: "aligns bottom and center, area is zero based", |
| 125 | + | rect: image.Rect(0, 0, 3, 3), |
| 126 | + | area: image.Rect(0, 0, 1, 1), |
| 127 | + | hAlign: align.HorizontalCenter, |
| 128 | + | vAlign: align.VerticalBottom, |
| 129 | + | want: image.Rect(1, 2, 2, 3), |
| 130 | + | }, |
| 131 | + | { |
| 132 | + | desc: "aligns bottom and right, area is zero based", |
| 133 | + | rect: image.Rect(0, 0, 3, 3), |
| 134 | + | area: image.Rect(0, 0, 1, 1), |
| 135 | + | hAlign: align.HorizontalRight, |
| 136 | + | vAlign: align.VerticalBottom, |
| 137 | + | want: image.Rect(2, 2, 3, 3), |
| 138 | + | }, |
| 139 | + | { |
| 140 | + | desc: "aligns top and left, area isn't zero based", |
| 141 | + | rect: image.Rect(0, 0, 3, 3), |
| 142 | + | area: image.Rect(0, 0, 1, 1), |
| 143 | + | hAlign: align.HorizontalLeft, |
| 144 | + | vAlign: align.VerticalTop, |
| 145 | + | want: image.Rect(0, 0, 1, 1), |
| 146 | + | }, |
| 147 | + | { |
| 148 | + | desc: "aligns top and center, area isn't zero based", |
| 149 | + | rect: image.Rect(0, 0, 3, 3), |
| 150 | + | area: image.Rect(1, 1, 2, 2), |
| 151 | + | hAlign: align.HorizontalCenter, |
| 152 | + | vAlign: align.VerticalTop, |
| 153 | + | want: image.Rect(1, 0, 2, 1), |
| 154 | + | }, |
| 155 | + | { |
| 156 | + | desc: "aligns top and right, area isn't zero based", |
| 157 | + | rect: image.Rect(0, 0, 3, 3), |
| 158 | + | area: image.Rect(1, 1, 2, 2), |
| 159 | + | hAlign: align.HorizontalRight, |
| 160 | + | vAlign: align.VerticalTop, |
| 161 | + | want: image.Rect(2, 0, 3, 1), |
| 162 | + | }, |
| 163 | + | { |
| 164 | + | desc: "aligns middle and left, area isn't zero based", |
| 165 | + | rect: image.Rect(0, 0, 3, 3), |
| 166 | + | area: image.Rect(1, 1, 2, 2), |
| 167 | + | hAlign: align.HorizontalLeft, |
| 168 | + | vAlign: align.VerticalMiddle, |
| 169 | + | want: image.Rect(0, 1, 1, 2), |
| 170 | + | }, |
| 171 | + | { |
| 172 | + | desc: "aligns middle and center, area isn't zero based", |
| 173 | + | rect: image.Rect(0, 0, 3, 3), |
| 174 | + | area: image.Rect(1, 1, 2, 2), |
| 175 | + | hAlign: align.HorizontalCenter, |
| 176 | + | vAlign: align.VerticalMiddle, |
| 177 | + | want: image.Rect(1, 1, 2, 2), |
| 178 | + | }, |
| 179 | + | { |
| 180 | + | desc: "aligns middle and right, area isn't zero based", |
| 181 | + | rect: image.Rect(0, 0, 3, 3), |
| 182 | + | area: image.Rect(1, 1, 2, 2), |
| 183 | + | hAlign: align.HorizontalRight, |
| 184 | + | vAlign: align.VerticalMiddle, |
| 185 | + | want: image.Rect(2, 1, 3, 2), |
| 186 | + | }, |
| 187 | + | { |
| 188 | + | desc: "aligns bottom and left, area isn't zero based", |
| 189 | + | rect: image.Rect(0, 0, 3, 3), |
| 190 | + | area: image.Rect(1, 1, 2, 2), |
| 191 | + | hAlign: align.HorizontalLeft, |
| 192 | + | vAlign: align.VerticalBottom, |
| 193 | + | want: image.Rect(0, 2, 1, 3), |
| 194 | + | }, |
| 195 | + | { |
| 196 | + | desc: "aligns bottom and center, area isn't zero based", |
| 197 | + | rect: image.Rect(0, 0, 3, 3), |
| 198 | + | area: image.Rect(1, 1, 2, 2), |
| 199 | + | hAlign: align.HorizontalCenter, |
| 200 | + | vAlign: align.VerticalBottom, |
| 201 | + | want: image.Rect(1, 2, 2, 3), |
| 202 | + | }, |
| 203 | + | { |
| 204 | + | desc: "aligns bottom and right, area isn't zero based", |
| 205 | + | rect: image.Rect(0, 0, 3, 3), |
| 206 | + | area: image.Rect(1, 1, 2, 2), |
| 207 | + | hAlign: align.HorizontalRight, |
| 208 | + | vAlign: align.VerticalBottom, |
| 209 | + | want: image.Rect(2, 2, 3, 3), |
| 210 | + | }, |
| 211 | + | } |
| 212 | + | |
| 213 | + | for _, tc := range tests { |
| 214 | + | t.Run(tc.desc, func(t *testing.T) { |
| 215 | + | got, err := Rectangle(tc.rect, tc.area, tc.hAlign, tc.vAlign) |
| 216 | + | if (err != nil) != tc.wantErr { |
| 217 | + | t.Errorf("Rectangle => unexpected error: %v, wantErr: %v", err, tc.wantErr) |
| 218 | + | } |
| 219 | + | if err != nil { |
| 220 | + | return |
| 221 | + | } |
| 222 | + | |
| 223 | + | if diff := pretty.Compare(tc.want, got); diff != "" { |
| 224 | + | t.Errorf("Rectangle => unexpected diff (-want, +got):\n%s", diff) |
| 225 | + | } |
| 226 | + | }) |
| 227 | + | } |
| 228 | + | } |
| 229 | + | |
| 230 | + | func TestText(t *testing.T) { |
| 231 | + | tests := []struct { |
| 232 | + | desc string |
| 233 | + | rect image.Rectangle |
| 234 | + | text string |
| 235 | + | hAlign align.Horizontal |
| 236 | + | vAlign align.Vertical |
| 237 | + | want image.Point |
| 238 | + | wantErr bool |
| 239 | + | }{ |
| 240 | + | { |
| 241 | + | desc: "fails when text contains newline", |
| 242 | + | rect: image.Rect(0, 0, 3, 3), |
| 243 | + | text: "a\nb", |
| 244 | + | wantErr: true, |
| 245 | + | }, |
| 246 | + | { |
| 247 | + | desc: "aligns text top and left", |
| 248 | + | rect: image.Rect(1, 1, 4, 4), |
| 249 | + | text: "a", |
| 250 | + | hAlign: align.HorizontalLeft, |
| 251 | + | vAlign: align.VerticalTop, |
| 252 | + | want: image.Point{1, 1}, |
| 253 | + | }, |
| 254 | + | { |
| 255 | + | desc: "aligns text top and center", |
| 256 | + | rect: image.Rect(1, 1, 4, 4), |
| 257 | + | text: "a", |
| 258 | + | hAlign: align.HorizontalCenter, |
| 259 | + | vAlign: align.VerticalTop, |
| 260 | + | want: image.Point{2, 1}, |
| 261 | + | }, |
| 262 | + | { |
| 263 | + | desc: "aligns text top and right", |
| 264 | + | rect: image.Rect(1, 1, 4, 4), |
| 265 | + | text: "a", |
| 266 | + | hAlign: align.HorizontalRight, |
| 267 | + | vAlign: align.VerticalTop, |
| 268 | + | want: image.Point{3, 1}, |
| 269 | + | }, |
| 270 | + | { |
| 271 | + | desc: "aligns text middle and left", |
| 272 | + | rect: image.Rect(1, 1, 4, 4), |
| 273 | + | text: "a", |
| 274 | + | hAlign: align.HorizontalLeft, |
| 275 | + | vAlign: align.VerticalMiddle, |
| 276 | + | want: image.Point{1, 2}, |
| 277 | + | }, |
| 278 | + | { |
| 279 | + | desc: "aligns half-width text rune middle and center", |
| 280 | + | rect: image.Rect(1, 1, 4, 4), |
| 281 | + | text: "a", |
| 282 | + | hAlign: align.HorizontalCenter, |
| 283 | + | vAlign: align.VerticalMiddle, |
| 284 | + | want: image.Point{2, 2}, |
| 285 | + | }, |
| 286 | + | { |
| 287 | + | desc: "aligns full-width text rune middle and center", |
| 288 | + | rect: image.Rect(1, 1, 4, 4), |
| 289 | + | text: "界", |
| 290 | + | hAlign: align.HorizontalCenter, |
| 291 | + | vAlign: align.VerticalMiddle, |
| 292 | + | want: image.Point{1, 2}, |
| 293 | + | }, |
| 294 | + | { |
| 295 | + | desc: "aligns text middle and right", |
| 296 | + | rect: image.Rect(1, 1, 4, 4), |
| 297 | + | text: "a", |
| 298 | + | hAlign: align.HorizontalRight, |
| 299 | + | vAlign: align.VerticalMiddle, |
| 300 | + | want: image.Point{3, 2}, |
| 301 | + | }, |
| 302 | + | { |
| 303 | + | desc: "aligns text bottom and left", |
| 304 | + | rect: image.Rect(1, 1, 4, 4), |
| 305 | + | text: "a", |
| 306 | + | hAlign: align.HorizontalLeft, |
| 307 | + | vAlign: align.VerticalBottom, |
| 308 | + | want: image.Point{1, 3}, |
| 309 | + | }, |
| 310 | + | { |
| 311 | + | desc: "aligns text bottom and center", |
| 312 | + | rect: image.Rect(1, 1, 4, 4), |
| 313 | + | text: "a", |
| 314 | + | hAlign: align.HorizontalCenter, |
| 315 | + | vAlign: align.VerticalBottom, |
| 316 | + | want: image.Point{2, 3}, |
| 317 | + | }, |
| 318 | + | { |
| 319 | + | desc: "aligns text bottom and right", |
| 320 | + | rect: image.Rect(1, 1, 4, 4), |
| 321 | + | text: "a", |
| 322 | + | hAlign: align.HorizontalRight, |
| 323 | + | vAlign: align.VerticalBottom, |
| 324 | + | want: image.Point{3, 3}, |
| 325 | + | }, |
| 326 | + | { |
| 327 | + | desc: "aligns text that is too long, assumes trimming", |
| 328 | + | rect: image.Rect(1, 1, 4, 4), |
| 329 | + | text: "abcd", |
| 330 | + | hAlign: align.HorizontalCenter, |
| 331 | + | vAlign: align.VerticalTop, |
| 332 | + | want: image.Point{1, 1}, |
| 333 | + | }, |
| 334 | + | } |
| 335 | + | |
| 336 | + | for _, tc := range tests { |
| 337 | + | t.Run(tc.desc, func(t *testing.T) { |
| 338 | + | got, err := Text(tc.rect, tc.text, tc.hAlign, tc.vAlign) |
| 339 | + | if (err != nil) != tc.wantErr { |
| 340 | + | t.Errorf("Text => unexpected error: %v, wantErr: %v", err, tc.wantErr) |
| 341 | + | } |
| 342 | + | if err != nil { |
| 343 | + | return |
| 344 | + | } |
| 345 | + | |
| 346 | + | if diff := pretty.Compare(tc.want, got); diff != "" { |
| 347 | + | t.Errorf("Text => unexpected diff (-want, +got):\n%s", diff) |
| 348 | + | } |
| 349 | + | }) |
| 350 | + | } |
| 351 | + | } |
| 352 | + | |