lini 1.0.0-alpha.0

Pretty diagrams, charts, and technical drawings from plain text, with fine-grained control. Compiles to clean, themeable SVG.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
/// Live-mode SVG for a source (palette vars stay `var(--lini-…)`).
fn svg(src: &str) -> String {
    crate::compile_str(src).expect("compile")
}

/// The layout-phase error message for a chart that resolves but won't lay out.
fn layout_err(src: &str) -> String {
    let toks = crate::lexer::lex(src).expect("lex");
    let file = crate::syntax::parser::parse(src, &toks).expect("parse");
    let lowered = crate::desugar::desugar(&file).expect("desugar");
    let program = crate::resolve::resolve_with_theme(&lowered, &[]).expect("resolve");
    crate::layout::layout(&program)
        .err()
        .expect("expected a layout error")
        .to_string()
}

#[test]
fn bars_chart_lowers_to_axis_bars_legend_and_title() {
    let s = svg(
        "|chart| \"T\" { categories: \"a\", \"b\" } [\n  |bars| \"S1\" { data: 3, 6 }\n  |bars| \"S2\" { data: 4, 2 }\n]\n",
    );
    assert!(s.contains("lini-chart"), "chart container class: {s}");
    // Palette walk: series 0 rose, series 1 teal — red skipped. Bars fill with the
    // soft tier (the outlined look, [SPEC 14.6]).
    assert!(s.contains("var(--lini-rose-soft)"), "series 0 hue: {s}");
    assert!(s.contains("var(--lini-teal-soft)"), "series 1 hue: {s}");
    assert!(!s.contains("var(--lini-red)"), "red is reserved: {s}");
    assert!(s.contains("var(--lini-grid)"), "gridlines: {s}");
    assert!(s.contains("<title>a · S1: 3</title>"), "bar title: {s}");
    assert!(s.contains(">T</text>"), "chart title text: {s}");
}

#[test]
fn a_line_series_draws_a_polyline() {
    let s = svg("|chart| { categories: \"a\", \"b\", \"c\" } [\n  |line| { data: 3, 6, 4 }\n]\n");
    assert!(s.contains("<polyline"), "polyline: {s}");
}

#[test]
fn a_dots_series_over_points_draws_ellipses() {
    let s = svg(
        "|chart| [\n  |axis| { side: bottom }\n  |axis| { side: left }\n  |dots| { data: 1 5, 2 3, 3 8 }\n]\n",
    );
    assert!(s.contains("<ellipse"), "dots render as ellipses: {s}");
}

#[test]
fn an_explicit_fill_overrides_the_palette_walk() {
    let s = svg("|chart| { categories: \"a\" } [\n  |bars| { data: 5; fill: --teal }\n]\n");
    assert!(s.contains("var(--lini-teal)"), "explicit fill kept: {s}");
    assert!(!s.contains("var(--lini-rose)"), "palette not walked: {s}");
}

#[test]
fn a_bar_radius_rounds_the_rect() {
    // The desugar defaults |bars| to radius 2; an explicit `radius:` overrides it.
    let s = svg("|chart| { categories: \"a\" } [\n  |bars| { data: 5; radius: 6 }\n]\n");
    assert!(
        s.contains("rx=\"6\""),
        "explicit bar radius rounds the rect: {s}"
    );
    let d = svg("|chart| { categories: \"a\" } [\n  |bars| { data: 5 }\n]\n");
    assert!(
        d.contains("rx=\"2\""),
        "the default bar radius rounds the rect: {d}"
    );
}

#[test]
fn a_bar_stroke_draws_an_outline_without_recoloring_the_fill() {
    // A `stroke:` on a fill shape is a separate outline [SPEC 14.6] — it must
    // not become the fill. With no `fill:`, the body stays the palette soft tier
    // (rose) and the stroke is the outline (sky); the old bug made the body sky.
    let s = svg("|chart| { categories: \"a\" } [\n  |bars| { data: 5; stroke: --sky }\n]\n");
    assert!(
        s.contains("var(--lini-rose-soft)"),
        "the fill stays the palette soft tier: {s}"
    );
    assert!(
        s.contains("var(--lini-sky)"),
        "the stroke draws as an outline: {s}"
    );
}

