dampen-core 0.3.2

Core parser, IR, and traits for Dampen UI framework
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
//! Codegen snapshot tests
//!
//! These tests verify that code generation produces consistent output.
//! They use insta for snapshot testing.

use dampen_core::{HandlerSignature, generate_application, parse};

#[test]
fn test_codegen_simple_button() {
    let xml = r#"<button label="Click" on_click="handle_click" />"#;
    let doc = parse(xml).unwrap();

    let handlers = vec![HandlerSignature {
        name: "handle_click".to_string(),
        param_type: None,
        returns_command: false,
    }];

    let output = generate_application(&doc, "Model", "Message", &handlers).unwrap();

    // Use insta to snapshot test
    insta::assert_snapshot!(output.code);
}

#[test]
fn test_codegen_text_with_binding() {
    let xml = r#"<text value="{counter}" size="24" />"#;
    let doc = parse(xml).unwrap();

    let handlers = vec![];

    let output = generate_application(&doc, "Model", "Message", &handlers).unwrap();

    insta::assert_snapshot!(output.code);
}

#[test]
fn test_codegen_column_with_children() {
    let xml = r#"
        <column spacing="10">
            <text value="Header" />
            <button label="Submit" on_click="submit" />
        </column>
    "#;
    let doc = parse(xml).unwrap();

    let handlers = vec![HandlerSignature {
        name: "submit".to_string(),
        param_type: None,
        returns_command: false,
    }];

    let output = generate_application(&doc, "Model", "Message", &handlers).unwrap();

    insta::assert_snapshot!(output.code);
}

#[test]
fn test_codegen_conditional_enabled() {
    let xml = r#"<button label="Submit" on_click="submit" enabled="{count > 0}" />"#;
    let doc = parse(xml).unwrap();

    let handlers = vec![HandlerSignature {
        name: "submit".to_string(),
        param_type: None,
        returns_command: false,
    }];

    let output = generate_application(&doc, "Model", "Message", &handlers).unwrap();

    insta::assert_snapshot!(output.code);
}

#[test]
fn test_codegen_handler_with_value() {
    let xml = r#"<text_input value="{name}" on_input="update_name" />"#;
    let doc = parse(xml).unwrap();

    let handlers = vec![HandlerSignature {
        name: "update_name".to_string(),
        param_type: Some("String".to_string()),
        returns_command: false,
    }];

    let output = generate_application(&doc, "Model", "Message", &handlers).unwrap();

    insta::assert_snapshot!(output.code);
}

#[test]
fn test_codegen_formatted_binding() {
    let xml = r#"<text value="Count: {count}, Name: {name}" />"#;
    let doc = parse(xml).unwrap();

    let handlers = vec![];

    let output = generate_application(&doc, "Model", "Message", &handlers).unwrap();

    insta::assert_snapshot!(output.code);
}

#[test]
fn test_codegen_complex_layout() {
    let xml = r#"
        <column padding="20" spacing="15">
            <text value="Todo App" size="32" weight="bold" />
            <row spacing="10">
                <text_input
                    value="{new_item}"
                    on_input="update_new_item"
                    placeholder="Add todo..."
                    width="fill"
                />
                <button label="Add" on_click="add_item" enabled="{new_item.len() > 0}" />
            </row>
            <rule />
            <text value="{items.len()} items" />
        </column>
    "#;
    let doc = parse(xml).unwrap();

    let handlers = vec![
        HandlerSignature {
            name: "update_new_item".to_string(),
            param_type: Some("String".to_string()),
            returns_command: false,
        },
        HandlerSignature {
            name: "add_item".to_string(),
            param_type: None,
            returns_command: false,
        },
    ];

    let output = generate_application(&doc, "Model", "Message", &handlers).unwrap();

    insta::assert_snapshot!(output.code);
}

#[test]
fn test_codegen_method_calls() {
    let xml = r#"
        <column>
            <text value="{items.len()}" />
            <text value="{name.to_uppercase()}" />
            <button label="Clear" on_click="clear" enabled="{items.len() > 0}" />
        </column>
    "#;
    let doc = parse(xml).unwrap();

    let handlers = vec![HandlerSignature {
        name: "clear".to_string(),
        param_type: None,
        returns_command: false,
    }];

    let output = generate_application(&doc, "Model", "Message", &handlers).unwrap();

    insta::assert_snapshot!(output.code);
}

