Skip to content

Tests: c import auto methods coverage (§16) #485

Closed

Description

@ehartford

Summary

Add tests for c_import auto-method generation. The implementation already detects method and constructor patterns; the missing work is committed coverage for the §16.2a rules and opt-out interactions.

Spec

§16.2a says c_import detects C function names like structname_method(self, ...) and generates method syntax that is zero-cost sugar over the C function call (docs/with-specification.md:8205). Detection converts struct names to snake_case, treats first-parameter *S/*mut S/*const S/S functions as method candidates, treats *S/*mut S returns without self as constructor candidates, strips the prefix for the method name, supports type-call constructor sugar for .new, and provides no_methods opt-out (docs/with-specification.md:8225, docs/with-specification.md:8281).

Current state

ci_detect_member_functions scans imported functions and emits wrappers when the first parameter or return type matches a known struct (src/CImport.w:1220). It precomputes snake-case prefixes with ci_compute_snake_prefix (src/CImport.w:1308), strips prefixes with ci_strip_snake_prefix / ci_strip_struct_prefix (src/CImport.w:1356), emits member wrappers (src/CImport.w:1396), and emits constructor wrappers (src/CImport.w:1435). The requirement campaign found this implemented but not directly covered.

Implementation plan

  1. Add a C header fixture with typedef struct GHashTable GHashTable;, g_hash_table_new, g_hash_table_insert, g_hash_table_lookup, and a second type to test ambiguity/prefix selection.
  2. Write a With test that imports the header and checks that .new, .insert, .lookup, and type-call constructor sugar type-check and lower to the generated wrappers.
  3. Include first-parameter variations: *mut S, *const S, and by-value S if the importer supports by-value structs.
  4. Add negative/opt-out coverage for no_methods: true and no_methods: "Type" from c_import: no_methods opt-out option (§16.2a) #416, proving flat C functions remain available.
  5. Add a collision/ambiguity fixture if practical: longest prefix wins; equal length neither claims the function.

Test plan

Add test/behavior/behav_c_import_auto_methods.h and .w for positive method/constructor coverage. Add compile/check tests for no_methods disabling generated methods. Add an import surface snapshot or with check fixture if runtime linking to C functions is not needed.

Acceptance criteria

  • Auto-generated member methods are covered for first-param type matching.
  • Constructor wrapper and type-call sugar are covered.
  • Prefix stripping and snake-case conversion are covered.
  • no_methods opt-out behavior is covered without removing flat functions.
  • with build && with build :fixpoint && with build :test pass.

Dependencies

Related to #416 for no_methods opt-out.

Traceability

From the requirements campaign (docs/requirements.md, spec v7.1). Affected requirement IDs: 16.2.2.1, 16.2.2.10, 16.2.2.2, 16.2.2.5, 16.2.2.6, 16.2.2.7, 16.2.2.8.

Activity

  1. ehartford commented on Jul 3, 2026

    @ehartford
    ContributorAuthor

    Closing — c_import factory-as-constructor sugar (Counter.make) pinned by behav_c_import_auto_constructor (3628d82); method sugar + no_methods opt-out already covered (behav_c_import_auto_method_baseline, behav_c_import_no_methods_flat, err_c_import_no_methods_*). §16.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions