.githooks/
compiler/
kernel/
lib/
examples/
std/
arch/
bits/
char/
collections/
fs/
hash/
lang/
alloc/
ast/
gen/
il/
layout/
tests.rad
25.8 KiB
text.rad
26.8 KiB
lower/
manifest/
module/
parser/
resolver/
scanner/
sexpr/
alloc.rad
1.2 KiB
ast.rad
36.0 KiB
gen.rad
513 B
il.rad
48.5 KiB
layout.rad
6.9 KiB
lower.rad
487.6 KiB
manifest.rad
22.5 KiB
module.rad
17.8 KiB
package.rad
1.5 KiB
parser.rad
114.1 KiB
pretty.rad
103.6 KiB
resolver.rad
508.1 KiB
scanner.rad
19.3 KiB
sexpr.rad
7.1 KiB
strings.rad
3.3 KiB
types.rad
620 B
mem/
sys/
arch.rad
68 B
bits.rad
1.6 KiB
char.rad
1.5 KiB
collections.rad
39 B
fmt.rad
11.6 KiB
fs.rad
11.0 KiB
hash.rad
41 B
intrinsics.rad
467 B
io.rad
1.7 KiB
lang.rad
335 B
mem.rad
12.0 KiB
mem.ras
919 B
process.rad
6.2 KiB
std.manifest
4.9 KiB
std.rad
349 B
sys.rad
1.0 KiB
testing.rad
2.4 KiB
tests.rad
26.6 KiB
vec.rad
3.2 KiB
scripts/
seed/
sublime/
test/
vim/
.gitignore
333 B
.gitsigners
224 B
AGENTS.md
8.3 KiB
CONTRIBUTING
2.1 KiB
LICENSE
1.1 KiB
Makefile
2.5 KiB
README
3.4 KiB
STYLE
3.2 KiB
std.lib
2.1 KiB
std.lib.test
1.1 KiB
lib/std/lang/layout/tests.rad
raw
| 1 | //! Tests for the text renderer. |
| 2 | |
| 3 | use std::io; |
| 4 | use std::lang::sexpr; |
| 5 | use std::mem; |
| 6 | use std::mem::heap; |
| 7 | use std::testing; |
| 8 | |
| 9 | use std::lang::layout; |
| 10 | use std::lang::layout::Child; |
| 11 | use std::lang::layout::Gap; |
| 12 | use std::lang::layout::Item; |
| 13 | use std::lang::layout::Layout; |
| 14 | use std::lang::layout::Placement; |
| 15 | use std::lang::layout::Role; |
| 16 | |
| 17 | /// Placement on the same line with no gap. |
| 18 | fn none() -> Placement { |
| 19 | return Placement::Inline(Gap::None); |
| 20 | } |
| 21 | |
| 22 | /// Placement on the same line after a space. |
| 23 | fn space() -> Placement { |
| 24 | return Placement::Inline(Gap::Space); |
| 25 | } |
| 26 | |
| 27 | /// Soft placement with no gap, on the same line when flat. |
| 28 | fn soft() -> Placement { |
| 29 | return Placement::SoftLine(Gap::None); |
| 30 | } |
| 31 | |
| 32 | /// Soft placement with a space, on the same line when flat. |
| 33 | fn softSpace() -> Placement { |
| 34 | return Placement::SoftLine(Gap::Space); |
| 35 | } |
| 36 | |
| 37 | /// Soft indented placement with no gap. |
| 38 | fn softIndent() -> Placement { |
| 39 | return Placement::SoftIndent(Gap::None); |
| 40 | } |
| 41 | |
| 42 | /// Allocate a text layout with the given role. |
| 43 | fn leaf(a: &heap::Heap, s: *[u8], role: Role) -> *Layout { |
| 44 | return try! layout::new(a, nil, Item::Text { text: s, role }); |
| 45 | } |
| 46 | |
| 47 | /// Allocate a name text layout. |
| 48 | fn text(a: &heap::Heap, s: *[u8]) -> *Layout { |
| 49 | return leaf(a, s, Role::Name); |
| 50 | } |
| 51 | |
| 52 | /// Allocate a comment text layout. |
| 53 | fn comment(a: &heap::Heap, s: *[u8]) -> *Layout { |
| 54 | return leaf(a, s, Role::Comment); |
| 55 | } |
| 56 | |
| 57 | /// Allocate a text layout that is present only in a broken box. |
| 58 | fn brokenText(a: &heap::Heap, s: *[u8]) -> *Layout { |
| 59 | let item = Item::BrokenText { text: s, role: Role::Punct }; |
| 60 | return try! layout::new(a, nil, item); |
| 61 | } |
| 62 | |
| 63 | /// Allocate a blank line layout. |
| 64 | fn blank(a: &heap::Heap) -> *Layout { |
| 65 | return try! layout::new(a, nil, Item::Blank); |
| 66 | } |
| 67 | |
| 68 | /// Allocate a choice between the given alternatives. |
| 69 | fn choice(a: &heap::Heap, alts: &[*Layout]) -> *Layout { |
| 70 | let mut copy: *mut [*Layout] = &mut []; |
| 71 | for alt in alts { |
| 72 | try! copy.append(alt, a); |
| 73 | } |
| 74 | return try! layout::new(a, nil, Item::Choice(copy)); |
| 75 | } |
| 76 | |
| 77 | /// Allocate a box layout that is always broken. |
| 78 | fn broken(a: &heap::Heap, inner: *Layout) -> *Layout { |
| 79 | return try! layout::new(a, nil, Item::Broken(inner)); |
| 80 | } |
| 81 | |
| 82 | /// Allocate a box layout with a copy of the given children. |
| 83 | fn box(a: &heap::Heap, kids: &[Child]) -> *Layout { |
| 84 | let mut copy: *mut [Child] = &mut []; |
| 85 | for kid in kids { |
| 86 | try! copy.append(kid, a); |
| 87 | } |
| 88 | return try! layout::new(a, nil, Item::Box(copy)); |
| 89 | } |
| 90 | |
| 91 | /// Build a child. |
| 92 | fn at(placement: Placement, layout: *Layout) -> Child { |
| 93 | return Child { placement, layout }; |
| 94 | } |
| 95 | |
| 96 | /// Render the layout and check the output text. |
| 97 | unsafe fn expectRender( |
| 98 | root: *Layout, width: u32, indent: u32, expected: *[u8] |
| 99 | ) throws (testing::TestError) { |
| 100 | static OUT: [u8; 512] = [0; 512]; |
| 101 | let mut buf = sexpr::Buffer { buf: &mut OUT[..], pos: 0 }; |
| 102 | { |
| 103 | let sink: &mut opaque sexpr::Output = &mut buf; |
| 104 | let options = layout::text::Options { width, indent }; |
| 105 | layout::text::render(root, options, sink); |
| 106 | } |
| 107 | if not mem::eq(&OUT[.. buf.pos], expected) { |
| 108 | io::print("--- expected\n"); |
| 109 | io::print(expected); |
| 110 | io::print("--- actual\n"); |
| 111 | io::print(&OUT[.. buf.pos]); |
| 112 | io::print("---\n"); |
| 113 | throw testing::TestError::Failed; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | /// Build `call(aaaa, bbbb, cccc)` with soft breaks, followed by `;`. |
| 118 | fn call(a: &heap::Heap, tail: *[u8]) -> *Layout { |
| 119 | let args = box( |
| 120 | a, |
| 121 | &[ |
| 122 | at(none(), text(a, "aaaa,")), |
| 123 | at(softSpace(), text(a, "bbbb,")), |
| 124 | at(softSpace(), text(a, "cccc")), |
| 125 | ] |
| 126 | ); |
| 127 | let callee = box( |
| 128 | a, |
| 129 | &[ |
| 130 | at(none(), text(a, "call(")), |
| 131 | at(softIndent(), args), |
| 132 | at(soft(), text(a, ")")), |
| 133 | ] |
| 134 | ); |
| 135 | return box(a, &[at(none(), callee), at(none(), text(a, tail))]); |
| 136 | } |
| 137 | |
| 138 | /// Test that a box that fits stays flat. |
| 139 | @test unsafe fn testFlatWhenFits() throws (testing::TestError) { |
| 140 | static STORAGE: [u8; 4096] = [0; 4096]; |
| 141 | let arena = heap::over(&mut STORAGE[..]); |
| 142 | let a: &heap::Heap = arena; |
| 143 | let root = call(a, ""); |
| 144 | |
| 145 | try expectRender(root, 80, 4, "call(aaaa, bbbb, cccc)\n"); |
| 146 | } |
| 147 | |
| 148 | /// Test the exact-fit boundary, with and without following content. |
| 149 | @test unsafe fn testExactFitBoundary() throws (testing::TestError) { |
| 150 | static STORAGE: [u8; 4096] = [0; 4096]; |
| 151 | let arena = heap::over(&mut STORAGE[..]); |
| 152 | let a: &heap::Heap = arena; |
| 153 | let bare = call(a, ""); |
| 154 | let semi = call(a, ";"); |
| 155 | |
| 156 | try expectRender(bare, 22, 4, "call(aaaa, bbbb, cccc)\n"); |
| 157 | try expectRender(bare, 21, 4, "call(\n aaaa, bbbb, cccc\n)\n"); |
| 158 | try expectRender(semi, 23, 4, "call(aaaa, bbbb, cccc);\n"); |
| 159 | try expectRender(semi, 22, 4, "call(\n aaaa, bbbb, cccc\n);\n"); |
| 160 | } |
| 161 | |
| 162 | /// Test that a broken box can contain flat boxes and broken ones. |
| 163 | @test unsafe fn testNestedBoxesDecideIndependently() |
| 164 | throws (testing::TestError) |
| 165 | { |
| 166 | static STORAGE: [u8; 4096] = [0; 4096]; |
| 167 | let arena = heap::over(&mut STORAGE[..]); |
| 168 | let a: &heap::Heap = arena; |
| 169 | let root = call(a, ""); |
| 170 | |
| 171 | try expectRender(root, 20, 4, "call(\n aaaa, bbbb, cccc\n)\n"); |
| 172 | try expectRender(root, 15, 4, "call(\n aaaa,\n bbbb,\n cccc\n)\n"); |
| 173 | |
| 174 | let inner = box( |
| 175 | a, |
| 176 | &[ |
| 177 | at(none(), text(a, "bb")), |
| 178 | at(softSpace(), text(a, "cc")), |
| 179 | ] |
| 180 | ); |
| 181 | let outer = box( |
| 182 | a, |
| 183 | &[ |
| 184 | at(none(), text(a, "aaaaaaaa")), |
| 185 | at(softSpace(), inner), |
| 186 | at(softSpace(), text(a, "dddddddd")), |
| 187 | ] |
| 188 | ); |
| 189 | try expectRender(outer, 23, 4, "aaaaaaaa bb cc dddddddd\n"); |
| 190 | try expectRender(outer, 20, 4, "aaaaaaaa\nbb cc\ndddddddd\n"); |
| 191 | try expectRender(outer, 4, 4, "aaaaaaaa\nbb\ncc\ndddddddd\n"); |
| 192 | } |
| 193 | |
| 194 | /// Test indentation levels for `Indent` and `SoftIndent` children. |
| 195 | @test unsafe fn testIndentLevels() throws (testing::TestError) { |
| 196 | static STORAGE: [u8; 4096] = [0; 4096]; |
| 197 | let arena = heap::over(&mut STORAGE[..]); |
| 198 | let a: &heap::Heap = arena; |
| 199 | let deep = box( |
| 200 | a, |
| 201 | &[ |
| 202 | at(none(), text(a, "y")), |
| 203 | at(Placement::Indent, text(a, "z")), |
| 204 | ] |
| 205 | ); |
| 206 | let root = box( |
| 207 | a, |
| 208 | &[ |
| 209 | at(none(), text(a, "x")), |
| 210 | at(Placement::Indent, deep), |
| 211 | at(Placement::Line, text(a, "w")), |
| 212 | ] |
| 213 | ); |
| 214 | try expectRender(root, 80, 4, "x\n y\n z\nw\n"); |
| 215 | try expectRender(root, 80, 2, "x\n y\n z\nw\n"); |
| 216 | |
| 217 | let flat = box( |
| 218 | a, |
| 219 | &[ |
| 220 | at(none(), text(a, "ab")), |
| 221 | at(softIndent(), text(a, "cd")), |
| 222 | at(soft(), text(a, "ef")), |
| 223 | ] |
| 224 | ); |
| 225 | try expectRender(flat, 6, 4, "abcdef\n"); |
| 226 | try expectRender(flat, 5, 4, "ab\n cd\nef\n"); |
| 227 | } |
| 228 | |
| 229 | /// Test lazy indentation, blank lines, and the final newline. |
| 230 | @test unsafe fn testBlankLinesAndWhitespace() throws (testing::TestError) { |
| 231 | static STORAGE: [u8; 4096] = [0; 4096]; |
| 232 | let arena = heap::over(&mut STORAGE[..]); |
| 233 | let a: &heap::Heap = arena; |
| 234 | |
| 235 | let gap = box( |
| 236 | a, |
| 237 | &[ |
| 238 | at(none(), text(a, "a")), |
| 239 | at(Placement::Line, blank(a)), |
| 240 | at(Placement::Line, text(a, "b")), |
| 241 | ] |
| 242 | ); |
| 243 | try expectRender(gap, 80, 4, "a\n\nb\n"); |
| 244 | |
| 245 | let indented = box( |
| 246 | a, |
| 247 | &[ |
| 248 | at(none(), text(a, "a")), |
| 249 | at(Placement::Indent, blank(a)), |
| 250 | at(Placement::Indent, text(a, "b")), |
| 251 | ] |
| 252 | ); |
| 253 | try expectRender(indented, 80, 4, "a\n\n b\n"); |
| 254 | |
| 255 | let trailing = box( |
| 256 | a, |
| 257 | &[ |
| 258 | at(none(), text(a, "a")), |
| 259 | at(Placement::Line, blank(a)), |
| 260 | at(Placement::Line, blank(a)), |
| 261 | ] |
| 262 | ); |
| 263 | try expectRender(trailing, 80, 4, "a\n"); |
| 264 | |
| 265 | let leading = box( |
| 266 | a, |
| 267 | &[ |
| 268 | at(Placement::Line, blank(a)), |
| 269 | at(space(), text(a, "a")), |
| 270 | ] |
| 271 | ); |
| 272 | try expectRender(leading, 80, 4, "a\n"); |
| 273 | |
| 274 | let dropped = box( |
| 275 | a, |
| 276 | &[ |
| 277 | at(none(), text(a, "a")), |
| 278 | at(Placement::Line, box(a, &[at(space(), text(a, "b"))])), |
| 279 | ] |
| 280 | ); |
| 281 | try expectRender(dropped, 80, 4, "a\nb\n"); |
| 282 | |
| 283 | try expectRender(box(a, &[]), 80, 4, ""); |
| 284 | } |
| 285 | |
| 286 | /// Test that a comment forces the content after it onto a new line. |
| 287 | @test unsafe fn testCommentForcesBreak() throws (testing::TestError) { |
| 288 | static STORAGE: [u8; 4096] = [0; 4096]; |
| 289 | let arena = heap::over(&mut STORAGE[..]); |
| 290 | let a: &heap::Heap = arena; |
| 291 | |
| 292 | let after = box( |
| 293 | a, |
| 294 | &[ |
| 295 | at(none(), text(a, "x")), |
| 296 | at(space(), comment(a, "// c")), |
| 297 | at(space(), text(a, "y")), |
| 298 | ] |
| 299 | ); |
| 300 | try expectRender(after, 80, 4, "x // c\ny\n"); |
| 301 | |
| 302 | let args = box( |
| 303 | a, |
| 304 | &[ |
| 305 | at(none(), text(a, "a,")), |
| 306 | at(softSpace(), comment(a, "// c")), |
| 307 | at(softSpace(), text(a, "b")), |
| 308 | ] |
| 309 | ); |
| 310 | let call = box( |
| 311 | a, |
| 312 | &[ |
| 313 | at(none(), text(a, "f(")), |
| 314 | at(softIndent(), args), |
| 315 | at(soft(), text(a, ")")), |
| 316 | ] |
| 317 | ); |
| 318 | try expectRender(call, 80, 4, "f(\n a,\n // c\n b\n)\n"); |
| 319 | |
| 320 | let indented = box( |
| 321 | a, |
| 322 | &[ |
| 323 | at(none(), text(a, "{")), |
| 324 | at( |
| 325 | Placement::Indent, |
| 326 | box( |
| 327 | a, |
| 328 | &[ |
| 329 | at(none(), comment(a, "// c")), |
| 330 | at(space(), text(a, "z")), |
| 331 | ] |
| 332 | ) |
| 333 | ), |
| 334 | ] |
| 335 | ); |
| 336 | try expectRender(indented, 80, 4, "{\n // c\n z\n"); |
| 337 | } |
| 338 | |
| 339 | /// Test that a trailing comment before a line break does not break the box. |
| 340 | @test unsafe fn testTrailingCommentFits() throws (testing::TestError) { |
| 341 | static STORAGE: [u8; 4096] = [0; 4096]; |
| 342 | let arena = heap::over(&mut STORAGE[..]); |
| 343 | let a: &heap::Heap = arena; |
| 344 | let inner = box( |
| 345 | a, |
| 346 | &[ |
| 347 | at(none(), text(a, "a")), |
| 348 | at(softSpace(), text(a, "b")), |
| 349 | at(space(), comment(a, "// c")), |
| 350 | ] |
| 351 | ); |
| 352 | let root = box( |
| 353 | a, |
| 354 | &[ |
| 355 | at(none(), inner), |
| 356 | at(Placement::Line, text(a, "d")), |
| 357 | ] |
| 358 | ); |
| 359 | try expectRender(root, 80, 4, "a b // c\nd\n"); |
| 360 | } |
| 361 | |
| 362 | /// Test that a comment takes no width, even when it is longer than the width. |
| 363 | @test unsafe fn testCommentWidth() throws (testing::TestError) { |
| 364 | static STORAGE: [u8; 4096] = [0; 4096]; |
| 365 | let arena = heap::over(&mut STORAGE[..]); |
| 366 | let a: &heap::Heap = arena; |
| 367 | let long = "// a comment past the width"; |
| 368 | |
| 369 | let trailing = box( |
| 370 | a, |
| 371 | &[ |
| 372 | at(none(), call(a, ";")), |
| 373 | at(space(), comment(a, long)), |
| 374 | ] |
| 375 | ); |
| 376 | try expectRender( |
| 377 | trailing, 24, 4, "call(aaaa, bbbb, cccc); // a comment past the width\n" |
| 378 | ); |
| 379 | |
| 380 | let alone = box(a, &[at(none(), comment(a, long))]); |
| 381 | try expectRender(alone, 24, 4, "// a comment past the width\n"); |
| 382 | |
| 383 | // Content after the comment on the same line still cannot fit. |
| 384 | let args = box( |
| 385 | a, |
| 386 | &[ |
| 387 | at(none(), text(a, "a,")), |
| 388 | at(space(), comment(a, long)), |
| 389 | at(softSpace(), text(a, "b")), |
| 390 | ] |
| 391 | ); |
| 392 | let after = box( |
| 393 | a, |
| 394 | &[ |
| 395 | at(none(), text(a, "f(")), |
| 396 | at(softIndent(), args), |
| 397 | at(soft(), text(a, ")")), |
| 398 | ] |
| 399 | ); |
| 400 | try expectRender( |
| 401 | after, 24, 4, "f(\n a, // a comment past the width\n b\n)\n" |
| 402 | ); |
| 403 | } |
| 404 | |
| 405 | /// Build `if cond {` with a soft brace followed by a body. |
| 406 | fn ifStmt(a: &heap::Heap) -> *Layout { |
| 407 | let header = box( |
| 408 | a, |
| 409 | &[ |
| 410 | at(none(), text(a, "if")), |
| 411 | at(space(), text(a, "cond")), |
| 412 | at(softSpace(), text(a, "{")), |
| 413 | ] |
| 414 | ); |
| 415 | return box( |
| 416 | a, |
| 417 | &[ |
| 418 | at(none(), header), |
| 419 | at(Placement::Indent, text(a, "x;")), |
| 420 | at(Placement::Line, text(a, "}")), |
| 421 | ] |
| 422 | ); |
| 423 | } |
| 424 | |
| 425 | /// Test that the brace stays on the header line when it fits. |
| 426 | @test unsafe fn testIfBraceBehavior() throws (testing::TestError) { |
| 427 | static STORAGE: [u8; 4096] = [0; 4096]; |
| 428 | let arena = heap::over(&mut STORAGE[..]); |
| 429 | let a: &heap::Heap = arena; |
| 430 | let root = ifStmt(a); |
| 431 | |
| 432 | try expectRender(root, 9, 4, "if cond {\n x;\n}\n"); |
| 433 | try expectRender(root, 8, 4, "if cond\n{\n x;\n}\n"); |
| 434 | } |
| 435 | |
| 436 | /// Test that broken-only text appears only in a broken box, and counts for |
| 437 | /// the fit of a box that is flat. |
| 438 | @test unsafe fn testBrokenText() throws (testing::TestError) { |
| 439 | static STORAGE: [u8; 4096] = [0; 4096]; |
| 440 | let arena = heap::over(&mut STORAGE[..]); |
| 441 | let a: &heap::Heap = arena; |
| 442 | let list = box( |
| 443 | a, |
| 444 | &[ |
| 445 | at(none(), text(a, "ab")), |
| 446 | at(softSpace(), text(a, "cd")), |
| 447 | at(none(), brokenText(a, ",")), |
| 448 | ] |
| 449 | ); |
| 450 | try expectRender(list, 5, 4, "ab cd\n"); |
| 451 | try expectRender(list, 4, 4, "ab\ncd,\n"); |
| 452 | |
| 453 | // A flat box nested in a broken one omits its own broken text. |
| 454 | let inner = box( |
| 455 | a, |
| 456 | &[ |
| 457 | at(none(), text(a, "x")), |
| 458 | at(softSpace(), text(a, "y")), |
| 459 | at(none(), brokenText(a, ";")), |
| 460 | ] |
| 461 | ); |
| 462 | let outer = box( |
| 463 | a, |
| 464 | &[ |
| 465 | at(none(), text(a, "aaaa")), |
| 466 | at(softSpace(), inner), |
| 467 | at(none(), brokenText(a, ",")), |
| 468 | ] |
| 469 | ); |
| 470 | try expectRender(outer, 8, 4, "aaaa x y\n"); |
| 471 | try expectRender(outer, 7, 4, "aaaa\nx y,\n"); |
| 472 | try expectRender(outer, 2, 4, "aaaa\nx\ny;,\n"); |
| 473 | } |
| 474 | |
| 475 | /// Test that a box fits when the content after it can break before the end |
| 476 | /// of the line, even if that content does not fit flat. |
| 477 | @test unsafe fn testLaterBreaksCount() throws (testing::TestError) { |
| 478 | static STORAGE: [u8; 4096] = [0; 4096]; |
| 479 | let arena = heap::over(&mut STORAGE[..]); |
| 480 | let a: &heap::Heap = arena; |
| 481 | let chain = box( |
| 482 | a, |
| 483 | &[ |
| 484 | at(none(), text(a, "aa +")), |
| 485 | at(softSpace(), text(a, "bb")), |
| 486 | ] |
| 487 | ); |
| 488 | let root = box(a, &[at(none(), chain), at(space(), call(a, ";"))]); |
| 489 | |
| 490 | try expectRender(root, 40, 4, "aa + bb call(aaaa, bbbb, cccc);\n"); |
| 491 | try expectRender(root, 20, 4, "aa + bb call(\n aaaa, bbbb, cccc\n);\n"); |
| 492 | try expectRender( |
| 493 | root, 12, 4, "aa +\nbb call(\n aaaa,\n bbbb,\n cccc\n);\n" |
| 494 | ); |
| 495 | } |
| 496 | |
| 497 | /// Build `lhs ==` followed by `rhs` on the same line if it fits, or on the |
| 498 | /// next line, then `tail`. |
| 499 | fn pair(a: &heap::Heap, tail: *[u8]) -> *Layout { |
| 500 | let same = box(a, &[at(space(), text(a, "rhs"))]); |
| 501 | let next = box(a, &[at(Placement::Indent, text(a, "rhs"))]); |
| 502 | return box( |
| 503 | a, |
| 504 | &[ |
| 505 | at(none(), text(a, "lhs ==")), |
| 506 | at(none(), choice(a, &[same, next])), |
| 507 | at(none(), text(a, tail)), |
| 508 | ] |
| 509 | ); |
| 510 | } |
| 511 | |
| 512 | /// Test that a choice takes its first alternative that fits, else its last. |
| 513 | @test unsafe fn testChoice() throws (testing::TestError) { |
| 514 | static STORAGE: [u8; 4096] = [0; 4096]; |
| 515 | let arena = heap::over(&mut STORAGE[..]); |
| 516 | let a: &heap::Heap = arena; |
| 517 | |
| 518 | try expectRender(pair(a, ""), 10, 4, "lhs == rhs\n"); |
| 519 | try expectRender(pair(a, ""), 9, 4, "lhs ==\n rhs\n"); |
| 520 | try expectRender(pair(a, ";"), 11, 4, "lhs == rhs;\n"); |
| 521 | try expectRender(pair(a, ";"), 10, 4, "lhs ==\n rhs;\n"); |
| 522 | |
| 523 | // Content before a choice can break at its last alternative. |
| 524 | let chain = box( |
| 525 | a, |
| 526 | &[ |
| 527 | at(none(), text(a, "aaaa,")), |
| 528 | at(softSpace(), text(a, "bbbb")), |
| 529 | ] |
| 530 | ); |
| 531 | let same = box(a, &[at(space(), text(a, "rhs"))]); |
| 532 | let next = box(a, &[at(Placement::Indent, text(a, "rhs"))]); |
| 533 | let root = box( |
| 534 | a, |
| 535 | &[ |
| 536 | at(none(), chain), |
| 537 | at(space(), text(a, "==")), |
| 538 | at(none(), choice(a, &[same, next])), |
| 539 | ] |
| 540 | ); |
| 541 | try expectRender(root, 13, 4, "aaaa, bbbb ==\n rhs\n"); |
| 542 | |
| 543 | // An enclosing box measures a choice by its first alternative. |
| 544 | let list = box( |
| 545 | a, |
| 546 | &[ |
| 547 | at(none(), text(a, "f(")), |
| 548 | at(softIndent(), pair(a, "")), |
| 549 | at(soft(), text(a, ")")), |
| 550 | ] |
| 551 | ); |
| 552 | try expectRender(list, 13, 4, "f(lhs == rhs)\n"); |
| 553 | try expectRender(list, 12, 4, "f(\n lhs ==\n rhs\n)\n"); |
| 554 | } |
| 555 | |
| 556 | /// Test that a broken box breaks even when it fits, and that a choice |
| 557 | /// measures it up to its first break. |
| 558 | @test unsafe fn testBroken() throws (testing::TestError) { |
| 559 | static STORAGE: [u8; 4096] = [0; 4096]; |
| 560 | let arena = heap::over(&mut STORAGE[..]); |
| 561 | let a: &heap::Heap = arena; |
| 562 | let fields = box( |
| 563 | a, |
| 564 | &[ |
| 565 | at(none(), text(a, "a: 1,")), |
| 566 | at(softSpace(), text(a, "b: 2")), |
| 567 | ] |
| 568 | ); |
| 569 | let rec = box( |
| 570 | a, |
| 571 | &[ |
| 572 | at(none(), text(a, "R {")), |
| 573 | at(Placement::SoftIndent(Gap::Space), fields), |
| 574 | at(Placement::SoftLine(Gap::Space), text(a, "}")), |
| 575 | ] |
| 576 | ); |
| 577 | try expectRender(broken(a, rec), 80, 4, "R {\n a: 1, b: 2\n}\n"); |
| 578 | |
| 579 | let flat = box( |
| 580 | a, |
| 581 | &[ |
| 582 | at(none(), text(a, "f(x, ")), |
| 583 | at(none(), rec), |
| 584 | at(none(), text(a, ")")), |
| 585 | ] |
| 586 | ); |
| 587 | let hug = box( |
| 588 | a, |
| 589 | &[ |
| 590 | at(none(), text(a, "f(x, ")), |
| 591 | at(none(), broken(a, rec)), |
| 592 | at(none(), text(a, ")")), |
| 593 | ] |
| 594 | ); |
| 595 | let root = choice(a, &[flat, hug, text(a, "vertical")]); |
| 596 | try expectRender(root, 22, 4, "f(x, R { a: 1, b: 2 })\n"); |
| 597 | try expectRender(root, 14, 4, "f(x, R {\n a: 1, b: 2\n})\n"); |
| 598 | try expectRender(root, 12, 4, "f(x, R {\n a: 1,\n b: 2\n})\n"); |
| 599 | try expectRender(root, 7, 4, "vertical\n"); |
| 600 | } |
| 601 | |
| 602 | /// Test that a choice measures an alternative that starts on a new line on |
| 603 | /// that line. |
| 604 | @test unsafe fn testChoiceOnNewLine() throws (testing::TestError) { |
| 605 | static STORAGE: [u8; 4096] = [0; 4096]; |
| 606 | let arena = heap::over(&mut STORAGE[..]); |
| 607 | let a: &heap::Heap = arena; |
| 608 | let value = box( |
| 609 | a, |
| 610 | &[ |
| 611 | at(none(), text(a, "aaaa +")), |
| 612 | at(Placement::SoftIndent(Gap::Space), text(a, "bbbb")), |
| 613 | ] |
| 614 | ); |
| 615 | let same = box(a, &[at(space(), value)]); |
| 616 | let next = box(a, &[at(Placement::Indent, value)]); |
| 617 | let root = box( |
| 618 | a, |
| 619 | &[ |
| 620 | at(none(), text(a, "x =")), |
| 621 | at(none(), choice(a, &[same, next, same])), |
| 622 | at(none(), text(a, ";")), |
| 623 | ] |
| 624 | ); |
| 625 | try expectRender(root, 16, 2, "x = aaaa + bbbb;\n"); |
| 626 | try expectRender(root, 15, 2, "x =\n aaaa + bbbb;\n"); |
| 627 | try expectRender(root, 10, 2, "x = aaaa +\n bbbb;\n"); |
| 628 | } |
| 629 | |
| 630 | /// Allocate a tab stop of the given column. |
| 631 | fn tab(a: &heap::Heap, column: u32) -> *Layout { |
| 632 | return try! layout::new(a, nil, Item::Tab(column)); |
| 633 | } |
| 634 | |
| 635 | /// Build a row of `name`, a tab stop of column 0, and the text `rest` after a |
| 636 | /// space. |
| 637 | fn arrow( |
| 638 | a: &heap::Heap, name: *[u8], rest: *[u8] |
| 639 | ) -> *Layout { |
| 640 | return box( |
| 641 | a, |
| 642 | &[ |
| 643 | at(none(), text(a, name)), |
| 644 | at(none(), tab(a, 0)), |
| 645 | at(space(), text(a, rest)), |
| 646 | ] |
| 647 | ); |
| 648 | } |
| 649 | |
| 650 | /// Build the rows of `rows` on lines of their own. |
| 651 | fn table(a: &heap::Heap, rows: &[*Layout]) -> *Layout { |
| 652 | let mut kids: *mut [Child] = &mut []; |
| 653 | for row in rows { |
| 654 | try! kids.append(Child { placement: Placement::Line, layout: row }, a); |
| 655 | } |
| 656 | return try! layout::new(a, nil, Item::Box(kids)); |
| 657 | } |
| 658 | |
| 659 | /// Test that the cells of consecutive rows are padded to the widest one. |
| 660 | @test unsafe fn testAlignedCells() throws (testing::TestError) { |
| 661 | static STORAGE: [u8; 4096] = [0; 4096]; |
| 662 | let arena = heap::over(&mut STORAGE[..]); |
| 663 | let a: &heap::Heap = arena; |
| 664 | let root = table( |
| 665 | a, |
| 666 | &[ |
| 667 | arrow(a, "a", "=> x"), |
| 668 | arrow(a, "bbb", "=> y"), |
| 669 | arrow(a, "cc", "=> z"), |
| 670 | ] |
| 671 | ); |
| 672 | try expectRender(root, 80, 4, "a => x\nbbb => y\ncc => z\n"); |
| 673 | } |
| 674 | |
| 675 | /// Test that a run is not aligned when the padding would pass the width. |
| 676 | @test unsafe fn testAlignedCellsWidth() throws (testing::TestError) { |
| 677 | static STORAGE: [u8; 4096] = [0; 4096]; |
| 678 | let arena = heap::over(&mut STORAGE[..]); |
| 679 | let a: &heap::Heap = arena; |
| 680 | let root = table( |
| 681 | a, |
| 682 | &[ |
| 683 | arrow(a, "a", "=> x"), |
| 684 | arrow(a, "bbb", "=> yy"), |
| 685 | ] |
| 686 | ); |
| 687 | try expectRender(root, 9, 4, "a => x\nbbb => yy\n"); |
| 688 | try expectRender(root, 8, 4, "a => x\nbbb => yy\n"); |
| 689 | } |
| 690 | |
| 691 | /// Test that a row that does not fit on one line, a blank line, and a line on |
| 692 | /// its own end a run. |
| 693 | @test unsafe fn testAlignedRuns() throws (testing::TestError) { |
| 694 | static STORAGE: [u8; 4096] = [0; 4096]; |
| 695 | let arena = heap::over(&mut STORAGE[..]); |
| 696 | let a: &heap::Heap = arena; |
| 697 | let long = box( |
| 698 | a, |
| 699 | &[ |
| 700 | at(none(), text(a, "cccccc")), |
| 701 | at(none(), tab(a, 0)), |
| 702 | at( |
| 703 | space(), |
| 704 | box( |
| 705 | a, |
| 706 | &[ |
| 707 | at(none(), text(a, "=>")), |
| 708 | at(softIndent(), text(a, "zzzzzzzzzzzzzzzz")), |
| 709 | ] |
| 710 | ) |
| 711 | ), |
| 712 | ] |
| 713 | ); |
| 714 | let root = table( |
| 715 | a, |
| 716 | &[ |
| 717 | arrow(a, "a", "=> x"), |
| 718 | arrow(a, "bb", "=> y"), |
| 719 | long, |
| 720 | arrow(a, "d", "=> x"), |
| 721 | arrow(a, "ee", "=> y"), |
| 722 | blank(a), |
| 723 | arrow(a, "f", "=> x"), |
| 724 | arrow(a, "gg", "=> y"), |
| 725 | text(a, "// own line"), |
| 726 | arrow(a, "h", "=> x"), |
| 727 | arrow(a, "ii", "=> y"), |
| 728 | ] |
| 729 | ); |
| 730 | try expectRender( |
| 731 | root, |
| 732 | 16, |
| 733 | 4, |
| 734 | "a => x\nbb => y\ncccccc =>\n zzzzzzzzzzzzzzzz\nd => x\nee => y\n\nf => x\ngg => y\n// own line\nh => x\nii => y\n" |
| 735 | ); |
| 736 | } |
| 737 | |
| 738 | /// Test that cells of two columns are padded one inside the other, and that a |
| 739 | /// trailing comment column counts the padding of the cell inside it. |
| 740 | @test unsafe fn testAlignedColumns() throws (testing::TestError) { |
| 741 | static STORAGE: [u8; 4096] = [0; 4096]; |
| 742 | let arena = heap::over(&mut STORAGE[..]); |
| 743 | let a: &heap::Heap = arena; |
| 744 | let row1 = box( |
| 745 | a, |
| 746 | &[ |
| 747 | at(none(), arrow(a, "a", "=> x")), |
| 748 | at(none(), tab(a, 1)), |
| 749 | at(space(), comment(a, "// one")), |
| 750 | ] |
| 751 | ); |
| 752 | let row2 = box( |
| 753 | a, |
| 754 | &[ |
| 755 | at(none(), arrow(a, "bbb", "=> yy")), |
| 756 | at(none(), tab(a, 1)), |
| 757 | at(space(), comment(a, "// two")), |
| 758 | ] |
| 759 | ); |
| 760 | let root = table(a, &[row1, row2, arrow(a, "c", "=> z")]); |
| 761 | try expectRender( |
| 762 | root, 80, 4, "a => x // one\nbbb => yy // two\nc => z\n" |
| 763 | ); |
| 764 | } |
| 765 | |
| 766 | /// Test that a row can be a child and the children after it on its line. |
| 767 | @test unsafe fn testAlignedSiblings() throws (testing::TestError) { |
| 768 | static STORAGE: [u8; 4096] = [0; 4096]; |
| 769 | let arena = heap::over(&mut STORAGE[..]); |
| 770 | let a: &heap::Heap = arena; |
| 771 | let root = box( |
| 772 | a, |
| 773 | &[ |
| 774 | at(Placement::Line, text(a, "a,")), |
| 775 | at(none(), tab(a, 1)), |
| 776 | at(space(), comment(a, "// one")), |
| 777 | at(Placement::Line, text(a, "bbb,")), |
| 778 | at(none(), tab(a, 1)), |
| 779 | at(space(), comment(a, "// two")), |
| 780 | at(Placement::Line, text(a, "cc,")), |
| 781 | ] |
| 782 | ); |
| 783 | try expectRender(root, 80, 4, "a, // one\nbbb, // two\ncc,\n"); |
| 784 | } |
| 785 | |
| 786 | /// Test that a layout records the tab stops and broken parts in it, and |
| 787 | /// whether it is plain. |
| 788 | @test unsafe fn testLayoutFacts() throws (testing::TestError) { |
| 789 | static STORAGE: [u8; 4096] = [0; 4096]; |
| 790 | let arena = heap::over(&mut STORAGE[..]); |
| 791 | let a: &heap::Heap = arena; |
| 792 | |
| 793 | let plain = call(a, ";"); |
| 794 | try testing::expect(plain.plain); |
| 795 | try testing::expect(not plain.broken); |
| 796 | try testing::expect(plain.tabs == 0); |
| 797 | |
| 798 | let stop = box(a, &[at(none(), text(a, "a")), at(none(), tab(a, 1))]); |
| 799 | try testing::expect(not stop.plain); |
| 800 | try testing::expect(stop.tabs == 2); |
| 801 | |
| 802 | let lines = box(a, &[at(none(), text(a, "a")), at(Placement::Line, text(a, "b"))]); |
| 803 | try testing::expect(not lines.plain); |
| 804 | |
| 805 | let opened = box(a, &[at(none(), broken(a, plain))]); |
| 806 | try testing::expect(opened.broken); |
| 807 | try testing::expect(not opened.plain); |
| 808 | try testing::expect(not box(a, &[at(none(), choice(a, &[plain, opened]))]).broken); |
| 809 | try testing::expect(box(a, &[at(none(), choice(a, &[opened, plain]))]).broken); |
| 810 | } |
| 811 | |
| 812 | /// Test that boxes nested in a plain box that fits are flat, and that a box |
| 813 | /// nested in a plain box that does not fit is measured on its own. |
| 814 | @test unsafe fn testNestedPlainBoxes() throws (testing::TestError) { |
| 815 | static STORAGE: [u8; 8192] = [0; 8192]; |
| 816 | let arena = heap::over(&mut STORAGE[..]); |
| 817 | let a: &heap::Heap = arena; |
| 818 | let outer = box( |
| 819 | a, |
| 820 | &[ |
| 821 | at(none(), text(a, "f(")), |
| 822 | at(softIndent(), call(a, "")), |
| 823 | at(soft(), text(a, ")")), |
| 824 | ] |
| 825 | ); |
| 826 | try expectRender(outer, 80, 2, "f(call(aaaa, bbbb, cccc))\n"); |
| 827 | try expectRender(outer, 24, 2, "f(\n call(aaaa, bbbb, cccc)\n)\n"); |
| 828 | try expectRender( |
| 829 | outer, 12, 2, "f(\n call(\n aaaa,\n bbbb,\n cccc\n )\n)\n" |
| 830 | ); |
| 831 | } |
| 832 | |
| 833 | /// Fixed rendering selects first alternatives and retains explicit breaks and comments. |
| 834 | @test unsafe fn fixedRendering() throws (testing::TestError) { |
| 835 | static storage: [u8; 8192] = [0; 8192]; |
| 836 | static bytes: [u8; 128] = [0; 128]; |
| 837 | let arena = heap::over(&mut storage[..]); |
| 838 | let a: &heap::Heap = arena; |
| 839 | let mandatory = broken( |
| 840 | a, |
| 841 | box( |
| 842 | a, |
| 843 | &[ |
| 844 | at(none(), text(a, "three")), |
| 845 | at(Placement::SoftIndent(Gap::Space), text(a, "four")), |
| 846 | at(none(), brokenText(a, ",")), |
| 847 | ] |
| 848 | ) |
| 849 | ); |
| 850 | let root = box( |
| 851 | a, |
| 852 | &[ |
| 853 | at(none(), text(a, "one")), |
| 854 | at(none(), tab(a, 0)), |
| 855 | at(none(), brokenText(a, "?")), |
| 856 | at(softSpace(), choice(a, &[text(a, "two"), text(a, "other")])), |
| 857 | at(Placement::Line, mandatory), |
| 858 | at(Placement::Line, comment(a, "// note")), |
| 859 | at(space(), text(a, "after")), |
| 860 | ] |
| 861 | ); |
| 862 | let mut output = sexpr::Buffer { buf: &mut bytes[..] as *mut [u8], pos: 0 }; |
| 863 | layout::text::renderFixed(root, 4, &mut output); |
| 864 | assert mem::eq(&output.buf[.. output.pos], "one two\nthree\n four,\n// note\nafter\n"); |
| 865 | set output.pos = 0; |
| 866 | layout::text::renderFixed(box(a, &[]), 4, &mut output); |
| 867 | assert output.pos == 0; |
| 868 | } |