1// Copyright 2019 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 15package segdisp 16 17import ( 18 "image" 19 "testing" 20 21 "github.com/kylelemons/godebug/pretty" 22 "github.com/mum4k/termdash/private/canvas" 23 "github.com/mum4k/termdash/private/canvas/braille" 24 "github.com/mum4k/termdash/private/canvas/braille/testbraille" 25) 26 27func TestRequired(t *testing.T) { 28 tests := []struct { 29 desc string 30 cellArea image.Rectangle 31 want image.Rectangle 32 wantErr bool 33 }{ 34 { 35 desc: "fails when area isn't wide enough", 36 cellArea: image.Rect(0, 0, MinCols-1, MinRows), 37 wantErr: true, 38 }, 39 { 40 desc: "fails when area isn't tall enough", 41 cellArea: image.Rect(0, 0, MinCols, MinRows-1), 42 wantErr: true, 43 }, 44 { 45 desc: "returns same area when no adjustment needed", 46 cellArea: image.Rect(0, 0, MinCols, MinRows), 47 want: image.Rect(0, 0, MinCols, MinRows), 48 }, 49 { 50 desc: "adjusts width to aspect ratio", 51 cellArea: image.Rect(0, 0, MinCols+100, MinRows), 52 want: image.Rect(0, 0, MinCols, MinRows), 53 }, 54 { 55 desc: "adjusts height to aspect ratio", 56 cellArea: image.Rect(0, 0, MinCols, MinRows+100), 57 want: image.Rect(0, 0, MinCols, MinRows), 58 }, 59 { 60 desc: "adjusts larger area to aspect ratio", 61 cellArea: image.Rect(0, 0, MinCols*2, MinRows*4), 62 want: image.Rect(0, 0, 12, 10), 63 }, 64 } 65 66 for _, tc := range tests { 67 t.Run(tc.desc, func(t *testing.T) { 68 got, err := Required(tc.cellArea) 69 if (err != nil) != tc.wantErr { 70 t.Errorf("Required => unexpected error: %v, wantErr: %v", err, tc.wantErr) 71 } 72 if err != nil { 73 return 74 } 75 76 if diff := pretty.Compare(tc.want, got); diff != "" { 77 t.Errorf("Required => unexpected diff (-want, +got):\n%s", diff) 78 } 79 }) 80 } 81} 82 83func TestToBraille(t *testing.T) { 84 tests := []struct { 85 desc string 86 cellArea image.Rectangle 87 wantBC *braille.Canvas 88 wantAr image.Rectangle 89 wantErr bool 90 }{ 91 { 92 desc: "fails when area isn't wide enough", 93 cellArea: image.Rect(0, 0, MinCols-1, MinRows), 94 wantErr: true, 95 }, 96 { 97 desc: "canvas creates braille with the desired aspect ratio", 98 cellArea: image.Rect(0, 0, MinCols, MinRows), 99 wantBC: testbraille.MustNew(image.Rect(0, 0, MinCols, MinRows)), 100 wantAr: image.Rect(0, 0, MinCols*braille.ColMult, MinRows*braille.RowMult), 101 }, 102 } 103 104 for _, tc := range tests { 105 t.Run(tc.desc, func(t *testing.T) { 106 cvs, err := canvas.New(tc.cellArea) 107 if err != nil { 108 t.Fatalf("canvas.New => unexpected error: %v", err) 109 } 110 111 gotBC, gotAr, err := ToBraille(cvs) 112 if (err != nil) != tc.wantErr { 113 t.Errorf("ToBraille => unexpected error: %v, wantErr: %v", err, tc.wantErr) 114 } 115 if err != nil { 116 return 117 } 118 119 if diff := pretty.Compare(tc.wantBC, gotBC); diff != "" { 120 t.Errorf("ToBraille => unexpected braille canvas, diff (-want, +got):\n%s", diff) 121 } 122 if diff := pretty.Compare(tc.wantAr, gotAr); diff != "" { 123 t.Errorf("ToBraille => unexpected area, diff (-want, +got):\n%s", diff) 124 } 125 }) 126 } 127} 128 129func TestSegmentSize(t *testing.T) { 130 tests := []struct { 131 desc string 132 ar image.Rectangle 133 want int 134 }{ 135 { 136 desc: "zero area", 137 ar: image.ZR, 138 want: 0, 139 }, 140 { 141 desc: "smallest segment size", 142 ar: image.Rect(0, 0, 15, 1), 143 want: 1, 144 }, 145 { 146 desc: "allows even size of two", 147 ar: image.Rect(0, 0, 22, 1), 148 want: 2, 149 }, 150 { 151 desc: "lands on even width, corrected to odd", 152 ar: image.Rect(0, 0, 44, 1), 153 want: 5, 154 }, 155 { 156 desc: "lands on odd width", 157 ar: image.Rect(0, 0, 55, 1), 158 want: 5, 159 }, 160 } 161 162 for _, tc := range tests { 163 t.Run(tc.desc, func(t *testing.T) { 164 got := SegmentSize(tc.ar) 165 if got != tc.want { 166 t.Errorf("SegmentSize => %d, want %d", got, tc.want) 167 } 168 }) 169 } 170} 171