#[test]
fn bars_default_to_an_outlined_look() {
    // A default bar fills with the soft tier and gains a deep edge [SPEC 14.6].
    let s = svg("|chart| { categories: \"a\" } [\n  |bars| { data: 5 }\n]\n");
    assert!(s.contains("var(--lini-rose-soft)"), "soft fill: {s}");
    assert!(s.contains("var(--lini-rose-deep)"), "deep edge: {s}");
}

#[test]
fn a_bar_stroke_none_opts_out_of_the_edge() {
    // `stroke: none` overrides the class `auto` sentinel — a flat bar, no edge.
    let s = svg("|chart| { categories: \"a\" } [\n  |bars| { data: 5; stroke: none }\n]\n");
    assert!(s.contains("var(--lini-rose-soft)"), "soft fill stays: {s}");
    assert!(!s.contains("var(--lini-rose-deep)"), "no deep edge: {s}");
}

#[test]
fn a_slice_stroke_outlines_without_recoloring_the_fill() {
    // The pie bug [SPEC 14.6]: `stroke:` on a slice recoloured its fill and
    // drew no outline. Now slice 0's fill walks the palette soft tier (rose) and the
    // stroke is a separate outline (sky).
    let s = svg(
        "|pie| [\n  |slice| \"a\" { value: 1; stroke: --sky }\n  |slice| \"b\" { value: 1 }\n]\n",
    );
    assert!(
        s.contains("var(--lini-rose-soft)"),
        "slice 0 fill stays the palette soft walk: {s}"
    );
    assert!(
        s.contains("var(--lini-sky)"),
        "slice 0 stroke draws as an outline: {s}"
    );
}

#[test]
fn the_chart_gap_tunes_the_title_inset() {
    // `gap:` sets the title→plot space [SPEC 14.6], so different gaps shift the
    // plot geometry; the default (10) is set on the .lini-chart class at desugar.
    let tight = svg("|chart| \"T\" { categories: \"a\"; gap: 0 } [\n  |bars| { data: 5 }\n]\n");
    let loose = svg("|chart| \"T\" { categories: \"a\"; gap: 60 } [\n  |bars| { data: 5 }\n]\n");
    assert_ne!(
        tight, loose,
        "the chart 'gap' changes the title / plot spacing"
    );
}

#[test]
fn a_dual_axis_chart_binds_series_by_id() {
    let s = svg(
        "|chart| { categories: \"a\", \"b\" } [\n  |axis#n| { side: left }\n  |axis#p| { side: right }\n  |bars| { data: 10, 20; axis: n }\n  |line| { data: 4, 9; axis: p }\n]\n",
    );
    assert!(s.contains("<line "), "the 2-point line: {s}");
    // Each axis's domain comes from its bound series: bars 10/20 → a left axis to
    // 20, line 4/9 → a right axis to 10 (whose 1-2 ticks include 8; the left's
    // 0-5-10-15-20 do not). Distinct domains prove the by-id binding.
    assert!(s.contains(">20</text>"), "left axis from bars: {s}");
    assert!(s.contains(">8</text>"), "right axis from line: {s}");
}

#[test]
fn an_unknown_axis_id_errors_with_a_suggestion() {
    let e = layout_err(
        "|chart| { categories: \"a\" } [\n  |axis#v| { side: left }\n  |line| { data: 1; axis: nope }\n]\n",
    );
    assert!(e.contains("axis 'nope' not found"), "{e}");
    assert!(e.contains("'v'"), "suggests the known id: {e}");
}

#[test]
fn empty_chart_errors() {
    assert!(layout_err("|chart| \"T\"\n").contains("at least one series"));
}

#[test]
fn data_count_must_match_categories() {
    let e = layout_err("|chart| { categories: \"a\", \"b\" } [\n  |bars| { data: 1, 2, 3 }\n]\n");
    assert!(e.contains("3 values but the chart has 2 categories"), "{e}");
}

#[test]
fn data_and_fn_together_error() {
    let e = layout_err("|chart| { categories: \"a\" } [\n  |bars| { data: 1; fn: (2) }\n]\n");
    assert!(e.contains("not both"), "{e}");
}

#[test]
fn a_non_series_child_is_rejected() {
    let e = layout_err("|chart| [\n  |box| \"x\"\n]\n");
    assert!(e.contains("series"), "{e}");
}

