alef 0.79.3

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
use super::*;

#[cfg(test)]
mod strict_field_availability_marker_tests {
    use super::render_assertion;
    use crate::e2e::field_access::FieldResolver;
    use crate::e2e::fixture::Assertion;
    use std::collections::{HashMap, HashSet};

    /// Regression test for alef task #81: Kotlin's "skipped: field not available"
    /// comment text must survive as the exact marker the shared
    /// `crate::e2e::codegen::fail_on_unavailable_field_markers` mechanism matches on
    /// (wired into `kotlin/test_method.rs`, shared by `kotlin` and `kotlin_android`),
    /// so arming `ALEF_E2E_STRICT_FIELD_AVAILABILITY` turns a dropped field assertion
    /// into a generation-time failure instead of a silently-passing comment.
    #[test]
    fn unavailable_field_skip_comment_carries_the_strict_mode_marker() {
        let result_fields: HashSet<String> = ["content".to_string()].into_iter().collect();
        let resolver = FieldResolver::new(
            &HashMap::new(),
            &HashSet::new(),
            &result_fields,
            &HashSet::new(),
            &HashSet::new(),
        );
        let assertion = Assertion {
            assertion_type: "equals".to_string(),
            field: Some("nonexistent_field".to_string()),
            value: Some(serde_json::json!("x")),
            ..Assertion::default()
        };
        let mut out = String::new();
        render_assertion(
            &mut out,
            &assertion,
            "result",
            "SampleClient",
            &resolver,
            false,
            false,
            &HashSet::new(),
            &HashSet::new(),
            &HashMap::new(),
            false,
            false,
            true,
        );
        assert!(out.contains("field 'nonexistent_field' not available"), "got: {out}");
    }
}

#[cfg(test)]
mod is_true_optional_field_tests {
    use super::render_assertion;
    use crate::e2e::field_access::FieldResolver;
    use crate::e2e::fixture::Assertion;
    use std::collections::{HashMap, HashSet};

    fn render(assertion: &Assertion, optional_field: &str, kotlin_android_style: bool) -> String {
        let optional: HashSet<String> = [optional_field.to_string()].into_iter().collect();
        let resolver = FieldResolver::new(
            &HashMap::new(),
            &optional,
            &HashSet::new(),
            &HashSet::new(),
            &HashSet::new(),
        );
        let mut out = String::new();
        render_assertion(
            &mut out,
            assertion,
            "result",
            "SampleClient",
            &resolver,
            false,
            false,
            &HashSet::new(),
            &HashSet::new(),
            &HashMap::new(),
            false,
            kotlin_android_style,
            true,
        );
        out
    }

    fn is_true_assertion(field: &str) -> Assertion {
        Assertion {
            assertion_type: "is_true".to_string(),
            field: Some(field.to_string()),
            ..Assertion::default()
        }
    }

    /// `Option<DataNode>` presence on the Kotlin (JVM) target: before the fix this rendered
    /// `assertTrue(result.data() == true, ...)`, which compiles (`==` is Any?-to-Any?
    /// structural equality) but is always false for a present non-Boolean nullable.
    #[test]
    fn kotlin_is_true_on_optional_struct_field_checks_presence() {
        let out = render(&is_true_assertion("data"), "data", false);
        assert_eq!(
            out,
            "        assertTrue(result.data() != null, \"expected true (non-null)\")\n"
        );
    }

    /// Same fixture, kotlin_android target: properties (no `()`), same nullability fix.
    #[test]
    fn kotlin_android_is_true_on_optional_struct_field_checks_presence() {
        let out = render(&is_true_assertion("data"), "data", true);
        assert_eq!(
            out,
            "        assertTrue(result.data != null, \"expected true (non-null)\")\n"
        );
    }

    #[test]
    fn kotlin_android_is_false_on_optional_struct_field_checks_absence() {
        let out = render(
            &Assertion {
                assertion_type: "is_false".to_string(),
                field: Some("data".to_string()),
                ..Assertion::default()
            },
            "data",
            true,
        );
        assert_eq!(
            out,
            "        assertTrue(result.data == null, \"expected false (null)\")\n"
        );
    }

    #[test]
    fn kotlin_android_is_true_on_non_optional_field_is_unchanged() {
        let resolver = FieldResolver::new(
            &HashMap::new(),
            &HashSet::new(),
            &HashSet::new(),
            &HashSet::new(),
            &HashSet::new(),
        );
        let mut out = String::new();
        render_assertion(
            &mut out,
            &is_true_assertion("active"),
            "result",
            "SampleClient",
            &resolver,
            false,
            false,
            &HashSet::new(),
            &HashSet::new(),
            &HashMap::new(),
            false,
            true,
            true,
        );
        assert_eq!(out, "        assertTrue(result.active == true, \"expected true\")\n");
    }
}

