alef 0.78.0

Opinionated polyglot binding generator for Rust libraries
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
//! Tests for Dart plugin-trait test stub generation.
//!
//! These tests verify that the e2e stub emitter (`src/e2e/codegen/dart.rs::emit_test_backend`)
//! generates correct Dart code for trait-bridge plugin implementations. They ensure that:
//!
//! 1. Async trait methods generate `async` stub methods with `Future<R>` returns
//! 2. Sync trait methods generate non-async stubs with direct return types
//! 3. Return type mapping preserves async wrappers (generates `Future<T>` not an unbridged named type)
//! 4. Internal-only named types are mapped to explicit bridge carriers
//! 5. Wrapper fields use appropriate initialization (factory call without eager construction)
//! 6. Unimplemented trait methods throw `UnimplementedError()` instead of returning empty defaults

#[cfg(test)]
mod plugin_trait_stub_generation {
    use crate::core::config::TraitBridgeConfig;
    use crate::core::ir::{MethodDef, ParamDef, PrimitiveType, ReceiverKind, TypeRef};
    use crate::e2e::fixture::Fixture;
    use serde_json::json;

    fn emit_test_backend_dart(
        bridge: &TraitBridgeConfig,
        methods: &[&MethodDef],
        fixture: &Fixture,
    ) -> crate::e2e::codegen::TestBackendEmission {
        crate::e2e::codegen::emit_test_backend("dart", bridge, methods, fixture, &[], "")
    }

    /// Helper to create a test trait bridge.
    fn make_trait_bridge(name: &str, super_trait: Option<&str>) -> TraitBridgeConfig {
        TraitBridgeConfig {
            trait_name: name.to_string(),
            super_trait: super_trait.map(|s| s.to_string()),
            register_fn: Some(format!("register_{}", name.to_lowercase())),
            unregister_fn: Some(format!("unregister_{}", name.to_lowercase())),
            clear_fn: Some(format!("clear_{}", name.to_lowercase())),
            ..Default::default()
        }
    }

    /// Helper to create a test method with specified async-ness and return type.
    fn make_method(name: &str, is_async: bool, return_type: TypeRef, params: Vec<ParamDef>) -> MethodDef {
        MethodDef {
            name: name.to_string(),
            params,
            return_type,
            is_async,
            is_static: false,
            error_type: None,
            doc: String::new(),
            receiver: Some(ReceiverKind::Ref),
            cfg: None,
            sanitized: false,
            trait_source: None,
            returns_ref: false,
            returns_cow: false,
            return_newtype_wrapper: None,
            has_default_impl: false,
            binding_excluded: false,
            binding_exclusion_reason: None,
            version: Default::default(),
        }
    }

    /// Helper to create a fixture.
    fn make_fixture(id: &str, plugin_name: Option<&str>) -> Fixture {
        let mut input_json = json!({});
        if let Some(name) = plugin_name {
            input_json["name"] = json!(name);
        }

        Fixture {
            id: id.to_string(),
            category: None,
            description: "test".to_string(),
            docs: None,
            requirements: Vec::new(),
            tags: vec![],
            skip: None,
            env: None,
            setup: Vec::new(),
            call: None,
            input: input_json,
            mock_response: None,
            source: String::new(),
            http: None,
            asyncapi: None,
            websocket: None,
            preserve_input_urls: false,
            assertions: vec![],
            visitor: None,
            args: vec![],
            assertion_recipes: vec![],
        }
    }

    #[test]
    fn async_method_generates_async_keyword_and_future_return() {
        let bridge = make_trait_bridge("TestBackend", Some("Plugin"));
        let async_method = make_method("process", true, TypeRef::Named("ExtractionResult".to_string()), vec![]);
        let methods = [&async_method];
        let fixture = make_fixture("async_test", Some("test-backend"));

        let emission = emit_test_backend_dart(&bridge, &methods, &fixture);

        assert!(
            emission.setup_block.contains("Future<ExtractionResult> process(")
                || emission.setup_block.contains("Future< ExtractionResult > process("),
            "async method must have Future<T> return type, got:\n{}",
            emission.setup_block
        );
        assert!(
            emission.setup_block.contains("async =>"),
            "async method must have async keyword, got:\n{}",
            emission.setup_block
        );
    }