#[test]
fn a_fn_series_samples_a_curve_over_the_x_domain() {
    let s = svg(
        "|chart| [\n  |axis| { side: bottom; range: 0 10 }\n  |axis| { side: left }\n  |line| { fn: (x*x); samples: 12 }\n]\n",
    );
    assert!(s.contains("<polyline"), "sampled fn polyline: {s}");
    // x² over 0..10 peaks at 100 → the value axis auto-fits to 100.
    assert!(
        s.contains(">100</text>"),
        "value axis fits the sampled data: {s}"
    );
}

#[test]
fn an_area_series_fills_a_polygon() {
    let s = svg("|chart| { categories: \"a\", \"b\", \"c\" } [\n  |area| { data: 3, 6, 4 }\n]\n");
    assert!(s.contains("<polygon"), "area fill: {s}");
}

#[test]
fn a_log_axis_draws_decade_ticks() {
    let s = svg(
        "|chart| { categories: \"a\", \"b\" } [\n  |axis| { side: left; scale: log }\n  |bars| { data: 10, 1000 }\n]\n",
    );
    assert!(s.contains(">100</text>"), "decade tick: {s}");
    assert!(s.contains(">1000</text>"), "decade tick: {s}");
}

#[test]
fn a_log_axis_over_a_non_positive_domain_errors() {
    let e = layout_err(
        "|chart| { categories: \"a\" } [\n  |axis| { side: left; scale: log; range: -1 10 }\n  |bars| { data: 5 }\n]\n",
    );
    assert!(e.contains("domain above 0"), "{e}");
}

#[test]
fn a_smooth_curve_resamples_densely() {
    let s = svg(
        "|chart| { categories: \"a\", \"b\", \"c\", \"d\" } [\n  |line| { data: 1, 8, 2, 6; curve: smooth }\n]\n",
    );
    // The monotone cubic is resampled into a many-point polyline, not 4 segments.
    let pts = s
        .split("<polyline points=\"")
        .nth(1)
        .and_then(|t| t.split('"').next())
        .unwrap_or("");
    assert!(
        pts.split(' ').count() > 20,
        "smooth resamples densely, got {} points",
        pts.split(' ').count()
    );
}

#[test]
fn a_fn_list_without_bands_reports_the_mismatch() {
    let e = layout_err(
        "|chart| [\n  |axis| { side: bottom; range: 0 1 }\n  |axis| { side: left }\n  |line| { fn: (1), (2) }\n]\n",
    );
    assert!(e.contains("2 formulas"), "{e}");
    assert!(e.contains("0 bands"), "{e}");
}

#[test]
fn a_filled_band_shades_the_plot_and_labels_it() {
    let s = svg(
        "|chart| { categories: \"a\", \"b\" } [\n  |bars| { data: 5, 8 }\n  |band| \"zone\" { span: 0 1; fill: --amber }\n]\n",
    );
    // Amber is unused by the palette walk, so it is unambiguously the band.
    assert!(s.contains("var(--lini-amber)"), "band shade tint: {s}");
    assert!(s.contains("opacity"), "the shade is translucent: {s}");
    assert!(s.contains(">zone</text>"), "band tick label: {s}");
}

#[test]
fn an_unfilled_band_draws_a_divider_not_a_shade() {
    let s = svg(
        "|chart| { categories: \"a\", \"b\", \"c\" } [\n  |bars| { data: 5, 8, 6 }\n  |band| \"L\" { span: 0 1 }\n  |band| \"R\" { span: 1 3 }\n]\n",
    );
    assert!(
        s.contains(">L</text>") && s.contains(">R</text>"),
        "band ticks: {s}"
    );
    assert!(
        !s.contains("opacity"),
        "no shade is drawn for an unfilled band: {s}"
    );
}

#[test]
fn a_segmented_fn_draws_one_polyline_across_the_bands() {
    let s = svg(
        "|chart| [\n  |axis| { side: bottom }\n  |axis| { side: left }\n  |band| { span: 0 1 }\n  |band| { span: 1 2 }\n  |line| { fn: (u), (1-u) }\n]\n",
    );
    assert!(s.contains("<polyline"), "segmented curve polyline: {s}");
}

#[test]
fn a_fn_list_length_must_match_the_band_count() {
    let e = layout_err(
        "|chart| [\n  |axis| { side: bottom }\n  |axis| { side: left }\n  |band| { span: 0 1 }\n  |line| { fn: (1), (2), (3) }\n]\n",
    );
    assert!(e.contains("3 formulas"), "{e}");
    assert!(e.contains("1 bands"), "{e}");
}

