hyperchad_template 0.3.0

HyperChad template package
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
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
use hyperchad_template::container;
use hyperchad_transformer::Number;

#[test]
fn test_basic_calc_syntax() {
    let containers = container! {
        div width=calc(100 - 20) height=calc(50 + 30) {
            "Basic calc test"
        }
    };

    assert_eq!(containers.len(), 1, "Should generate exactly one container");

    // Test the string representation of the calc expressions
    assert_eq!(
        containers[0].width.as_ref().unwrap().to_string(),
        "calc(100 - 20)"
    );

    assert_eq!(
        containers[0].height.as_ref().unwrap().to_string(),
        "calc(50 + 30)"
    );
}

#[test]
fn test_calc_with_variables() {
    let margin = 10;
    let base_width = 100;

    let containers = container! {
        div width=calc(base_width - margin) height=calc(50 + margin) {
            "Variable calc test"
        }
    };

    assert_eq!(containers.len(), 1, "Should generate exactly one container");

    // Variables are evaluated at compile time but preserved in calc format
    assert_eq!(
        containers[0].width.as_ref().unwrap().to_string(),
        "calc(100 - 10)"
    );
    assert_eq!(
        containers[0].height.as_ref().unwrap().to_string(),
        "calc(50 + 10)"
    );
}

#[test]
fn test_calc_with_parentheses() {
    let containers = container! {
        div width=calc(10 + 20 * 3) height=calc((10 + 20) * 3) {
            "Parentheses calc test"
        }
    };

    assert_eq!(containers.len(), 1, "Should generate exactly one container");

    // Math operations are preserved in calc expressions, respecting precedence
    // Width: 10 + (20 * 3) = Add(10, Multiply(20, 3))
    assert_eq!(
        containers[0].width.as_ref().unwrap().to_string(),
        "calc(10 + 20 * 3)"
    );

    // Height: (10 + 20) * 3 = Multiply(Add(10, 20), 3)
    assert_eq!(
        containers[0].height.as_ref().unwrap().to_string(),
        "calc(10 + 20 * 3)"
    );

    // The expressions should be different structures
    assert_ne!(
        containers[0].width, containers[0].height,
        "Width and height should have different calculation structures"
    );
}

#[test]
fn test_calc_with_unit_functions() {
    let height_value = 50;
    let width_value = 80;

    let containers = container! {
        div width=calc(percent(width_value) - 10) height=calc(vh(height_value) + 20) {
            "Unit function calc test"
        }
    };

    assert_eq!(containers.len(), 1, "Should generate exactly one container");

    // Unit functions are evaluated and preserved in calc expressions
    assert_eq!(
        containers[0].width.as_ref().unwrap().to_string(),
        "calc(80% - 10)"
    );
    assert_eq!(
        containers[0].height.as_ref().unwrap().to_string(),
        "calc(50vh + 20)"
    );
}

#[test]
fn test_calc_with_number_types() {
    let base_width = Number::Integer(80);
    let margin = Number::Integer(20);

    let containers = container! {
        div width=calc(base_width - 10) height=calc(margin * 2) {
            "Number types calc test"
        }
    };

    assert_eq!(containers.len(), 1, "Should generate exactly one container");

    // Number types are handled in calc expressions
    assert_eq!(
        containers[0].width.as_ref().unwrap().to_string(),
        "calc(80 - 10)"
    );
    assert_eq!(
        containers[0].height.as_ref().unwrap().to_string(),
        "calc(20 * 2)"
    );
}

#[test]
fn test_calc_with_all_operations() {
    let containers = container! {
        div {
            // Addition
            div width=calc(50 + 30) { "Addition" }
            // Subtraction
            div width=calc(100 - 20) { "Subtraction" }
            // Multiplication
            div width=calc(25 * 4) { "Multiplication" }
            // Division
            div width=calc(200 / 2) { "Division" }
        }
    };

    assert_eq!(containers.len(), 1, "Should generate exactly one container");
    assert_eq!(
        containers[0].children.len(),
        4,
        "Should have 4 child containers"
    );

    // All operations should be preserved in calc format
    assert_eq!(
        containers[0].children[0]
            .width
            .as_ref()
            .unwrap()
            .to_string(),
        "calc(50 + 30)"
    );
    assert_eq!(
        containers[0].children[1]
            .width
            .as_ref()
            .unwrap()
            .to_string(),
        "calc(100 - 20)"
    );
    assert_eq!(
        containers[0].children[2]
            .width
            .as_ref()
            .unwrap()
            .to_string(),
        "calc(25 * 4)"
    );
    assert_eq!(
        containers[0].children[3]
            .width
            .as_ref()
            .unwrap()
            .to_string(),
        "calc(200 / 2)"
    );
}

#[test]
fn test_calc_with_percent() {
    let containers = container! {
        div width=calc(50% + 30) { "Percent" }
    };

    assert_eq!(containers.len(), 1, "Should generate exactly one container");

    assert_eq!(
        containers[0].width.as_ref().unwrap().to_string(),
        "calc(50% + 30)",
    );
}

#[test]
fn test_calc_with_conditionals() {
    let is_mobile = true;
    let desktop_width = 80;
    let mobile_width = 90;

    let containers = container! {
        div width=calc(if is_mobile { mobile_width } else { desktop_width }) {
            "Conditional calc test"
        }
    };

    assert_eq!(containers.len(), 1, "Should generate exactly one container");

    // Conditional expressions are evaluated at compile time but preserved in calc
    assert_eq!(
        containers[0].width.as_ref().unwrap().to_string(),
        "calc(90)"
    );
}

#[test]
fn test_calc_division_safety() {
    let containers = container! {
        div width=calc(100 / 2) height=calc(50 / 1) {
            "Division safety test"
        }
    };

    assert_eq!(containers.len(), 1, "Should generate exactly one container");

    // Division operations should be preserved in calc format
    assert_eq!(
        containers[0].width.as_ref().unwrap().to_string(),
        "calc(100 / 2)"
    );
    assert_eq!(
        containers[0].height.as_ref().unwrap().to_string(),
        "calc(50 / 1)"
    );
}

#[test]
fn test_calc_with_method_calls() {
    let values = [10, 20, 30];
    let len = values.len() as i64;
    let first = values[0];
    let second = values[1];

    let containers = container! {
        div width=calc(len * 20) height=calc(first + second) {
            "Method calls calc test"
        }
    };

    assert_eq!(containers.len(), 1, "Should generate exactly one container");

    // Method call results are evaluated at compile time but preserved in calc
    assert_eq!(
        containers[0].width.as_ref().unwrap().to_string(),
        "calc(3 * 20)"
    );
    assert_eq!(
        containers[0].height.as_ref().unwrap().to_string(),
        "calc(10 + 20)"
    );
}

#[test]
fn test_calc_with_helper_functions() {
    let value = 50;

    let containers = container! {
        div {
            div width=calc(percent(value)) { "Percent helper" }
            div width=calc(vw(value)) { "VW helper" }
            div width=calc(vh(value)) { "VH helper" }
            div width=calc(dvw(value)) { "DVW helper" }
            div width=calc(dvh(value)) { "DVH helper" }
        }
    };

    assert_eq!(containers.len(), 1, "Should generate exactly one container");
    assert_eq!(
        containers[0].children.len(),
        5,
        "Should have 5 child containers"
    );

    // Helper functions are evaluated and preserved in calc expressions
    assert_eq!(
        containers[0].children[0]
            .width
            .as_ref()
            .unwrap()
            .to_string(),
        "calc(50%)"
    );
    assert_eq!(
        containers[0].children[1]
            .width
            .as_ref()
            .unwrap()
            .to_string(),
        "calc(50vw)"
    );
    assert_eq!(
        containers[0].children[2]
            .width
            .as_ref()
            .unwrap()
            .to_string(),
        "calc(50vh)"
    );
    assert_eq!(
        containers[0].children[3]
            .width
            .as_ref()
            .unwrap()
            .to_string(),
        "calc(50dvw)"
    );
    assert_eq!(
        containers[0].children[4]
            .width
            .as_ref()
            .unwrap()
            .to_string(),
        "calc(50dvh)"
    );
}

