hyperchad_template 0.2.0

HyperChad template package
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
use hyperchad_template::container;
use hyperchad_transformer_models::*;

#[test]
fn test_bare_identifier_enum_attributes() {
    // Test visibility with kebab-case bare identifiers (new syntax)
    let containers = container! {
        div visibility=hidden { "Hidden div" }
    };
    assert_eq!(containers.len(), 1);
    assert_eq!(containers[0].visibility, Some(Visibility::Hidden));

    let containers = container! {
        div visibility=visible { "Visible div" }
    };
    assert_eq!(containers.len(), 1);
    assert_eq!(containers[0].visibility, Some(Visibility::Visible));

    // Test layout direction with kebab-case bare identifiers
    let containers = container! {
        div direction=row { "Row layout" }
    };
    assert_eq!(containers.len(), 1);
    assert_eq!(containers[0].direction, LayoutDirection::Row);

    let containers = container! {
        div direction=column { "Column layout" }
    };
    assert_eq!(containers.len(), 1);
    assert_eq!(containers[0].direction, LayoutDirection::Column);

    // Test position with kebab-case bare identifiers
    let containers = container! {
        div position=fixed { "Fixed position" }
    };
    assert_eq!(containers.len(), 1);
    assert_eq!(containers[0].position, Some(Position::Fixed));

    let containers = container! {
        div position=relative { "Relative position" }
    };
    assert_eq!(containers.len(), 1);
    assert_eq!(containers[0].position, Some(Position::Relative));

    // Test align-items with kebab-case bare identifiers
    let containers = container! {
        div align-items=center { "Centered items" }
    };
    assert_eq!(containers.len(), 1);
    assert_eq!(containers[0].align_items, Some(AlignItems::Center));

    let containers = container! {
        div align-items=start { "Start alignment" }
    };
    assert_eq!(containers.len(), 1);
    assert_eq!(containers[0].align_items, Some(AlignItems::Start));

    // Test justify-content with kebab-case bare identifiers
    let containers = container! {
        div justify-content=center { "Centered content" }
    };
    assert_eq!(containers.len(), 1);
    assert_eq!(containers[0].justify_content, Some(JustifyContent::Center));

    let containers = container! {
        div justify-content=space-between { "Space between" }
    };
    assert_eq!(containers.len(), 1);
    assert_eq!(
        containers[0].justify_content,
        Some(JustifyContent::SpaceBetween)
    );

    // Test cursor with kebab-case bare identifiers
    let containers = container! {
        div cursor=pointer { "Pointer cursor" }
    };
    assert_eq!(containers.len(), 1);
    assert_eq!(containers[0].cursor, Some(Cursor::Pointer));

    let containers = container! {
        div cursor=auto { "Auto cursor" }
    };
    assert_eq!(containers.len(), 1);
    assert_eq!(containers[0].cursor, Some(Cursor::Auto));
}

#[test]
fn test_string_literal_enum_attributes() {
    // Test that string literals work (kebab-case conversion)
    let containers = container! {
        div visibility="hidden" { "Hidden with string" }
    };
    assert_eq!(containers.len(), 1);
    assert_eq!(containers[0].visibility, Some(Visibility::Hidden));

    let containers = container! {
        div visibility="visible" { "Visible with string" }
    };
    assert_eq!(containers.len(), 1);
    assert_eq!(containers[0].visibility, Some(Visibility::Visible));

    let containers = container! {
        div direction="row" { "Row layout" }
    };
    assert_eq!(containers.len(), 1);
    assert_eq!(containers[0].direction, LayoutDirection::Row);

    let containers = container! {
        div direction="column" { "Column layout" }
    };
    assert_eq!(containers.len(), 1);
    assert_eq!(containers[0].direction, LayoutDirection::Column);

    let containers = container! {
        div position="fixed" { "Fixed position" }
    };
    assert_eq!(containers.len(), 1);
    assert_eq!(containers[0].position, Some(Position::Fixed));

    let containers = container! {
        div position="absolute" { "Absolute position" }
    };
    assert_eq!(containers.len(), 1);
    assert_eq!(containers[0].position, Some(Position::Absolute));

    let containers = container! {
        div position="relative" { "Relative position" }
    };
    assert_eq!(containers.len(), 1);
    assert_eq!(containers[0].position, Some(Position::Relative));
}