#[test]
fn test_codegen_conditional_expressions() {
    let xml = r#"
        <column>
            <text value="{if is_loading then 'Loading...' else 'Ready'}" />
            <text value="{if error then 'Error' else 'Success'}" />
            <button
                label="Submit"
                on_click="submit"
                enabled="{count > 0}"
            />
        </column>
    "#;
    let doc = parse(xml).unwrap();

    let handlers = vec![HandlerSignature {
        name: "submit".to_string(),
        param_type: None,
        returns_command: false,
    }];

    let output = generate_application(&doc, "Model", "Message", &handlers).unwrap();

    insta::assert_snapshot!(output.code);
}

#[test]
fn test_codegen_zero_runtime_dependencies() {
    let xml = r#"
        <column>
            <text value="{name}" />
            <text value="Count: {count + 1}" />
            <button label="Click" on_click="handle_click" />
        </column>
    "#;
    let doc = parse(xml).unwrap();

    let handlers = vec![HandlerSignature {
        name: "handle_click".to_string(),
        param_type: None,
        returns_command: false,
    }];

    let output = generate_application(&doc, "Model", "Message", &handlers).unwrap();
    let code = output.code;

    assert!(
        !code.contains("to_binding_value"),
        "Generated code should not contain to_binding_value() calls"
    );
    assert!(
        !code.contains("BindingValue"),
        "Generated code should not contain BindingValue type"
    );
    assert!(
        code.contains("to_string"),
        "Generated code should use to_string() for string conversion"
    );
}

// ============================================================================
// Comprehensive Widget Type Coverage Tests
// ============================================================================

#[test]
fn test_codegen_container() {
    let xml =
        r#"<container width="300" height="200" padding="20"><text value="Inside" /></container>"#;
    let doc = parse(xml).unwrap();
    let handlers = vec![];
    let output = generate_application(&doc, "Model", "Message", &handlers).unwrap();
    insta::assert_snapshot!(output.code);
}

#[test]
fn test_codegen_scrollable() {
    let xml = r#"
        <scrollable height="400">
            <column>
                <text value="Line 1" />
                <text value="Line 2" />
                <text value="Line 3" />
            </column>
        </scrollable>
    "#;
    let doc = parse(xml).unwrap();
    let handlers = vec![];
    let output = generate_application(&doc, "Model", "Message", &handlers).unwrap();
    insta::assert_snapshot!(output.code);
}

#[test]
fn test_codegen_stack() {
    let xml = r#"
        <stack>
            <container><text value="Background" /></container>
            <container><text value="Overlay" /></container>
        </stack>
    "#;
    let doc = parse(xml).unwrap();
    let handlers = vec![];
    let output = generate_application(&doc, "Model", "Message", &handlers).unwrap();
    insta::assert_snapshot!(output.code);
}

#[test]
fn test_codegen_checkbox() {
    let xml = r#"<checkbox label="Accept Terms" checked="{accepted}" on_toggle="toggle_accept" />"#;
    let doc = parse(xml).unwrap();
    let handlers = vec![HandlerSignature {
        name: "toggle_accept".to_string(),
        param_type: Some("bool".to_string()),
        returns_command: false,
    }];
    let output = generate_application(&doc, "Model", "Message", &handlers).unwrap();
    insta::assert_snapshot!(output.code);
}

#[test]
fn test_codegen_slider() {
    let xml = r#"<slider min="0" max="100" value="{volume}" on_change="set_volume" />"#;
    let doc = parse(xml).unwrap();
    let handlers = vec![HandlerSignature {
        name: "set_volume".to_string(),
        param_type: Some("f32".to_string()),
        returns_command: false,
    }];
    let output = generate_application(&doc, "Model", "Message", &handlers).unwrap();
    insta::assert_snapshot!(output.code);
}

