Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for aliases #171

Closed
gavv opened this issue Nov 24, 2022 · 2 comments · Fixed by #227
Closed

Add support for aliases #171

gavv opened this issue Nov 24, 2022 · 2 comments · Fixed by #227
Assignees
Labels
feature New feature or request help wanted Contributions are welcome
Milestone

Comments

@gavv
Copy link
Owner

gavv commented Nov 24, 2022

httpexpect is able to print assertion chain path on failure. For example, when you write:

e.GET("/endpoint").
  Expect().
  JSON().Object().Value("foo").
  Array().Element(0).Number()

and assertion fails, you will see:

        expected: value is number
        
        assertion:
          Request("GET").Expect().JSON().Object().Value("foo").Array().
            Element(0).Number()

Often it is convenient to create variables when making assertions:

foo := e.GET("/endpoint").
  Expect().
  JSON().Object().Value("foo").Array()

foo.Element(0).Number()
foo.Element(1).Number()
...

In this case, it would be nice if variables names were reflected in the failure message.

This issue suggest to implement support for aliases, which will work like this:

foo := e.GET("/endpoint").
  Expect().
  JSON().Object().Value("foo").Array().Alias("foo")

foo.Element(0).Number()
        expected: value is number
        
        assertion:
          foo.Element(0).Number()

To make it possible, the following changes are needed:

  • Add AssertionContext.AliasedPath. It will be similar to AssertionContext.Path, but will take into account aliases.
  • chain.enter() and chain.leave() should update both Path and AliasedPath
  • chain.clone() should clone both Path and AliasedPath
  • chain will provide new setAlias(name) method that will replace AliasedPath with a new slice with single element name, and keep Path untouched
  • To every inspector struct (Array, Object, Number, etc), add Alias() method that accepts a string name and returns receiver; it will just call chain.setAlias.

After this changes, AssertionContext.AliasedPath will automatically contain the value we're interested in. Now we just need to update DefaultFormatter:

  • add DisableAliases flag (zero by default), which disables new feature (enabled by default)
  • if aliases are enabled, fill FormatData.AssertPath using AliasedPath instead of Path

And that's it.

We also need to add unit tests:

  • comprehensive tests for alias support in chain
  • short unit test (or maybe new assertion in existing test) for every inspector object (Array, Object, etc)
  • e2e test for aliases in reports (see Add end-to-end tests for report formatting #165)

Finally we should add documentation:

  • added public methods should be documented
  • we should add a section to README that explains the feature
@gavv gavv added feature New feature or request help wanted Contributions are welcome labels Nov 24, 2022
@k4n4ry
Copy link
Contributor

k4n4ry commented Jan 10, 2023

Hi @gavv, can I pick this up?

@gavv
Copy link
Owner Author

gavv commented Jan 10, 2023

You're welcome, thanks!

@gavv gavv closed this as completed in #227 Jan 26, 2023
@gavv gavv added this to the v2 milestone Jan 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request help wanted Contributions are welcome
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants