alef 0.83.1

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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
//! Core `render_assertion` coverage: optional-field presence semantics, bracket-wildcard
//! traversal, and the IR-oracle vs `result_fields` precedence for field availability.
//!
//! Split out of `assertions.rs`, which is over the 1000-line cap and may not grow.

use super::*;
use crate::e2e::field_access::FieldResolver;
use std::collections::{HashMap, HashSet};

fn make_assertion(field: &str, value: &str) -> Assertion {
    Assertion {
        assertion_type: "equals".to_string(),
        field: Some(field.to_string()),
        value: Some(serde_json::Value::String(value.to_string())),
        ..Default::default()
    }
}

fn contains_assertion(field: &str, value: &str) -> Assertion {
    Assertion {
        assertion_type: "contains".to_string(),
        field: Some(field.to_string()),
        value: Some(serde_json::Value::String(value.to_string())),
        ..Default::default()
    }
}

/// Shared fixture setup: every test in this file renders one assertion against a
/// resolver-configured `result` value, with every other `AssertionRenderContext` field
/// held at its default (non-streaming, non-array, no optional locals) value.
fn render_with_resolver(assertion: &Assertion, resolver: &FieldResolver) -> String {
    let mut out = String::new();
    let context = AssertionRenderContext {
        effective_result_var: "result",
        import_alias: "pkg",
        field_resolver: resolver,
        optional_locals: &HashMap::new(),
        numeric_scalar_fields: &HashSet::new(),
        presence_checked_fields: &HashSet::new(),
        result_is_simple: false,
        result_is_array: false,
        is_streaming: false,
        streaming_item_type: None,
    };
    render_assertion(&mut out, &context, assertion);
    out
}

fn render_bare(assertion: &Assertion) -> String {
    let resolver = FieldResolver::new(
        &HashMap::new(),
        &HashSet::new(),
        &HashSet::new(),
        &HashSet::new(),
        &HashSet::new(),
    );
    render_with_resolver(assertion, &resolver)
}

/// Render `assertion` against a resolver where `optional_field` is the one field
/// declared `Option<T>` -- used to cover the Go `*T` (Option<struct>) presence-check
/// regression: `is_true` used to unconditionally deref (`*result.Data`), which
/// requires `T = bool` and does not compile for `Option<DataNode>`.
fn render_with_optional_field(assertion: &Assertion, optional_field: &str) -> 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(),
    );
    render_with_resolver(assertion, &resolver)
}

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

#[test]
fn is_true_on_optional_struct_field_checks_presence_not_a_bool_deref() {
    let out = render_with_optional_field(&is_true_assertion("data"), "data");
    assert_eq!(out, "\tassert.NotNil(t, result.Data, \"expected true (non-nil)\")\n");
}

#[test]
fn is_false_on_optional_struct_field_checks_absence() {
    let out = render_with_optional_field(
        &Assertion {
            assertion_type: "is_false".to_string(),
            field: Some("data".to_string()),
            ..Default::default()
        },
        "data",
    );
    assert_eq!(out, "\tassert.Nil(t, result.Data, \"expected false (nil)\")\n");
}

#[test]
fn is_true_on_non_optional_field_is_unchanged() {
    let out = render_bare(&is_true_assertion("active"));
    assert_eq!(out, "\tassert.True(t, result.Active, \"expected true\")\n");
}

#[test]
fn wildcard_contains_scans_every_element_not_just_index_zero() {
    let out = render_bare(&contains_assertion("links[].link_type", "external"));
    assert!(out.contains("for _, e := range result.Links {"), "got: {out}");
    assert!(out.contains("e.LinkType"), "got: {out}");
    assert!(!out.contains("[0]"), "wildcard must not lower to index 0, got: {out}");
}

#[test]
fn explicit_numeric_index_still_targets_that_element() {
    let out = render_bare(&contains_assertion("links[0].link_type", "external"));
    assert!(out.contains("result.Links[0].LinkType"), "got: {out}");
    assert!(
        !out.contains("range"),
        "explicit index must not become a scan, got: {out}"
    );
}

/// Codegen-level canary for the wildcard defect. A fixture array whose match lives in
/// element 1 is only detected by code that visits every element; the pre-fix renderer
/// emitted a single `result.Links[0]` accessor, so this assertion pair fails against it.
/// It cannot execute the generated Go, so it pins the property structurally: an
/// element-1-only match is caught iff the emitted loop is unbounded and the value is
/// tested inside it. ~keep
#[test]
fn wildcard_match_in_element_one_is_reachable() {
    let out = render_bare(&contains_assertion("links[].link_type", "internal"));
    let loop_start = out
        .find("for _, e := range")
        .expect("expected an unbounded element scan");
    let check = out
        .find("strings.Contains")
        .expect("expected a per-element containment check");
    assert!(
        check > loop_start,
        "containment check must sit inside the scan, got: {out}"
    );
    assert!(
        !out.contains("result.Links[0]"),
        "an index-0 accessor would miss a match in element 1, got: {out}"
    );
}