#[test]
fn test_alignment_enum_attributes() {
    // Test align-items with string literals - using correct enum variants
    let containers = container! {
        div align-items="center" { "Centered items" }
    };
    assert_eq!(containers.len(), 1);
    assert_eq!(containers[0].align_items, Some(AlignItems::Center));

    let containers = container! {
        div align-items="start" { "Start alignment" }
    };
    assert_eq!(containers.len(), 1);
    assert_eq!(containers[0].align_items, Some(AlignItems::Start));

    let containers = container! {
        div align-items="end" { "End alignment" }
    };
    assert_eq!(containers.len(), 1);
    assert_eq!(containers[0].align_items, Some(AlignItems::End));

    // Test justify-content with string literals
    let containers = container! {
        div justify-content="center" { "Centered content" }
    };
    assert_eq!(containers.len(), 1);
    assert_eq!(containers[0].justify_content, Some(JustifyContent::Center));

    let containers = container! {
        div justify-content="space-between" { "Space between" }
    };
    assert_eq!(containers.len(), 1);
    assert_eq!(
        containers[0].justify_content,
        Some(JustifyContent::SpaceBetween)
    );

    let containers = container! {
        div justify-content="space-evenly" { "Space evenly" }
    };
    assert_eq!(containers.len(), 1);
    assert_eq!(
        containers[0].justify_content,
        Some(JustifyContent::SpaceEvenly)
    );
}

#[test]
fn test_text_enum_attributes() {
    // Test text-align with string literals - using correct enum variants
    let containers = container! {
        div text-align="center" { "Centered text" }
    };
    assert_eq!(containers.len(), 1);
    assert_eq!(containers[0].text_align, Some(TextAlign::Center));

    let containers = container! {
        div text-align="start" { "Start aligned text" }
    };
    assert_eq!(containers.len(), 1);
    assert_eq!(containers[0].text_align, Some(TextAlign::Start));

    let containers = container! {
        div text-align="end" { "End aligned text" }
    };
    assert_eq!(containers.len(), 1);
    assert_eq!(containers[0].text_align, Some(TextAlign::End));

    let containers = container! {
        div text-align="justify" { "Justified text" }
    };
    assert_eq!(containers.len(), 1);
    assert_eq!(containers[0].text_align, Some(TextAlign::Justify));
}

#[test]
fn test_overflow_enum_attributes() {
    // Test overflow-x with string literals - using correct enum variants
    let containers = container! {
        div overflow-x="hidden" { "Hidden overflow-x" }
    };
    assert_eq!(containers.len(), 1);
    assert_eq!(containers[0].overflow_x, LayoutOverflow::Hidden);

    let containers = container! {
        div overflow-x="scroll" { "Scroll overflow-x" }
    };
    assert_eq!(containers.len(), 1);
    assert_eq!(containers[0].overflow_x, LayoutOverflow::Scroll);

    let containers = container! {
        div overflow-x="auto" { "Auto overflow-x" }
    };
    assert_eq!(containers.len(), 1);
    assert_eq!(containers[0].overflow_x, LayoutOverflow::Auto);

    // Test overflow-y with string literals
    let containers = container! {
        div overflow-y="auto" { "Auto overflow-y" }
    };
    assert_eq!(containers.len(), 1);
    assert_eq!(containers[0].overflow_y, LayoutOverflow::Auto);
}

#[test]
fn test_cursor_enum_attributes() {
    // Test cursor with string literals - using correct enum variants
    let containers = container! {
        div cursor="pointer" { "Pointer cursor" }
    };
    assert_eq!(containers.len(), 1);
    assert_eq!(containers[0].cursor, Some(Cursor::Pointer));

    let containers = container! {
        div cursor="auto" { "Auto cursor" }
    };
    assert_eq!(containers.len(), 1);
    assert_eq!(containers[0].cursor, Some(Cursor::Auto));

    let containers = container! {
        div cursor="text" { "Text cursor" }
    };
    assert_eq!(containers.len(), 1);
    assert_eq!(containers[0].cursor, Some(Cursor::Text));

    let containers = container! {
        div cursor="grab" { "Grab cursor" }
    };
    assert_eq!(containers.len(), 1);
    assert_eq!(containers[0].cursor, Some(Cursor::Grab));
}