    #[test]
    fn sync_method_generates_future_stub_for_trait_bridge_factory() {
        let bridge = make_trait_bridge("TestValidator", Some("Plugin"));
        let sync_method = make_method("validate", false, TypeRef::Primitive(PrimitiveType::Bool), vec![]);
        let methods = [&sync_method];
        let fixture = make_fixture("sync_test", Some("test-validator"));

        let emission = emit_test_backend_dart(&bridge, &methods, &fixture);

        assert!(
            emission.setup_block.contains("Future<bool> validate()"),
            "sync method must be adapted to Future<T>, got:\n{}",
            emission.setup_block
        );
        let validate_section = emission
            .setup_block
            .lines()
            .filter(|l| l.contains("validate"))
            .collect::<Vec<_>>()
            .join("\n");
        assert!(
            validate_section.contains("async =>"),
            "sync method stub must be async for the factory callback, got:\n{}",
            validate_section
        );
    }

    #[test]
    fn internal_record_type_maps_to_bridge_type() {
        let bridge = make_trait_bridge("TestExtractor", Some("Plugin"));
        let method_with_internal = make_method("extract", true, TypeRef::Named("InternalRecord".to_string()), vec![]);
        let methods = [&method_with_internal];
        let fixture = make_fixture("extract_test", Some("test-extractor"));

        let emission = emit_test_backend_dart(&bridge, &methods, &fixture);

        assert!(
            emission.setup_block.contains("Future<InternalRecordBridge>")
                || emission.setup_block.contains("Future< InternalRecordBridge >"),
            "InternalRecord return type must be mapped to InternalRecordBridge, got:\n{}",
            emission.setup_block
        );
    }

    #[test]
    fn wrapper_instance_awaits_async_factory_call() {
        let bridge = make_trait_bridge("OcrBackend", Some("Plugin"));
        let method = make_method("process", true, TypeRef::Named("String".to_string()), vec![]);
        let methods = [&method];
        let fixture = make_fixture("ocr_test", Some("test-ocr"));

        let emission = emit_test_backend_dart(&bridge, &methods, &fixture);

        assert!(
            emission.setup_block.contains("await createOcrBackendDartImpl"),
            "factory call must be awaited, got:\n{}",
            emission.setup_block
        );
        assert!(
            emission.setup_block.contains("createOcrBackendDartImpl("),
            "factory function must be called, got:\n{}",
            emission.setup_block
        );
    }

    #[test]
    fn method_callbacks_provided_for_all_methods() {
        let bridge = make_trait_bridge("MultiMethod", Some("Plugin"));
        let method1 = make_method("doFirst", true, TypeRef::Primitive(PrimitiveType::Bool), vec![]);
        let method2 = make_method("doSecond", true, TypeRef::Named("String".to_string()), vec![]);
        let methods = [&method1, &method2];
        let fixture = make_fixture("multi_test", Some("test-multi"));

        let emission = emit_test_backend_dart(&bridge, &methods, &fixture);

        assert!(
            emission.setup_block.contains("doFirst:") && emission.setup_block.contains("doSecond:"),
            "all methods must have callbacks in factory call, got:\n{}",
            emission.setup_block
        );
    }

    #[test]
    fn fixture_input_name_used_as_plugin_name() {
        let bridge = make_trait_bridge("TestBackend", Some("Plugin"));
        let method = make_method("test", true, TypeRef::Primitive(PrimitiveType::Bool), vec![]);
        let methods = [&method];
        let fixture = make_fixture("some_id", Some("my-custom-backend"));

        let emission = emit_test_backend_dart(&bridge, &methods, &fixture);

        assert!(
            emission.setup_block.contains("pluginName: 'my-custom-backend'"),
            "pluginName must use fixture input name field, got:\n{}",
            emission.setup_block
        );
        assert!(
            !emission.setup_block.contains("pluginName: 'some_id'"),
            "pluginName must not use fixture id when input name is available, got:\n{}",
            emission.setup_block
        );
    }

