|
| 1 | +package minimap |
| 2 | + |
| 3 | +import "testing" |
| 4 | + |
| 5 | +func TestActive(t *testing.T) { |
| 6 | + tests := []struct { |
| 7 | + name string |
| 8 | + m Model |
| 9 | + want bool |
| 10 | + }{ |
| 11 | + {"zero", Model{}, false}, |
| 12 | + {"no height", Model{TotalLines: 100, MapHeight: 0}, false}, |
| 13 | + {"no lines", Model{TotalLines: 0, MapHeight: 20}, false}, |
| 14 | + {"negative height", Model{TotalLines: 100, MapHeight: -5}, false}, |
| 15 | + {"live", Model{TotalLines: 100, MapHeight: 20}, true}, |
| 16 | + {"single line single row", Model{TotalLines: 1, MapHeight: 1}, true}, |
| 17 | + } |
| 18 | + for _, tt := range tests { |
| 19 | + t.Run(tt.name, func(t *testing.T) { |
| 20 | + if got := tt.m.Active(); got != tt.want { |
| 21 | + t.Fatalf("Active() = %v, want %v", got, tt.want) |
| 22 | + } |
| 23 | + }) |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +func TestDownsampled(t *testing.T) { |
| 28 | + if !(Model{TotalLines: 100, MapHeight: 20}).Downsampled() { |
| 29 | + t.Fatal("100 lines in 20 rows should downsample") |
| 30 | + } |
| 31 | + if (Model{TotalLines: 5, MapHeight: 10}).Downsampled() { |
| 32 | + t.Fatal("5 lines in 10 rows should not downsample") |
| 33 | + } |
| 34 | + if (Model{TotalLines: 10, MapHeight: 10}).Downsampled() { |
| 35 | + t.Fatal("equal lines and rows should not downsample") |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func TestLineRangeInactive(t *testing.T) { |
| 40 | + m := Model{TotalLines: 0, MapHeight: 10} |
| 41 | + start, end := m.LineRange(0) |
| 42 | + if start != 0 || end != 0 { |
| 43 | + t.Fatalf("inactive LineRange = (%d,%d), want (0,0)", start, end) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func TestLineRangeOutOfRange(t *testing.T) { |
| 48 | + m := Model{TotalLines: 100, MapHeight: 10} |
| 49 | + for _, row := range []int{-1, 10, 999} { |
| 50 | + start, end := m.LineRange(row) |
| 51 | + if start != 100 || end != 100 { |
| 52 | + t.Fatalf("LineRange(%d) = (%d,%d), want (100,100)", row, start, end) |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +// When the document is shorter than the column, each source line draws on its |
| 58 | +// own row (identity, top-aligned) and rows past the end draw blank. |
| 59 | +func TestLineRangeNotDownsampled(t *testing.T) { |
| 60 | + m := Model{TotalLines: 5, MapHeight: 10} |
| 61 | + for row := 0; row < 5; row++ { |
| 62 | + start, end := m.LineRange(row) |
| 63 | + if start != row || end != row+1 { |
| 64 | + t.Fatalf("LineRange(%d) = (%d,%d), want (%d,%d)", row, start, end, row, row+1) |
| 65 | + } |
| 66 | + } |
| 67 | + for row := 5; row < 10; row++ { |
| 68 | + start, end := m.LineRange(row) |
| 69 | + if start != end { |
| 70 | + t.Fatalf("LineRange(%d) = (%d,%d), want empty span", row, start, end) |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +// A downsampled map tiles every source line across the rows with no gaps and no |
| 76 | +// overlaps: row r's end is row r+1's start, the first row starts at 0, and the |
| 77 | +// last row ends at TotalLines. |
| 78 | +func TestLineRangeDownsampledTiling(t *testing.T) { |
| 79 | + m := Model{TotalLines: 100, MapHeight: 10} |
| 80 | + prevEnd := 0 |
| 81 | + for row := 0; row < m.MapHeight; row++ { |
| 82 | + start, end := m.LineRange(row) |
| 83 | + if start != prevEnd { |
| 84 | + t.Fatalf("row %d starts at %d, want %d (contiguous)", row, start, prevEnd) |
| 85 | + } |
| 86 | + if end < start { |
| 87 | + t.Fatalf("row %d span (%d,%d) is inverted", row, start, end) |
| 88 | + } |
| 89 | + prevEnd = end |
| 90 | + } |
| 91 | + if prevEnd != m.TotalLines { |
| 92 | + t.Fatalf("last row ends at %d, want %d", prevEnd, m.TotalLines) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +// Uneven division must still tile without gaps: 7 lines over 3 rows. |
| 97 | +func TestLineRangeUnevenTiling(t *testing.T) { |
| 98 | + m := Model{TotalLines: 7, MapHeight: 3} |
| 99 | + prevEnd := 0 |
| 100 | + for row := 0; row < m.MapHeight; row++ { |
| 101 | + start, end := m.LineRange(row) |
| 102 | + if start != prevEnd { |
| 103 | + t.Fatalf("row %d starts at %d, want %d", row, start, prevEnd) |
| 104 | + } |
| 105 | + prevEnd = end |
| 106 | + } |
| 107 | + if prevEnd != 7 { |
| 108 | + t.Fatalf("tiling covered %d lines, want 7", prevEnd) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +// RowForLine is the inverse of LineRange: the start line of a row maps back to |
| 113 | +// that row. |
| 114 | +func TestRowForLineInverseOfLineRange(t *testing.T) { |
| 115 | + for _, m := range []Model{ |
| 116 | + {TotalLines: 100, MapHeight: 10}, |
| 117 | + {TotalLines: 7, MapHeight: 3}, |
| 118 | + {TotalLines: 10000, MapHeight: 20}, |
| 119 | + } { |
| 120 | + for row := 0; row < m.MapHeight; row++ { |
| 121 | + start, end := m.LineRange(row) |
| 122 | + if start == end { |
| 123 | + continue // blank row on a short doc has no start line |
| 124 | + } |
| 125 | + if got := m.RowForLine(start); got != row { |
| 126 | + t.Fatalf("T=%d M=%d: RowForLine(start=%d) = %d, want %d", |
| 127 | + m.TotalLines, m.MapHeight, start, got, row) |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +func TestRowForLineClamps(t *testing.T) { |
| 134 | + m := Model{TotalLines: 100, MapHeight: 10} |
| 135 | + if got := m.RowForLine(-50); got != 0 { |
| 136 | + t.Fatalf("RowForLine(-50) = %d, want 0", got) |
| 137 | + } |
| 138 | + if got := m.RowForLine(9999); got != m.MapHeight-1 { |
| 139 | + t.Fatalf("RowForLine(9999) = %d, want %d", got, m.MapHeight-1) |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +func TestRowForLineInactive(t *testing.T) { |
| 144 | + if got := (Model{}).RowForLine(5); got != 0 { |
| 145 | + t.Fatalf("inactive RowForLine = %d, want 0", got) |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +func TestViewportWindowInactive(t *testing.T) { |
| 150 | + start, end := (Model{}).ViewportWindow() |
| 151 | + if start != 0 || end != 0 { |
| 152 | + t.Fatalf("inactive ViewportWindow = (%d,%d), want (0,0)", start, end) |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +func TestViewportWindowZeroHeight(t *testing.T) { |
| 157 | + m := Model{TotalLines: 100, MapHeight: 10, ViewportTop: 5, ViewportHeight: 0} |
| 158 | + start, end := m.ViewportWindow() |
| 159 | + if start != 0 || end != 0 { |
| 160 | + t.Fatalf("zero-height viewport = (%d,%d), want (0,0)", start, end) |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +// On a document far taller than the column the band must never vanish: it is at |
| 165 | +// least one row tall even when the viewport spans a fraction of a single row. |
| 166 | +func TestViewportWindowBandNeverVanishes(t *testing.T) { |
| 167 | + m := Model{TotalLines: 10000, MapHeight: 20, ViewportTop: 0, ViewportHeight: 10} |
| 168 | + start, end := m.ViewportWindow() |
| 169 | + if end-start < 1 { |
| 170 | + t.Fatalf("band = [%d,%d), want at least one row", start, end) |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +// A viewport covering the whole document should light the whole column. |
| 175 | +func TestViewportWindowFullDocument(t *testing.T) { |
| 176 | + m := Model{TotalLines: 50, MapHeight: 10, ViewportTop: 0, ViewportHeight: 50} |
| 177 | + start, end := m.ViewportWindow() |
| 178 | + if start != 0 || end != m.MapHeight { |
| 179 | + t.Fatalf("full-doc window = [%d,%d), want [0,%d)", start, end, m.MapHeight) |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +// The band tracks the viewport down the document and stays within the column. |
| 184 | +func TestViewportWindowTracksAndClamps(t *testing.T) { |
| 185 | + m := Model{TotalLines: 100, MapHeight: 10, ViewportTop: 90, ViewportHeight: 20} |
| 186 | + start, end := m.ViewportWindow() |
| 187 | + if start < 0 || end > m.MapHeight { |
| 188 | + t.Fatalf("window [%d,%d) escapes column [0,%d]", start, end, m.MapHeight) |
| 189 | + } |
| 190 | + if end <= start { |
| 191 | + t.Fatalf("window [%d,%d) is empty", start, end) |
| 192 | + } |
| 193 | + // Viewport at the bottom should light the last row. |
| 194 | + if end != m.MapHeight { |
| 195 | + t.Fatalf("bottom viewport window ends at %d, want %d", end, m.MapHeight) |
| 196 | + } |
| 197 | +} |
| 198 | + |
| 199 | +func TestLineForRowRoundTrip(t *testing.T) { |
| 200 | + m := Model{TotalLines: 100, MapHeight: 10} |
| 201 | + for row := 0; row < m.MapHeight; row++ { |
| 202 | + line := m.LineForRow(row) |
| 203 | + if m.RowForLine(line) != row { |
| 204 | + t.Fatalf("LineForRow(%d)=%d does not round-trip via RowForLine", row, line) |
| 205 | + } |
| 206 | + } |
| 207 | +} |
| 208 | + |
| 209 | +func TestLineForRowClampsAndStaysInDocument(t *testing.T) { |
| 210 | + m := Model{TotalLines: 100, MapHeight: 10} |
| 211 | + for _, row := range []int{-5, 0, 9, 50} { |
| 212 | + line := m.LineForRow(row) |
| 213 | + if line < 0 || line > m.TotalLines-1 { |
| 214 | + t.Fatalf("LineForRow(%d) = %d, out of document [0,%d]", row, line, m.TotalLines-1) |
| 215 | + } |
| 216 | + } |
| 217 | +} |
| 218 | + |
| 219 | +func TestLineForRowInactive(t *testing.T) { |
| 220 | + if got := (Model{}).LineForRow(3); got != 0 { |
| 221 | + t.Fatalf("inactive LineForRow = %d, want 0", got) |
| 222 | + } |
| 223 | +} |
| 224 | + |
| 225 | +// A single-line, single-row map is the smallest active shape; every method must |
| 226 | +// stay in bounds. |
| 227 | +func TestSingletonMap(t *testing.T) { |
| 228 | + m := Model{TotalLines: 1, MapHeight: 1, ViewportTop: 0, ViewportHeight: 1} |
| 229 | + start, end := m.LineRange(0) |
| 230 | + if start != 0 || end != 1 { |
| 231 | + t.Fatalf("LineRange(0) = (%d,%d), want (0,1)", start, end) |
| 232 | + } |
| 233 | + if got := m.RowForLine(0); got != 0 { |
| 234 | + t.Fatalf("RowForLine(0) = %d, want 0", got) |
| 235 | + } |
| 236 | + ws, we := m.ViewportWindow() |
| 237 | + if ws != 0 || we != 1 { |
| 238 | + t.Fatalf("ViewportWindow = [%d,%d), want [0,1)", ws, we) |
| 239 | + } |
| 240 | + if got := m.LineForRow(0); got != 0 { |
| 241 | + t.Fatalf("LineForRow(0) = %d, want 0", got) |
| 242 | + } |
| 243 | +} |
0 commit comments