Skip to content

Commit

Permalink
Merge branch 'master' into iotevents-input
Browse files Browse the repository at this point in the history
  • Loading branch information
mergify[bot] authored Dec 10, 2021
2 parents ed80896 + de67aae commit fb25883
Show file tree
Hide file tree
Showing 281 changed files with 10,126 additions and 1,562 deletions.
40 changes: 20 additions & 20 deletions .github/workflows/issue-label-assign.yml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion .github/workflows/yarn-upgrade.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
uses: actions/checkout@v2

- name: Set up Node
uses: actions/setup-node@v2.4.1
uses: actions/setup-node@v2.5.0
with:
node-version: 12

Expand Down
118 changes: 118 additions & 0 deletions CHANGELOG.md

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ this capability, please see the

## More Resources
* [CDK Workshop](https://cdkworkshop.com/)
* [Construct Hub](https://constructs.dev) - Find and use open-source Cloud Development Kit (CDK) libraries
* **[CDK Construction Zone](https://www.twitch.tv/collections/9kCOGphNZBYVdA)** - A Twitch live coding series hosted by the CDK team, season one episodes:
* Triggers: Join us as we implement [Triggers](https://github.com/aws/aws-cdk-rfcs/issues/71), a Construct for configuring deploy time actions. Episodes 1-3:
* [S1E1](https://www.twitch.tv/videos/917691798): Triggers (part 1); **Participants:** @NetaNir, @eladb, @richardhboyd
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
"fs-extra": "^9.1.0",
"graceful-fs": "^4.2.8",
"jest-junit": "^13.0.0",
"jsii-diff": "^1.46.0",
"jsii-pacmak": "^1.46.0",
"jsii-reflect": "^1.46.0",
"jsii-rosetta": "^1.46.0",
"jsii-diff": "^1.47.0",
"jsii-pacmak": "^1.47.0",
"jsii-reflect": "^1.47.0",
"jsii-rosetta": "^1.47.0",
"lerna": "^4.0.0",
"patch-package": "^6.4.7",
"standard-version": "^9.3.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"@aws-cdk/cdk-integ-tools": "0.0.0",
"@aws-cdk/aws-autoscaling": "0.0.0",
"@aws-cdk/cfn2ts": "0.0.0",
"jest": "^27.3.1",
"jest": "^27.4.3",
"@aws-cdk/pkglint": "0.0.0",
"@aws-cdk/assert-internal": "0.0.0"
},
Expand Down
4 changes: 2 additions & 2 deletions packages/@aws-cdk/app-delivery/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@
"@aws-cdk/cdk-integ-tools": "0.0.0",
"@aws-cdk/pkglint": "0.0.0",
"@types/jest": "^27.0.3",
"fast-check": "^2.19.0",
"jest": "^27.3.1"
"fast-check": "^2.20.0",
"jest": "^27.4.3"
},
"repository": {
"type": "git",
Expand Down
6 changes: 3 additions & 3 deletions packages/@aws-cdk/assert-internal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
"@aws-cdk/cdk-build-tools": "0.0.0",
"@aws-cdk/pkglint": "0.0.0",
"@types/jest": "^27.0.3",
"jest": "^27.3.1",
"ts-jest": "^27.0.7"
"jest": "^27.4.3",
"ts-jest": "^27.1.1"
},
"dependencies": {
"@aws-cdk/cloud-assembly-schema": "0.0.0",
Expand All @@ -40,7 +40,7 @@
"peerDependencies": {
"@aws-cdk/core": "0.0.0",
"constructs": "^3.3.69",
"jest": "^27.3.1"
"jest": "^27.4.3"
},
"repository": {
"url": "https://github.com/aws/aws-cdk.git",
Expand Down
4 changes: 2 additions & 2 deletions packages/@aws-cdk/assert/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"aws-cdk-migration": "0.0.0",
"constructs": "^3.3.69",
"jest": "^27.3.1",
"ts-jest": "^27.0.7"
"ts-jest": "^27.1.1"
},
"dependencies": {
"@aws-cdk/cloudformation-diff": "0.0.0",
Expand Down Expand Up @@ -74,6 +74,6 @@
"stability": "experimental",
"maturity": "developer-preview",
"publishConfig": {
"tag": "latest"
"tag": "latest-1"
}
}
64 changes: 64 additions & 0 deletions packages/@aws-cdk/assertions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -399,3 +399,67 @@ template.hasResourceProperties('Foo::Bar', {
fredCapture.asArray(); // returns ["Flob", "Cat"]
waldoCapture.asString(); // returns "Qux"
```

With captures, a nested pattern can also be specified, so that only targets
that match the nested pattern will be captured. This pattern can be literals or
further Matchers.

```ts
// Given a template -
// {
// "Resources": {
// "MyBar1": {
// "Type": "Foo::Bar",
// "Properties": {
// "Fred": ["Flob", "Cat"],
// }
// }
// "MyBar2": {
// "Type": "Foo::Bar",
// "Properties": {
// "Fred": ["Qix", "Qux"],
// }
// }
// }
// }

const capture = new Capture(Match.arrayWith(['Cat']));
template.hasResourceProperties('Foo::Bar', {
Fred: capture,
});

capture.asArray(); // returns ['Flob', 'Cat']
```

When multiple resources match the given condition, each `Capture` defined in
the condition will capture all matching values. They can be paged through using
the `next()` API. The following example illustrates this -

```ts
// Given a template -
// {
// "Resources": {
// "MyBar": {
// "Type": "Foo::Bar",
// "Properties": {
// "Fred": "Flob",
// }
// },
// "MyBaz": {
// "Type": "Foo::Bar",
// "Properties": {
// "Fred": "Quib",
// }
// }
// }
// }

const fredCapture = new Capture();
template.hasResourceProperties('Foo::Bar', {
Fred: fredCapture,
});

fredCapture.asString(); // returns "Flob"
fredCapture.next(); // returns true
fredCapture.asString(); // returns "Quib"
```
85 changes: 61 additions & 24 deletions packages/@aws-cdk/assertions/lib/capture.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Match } from '.';
import { Matcher, MatchResult } from './matcher';
import { Type, getType } from './private/type';

Expand All @@ -8,31 +9,63 @@ import { Type, getType } from './private/type';
*/
export class Capture extends Matcher {
public readonly name: string;
private value: any = null;
/** @internal */
public _captured: any[] = [];
private idx = 0;

constructor() {
/**
* Initialize a new capture
* @param pattern a nested pattern or Matcher.
* If a nested pattern is provided `objectLike()` matching is applied.
*/
constructor(private readonly pattern?: any) {
super();
this.name = 'Capture';
}

public test(actual: any): MatchResult {
this.value = actual;

const result = new MatchResult(actual);
if (actual == null) {
result.push(this, [], `Can only capture non-nullish values. Found ${actual}`);
return result.recordFailure({
matcher: this,
path: [],
message: `Can only capture non-nullish values. Found ${actual}`,
});
}

if (this.pattern !== undefined) {
const innerMatcher = Matcher.isMatcher(this.pattern) ? this.pattern : Match.objectLike(this.pattern);
const innerResult = innerMatcher.test(actual);
if (innerResult.hasFailed()) {
return innerResult;
}
}

result.recordCapture({ capture: this, value: actual });
return result;
}

/**
* When multiple results are captured, move the iterator to the next result.
* @returns true if another capture is present, false otherwise
*/
public next(): boolean {
if (this.idx < this._captured.length - 1) {
this.idx++;
return true;
}
return false;
}

/**
* Retrieve the captured value as a string.
* An error is generated if no value is captured or if the value is not a string.
*/
public asString(): string {
this.checkNotNull();
if (getType(this.value) === 'string') {
return this.value;
this.validate();
const val = this._captured[this.idx];
if (getType(val) === 'string') {
return val;
}
this.reportIncorrectType('string');
}
Expand All @@ -42,9 +75,10 @@ export class Capture extends Matcher {
* An error is generated if no value is captured or if the value is not a number.
*/
public asNumber(): number {
this.checkNotNull();
if (getType(this.value) === 'number') {
return this.value;
this.validate();
const val = this._captured[this.idx];
if (getType(val) === 'number') {
return val;
}
this.reportIncorrectType('number');
}
Expand All @@ -54,9 +88,10 @@ export class Capture extends Matcher {
* An error is generated if no value is captured or if the value is not a boolean.
*/
public asBoolean(): boolean {
this.checkNotNull();
if (getType(this.value) === 'boolean') {
return this.value;
this.validate();
const val = this._captured[this.idx];
if (getType(val) === 'boolean') {
return val;
}
this.reportIncorrectType('boolean');
}
Expand All @@ -66,9 +101,10 @@ export class Capture extends Matcher {
* An error is generated if no value is captured or if the value is not an array.
*/
public asArray(): any[] {
this.checkNotNull();
if (getType(this.value) === 'array') {
return this.value;
this.validate();
const val = this._captured[this.idx];
if (getType(val) === 'array') {
return val;
}
this.reportIncorrectType('array');
}
Expand All @@ -78,21 +114,22 @@ export class Capture extends Matcher {
* An error is generated if no value is captured or if the value is not an object.
*/
public asObject(): { [key: string]: any } {
this.checkNotNull();
if (getType(this.value) === 'object') {
return this.value;
this.validate();
const val = this._captured[this.idx];
if (getType(val) === 'object') {
return val;
}
this.reportIncorrectType('object');
}

private checkNotNull(): void {
if (this.value == null) {
private validate(): void {
if (this._captured.length === 0) {
throw new Error('No value captured');
}
}

private reportIncorrectType(expected: Type): never {
throw new Error(`Captured value is expected to be ${expected} but found ${getType(this.value)}. ` +
`Value is ${JSON.stringify(this.value, undefined, 2)}`);
throw new Error(`Captured value is expected to be ${expected} but found ${getType(this._captured[this.idx])}. ` +
`Value is ${JSON.stringify(this._captured[this.idx], undefined, 2)}`);
}
}
Loading

0 comments on commit fb25883

Please sign in to comment.