#[test]
fn test_calc_vs_regular_attributes() {
    // Test that calc() DSL works alongside regular numeric attributes
    let containers_calc = container! {
        div width=calc(80 + 20) { "Calc width" }
    };

    let containers_regular = container! {
        div width=100 { "Regular width" }
    };

    // Both should generate valid containers
    assert_eq!(
        containers_calc.len(),
        1,
        "Calc should generate one container"
    );
    assert_eq!(
        containers_regular.len(),
        1,
        "Regular should generate one container"
    );

    // Calc should generate calc() expression, regular should generate simple value
    assert_eq!(
        containers_calc[0].width.as_ref().unwrap().to_string(),
        "calc(80 + 20)"
    );
    assert_eq!(
        containers_regular[0].width.as_ref().unwrap().to_string(),
        "100"
    );
}

#[test]
fn test_calc_syntax_parsing() {
    // The main value of calc() is that it provides a natural syntax for mathematical expressions
    // This test verifies the syntax is parsed correctly
    let a = 10;
    let b = 20;
    let c = 30;

    let containers = container! {
        div width=calc(((a + b) * 2) - c) {
            "Complex expression test"
        }
    };

    assert_eq!(containers.len(), 1, "Should generate exactly one container");

    // Complex expressions should be preserved in calc format
    assert_eq!(
        containers[0].width.as_ref().unwrap().to_string(),
        "calc(10 + 20 * 2 - 30)"
    );
}

#[test]
fn test_calc_error_handling() {
    // Test with valid expressions that might cause edge cases
    let containers = container! {
        div width=calc(100 * 0) height=calc(50 + 0) {
            "Edge case test"
        }
    };

    assert_eq!(containers.len(), 1, "Should generate exactly one container");

    // Edge cases should be preserved in calc format
    assert_eq!(
        containers[0].width.as_ref().unwrap().to_string(),
        "calc(100 * 0)"
    );
    assert_eq!(
        containers[0].height.as_ref().unwrap().to_string(),
        "calc(50 + 0)"
    );
}

#[test]
fn test_calc_with_min_function() {
    let containers = container! {
        div height=calc(min(65%, dvw(50))) { "Min function test" }
    };
    assert_eq!(
        containers[0].height.as_ref().unwrap().to_string(),
        "calc(min(65%, 50dvw))"
    );
}

#[test]
fn test_calc_with_max_function() {
    let containers = container! {
        div width=calc(max(300, 20%)) { "Max function test" }
    };
    assert_eq!(
        containers[0].width.as_ref().unwrap().to_string(),
        "calc(max(300, 20%))"
    );
}

#[test]
fn test_calc_with_clamp_function() {
    let containers = container! {
        div width=calc(clamp(300, 50%, 800)) { "Clamp function test" }
    };
    // clamp(min, preferred, max) expands to max(min, min(preferred, max))
    assert_eq!(
        containers[0].width.as_ref().unwrap().to_string(),
        "calc(max(300, min(50%, 800)))"
    );
}

#[test]
fn test_min_function_standalone() {
    let containers = container! {
        div height=min(65%, dvw(50)) { "Standalone min test" }
    };
    assert_eq!(
        containers[0].height.as_ref().unwrap().to_string(),
        "calc(min(65%, 50dvw))"
    );
}

#[test]
fn test_max_function_standalone() {
    let containers = container! {
        div width=max(300, 20%) { "Standalone max test" }
    };
    assert_eq!(
        containers[0].width.as_ref().unwrap().to_string(),
        "calc(max(300, 20%))"
    );
}

#[test]
fn test_clamp_function_standalone() {
    let containers = container! {
        div width=clamp(300, 50%, 800) { "Standalone clamp test" }
    };
    // clamp(min, preferred, max) expands to max(min, min(preferred, max))
    assert_eq!(
        containers[0].width.as_ref().unwrap().to_string(),
        "calc(max(300, min(50%, 800)))"
    );
}

#[test]
fn test_nested_css_math_functions() {
    let containers = container! {
        div height=calc(max(min(50%, 400), 200)) { "Nested CSS math test" }
    };
    assert_eq!(
        containers[0].height.as_ref().unwrap().to_string(),
        "calc(max(min(50%, 400), 200))"
    );
}

#[test]
fn test_css_math_with_variables() {
    let base_width = 400;
    let max_width = 800;
    let preferred = 50;

    let containers = container! {
        div width=clamp(base_width, percent(preferred), max_width) {
            "CSS math with variables"
        }
    };
    // clamp(min, preferred, max) expands to max(min, min(preferred, max))
    assert_eq!(
        containers[0].width.as_ref().unwrap().to_string(),
        "calc(max(400, min(50%, 800)))"
    );
}