/// Task #367: a fixture field path that traverses a tagged-union variant boundary
/// (`<union>.<variant>.<field>`, e.g. `shape.circle.radius`) is not a flat member chain in
/// Kotlin — `ShapeKind` is a sealed class and `radius` only exists on the `Circle` variant's
/// payload. These tests exercise the IR-general narrowing path added alongside the existing
/// hand-maintained `metadata.format.<variant>` parser
/// (`FieldResolver::tagged_union_split` + `FieldResolver::union_variant_payload`, driven by
/// `discriminated::try_render_generic_union_assertion`), using neutral fixture names rather
/// than any consumer's real domain types.
#[cfg(test)]
mod union_traversal_tests {
    use super::render_assertion;
    use crate::core::ir::{EnumDef, EnumVariant, FieldDef, PrimitiveType, TypeDef, TypeRef};
    use crate::e2e::field_access::FieldResolver;
    use crate::e2e::fixture::Assertion;
    use std::collections::{HashMap, HashSet};

    fn field(name: &str, ty: TypeRef) -> FieldDef {
        FieldDef {
            name: name.to_string(),
            ty,
            ..FieldDef::default()
        }
    }

    fn variant(name: &str, fields: Vec<FieldDef>) -> EnumVariant {
        EnumVariant {
            name: name.to_string(),
            fields,
            is_tuple: true,
            ..EnumVariant::default()
        }
    }

    /// `Report { shape: ShapeKind }`, `ShapeKind::Circle(CircleData)` (single payload, the
    /// `metadata.format.excel.sheet_count` shape), `ShapeKind::Square(width, height)` (two
    /// payload fields — no single type to narrow into), `CircleData { radius: u32 }`.
    fn shape_resolver(method_calls: &HashSet<String>, kotlin_android_style: bool) -> FieldResolver {
        let type_defs = vec![
            TypeDef {
                name: "Report".to_string(),
                fields: vec![field("shape", TypeRef::Named("ShapeKind".to_string()))],
                ..TypeDef::default()
            },
            TypeDef {
                name: "CircleData".to_string(),
                fields: vec![field("radius", TypeRef::Primitive(PrimitiveType::U32))],
                ..TypeDef::default()
            },
        ];
        let enums = vec![EnumDef {
            name: "ShapeKind".to_string(),
            variants: vec![
                variant("Circle", vec![field("_0", TypeRef::Named("CircleData".to_string()))]),
                variant(
                    "Square",
                    vec![
                        field("width", TypeRef::Primitive(PrimitiveType::U32)),
                        field("height", TypeRef::Primitive(PrimitiveType::U32)),
                    ],
                ),
            ],
            ..EnumDef::default()
        }];
        let _ = kotlin_android_style;
        FieldResolver::new(
            &HashMap::new(),
            &HashSet::new(),
            &HashSet::new(),
            &HashSet::new(),
            method_calls,
        )
        .with_ir_enum_map(
            FieldResolver::ir_enum_fields(&type_defs, &enums),
            Some("Report".to_string()),
        )
    }

    fn render(field_path: &str, resolver: &FieldResolver, kotlin_android_style: bool) -> String {
        let assertion = Assertion {
            assertion_type: "equals".to_string(),
            field: Some(field_path.to_string()),
            value: Some(serde_json::json!(5)),
            ..Assertion::default()
        };
        let mut out = String::new();
        render_assertion(
            &mut out,
            &assertion,
            "result",
            "SampleClient",
            resolver,
            false,
            false,
            &HashSet::new(),
            &HashSet::new(),
            &HashMap::new(),
            false,
            kotlin_android_style,
            true,
        );
        out
    }

    /// A single-payload variant narrows correctly on the plain (JVM, non-android) Kotlin
    /// target: `when (val union<Variant> = result.<field>()) { is <Union>.<Variant> -> { ... } }`,
    /// with the payload property name (`data`) computed from the IR the same way
    /// `kotlin_field_name_with_type` names it in the real generated binding — not hardcoded.
    #[test]
    fn single_payload_variant_narrows_on_plain_kotlin() {
        let method_calls: HashSet<String> = ["shape.circle".to_string()].into_iter().collect();
        let resolver = shape_resolver(&method_calls, false);
        let out = render("shape.circle.radius", &resolver, false);
        assert_eq!(
            out,
            "        when (val unionCircle = result.shape()) {\n\
             \x20           is ShapeKind.Circle -> {\n\
             \x20               assertEquals(5, unionCircle.data.radius!!, \"expected: 5\")\n\
             \x20           }\n\
             \x20           else -> kotlin.test.assertTrue(false, \"Expected Circle variant\")\n\
             \x20       }\n",
            "got: {out}"
        );
    }

    #[test]
    fn legacy_format_union_fails_when_the_runtime_variant_differs() {
        let result_fields: HashSet<String> = [
            "metadata".to_string(),
            "metadata.format".to_string(),
            "metadata.format.excel".to_string(),
            "metadata.format.excel.sheet_count".to_string(),
        ]
        .into_iter()
        .collect();
        let resolver = FieldResolver::new(
            &HashMap::new(),
            &HashSet::new(),
            &result_fields,
            &HashSet::new(),
            &HashSet::new(),
        );
        let out = render("metadata.format.excel.sheet_count", &resolver, true);

        assert!(
            out.contains("else -> kotlin.test.assertTrue(false, \"Expected Excel variant\")"),
            "got: {out}"
        );
    }

