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

feat(assertions): 'arrayWith' and 'objectLike' matchers #15195

Merged
merged 31 commits into from
Jun 25, 2021

Conversation

nija-at
Copy link
Contributor

@nija-at nija-at commented Jun 18, 2021

Implement a hasResource() API that supports partial matchers like
arrayWith() and objectLike().

This covers the gap with similar support available in the existing assert
module.

Re-using the same implementation while providing an ergonomic jsii API
proved challenging.
This change simply re-implements these matchers, heavily influenced by
existing implementations in the assert module.

Additionally, as a test case, the cognito and synthetics modules are migrated
to use the assertions module for its unit tests.


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@gitpod-io
Copy link

gitpod-io bot commented Jun 18, 2021

@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Jun 18, 2021
@nija-at nija-at force-pushed the nija-at/assertion-matchers branch from cc29908 to 5daf4bd Compare June 22, 2021 05:48
@nija-at nija-at force-pushed the nija-at/assertion-matchers branch from 7712413 to 3956026 Compare June 22, 2021 10:21
@nija-at nija-at self-assigned this Jun 22, 2021
@nija-at nija-at requested review from rix0rrr and a team June 22, 2021 10:24
@nija-at nija-at marked this pull request as ready for review June 22, 2021 10:25
Copy link
Contributor

@rix0rrr rix0rrr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like where this is going, but I had high hopes for v2 of this library which I'm now going to heap on you 😉

packages/@aws-cdk/assertions/lib/match.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/assertions/lib/match.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/assertions/lib/match.ts Outdated Show resolved Hide resolved
for (; patternIdx < this.pattern.length; patternIdx++) {
const p = this.pattern[patternIdx];
const e = (Match.isMatcher(p) || typeof p === 'object') ? ' ' : ` [${p}] `;
failures.push({ path: [], message: `Missing element${e}at pattern index ${patternIdx}` });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This touches on a big problem that the current matchers have: if we DIDN'T match, we can not report any useful information any more to the user.

Ideally, we'd print information on the CLOSEST match we found, by some distance metric that we need to invent. Maybe how deep it got into the tree?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we don't want to make the decision right here right now, we should leave the option open to add that later.

That means:

  • Either report the entire matching tree back with all alternatives tried & all errors encountered, so that the reporting mechanism has all the information it could possibly use to construct the best error message
  • Encapsulate the return value into some class type so that the data it tracks and stores is hidden and can be changed and enhanced at any point in time.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe flat count of errors is good enough as distance function...

Copy link
Contributor Author

@nija-at nija-at Jun 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I struggled with this a little. I couldn't find a simple solution to this. Instead, I opted to use error count as a distance function. We do need to improve on this, but I was hoping to do it a little more incrementally/iteratively.

Is that reasonable?

we should leave the option open to add that later

Why do we need either of these options?
I was going to simply hide the distance judgement within the Match class and return the final result.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was going to simply hide the distance judgement within the Match class and return the final result.

Obviously that, however you're incidentally also exposing the data you're using to make that decision, which means the data is going to be hard to change.

packages/@aws-cdk/assertions/lib/match.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/assertions/lib/match.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/assertions/lib/match.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/assertions/lib/match.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/assertions/lib/assertions.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/assertions/lib/match.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@BenChaimberg BenChaimberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mostly front-facing changes. "nitpick" means entirely optional and probably pedantic

packages/@aws-cdk/assertions/README.md Outdated Show resolved Hide resolved
packages/@aws-cdk/assertions/README.md Show resolved Hide resolved
packages/@aws-cdk/assertions/README.md Show resolved Hide resolved
packages/@aws-cdk/assertions/README.md Outdated Show resolved Hide resolved
packages/@aws-cdk/assertions/lib/assertions.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/assertions/test/match.test.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/assertions/test/match.test.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/assertions/test/match.test.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/assertions/test/match.test.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-cognito/test/user-pool.test.ts Outdated Show resolved Hide resolved
@nija-at nija-at requested review from rix0rrr and BenChaimberg June 24, 2021 17:55
@BenChaimberg BenChaimberg added the pr/do-not-merge This PR should not be merged at this time. label Jun 24, 2021
Copy link
Contributor

@BenChaimberg BenChaimberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just a couple of small things

packages/@aws-cdk/assertions/lib/assertions.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/assertions/lib/match.ts Show resolved Hide resolved
@nija-at nija-at force-pushed the nija-at/assertion-matchers branch from 438aaea to a5850c6 Compare June 25, 2021 11:16
@BenChaimberg BenChaimberg removed the pr/do-not-merge This PR should not be merged at this time. label Jun 25, 2021
@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject89A8053A-LhjRyN9kxr8o
  • Commit ID: 518bb3e
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@mergify mergify bot merged commit 20e2b79 into master Jun 25, 2021
@mergify mergify bot deleted the nija-at/assertion-matchers branch June 25, 2021 17:42
@mergify
Copy link
Contributor

mergify bot commented Jun 25, 2021

Thank you for contributing! Your pull request will be updated from master and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

hollanddd pushed a commit to hollanddd/aws-cdk that referenced this pull request Aug 26, 2021
Implement a `hasResource()` API that supports partial matchers like
`arrayWith()` and `objectLike()`.

This covers the gap with similar support available in the existing assert
module.

Re-using the same implementation while providing an ergonomic jsii API
proved challenging. 
This change simply re-implements these matchers, heavily influenced by
existing implementations in the assert module.

Additionally, as a test case, the cognito and synthetics modules are migrated
to use the assertions module for its unit tests.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contribution/core This is a PR that came from AWS.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants