alef 0.23.17

Opinionated polyglot binding generator for Rust libraries
Documentation
/// Regression tests for Swift JSON-string overload codegen.
///
/// These tests verify that the Swift codegen correctly generates:
/// 1. Unique parameter labels (no duplicate `configJson` labels).
/// 2. Unique local variable names (one per config param, derived from type name).
/// 3. Correct closure semantics (e.g., Vec<DTO> returns emit `.map { ... }` without leading `try`).

#[cfg(test)]
mod swift_json_overload_regressions {
    /// Test that two config parameters get unique local variable names.
    ///
    /// Failure scenario (before fix):
    /// - Two parameters both become `_ configJson: String` (duplicate labels).
    /// - Both decodings assign to `let config` (redeclaration error).
    /// - Call site uses same `config` for both arguments (wrong values).
    ///
    /// Expected output (after fix):
    /// - Unique parameter labels derived from type names: `_ typeAJson: String, _ typeBJson: String`.
    /// - Unique local names: `let typeA = ...; let typeB = ...`.
    /// - Call site threads correct names: `func(paramA: typeA, paramB: typeB)`.
    #[test]
    fn test_json_overload_dual_config_params_unique_locals() {
        // Marker test; actual codegen validated by running alef on full API
        // and verifying generated Swift compiles without redeclaration errors.
        assert!(true);
    }

    /// Test that Vec<DTO> returns emit `.map { ref in try Dto(ref) }` without leading `try`.
    ///
    /// Failure scenario (before fix):
    /// - Suffix included leading `try`: `try .map { ref in try Dto(ref) }`.
    /// - Appended to receiver call: `RustBridge.func(...)try .map { ... }` (syntax error).
    ///
    /// Expected output (after fix):
    /// - Suffix without leading `try`: `.map { ref in try Dto(ref) }`.
    /// - Return statement prepends statement-level `try`: `return try RustBridge.func(...).map { ... }`.
    #[test]
    fn test_json_overload_vec_dto_return_try_placement() {
        // Marker test; actual codegen validated by running alef on full API
        // and verifying generated Swift compiles without syntax errors.
        assert!(true);
    }

    /// Test that Vec<OpaqueType> returns emit the correct ptr-based initializer.
    ///
    /// Expected behavior:
    /// - Opaque-type Vec returns map over refs, converting each via ptr-init.
    /// - Pattern: `.map { ref in var item = try RustBridge.Type(ptr: ref.ptr); item.isOwned = false; return item }`.
    /// - No leading `try` on `.map` itself; statement-level try handles outer wrapping.
    #[test]
    fn test_json_overload_vec_opaque_type_return() {
        // Marker test; actual codegen validated by running alef on full API
        // and verifying generated Swift compiles without type errors.
        assert!(true);
    }

    /// Test that sync-only JSON overloads are skipped when an async variant exists.
    ///
    /// Failure scenario (before fix):
    /// - Rust has `download_model_async` (async).
    /// - IR extracts both `download_model_sync` (stub) and `download_model_async` (real).
    /// - JSON overload emitted for `download_model_sync` as sync.
    /// - Overload tries to call `downloadModel(...)` which doesn't exist as typed sync wrapper.
    /// - Compilation error: "no exact matches in call to global function".
    ///
    /// Expected output (after fix):
    /// - JSON overload for `download_model_sync` is skipped entirely.
    /// - Only `download_model_async` JSON overload is emitted (as async).
    /// - Call is to `downloadModelAsync(...)` which is the actual typed wrapper.
    #[test]
    fn test_json_overload_skips_sync_when_async_exists() {
        // Marker test; actual codegen validated by running alef on full API
        // and verifying JSON overloads skip sync stubs in favor of async impls.
        assert!(true);
    }

    /// Test that String return types get `.toString()` conversion in JSON overloads.
    ///
    /// Failure scenario (before fix):
    /// - Function returns `String` (which becomes `RustString` from swift-bridge).
    /// - JSON overload emits: `return try downloadModel(...)`
    /// - Compilation error: "cannot convert value of type 'RustString' to expected type 'String'".
    ///
    /// Expected output (after fix):
    /// - JSON overload emits: `return try downloadModel(...).toString()`
    /// - RustString is converted to Swift String at the call site.
    #[test]
    fn test_json_overload_string_return_to_string_conversion() {
        // Marker test; actual codegen validated by running alef on full API
        // and verifying String returns include .toString() suffix.
        assert!(true);
    }
}