    #[test]
    fn class_name_derived_from_fixture_id() {
        let bridge = make_trait_bridge("Backend", Some("Plugin"));
        let method = make_method("test", true, TypeRef::Primitive(PrimitiveType::Bool), vec![]);
        let methods = [&method];
        let fixture = make_fixture("custom_fixture_id", None);

        let emission = emit_test_backend_dart(&bridge, &methods, &fixture);

        assert!(
            emission
                .setup_block
                .contains("class TestStubCustomFixtureId extends Backend"),
            "class name must be derived from fixture id in PascalCase, got:\n{}",
            emission.setup_block
        );
    }

    #[test]
    fn method_parameters_are_typed() {
        let bridge = make_trait_bridge("TestBackend", Some("Plugin"));
        let param = ParamDef {
            name: "input".to_string(),
            ty: TypeRef::Named("String".to_string()),
            optional: false,
            ..Default::default()
        };

        let method = make_method("process", true, TypeRef::Named("Result".to_string()), vec![param]);
        let methods = [&method];
        let fixture = make_fixture("typed_params_test", Some("test-backend"));

        let emission = emit_test_backend_dart(&bridge, &methods, &fixture);

        assert!(
            emission.setup_block.contains("String input") || emission.setup_block.contains("String  input"),
            "parameters must be typed, not dynamic, got:\n{}",
            emission.setup_block
        );
    }

    use crate::core::ir::{EnumDef, EnumVariant, FieldDef};

    fn unit_variant(name: &str, is_default: bool) -> EnumVariant {
        EnumVariant {
            name: name.to_string(),
            fields: Vec::new(),
            is_default,
            ..Default::default()
        }
    }

    fn data_variant(name: &str) -> EnumVariant {
        EnumVariant {
            name: name.to_string(),
            fields: vec![FieldDef {
                name: "scale_max".to_string(),
                ty: TypeRef::Primitive(PrimitiveType::F64),
                ..Default::default()
            }],
            ..Default::default()
        }
    }

    fn emit_test_backend_dart_with_enums(
        bridge: &TraitBridgeConfig,
        methods: &[&MethodDef],
        fixture: &Fixture,
        enums: &[EnumDef],
    ) -> crate::e2e::codegen::TestBackendEmission {
        crate::e2e::codegen::emit_test_backend("dart", bridge, methods, fixture, enums, "")
    }

    /// The core regression: a Freezed-backed enum (any variant carries fields) must
    /// reference its default unit variant through its factory constructor call
    /// (`Type.variant()`), never a bare member reference -- which Dart parses as a
    /// constructor tear-off, not a constructed value, where a `Future<T>` return is
    /// required.
    #[test]
    fn sealed_class_enum_default_calls_the_factory_constructor() {
        let bridge = make_trait_bridge("TestBackend", Some("Plugin"));
        let method = make_method(
            "sample_classification",
            true,
            TypeRef::Named("SampleClassification".to_string()),
            vec![],
        );
        let methods = [&method];
        let fixture = make_fixture("sealed_default_test", Some("test-backend"));
        let enums = [EnumDef {
            name: "SampleClassification".to_string(),
            variants: vec![
                data_variant("Scored"),
                unit_variant("Baseline", true),
                unit_variant("Unset", false),
            ],
            ..Default::default()
        }];

        let emission = emit_test_backend_dart_with_enums(&bridge, &methods, &fixture, &enums);

        assert!(
            emission.setup_block.contains("SampleClassification.baseline()"),
            "must call the default unit variant's factory constructor, got:\n{}",
            emission.setup_block
        );
        assert!(
            !emission.setup_block.contains("SampleClassification.scored"),
            "must not reference the data-carrying first variant, got:\n{}",
            emission.setup_block
        );
    }