#[test]
fn a_mark_draws_a_reference_line_with_its_label() {
    let s = svg(
        "|chart| { categories: \"a\", \"b\" } [\n  |axis#v| { side: left }\n  |bars| { data: 5, 8 }\n  |mark| \"max\" { at: 6; axis: v; stroke: --red }\n]\n",
    );
    assert!(
        s.contains("var(--lini-red)"),
        "the reference line is the mark's stroke: {s}"
    );
    assert!(s.contains(">max</text>"), "the mark label: {s}");
}

#[test]
fn a_mark_point_draws_a_dot_and_a_label() {
    let s = svg(
        "|chart| { categories: \"a\", \"b\" } [\n  |axis#v| { side: left }\n  |bars| { data: 5, 8 }\n  |mark| \"pt\" { at: 1 6; axis: v }\n]\n",
    );
    assert!(s.contains("<ellipse"), "the point's dot: {s}");
    assert!(s.contains(">pt</text>"), "the point's label: {s}");
}

#[test]
fn marker_none_suppresses_the_point_dot() {
    let s = svg(
        "|chart| { categories: \"a\", \"b\" } [\n  |axis#v| { side: left }\n  |bars| { data: 5, 8 }\n  |mark| \"lbl\" { at: 1 6; axis: v; marker: none }\n]\n",
    );
    assert!(s.contains(">lbl</text>"), "the label still draws: {s}");
    assert!(!s.contains("<ellipse"), "no dot when 'marker: none': {s}");
}

#[test]
fn a_mark_needs_an_axis() {
    let e = layout_err(
        "|chart| { categories: \"a\" } [\n  |bars| { data: 5 }\n  |mark| \"x\" { at: 3 }\n]\n",
    );
    assert!(e.contains("needs 'axis:'"), "{e}");
}

#[test]
fn a_mark_at_takes_one_or_two_values() {
    let e = layout_err(
        "|chart| { categories: \"a\" } [\n  |axis#v| { side: left }\n  |bars| { data: 5 }\n  |mark| \"x\" { at: 1 2 3; axis: v }\n]\n",
    );
    assert!(e.contains("one value"), "{e}");
}

#[test]
fn stacked_bars_fit_the_per_category_sum() {
    let s = svg(
        "|chart| { categories: \"a\", \"b\"; bars: stacked } [\n  |bars| { data: 3, 4 }\n  |bars| { data: 5, 6 }\n]\n",
    );
    // Category b sums to 10, so the value axis reaches a 10 tick (grouped tops out
    // at 6). The 10 proves the stacked envelope drove the domain.
    assert!(
        s.contains(">10</text>"),
        "value axis fits the stack sum: {s}"
    );
}

#[test]
fn overlay_bars_are_translucent() {
    let s = svg(
        "|chart| { categories: \"a\", \"b\"; bars: overlay } [\n  |bars| { data: 3, 4 }\n  |bars| { data: 7, 6 }\n]\n",
    );
    assert!(s.contains("opacity"), "overlay bars carry an opacity: {s}");
}

#[test]
fn a_radial_line_draws_a_closed_radar_with_spoke_labels() {
    let s = svg(
        "|chart| { direction: radial; categories: \"a\", \"b\", \"c\" } [\n  |axis| { range: 0 5 }\n  |line| { data: 5, 3, 4 }\n]\n",
    );
    assert!(s.contains("<polyline"), "the radar loop: {s}");
    assert!(s.contains(">a</text>"), "a spoke (category) label: {s}");
}

#[test]
fn radial_bars_draw_wedge_polygons() {
    let s = svg(
        "|chart| { direction: radial; categories: \"a\", \"b\", \"c\" } [\n  |axis| { range: 0 10 }\n  |bars| { data: 8, 5, 9 }\n]\n",
    );
    assert!(s.contains("<polygon"), "wedge polygons: {s}");
}

#[test]
fn a_side_on_a_radial_axis_errors() {
    let e = layout_err(
        "|chart| { direction: radial; categories: \"a\", \"b\" } [\n  |axis| { side: left; range: 0 5 }\n  |line| { data: 3, 4 }\n]\n",
    );
    assert!(e.contains("radial"), "{e}");
}