#[test]
fn test_expression_enum_attributes() {
    // Test using expressions with enum values
    let containers = container! {
        div visibility=(Visibility::Hidden) { "Hidden with expression" }
    };
    assert_eq!(containers.len(), 1);
    assert_eq!(containers[0].visibility, Some(Visibility::Hidden));

    let containers = container! {
        div align-items=(AlignItems::Center) { "Center with expression" }
    };
    assert_eq!(containers.len(), 1);
    assert_eq!(containers[0].align_items, Some(AlignItems::Center));

    let containers = container! {
        div justify-content=(JustifyContent::SpaceBetween) { "Space between with expression" }
    };
    assert_eq!(containers.len(), 1);
    assert_eq!(
        containers[0].justify_content,
        Some(JustifyContent::SpaceBetween)
    );

    let containers = container! {
        div text-align=(TextAlign::Center) { "Center with expression" }
    };
    assert_eq!(containers.len(), 1);
    assert_eq!(containers[0].text_align, Some(TextAlign::Center));
}

#[test]
fn test_image_enum_attributes() {
    // Test image fit with string literals
    let containers = container! {
        image fit="cover" { }
    };
    assert_eq!(containers.len(), 1);
    if let hyperchad_transformer::Element::Image { fit, .. } = &containers[0].element {
        assert_eq!(fit, &Some(ImageFit::Cover));
    } else {
        panic!("Expected Image element");
    }

    let containers = container! {
        image fit="contain" { }
    };
    assert_eq!(containers.len(), 1);
    if let hyperchad_transformer::Element::Image { fit, .. } = &containers[0].element {
        assert_eq!(fit, &Some(ImageFit::Contain));
    } else {
        panic!("Expected Image element");
    }

    let containers = container! {
        image fit="fill" { }
    };
    assert_eq!(containers.len(), 1);
    if let hyperchad_transformer::Element::Image { fit, .. } = &containers[0].element {
        assert_eq!(fit, &Some(ImageFit::Fill));
    } else {
        panic!("Expected Image element");
    }

    // Test image loading with string literals
    let containers = container! {
        image loading="lazy" { }
    };
    assert_eq!(containers.len(), 1);
    if let hyperchad_transformer::Element::Image { loading, .. } = &containers[0].element {
        assert_eq!(loading, &Some(ImageLoading::Lazy));
    } else {
        panic!("Expected Image element");
    }

    let containers = container! {
        image loading="eager" { }
    };
    assert_eq!(containers.len(), 1);
    if let hyperchad_transformer::Element::Image { loading, .. } = &containers[0].element {
        assert_eq!(loading, &Some(ImageLoading::Eager));
    } else {
        panic!("Expected Image element");
    }
}

#[test]
fn test_anchor_enum_attributes() {
    // Test anchor target with string literals
    let containers = container! {
        anchor target="_blank" { "Link opens in new tab" }
    };
    assert_eq!(containers.len(), 1);
    if let hyperchad_transformer::Element::Anchor { target, .. } = &containers[0].element {
        assert_eq!(target, &Some(LinkTarget::Blank));
    } else {
        panic!("Expected Anchor element");
    }

    let containers = container! {
        anchor target="_self" { "Link opens in same tab" }
    };
    assert_eq!(containers.len(), 1);
    if let hyperchad_transformer::Element::Anchor { target, .. } = &containers[0].element {
        assert_eq!(target, &Some(LinkTarget::SelfTarget));
    } else {
        panic!("Expected Anchor element");
    }

    let containers = container! {
        anchor target="_parent" { "Link opens in parent frame" }
    };
    assert_eq!(containers.len(), 1);
    if let hyperchad_transformer::Element::Anchor { target, .. } = &containers[0].element {
        assert_eq!(target, &Some(LinkTarget::Parent));
    } else {
        panic!("Expected Anchor element");
    }

    let containers = container! {
        anchor target="_top" { "Link opens in top frame" }
    };
    assert_eq!(containers.len(), 1);
    if let hyperchad_transformer::Element::Anchor { target, .. } = &containers[0].element {
        assert_eq!(target, &Some(LinkTarget::Top));
    } else {
        panic!("Expected Anchor element");
    }
}