#[test]
fn test_codegen_picklist() {
    let xml = r#"<pick_list placeholder="Select option" options="Option1,Option2,Option3" selected="{selected_option}" on_select="handle_select" />"#;
    let doc = parse(xml).unwrap();
    let handlers = vec![HandlerSignature {
        name: "handle_select".to_string(),
        param_type: Some("String".to_string()),
        returns_command: false,
    }];
    let output = generate_application(&doc, "Model", "Message", &handlers).unwrap();
    insta::assert_snapshot!(output.code);
}

#[test]
fn test_codegen_toggler() {
    let xml = r#"<toggler label="Enable Feature" toggled="{feature_enabled}" on_toggle="toggle_feature" />"#;
    let doc = parse(xml).unwrap();
    let handlers = vec![HandlerSignature {
        name: "toggle_feature".to_string(),
        param_type: Some("bool".to_string()),
        returns_command: false,
    }];
    let output = generate_application(&doc, "Model", "Message", &handlers).unwrap();
    insta::assert_snapshot!(output.code);
}

#[test]
fn test_codegen_space() {
    let xml = r#"<column><text value="Before" /><space width="50" height="20" /><text value="After" /></column>"#;
    let doc = parse(xml).unwrap();
    let handlers = vec![];
    let output = generate_application(&doc, "Model", "Message", &handlers).unwrap();
    insta::assert_snapshot!(output.code);
}

#[test]
fn test_codegen_image() {
    let xml = r#"<image src="assets/logo.png" width="200" height="100" />"#;
    let doc = parse(xml).unwrap();
    let handlers = vec![];
    let output = generate_application(&doc, "Model", "Message", &handlers).unwrap();
    insta::assert_snapshot!(output.code);
}

#[test]
fn test_codegen_svg() {
    let xml = r#"<svg src="assets/icon.svg" width="24" height="24" />"#;
    let doc = parse(xml).unwrap();
    let handlers = vec![];
    let output = generate_application(&doc, "Model", "Message", &handlers).unwrap();
    insta::assert_snapshot!(output.code);
}

#[test]
fn test_codegen_radio() {
    let xml = r#"
        <column>
            <radio label="Option A" value="a" selected="{option}" on_select="select_option" />
            <radio label="Option B" value="b" selected="{option}" on_select="select_option" />
        </column>
    "#;
    let doc = parse(xml).unwrap();
    let handlers = vec![HandlerSignature {
        name: "select_option".to_string(),
        param_type: Some("String".to_string()),
        returns_command: false,
    }];
    let output = generate_application(&doc, "Model", "Message", &handlers).unwrap();
    insta::assert_snapshot!(output.code);
}

#[test]
fn test_codegen_progress_bar() {
    let xml = r#"<progress_bar value="{progress}" min="0" max="100" />"#;
    let doc = parse(xml).unwrap();
    let handlers = vec![];
    let output = generate_application(&doc, "Model", "Message", &handlers).unwrap();
    insta::assert_snapshot!(output.code);
}

#[test]
fn test_codegen_tooltip() {
    let xml = r#"
        <tooltip message="Click to perform action">
            <button label="Action" on_click="perform_action" />
        </tooltip>
    "#;
    let doc = parse(xml).unwrap();
    let handlers = vec![HandlerSignature {
        name: "perform_action".to_string(),
        param_type: None,
        returns_command: false,
    }];
    let output = generate_application(&doc, "Model", "Message", &handlers).unwrap();
    insta::assert_snapshot!(output.code);
}

// ComboBox test removed - widget not implemented yet

#[test]
fn test_codegen_float() {
    let xml = r#"
        <float>
            <column>
                <button label="Floating Button" on_click="float_action" />
            </column>
        </float>
    "#;
    let doc = parse(xml).unwrap();
    let handlers = vec![HandlerSignature {
        name: "float_action".to_string(),
        param_type: None,
        returns_command: false,
    }];
    let output = generate_application(&doc, "Model", "Message", &handlers).unwrap();
    insta::assert_snapshot!(output.code);
}