/// `wildcard_split` consumes the first `[].` only, so before the guard the emitted scan
/// ranged over `pages` while its body read `e.Links[0].Url` — a whole-array claim that
/// only ever inspected element zero of the inner slice. Pre-guard this test fails: the
/// skip line is absent and a `range` scan over `[0]` is present. ~keep
#[test]
fn nested_wildcard_should_emit_a_visible_skip_rather_than_an_index_zero_check() {
    let out = render_bare(&contains_assertion("pages[].links[].url", "example.test"));
    assert_eq!(
        out, "\t// skipped: nested array-wildcard field 'pages[].links[].url' not supported\n",
        "got: {out}"
    );
}

#[test]
fn two_wildcard_assertions_on_one_array_use_distinct_locals() {
    let first = render_bare(&contains_assertion("links[].link_type", "external"));
    let second = render_bare(&contains_assertion("links[].link_type", "internal"));
    let local_of = |s: &str| {
        let start = s.find("found").expect("expected a found local");
        s[start..start + s[start..].find(' ').expect("local is space-delimited")].to_string()
    };
    assert_ne!(local_of(&first), local_of(&second), "locals must not collide");
}

/// IR-oracle wiring regression (alef task #64): a field that is IR-reachable
/// (present, non-`binding_excluded`, on some IR type) but missing from the
/// hand-maintained `result_fields` config must still render a real assertion,
/// not a "skipped: field not available" comment — `go/test_function.rs` (and
/// the `go/test_file.rs` import-decision resolver) now thread
/// `FieldResolver::ir_field_sets(type_defs)` into `with_ir_fields`. ~keep
#[test]
fn go_ir_reachable_field_absent_from_result_fields_is_not_skipped() {
    let reachable: HashSet<String> = ["data".to_string()].into_iter().collect();
    let resolver = FieldResolver::new(
        &HashMap::new(),
        &HashSet::new(),
        &HashSet::new(),
        &HashSet::new(),
        &HashSet::new(),
    )
    .with_ir_fields(reachable, HashSet::new(), HashSet::new());
    let assertion = make_assertion("data", "hello");
    let out = render_with_resolver(&assertion, &resolver);
    assert!(!out.contains("skipped"), "got: {out}");
}

/// The negative-control half of the same regression: `internal_diagnostics`
/// represents a field carrying `#[doc(hidden)]` or `#[cfg_attr(alef,
/// alef(skip))]` in the real struct (a genuine `binding_excluded` field) —
/// NOT `#[serde(skip)]`, which alone does not exclude a field from the
/// binding surface. Even though it is listed in `result_fields` (a stale/
/// wrong config entry), the IR must still win and reject it. ~keep
#[test]
fn go_ir_excluded_field_present_in_result_fields_is_still_skipped() {
    let result_fields: HashSet<String> = ["internal_diagnostics".to_string()].into_iter().collect();
    let excluded: HashSet<String> = ["internal_diagnostics".to_string()].into_iter().collect();
    let resolver = FieldResolver::new(
        &HashMap::new(),
        &HashSet::new(),
        &result_fields,
        &HashSet::new(),
        &HashSet::new(),
    )
    .with_ir_fields(HashSet::new(), excluded, HashSet::new());
    let assertion = make_assertion("internal_diagnostics", "hello");
    let out = render_with_resolver(&assertion, &resolver);
    assert!(out.contains("skipped"), "got: {out}");
}

fn not_empty_assertion(field: &str) -> Assertion {
    Assertion {
        assertion_type: "not_empty".to_string(),
        field: Some(field.to_string()),
        ..Default::default()
    }
}

fn is_empty_assertion(field: &str) -> Assertion {
    Assertion {
        assertion_type: "is_empty".to_string(),
        field: Some(field.to_string()),
        ..Default::default()
    }
}

