godog icon indicating copy to clipboard operation
godog copied to clipboard

Enhancement: Support for custom types + `StepParam` type

Open tigh-latte opened this issue 9 months ago • 1 comments

🤔 What's changed?

Add a new supported type to step definitions, being:

  • Any type that is structured ontop of another supported type:
type MyCustomInt int
  • any type that implements StepParam:
type TestFile string

func (t *TestFile) LoadParam(ctx context.Context) (string, error) {}

⚡️ What's your motivation?

We make fairly heavy use of files for test data, along with fairly heavy use of go's template engine for our inputs and file content. This, while working well, has resulted in basically every single step we define having to start with one or more of a few common function calls:

func iExpectXToMatchY(ctx context.Context, x string, y string) error {
    rendered, err := loadAndRenderTestFile(y)
    if err != nil { /* you know how it goes */ }

    // assert that x == y
}

Which, while fine, is duplication that we've had to change a few times before. Combine this with not all templated input being files to be loaded, and you get a fair amount of duplicated boilerplate that eventually the nodupl linter complains about.

Ideally, this would be automatic while also being more declarative:

func iExpectXToMatchY(ctx context.Context, x string, y TestTemplateFile) error {
    //assert that x == string(y)
}

🏷️ What kind of change is this?

  • :zap: New feature (non-breaking change which adds new behaviour)

♻️ Anything particular you want feedback on?

Naming, new type placement, and general opinions on the feature would be good (such as, do we even want support for customer non-struct types?).

📋 Checklist:

  • [x] I agree to respect and uphold the Cucumber Community Code of Conduct
  • [x] I've changed the behaviour of the code
    • [x] I have added/updated tests to cover my changes.
  • [x] My change requires a change to the documentation.
    • [x] I have updated the documentation accordingly.
  • [x] Users should know about my change
    • [x] I have added an entry to the "Unreleased" section of the CHANGELOG, linking to this pull request.

This text was originally generated from a template, then edited by hand. You can modify the template here.

tigh-latte avatar Mar 19 '25 23:03 tigh-latte

Codecov Report

Attention: Patch coverage is 93.18182% with 3 lines in your changes missing coverage. Please review.

Project coverage is 77.97%. Comparing base (153db4e) to head (3bb2789). Report is 32 commits behind head on main.

Files with missing lines Patch % Lines
internal/models/stepdef.go 93.02% 2 Missing and 1 partial :warning:
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #682      +/-   ##
==========================================
- Coverage   83.21%   77.97%   -5.24%     
==========================================
  Files          28       41      +13     
  Lines        3413     4082     +669     
==========================================
+ Hits         2840     3183     +343     
- Misses        458      780     +322     
- Partials      115      119       +4     

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

codecov[bot] avatar Mar 19 '25 23:03 codecov[bot]