#[test]
fn test_mixed_enum_attribute_syntax() {
    // Test mixing strings and expressions
    let is_hidden = false;
    let containers = container! {
        div
            visibility="hidden"
            align-items="center"
            justify-content=(if is_hidden { JustifyContent::End } else { JustifyContent::Center })
            direction="row"
        {
            "Mixed syntax"
        }
    };

    assert_eq!(containers.len(), 1);
    assert_eq!(containers[0].visibility, Some(Visibility::Hidden));
    assert_eq!(containers[0].align_items, Some(AlignItems::Center));
    assert_eq!(containers[0].justify_content, Some(JustifyContent::Center));
    assert_eq!(containers[0].direction, LayoutDirection::Row);
}

#[test]
fn test_complex_expression_enum_attributes() {
    // Test using complex expressions that evaluate to enum values
    let use_center_alignment = true;
    let containers = container! {
        div align-items=(if use_center_alignment { AlignItems::Center } else { AlignItems::Start }) {
            "Dynamic alignment"
        }
    };
    assert_eq!(containers.len(), 1);
    assert_eq!(containers[0].align_items, Some(AlignItems::Center));

    // Test with expressions
    let containers = container! {
        div position=(Position::Fixed) { "Expression position" }
    };
    assert_eq!(containers.len(), 1);
    assert_eq!(containers[0].position, Some(Position::Fixed));

    // Test with function calls
    fn get_text_alignment() -> TextAlign {
        TextAlign::End
    }
    let containers = container! {
        div text-align=(get_text_alignment()) { "Function-determined alignment" }
    };
    assert_eq!(containers.len(), 1);
    assert_eq!(containers[0].text_align, Some(TextAlign::End));
}

#[test]
fn test_brace_wrapped_enum_attributes() {
    // Test expressions wrapped in braces
    let containers = container! {
        div visibility={Visibility::Hidden} { "Brace-wrapped expression" }
    };
    assert_eq!(containers.len(), 1);
    assert_eq!(containers[0].visibility, Some(Visibility::Hidden));

    // Test complex expressions in braces
    let should_be_visible = true;
    let containers = container! {
        div visibility={if should_be_visible { Visibility::Visible } else { Visibility::Hidden }} {
            "Brace-wrapped expression"
        }
    };
    assert_eq!(containers.len(), 1);
    assert_eq!(containers[0].visibility, Some(Visibility::Visible));
}

#[test]
fn test_multiple_enum_attributes_on_single_element() {
    // Test an element with multiple enum attributes using different syntax styles
    let containers = container! {
        div
            visibility="visible"
            direction="column"
            align-items="center"
            justify-content="space-between"
            position="relative"
            cursor="pointer"
            overflow-x="hidden"
            overflow-y="scroll"
            text-align="center"
        {
            "Element with many enum attributes"
        }
    };

    assert_eq!(containers.len(), 1);
    let container = &containers[0];

    assert_eq!(container.visibility, Some(Visibility::Visible));
    assert_eq!(container.direction, LayoutDirection::Column);
    assert_eq!(container.align_items, Some(AlignItems::Center));
    assert_eq!(
        container.justify_content,
        Some(JustifyContent::SpaceBetween)
    );
    assert_eq!(container.position, Some(Position::Relative));
    assert_eq!(container.cursor, Some(Cursor::Pointer));
    assert_eq!(container.overflow_x, LayoutOverflow::Hidden);
    assert_eq!(container.overflow_y, LayoutOverflow::Scroll);
    assert_eq!(container.text_align, Some(TextAlign::Center));
}

#[test]
fn test_default_enum_values() {
    // Test that elements get correct default values when no enum attributes are specified
    let containers = container! {
        div { "Default values" }
    };

    assert_eq!(containers.len(), 1);
    let container = &containers[0];

    // These should be None/Default as they weren't explicitly set
    assert_eq!(container.visibility, None);
    assert_eq!(container.direction, LayoutDirection::default());
    assert_eq!(container.align_items, None);
    assert_eq!(container.justify_content, None);
    assert_eq!(container.position, None);
    assert_eq!(container.cursor, None);
    assert_eq!(container.overflow_x, LayoutOverflow::default());
    assert_eq!(container.overflow_y, LayoutOverflow::default());
    assert_eq!(container.text_align, None);
}