/// `render_with_optional_field` marks "notes" as an `Option<T>` field with NO IR at all, so
/// `FieldResolver::target_field_is_pointer` returns `None` and (per `assertion_field_shape.rs`'s
/// documented `unwrap_or(false)` policy) resolves to "not a pointer". `not_empty` must honor that
/// exact answer instead of re-deriving pointer-ness from nullability alone: pre-fix, this emitted
/// `len(*result.Notes)` against a field the resolver never proved was a pointer -- the inverse of
/// the `SheetCount` defect. ~keep
#[test]
fn not_empty_on_optional_non_pointer_field_never_dereferences() {
    let out = render_with_optional_field(&not_empty_assertion("notes"), "notes");
    assert_eq!(
        out,
        "\tif result.Notes == nil || len(result.Notes) == 0 {\n\t\tt.Errorf(\"expected non-empty value\")\n\t}\n"
    );
}

/// Symmetric regression for `is_empty` -- see
/// `not_empty_on_optional_non_pointer_field_never_dereferences`. ~keep
#[test]
fn is_empty_on_optional_non_pointer_field_never_dereferences() {
    let out = render_with_optional_field(&is_empty_assertion("notes"), "notes");
    let expected = "\tif result.Notes != nil && len(result.Notes) != 0 {\n\
        \t\tt.Errorf(\"expected empty value, got %v\", result.Notes)\n\
        \t}\n";
    assert_eq!(out, expected);
}

fn count_min_assertion(field: &str, value: u64) -> Assertion {
    Assertion {
        assertion_type: "count_min".to_string(),
        field: Some(field.to_string()),
        value: Some(serde_json::Value::from(value)),
        ..Default::default()
    }
}

/// Render `assertion` against a resolver where `field` is declared both optional and an
/// array (`Option<Vec<T>>` flattened to a nilable Go slice, the `Elements`/
/// `DetectedLanguages` shape) and `presence_checked_fields` is populated iff
/// `has_sibling_presence_check` -- the two states `render_count_assertion` branches on.
fn render_count_on_nullable_slice(assertion: &Assertion, field: &str, has_sibling_presence_check: bool) -> String {
    let optional: HashSet<String> = [field.to_string()].into_iter().collect();
    let array_fields: HashSet<String> = [field.to_string()].into_iter().collect();
    let resolver = FieldResolver::new(
        &HashMap::new(),
        &optional,
        &HashSet::new(),
        &array_fields,
        &HashSet::new(),
    );
    let presence_checked_fields: HashSet<&str> = if has_sibling_presence_check {
        [field].into_iter().collect()
    } else {
        HashSet::new()
    };
    let mut out = String::new();
    let context = AssertionRenderContext {
        effective_result_var: "result",
        import_alias: "pkg",
        field_resolver: &resolver,
        optional_locals: &HashMap::new(),
        numeric_scalar_fields: &HashSet::new(),
        presence_checked_fields: &presence_checked_fields,
        result_is_simple: false,
        result_is_array: false,
        is_streaming: false,
        streaming_item_type: None,
    };
    render_assertion(&mut out, &context, assertion);
    out
}

/// CONFIRMED DEFECT regression: a `count_min`/`count_equals` assertion on a nullable slice
/// field with no sibling `not_empty` assertion must FAIL when the guard is nil, not
/// silently skip the check and let the test pass. Mirrors the real
/// `Test_ConfigElementTypes`/`Test_LanguageDetectionMultilingual` fixture shape, which
/// assert only `count_min` on a nullable slice and have no other presence check. ~keep
#[test]
fn count_min_on_nullable_slice_without_sibling_presence_check_fails_on_nil() {
    let assertion = count_min_assertion("tags", 1);
    let out = render_count_on_nullable_slice(&assertion, "tags", false);
    let expected = "\tif result.Tags != nil {\n\
        \t\tassert.GreaterOrEqual(t, len(result.Tags), 1, \"expected at least 1 elements\")\n\
        \t} else {\n\
        \t\tt.Errorf(\"expected at least 1 elements, got nil\")\n\
        \t}\n";
    assert_eq!(out, expected, "got: {out}");
}

/// Positive control: when a sibling `not_empty` assertion on the same field already fails
/// the test on nil (`render_not_empty`), the count assertion stays guard-only -- an `else`
/// here would only duplicate that failure, not close a hole.
#[test]
fn count_min_on_nullable_slice_with_sibling_presence_check_stays_guard_only() {
    let assertion = count_min_assertion("tags", 1);
    let out = render_count_on_nullable_slice(&assertion, "tags", true);
    let expected = "\tif result.Tags != nil {\n\
        \t\tassert.GreaterOrEqual(t, len(result.Tags), 1, \"expected at least 1 elements\")\n\
        \t}\n";
    assert_eq!(out, expected, "got: {out}");
}