    /// Same union, kotlin_android style: the accessor for the union field itself switches to
    /// property syntax (`result.shape`, no parens) while the narrowing shape is unchanged.
    #[test]
    fn single_payload_variant_narrows_on_kotlin_android() {
        let method_calls: HashSet<String> = ["shape.circle".to_string()].into_iter().collect();
        let resolver = shape_resolver(&method_calls, true);
        let out = render("shape.circle.radius", &resolver, true);
        assert!(out.contains("when (val unionCircle = result.shape) {"), "got: {out}");
        assert!(out.contains("is ShapeKind.Circle -> {"), "got: {out}");
        assert!(
            out.contains("assertEquals(5, unionCircle.data.radius!!, \"expected: 5\")"),
            "got: {out}"
        );
    }

    /// A variant with two payload fields has no single type to narrow into
    /// (`union_variant_payload` returns `None`), so this must render the loud, named
    /// `UnionTraversalNotImplementedForKotlin` skip — never fall through to a flat accessor
    /// chain like `.square().width()` against a sealed class, which would not compile.
    #[test]
    fn multi_field_variant_emits_the_named_gap_marker_instead_of_a_broken_accessor() {
        let method_calls: HashSet<String> = ["shape.square".to_string()].into_iter().collect();
        let resolver = shape_resolver(&method_calls, false);
        let out = render("shape.square.width", &resolver, false);
        assert_eq!(
            out,
            "        // skipped: field 'shape.square.width' crosses a tagged-union variant \
             boundary alef does not yet lower for this variant shape in Kotlin\n",
            "got: {out}"
        );
        assert!(
            !out.contains(".square()"),
            "must not emit a flat accessor into the sealed class: {out}"
        );
    }

    /// Sabotage-adjacent control: a plain non-union path through the SAME resolver must be
    /// completely unaffected — `tagged_union_split` only fires when `method_calls` names the
    /// exact traversed prefix, so an unrelated field never routes through this new code.
    #[test]
    fn unrelated_field_is_unaffected_by_the_union_detector() {
        let method_calls: HashSet<String> = ["shape.circle".to_string()].into_iter().collect();
        let resolver = shape_resolver(&method_calls, false);
        let out = render("shape", &resolver, false);
        assert!(out.contains("assertEquals(5, result.shape())"), "got: {out}");
    }
}

#[cfg(test)]
mod wildcard_tests {
    use super::render_assertion;
    use crate::e2e::field_access::FieldResolver;
    use crate::e2e::fixture::Assertion;
    use std::collections::{HashMap, HashSet};

    fn array_resolver(field: &str) -> FieldResolver {
        let names: HashSet<String> = [field.to_string()].into_iter().collect();
        FieldResolver::new(&HashMap::new(), &HashSet::new(), &names, &names, &HashSet::new())
    }

    fn render_contains(resolver: &FieldResolver, field: &str, value: &str) -> String {
        let assertion = Assertion {
            assertion_type: "contains".to_string(),
            field: Some(field.to_string()),
            value: Some(serde_json::Value::String(value.to_string())),
            ..Assertion::default()
        };
        let mut out = String::new();
        render_assertion(
            &mut out,
            &assertion,
            "result",
            "SampleClient",
            resolver,
            false,
            false,
            &HashSet::new(),
            &HashSet::new(),
            &HashMap::new(),
            false,
            false,
            true,
        );
        out
    }

    /// Baseline: a single wildcard still quantifies over the whole list, so the refusal added
    /// for the nested case cannot have been implemented by refusing wildcards generally. ~keep
    #[test]
    fn single_wildcard_still_quantifies_over_every_element() {
        let out = render_contains(&array_resolver("links"), "links[].url", "example.test");
        assert!(out.contains(".any {"), "got: {out}");
        assert!(!out.contains(".first()"), "wildcard must not pin element 0, got: {out}");
    }

    /// `wildcard_split` consumes the first `[].` only, so before the guard the `any {}` ranged
    /// over `pages` while its lambda read `e.links().first().url()` — a whole-array claim that
    /// only ever inspected element zero of the inner list. Kotlin is the worst case for
    /// spotting this in review: `index == 0` renders as `.first()`, never as a literal `[0]`,
    /// so an index-free assertion would have passed vacuously. Pre-guard this test fails: the
    /// skip line is absent and `.first()` is present. ~keep
    #[test]
    fn nested_wildcard_should_emit_a_visible_skip_rather_than_an_index_zero_check() {
        let out = render_contains(&array_resolver("pages"), "pages[].links[].url", "example.test");
        assert_eq!(
            out, "        // skipped: nested array-wildcard field 'pages[].links[].url' not supported\n",
            "got: {out}"
        );
    }
}