    /// An all-unit enum lowers to a genuine Dart `enum`; its default member must be
    /// referenced directly with lowerCamelCase casing and no call parentheses (it is not a
    /// constructor). Pins both halves of the reported defect: the exact variant name
    /// (`AutoCorrected` -> `autoCorrected`, not `autocorrected`) and the shape (no `()`).
    #[test]
    fn all_unit_enum_default_uses_lower_camel_case_member_with_no_parens() {
        let bridge = make_trait_bridge("TestBackend", Some("Plugin"));
        let method = make_method(
            "sample_orientation",
            true,
            TypeRef::Named("SampleOrientation".to_string()),
            vec![],
        );
        let methods = [&method];
        let fixture = make_fixture("plain_enum_default_test", Some("test-backend"));
        let enums = [EnumDef {
            name: "SampleOrientation".to_string(),
            variants: vec![
                unit_variant("AutoCorrected", true),
                unit_variant("PartiallyRotated", false),
                unit_variant("RequiresManualFix", false),
            ],
            ..Default::default()
        }];

        let emission = emit_test_backend_dart_with_enums(&bridge, &methods, &fixture, &enums);

        assert!(
            emission.setup_block.contains("SampleOrientation.autoCorrected"),
            "must reference the default variant in lowerCamelCase, got:\n{}",
            emission.setup_block
        );
        assert!(
            !emission.setup_block.contains("autocorrected"),
            "must not use the raw-lowercased casing that does not exist on the generated \
             type, got:\n{}",
            emission.setup_block
        );
        assert!(
            !emission.setup_block.contains("SampleOrientation.autoCorrected()"),
            "a plain-enum member is not a constructor call and must not be parenthesized, \
             got:\n{}",
            emission.setup_block
        );
    }

    /// Negative control: when no variant is marked `#[default]`, the first *fieldless*
    /// variant wins -- proving the fallback does not simply take `variants[0]` (which here
    /// carries fields and would need a real `scaleMax` value to compile).
    #[test]
    fn no_default_variant_falls_back_to_first_fieldless_variant() {
        let bridge = make_trait_bridge("TestBackend", Some("Plugin"));
        let method = make_method(
            "sample_classification",
            true,
            TypeRef::Named("SampleClassification".to_string()),
            vec![],
        );
        let methods = [&method];
        let fixture = make_fixture("no_default_variant_test", Some("test-backend"));
        let enums = [EnumDef {
            name: "SampleClassification".to_string(),
            variants: vec![data_variant("Scored"), unit_variant("Baseline", false)],
            ..Default::default()
        }];

        let emission = emit_test_backend_dart_with_enums(&bridge, &methods, &fixture, &enums);

        assert!(
            emission.setup_block.contains("SampleClassification.baseline()"),
            "must fall back to the first fieldless variant when none is marked default, \
             got:\n{}",
            emission.setup_block
        );
    }

    /// Negative control: a plain scalar return type is unaffected by the enum-lookup
    /// fallback.
    #[test]
    fn scalar_return_type_is_unaffected_by_enum_lookup() {
        let bridge = make_trait_bridge("TestValidator", Some("Plugin"));
        let method = make_method("priority", false, TypeRef::Primitive(PrimitiveType::I32), vec![]);
        let methods = [&method];
        let fixture = make_fixture("scalar_unaffected_test", Some("test-validator"));
        let enums = [EnumDef {
            name: "SampleClassification".to_string(),
            variants: vec![unit_variant("Baseline", true)],
            ..Default::default()
        }];

        let emission = emit_test_backend_dart_with_enums(&bridge, &methods, &fixture, &enums);

        assert!(
            emission.setup_block.contains("Future<int> priority()"),
            "got:\n{}",
            emission.setup_block
        );
    }
}