/// `count_equals` shares `render_count_assertion` with `count_min` -- confirm the `Equal`
/// method path also gets the failing `else` when uncovered.
#[test]
fn count_equals_on_nullable_slice_without_sibling_presence_check_fails_on_nil() {
    let assertion = Assertion {
        assertion_type: "count_equals".to_string(),
        field: Some("tags".to_string()),
        value: Some(serde_json::Value::from(2u64)),
        ..Default::default()
    };
    let out = render_count_on_nullable_slice(&assertion, "tags", false);
    let expected = "\tif result.Tags != nil {\n\
        \t\tassert.Equal(t, len(result.Tags), 2, \"expected exactly 2 elements\")\n\
        \t} else {\n\
        \t\tt.Errorf(\"expected exactly 2 elements, got nil\")\n\
        \t}\n";
    assert_eq!(out, expected, "got: {out}");
}

fn min_length_assertion(field: &str, value: u64) -> Assertion {
    Assertion {
        assertion_type: "min_length".to_string(),
        field: Some(field.to_string()),
        value: Some(serde_json::Value::from(value)),
        ..Default::default()
    }
}

/// Render `assertion` against a resolver where `field` is declared optional (a nilable,
/// non-pointer field -- the `Notes` shape used elsewhere in this file) and
/// `presence_checked_fields` is populated iff `has_sibling_presence_check` -- the two
/// states `render_length_assertion` branches on.
fn render_length_on_optional_field(assertion: &Assertion, field: &str, has_sibling_presence_check: bool) -> String {
    let optional: HashSet<String> = [field.to_string()].into_iter().collect();
    let resolver = FieldResolver::new(
        &HashMap::new(),
        &optional,
        &HashSet::new(),
        &HashSet::new(),
        &HashSet::new(),
    );
    let presence_checked_fields: HashSet<&str> = if has_sibling_presence_check {
        [field].into_iter().collect()
    } else {
        HashSet::new()
    };
    let mut out = String::new();
    let context = AssertionRenderContext {
        effective_result_var: "result",
        import_alias: "pkg",
        field_resolver: &resolver,
        optional_locals: &HashMap::new(),
        numeric_scalar_fields: &HashSet::new(),
        presence_checked_fields: &presence_checked_fields,
        result_is_simple: false,
        result_is_array: false,
        is_streaming: false,
        streaming_item_type: None,
    };
    render_assertion(&mut out, &context, assertion);
    out
}

/// CONFIRMED DEFECT regression (the `render_length_assertion` sibling of
/// `count_min_on_nullable_slice_without_sibling_presence_check_fails_on_nil`): a
/// `min_length`/`max_length` assertion on an optional field with no sibling `not_empty`
/// assertion must FAIL when the guard is nil, not silently skip the check. ~keep
#[test]
fn min_length_on_optional_field_without_sibling_presence_check_fails_on_nil() {
    let assertion = min_length_assertion("notes", 5);
    let out = render_length_on_optional_field(&assertion, "notes", false);
    let expected = "\tif result.Notes != nil {\n\
        \t\tassert.GreaterOrEqual(t, len(result.Notes), 5, \"expected length >= 5\")\n\
        \t} else {\n\
        \t\tt.Errorf(\"expected length >= 5, got nil\")\n\
        \t}\n";
    assert_eq!(out, expected, "got: {out}");
}

/// Positive control: a sibling `not_empty` assertion on the same field already fails the
/// test on nil, so `min_length` stays guard-only.
#[test]
fn min_length_on_optional_field_with_sibling_presence_check_stays_guard_only() {
    let assertion = min_length_assertion("notes", 5);
    let out = render_length_on_optional_field(&assertion, "notes", true);
    let expected = "\tif result.Notes != nil {\n\
        \t\tassert.GreaterOrEqual(t, len(result.Notes), 5, \"expected length >= 5\")\n\
        \t}\n";
    assert_eq!(out, expected, "got: {out}");
}

/// `max_length` shares `render_length_assertion` with `min_length` -- confirm the
/// `LessOrEqual` method path also gets the failing `else` when uncovered.
#[test]
fn max_length_on_optional_field_without_sibling_presence_check_fails_on_nil() {
    let assertion = Assertion {
        assertion_type: "max_length".to_string(),
        field: Some("notes".to_string()),
        value: Some(serde_json::Value::from(50u64)),
        ..Default::default()
    };
    let out = render_length_on_optional_field(&assertion, "notes", false);
    let expected = "\tif result.Notes != nil {\n\
        \t\tassert.LessOrEqual(t, len(result.Notes), 50, \"expected length <= 50\")\n\
        \t} else {\n\
        \t\tt.Errorf(\"expected length <= 50, got nil\")\n\
        \t}\n";
    assert_eq!(out, expected, "got: {out}");
}