#[test]
fn a_row_chart_lays_categories_left_and_values_below() {
    let s = svg(
        "|chart| { direction: row; categories: \"a\", \"b\" } [\n  |axis| \"v\" { side: bottom }\n  |bars| { data: 5, 10 }\n]\n",
    );
    assert!(s.contains("<rect"), "horizontal bars: {s}");
    assert!(s.contains(">a</text>"), "a category label (left): {s}");
    assert!(s.contains(">10</text>"), "a value tick (below): {s}");
}

#[test]
fn a_row_line_projects_through_the_same_builder() {
    let s = svg(
        "|chart| { direction: row; categories: \"a\", \"b\", \"c\" } [\n  |line| { data: 3, 6, 4 }\n]\n",
    );
    assert!(
        s.contains("<polyline"),
        "the row line reuses the cartesian builder: {s}"
    );
}

#[test]
fn an_unknown_direction_errors() {
    let e = layout_err(
        "|chart| { direction: sideways; categories: \"a\" } [\n  |bars| { data: 5 }\n]\n",
    );
    assert!(e.contains("column, row, or radial"), "{e}");
}

#[test]
fn a_pie_draws_slice_wedges_and_a_legend() {
    let s = svg("|pie| \"T\" [\n  |slice| \"a\" { value: 3 }\n  |slice| \"b\" { value: 1 }\n]\n");
    assert!(s.contains("<polygon"), "slice wedges: {s}");
    assert!(
        s.contains("var(--lini-rose-soft)"),
        "slice 0 walks the palette (soft): {s}"
    );
    assert!(
        s.contains("var(--lini-teal-soft)"),
        "slice 1 walks the palette (soft): {s}"
    );
    assert!(s.contains(">a</text>"), "a legend label: {s}");
}

#[test]
fn a_non_slice_child_of_a_pie_errors() {
    let e = layout_err("|pie| [\n  |bars| { data: 1 }\n]\n");
    assert!(e.contains("'|slice|' only"), "{e}");
}

#[test]
fn an_empty_pie_errors() {
    assert!(layout_err("|pie| \"T\"\n").contains("at least one '|slice|'"));
}

#[test]
fn a_negative_slice_value_errors() {
    let e = layout_err("|pie| [\n  |slice| { value: -1 }\n]\n");
    assert!(e.contains("≥ 0"), "{e}");
}

#[test]
fn a_pie_summing_to_zero_errors() {
    let e = layout_err("|pie| [\n  |slice| { value: 0 }\n  |slice| { value: 0 }\n]\n");
    assert!(e.contains("sum to zero"), "{e}");
}

#[test]
fn a_hole_out_of_range_errors() {
    let e = layout_err("|pie| { hole: 1.5 } [\n  |slice| { value: 1 }\n]\n");
    assert!(e.contains("fraction 0..1"), "{e}");
}

#[test]
fn bubbles_render_as_ovals_with_a_title_floor() {
    let s = svg(
        "|chart| [\n  |axis| { side: bottom }\n  |axis| { side: left }\n  |bubble| \"A\" { at: 1 2; value: 4 }\n  |bubble| \"B\" { at: 3 4; value: 16 }\n]\n",
    );
    assert!(s.contains("<ellipse"), "bubbles are ovals: {s}");
    assert!(
        s.contains("<title>B: 16</title>"),
        "the bubble <title> floor: {s}"
    );
}

#[test]
fn a_bubble_needs_at_and_value() {
    let e = layout_err(
        "|chart| [\n  |axis| { side: bottom }\n  |axis| { side: left }\n  |bubble| \"A\" { at: 1 2 }\n]\n",
    );
    assert!(e.contains("needs 'at:' (x y) and 'value:'"), "{e}");
}

#[test]
fn auto_tooltips_add_a_hover_card_over_the_title_floor() {
    // The default mode is `auto`: the <title> floor plus the live hover card.
    let s = svg("|chart| { categories: \"a\" } [\n  |bars| { data: 5 }\n]\n");
    assert!(s.contains("lini-chart-tip"), "the hover card: {s}");
    assert!(
        s.contains("<title>a: 5</title>"),
        "the title floor stays: {s}"
    );
    assert!(
        s.contains(":hover ~ .lini-tip-0"),
        "the reveal rule links the mark to its card: {s}"
    );
    assert!(s.contains("lini-hit-0"), "the hovered mark is tagged: {s}");
}