#[test]
fn test_complex_css_math_expression() {
    let containers = container! {
        div height=calc(min(vh(100) - 80, max(400, 50% + 100))) {
            "Complex CSS math expression"
        }
    };
    // The sub-expressions are simplified into a single calc expression
    assert_eq!(
        containers[0].height.as_ref().unwrap().to_string(),
        "calc(min(100vh - 80, max(400, 50% + 100)))"
    );
}

#[test]
fn test_css_math_with_multiple_arguments() {
    let containers = container! {
        div width=min(300, 50%, vh(80), vw(90)) { "Multiple arguments test" }
    };
    // min(a, b, c, d) chains as min(a, min(b, min(c, d)))
    assert_eq!(
        containers[0].width.as_ref().unwrap().to_string(),
        "calc(min(300, min(50%, min(80vh, 90vw))))"
    );
}

#[test]
fn test_raw_percent_values() {
    // Test that raw percent values (without calc()) work correctly
    let containers = container! {
        div {
            div width=100% height=50% { "Raw percent test" }
            div margin=25% padding=10% { "Margin/padding percent test" }
            div max-width=75% min-height=30% { "Min/max percent test" }
        }
    };

    assert_eq!(containers.len(), 1, "Should generate exactly one container");
    assert_eq!(
        containers[0].children.len(),
        3,
        "Should have 3 child containers"
    );

    // Test width and height with raw percents
    assert_eq!(
        containers[0].children[0]
            .width
            .as_ref()
            .unwrap()
            .to_string(),
        "100%"
    );
    assert_eq!(
        containers[0].children[0]
            .height
            .as_ref()
            .unwrap()
            .to_string(),
        "50%"
    );

    // Test margin and padding with raw percents
    assert_eq!(
        containers[0].children[1]
            .margin_top
            .as_ref()
            .unwrap()
            .to_string(),
        "25%"
    );
    assert_eq!(
        containers[0].children[1]
            .margin_right
            .as_ref()
            .unwrap()
            .to_string(),
        "25%"
    );
    assert_eq!(
        containers[0].children[1]
            .margin_bottom
            .as_ref()
            .unwrap()
            .to_string(),
        "25%"
    );
    assert_eq!(
        containers[0].children[1]
            .margin_left
            .as_ref()
            .unwrap()
            .to_string(),
        "25%"
    );
    assert_eq!(
        containers[0].children[1]
            .padding_top
            .as_ref()
            .unwrap()
            .to_string(),
        "10%"
    );
    assert_eq!(
        containers[0].children[1]
            .padding_right
            .as_ref()
            .unwrap()
            .to_string(),
        "10%"
    );
    assert_eq!(
        containers[0].children[1]
            .padding_bottom
            .as_ref()
            .unwrap()
            .to_string(),
        "10%"
    );
    assert_eq!(
        containers[0].children[1]
            .padding_left
            .as_ref()
            .unwrap()
            .to_string(),
        "10%"
    );

    // Test max-width and min-height with raw percents
    assert_eq!(
        containers[0].children[2]
            .max_width
            .as_ref()
            .unwrap()
            .to_string(),
        "75%"
    );
    assert_eq!(
        containers[0].children[2]
            .min_height
            .as_ref()
            .unwrap()
            .to_string(),
        "30%"
    );
}

#[test]
fn test_canvas_percent_values() {
    // Test that canvas elements handle percent values correctly for width/height
    let containers = container! {
        div {
            canvas width=100% height=50% { "Canvas with percents" }
            div width=100% height=50% { "Div with percents" }
        }
    };

    assert_eq!(containers.len(), 1, "Should generate exactly one container");
    assert_eq!(
        containers[0].children.len(),
        2,
        "Should have 2 child containers"
    );

    // Canvas width/height should work the same as div width/height
    assert_eq!(
        containers[0].children[0]
            .width
            .as_ref()
            .unwrap()
            .to_string(),
        "100%"
    );
    assert_eq!(
        containers[0].children[0]
            .height
            .as_ref()
            .unwrap()
            .to_string(),
        "50%"
    );
    assert_eq!(
        containers[0].children[1]
            .width
            .as_ref()
            .unwrap()
            .to_string(),
        "100%"
    );
    assert_eq!(
        containers[0].children[1]
            .height
            .as_ref()
            .unwrap()
            .to_string(),
        "50%"
    );
}