#[test]
fn test_codegen_complex_nested() {
    let xml = r#"
        <scrollable height="600">
            <column padding="20" spacing="10">
                <text value="Dashboard" size="32" weight="bold" />
                <rule />
                <row spacing="20">
                    <container width="300" padding="15">
                        <column spacing="10">
                            <text value="Stats" size="20" weight="bold" />
                            <text value="Total: {total}" />
                            <text value="Active: {active}" />
                            <progress_bar value="{progress}" min="0" max="100" />
                        </column>
                    </container>
                    <container width="400" padding="15">
                        <column spacing="10">
                            <text value="Actions" size="20" weight="bold" />
                            <checkbox label="Enable feature" checked="{enabled}" on_toggle="toggle" />
                            <slider min="0" max="100" value="{value}" on_change="update_value" />
                            <row spacing="10">
                                <button label="Save" on_click="save" />
                                <button label="Cancel" on_click="cancel" />
                            </row>
                        </column>
                    </container>
                </row>
            </column>
        </scrollable>
    "#;
    let doc = parse(xml).unwrap();
    let handlers = vec![
        HandlerSignature {
            name: "toggle".to_string(),
            param_type: Some("bool".to_string()),
            returns_command: false,
        },
        HandlerSignature {
            name: "update_value".to_string(),
            param_type: Some("f32".to_string()),
            returns_command: false,
        },
        HandlerSignature {
            name: "save".to_string(),
            param_type: None,
            returns_command: false,
        },
        HandlerSignature {
            name: "cancel".to_string(),
            param_type: None,
            returns_command: false,
        },
    ];
    let output = generate_application(&doc, "Model", "Message", &handlers).unwrap();
    insta::assert_snapshot!(output.code);
}

#[test]
fn test_codegen_all_binding_types() {
    let xml = r#"
        <column>
            <text value="{field}" />
            <text value="{field.subfield}" />
            <text value="{field.method()}" />
            <text value="{a + b}" />
            <text value="{a - b}" />
            <text value="{a * b}" />
            <text value="{a / b}" />
            <text value="{a == b}" />
            <text value="{a != b}" />
            <text value="{a &lt; b}" />
            <text value="{a &gt; b}" />
            <text value="{if cond then 'yes' else 'no'}" />
        </column>
    "#;
    let doc = parse(xml).unwrap();
    let handlers = vec![];
    let output = generate_application(&doc, "Model", "Message", &handlers).unwrap();
    insta::assert_snapshot!(output.code);
}

#[test]
fn test_codegen_data_table() {
    let xml = r#"<data_table data="{users}">
        <data_column header="Name" field="name" />
        <data_column header="Email" field="email" width="fill" />
    </data_table>"#;
    let doc = parse(xml).unwrap();
    let handlers = vec![];
    let output = generate_application(&doc, "Model", "Message", &handlers).unwrap();
    insta::assert_snapshot!(output.code);
}

#[test]
fn test_codegen_data_table_event() {
    let xml = r#"<data_table data="{users}" on_row_click="select_user">
        <data_column header="Name" field="name" />
    </data_table>"#;
    let doc = parse(xml).unwrap();
    let handlers = vec![dampen_core::HandlerSignature {
        name: "select_user".to_string(),
        param_type: None,
        returns_command: false,
    }];
    let output = generate_application(&doc, "Model", "Message", &handlers).unwrap();

    let code = output.code.to_string();
    // assert!(code.contains("on_row_click"));
    // assert!(code.contains("select_user"));
    // data="{users}" -> model.users
    assert!(code.contains("users"));
    insta::assert_snapshot!(output.code);
}

#[test]
fn test_codegen_data_table_template() {
    let xml = r#"<data_table data="{users}">
        <data_column header="Actions">
             <button label="Delete {index}" on_click="delete" />
        </data_column>
    </data_table>"#;
    let doc = parse(xml).unwrap();
    let handlers = vec![dampen_core::HandlerSignature {
        name: "delete".to_string(),
        param_type: None,
        returns_command: false,
    }];
    let output = generate_application(&doc, "Model", "Message", &handlers).unwrap();
    insta::assert_snapshot!(output.code);
}