#[test]
fn tooltip_none_drops_the_floor() {
    let s = svg("|chart| { categories: \"a\"; tooltip: none } [\n  |bars| { data: 5 }\n]\n");
    assert!(!s.contains("<title>"), "no title floor: {s}");
    assert!(!s.contains("lini-chart-tip"), "no card: {s}");
}

#[test]
fn labels_draw_inline_under_auto() {
    // A series' `labels:` show on the plot (default auto) as `.lini-chart-label` text,
    // over the hover card the value still rides.
    let s = svg(
        "|chart| { categories: \"a\", \"b\" } [\n  |line| { data: 3, 6; labels: \"lo\", \"hi\" }\n]\n",
    );
    assert!(s.contains("lini-chart-label"), "inline label class: {s}");
    assert!(
        s.contains(">lo</text>") && s.contains(">hi</text>"),
        "tag text: {s}"
    );
    assert!(
        s.contains("pointer-events: none"),
        "inline labels pass hover through: {s}"
    );
}

#[test]
fn tooltip_hover_keeps_tags_off_the_plot() {
    // `tooltip: hover` keeps the card (bars are hit targets) but draws no inline label,
    // even with labels.
    let s = svg(
        "|chart| { categories: \"a\", \"b\"; tooltip: hover } [\n  |bars| { data: 3, 6; labels: \"lo\", \"hi\" }\n]\n",
    );
    assert!(!s.contains("lini-chart-label"), "no inline label: {s}");
    assert!(s.contains("lini-chart-tip"), "the hover card stays: {s}");
}

#[test]
fn a_series_tooltip_overrides_the_chart_default() {
    // The chart says hover (no inline); the series opts back into always.
    let s = svg(
        "|chart| { categories: \"a\", \"b\"; tooltip: hover } [\n  |line| { data: 3, 6; labels: \"lo\", \"hi\"; tooltip: always }\n]\n",
    );
    assert!(
        s.contains("lini-chart-label"),
        "series override shows inline: {s}"
    );
}

#[test]
fn an_arrow_marker_on_a_series_errors() {
    let e = layout_err(
        "|chart| { categories: \"a\", \"b\" } [\n  |line| { data: 3, 6; marker: arrow }\n]\n",
    );
    assert!(e.contains("no centred form"), "{e}");
    assert!(e.contains("dot, circle, or diamond"), "{e}");
}

#[test]
fn a_circle_marker_is_bigger_than_a_dot() {
    // A line vertex `circle` is a hover-sized point; `dot` stays small.
    let c =
        svg("|chart| { categories: \"a\", \"b\" } [\n  |line| { data: 3, 6; marker: circle }\n]\n");
    let d =
        svg("|chart| { categories: \"a\", \"b\" } [\n  |line| { data: 3, 6; marker: dot }\n]\n");
    assert!(c.contains("rx=\"5.5\""), "circle marker radius: {c}");
    assert!(d.contains("rx=\"2.5\""), "dot marker radius: {d}");
}

#[test]
fn a_diamond_marker_draws_a_rhombus() {
    let s = svg(
        "|chart| { categories: \"a\", \"b\" } [\n  |line| { data: 3, 6; marker: diamond }\n]\n",
    );
    assert!(s.contains("<polygon"), "diamond marker is a polygon: {s}");
}

#[test]
fn data_text_is_normal_weight_chrome_is_semibold() {
    // Chrome reads semibold [SPEC 14.6]: the title through its own
    // `.lini-chart-title` rule (14px/600, nothing inlined), the legend inline
    // (semibold emits as CSS 600); data text — axis ticks, labels — states
    // `normal` so the numbers never shout.
    let s = svg(
        "|chart| \"Cost\" { categories: \"a\", \"b\" } [\n  |bars| \"A\" { data: 5, 8 }\n  |bars| \"B\" { data: 3, 4 }\n]\n",
    );
    assert!(
        s.contains(".lini .lini-chart-title { font-size: 14px; font-weight: 600; }"),
        "title rule: {s}"
    );
    assert!(
        s.contains("<text class=\"lini-text lini-chart-title\" x=\"0\"")
            && s.contains(">Cost</text>"),
        "title classed, no inline font: {s}"
    );
    assert!(
        s.contains("font-size: 11px; font-weight: 600\">A</text>"),
        "legend semibold: {s}"
    );
    assert!(
        s.contains("font-size: 11px; font-weight: normal\">a</text>"),
        "axis tick normal: {s}"
    );
}

#[test]
fn labels_count_must_match_the_data() {
    let e = layout_err(
        "|chart| { categories: \"a\", \"b\" } [\n  |line| { data: 3, 6; labels: \"only\" }\n]\n",
    );
    assert!(e.contains("1 entries but the series has 2"), "{e}");
}

#[test]
fn labels_on_a_fn_series_error() {
    let e = layout_err(
        "|chart| [\n  |axis| { side: bottom; range: 0 10 }\n  |axis| { side: left }\n  |line| { fn: (x); labels: \"a\", \"b\" }\n]\n",
    );
    assert!(e.contains("needs explicit 'data'"), "{e}");
}

#[test]
fn a_legacy_space_data_list_errors_with_the_comma_spelling() {
    // The 0.21 comma law [SPEC 2/20]: `data: 9 15 24` is the pre-law spelling.
    let e = layout_err("|chart| [\n  |bars| { data: 9 15 24 }\n]\n");
    assert!(
        e.contains("'data' takes comma-separated values — 'data: 9, 15, 24'"),
        "{e}"
    );
}

#[test]
fn a_lone_space_pair_is_one_point_never_two_values() {
    // `data: 10 20` [SPEC 2]: one `x y` point — it draws a dot, not two bars.
    let s = svg("|chart| [\n  |axis| { side: bottom }\n  |dots| { data: 10 20 }\n]\n");
    assert!(s.contains("lini-chart"), "{s}");
    let e = layout_err("|chart| { categories: \"a\", \"b\" } [\n  |bars| { data: 10 20 }\n]\n");
    assert!(e.contains("not 'x y' points"), "{e}");
}

#[test]
fn row_bands_and_marks_flip_with_the_direction() {
    // [SPEC 14.5/14.7]: in a row chart the value axis runs along the bottom, so
    // a value-bound band shades a vertical strip and a value-bound mark draws a
    // vertical dashed line — same declarations, flipped projection.
    let s = svg(
        "|chart| { direction: row; categories: \"a\", \"b\" } [\n  |axis#v| { side: bottom; range: 0 20 }\n  |bars| { data: 12, 8 }\n  |band| \"hot\" { span: 10 15; axis: v; fill: --amber }\n  |mark| \"target\" { at: 10; axis: v; stroke-style: dashed }\n]\n",
    );
    // The band's wash: full plot height, x-span 10..15 — a rect taller than wide.
    let band = s.find("opacity: 0.15").expect("the band wash");
    let after = &s[band..];
    let rect = &after[..after.find("/>").unwrap()];
    let dim = |k: &str| {
        let i = rect.find(k).unwrap() + k.len() + 2;
        rect[i..i + rect[i..].find('"').unwrap()]
            .parse::<f64>()
            .unwrap()
    };
    assert!(
        dim("height") > dim("width"),
        "a row value-band is a vertical strip: {rect}"
    );
    // The mark's reference line: vertical (both endpoints share one x).
    let mark = s.find("stroke-dasharray: 5").expect("the dashed mark line");
    let after = &s[mark..];
    let line = &after[after.find("<line ").unwrap()..];
    let line = &line[..line.find("/>").unwrap()];
    let coord = |k: &str| {
        let i = line.find(k).unwrap() + k.len() + 2;
        line[i..i + line[i..].find('"').unwrap()]
            .parse::<f64>()
            .unwrap()
    };
    assert!(
        (coord("x1") - coord("x2")).abs() < 1e-9 && coord("y1") != coord("y2"),
        "vertical reference line: {line}"
    );
    assert!(s.contains(">target<"), "the mark label draws: {s}");
    assert!(s.contains(">hot<"), "the band tick draws: {s}");
}

#[test]
fn radial_bands_and_marks_error() {
    // The flip is never silently lossy [SPEC 14.7/20].
    let e = layout_err(
        "|chart| { direction: radial; categories: \"a\", \"b\", \"c\" } [\n  |line| { data: 1, 2, 3 }\n  |mark| \"x\" { at: 2 }\n]\n",
    );
    assert!(
        e.contains("a radial chart draws no bands / marks yet — remove it or change 'direction'"),
        "{e}"
    );
}