Skip to content

Commit

Permalink
fix(assert): support multiline strings with stringLike() (aws#17692)
Browse files Browse the repository at this point in the history
Updates the `RegExp` constructor to support multiline. Resolves aws#17691

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
diegotry authored and Andrew Beresford committed Nov 29, 2021
1 parent 2ab3446 commit 52577b0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/@aws-cdk/assert-internal/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ The following matchers exist:
back to exact value matching.
- `arrayWith(E, [F, ...])` - value must be an array containing the given elements (or matchers) in any order.
- `stringLike(S)` - value must be a string matching `S`. `S` may contain `*` as wildcard to match any number
of characters.
of characters. Multiline strings are supported.
- `anything()` - matches any value.
- `notMatching(M)` - any value that does NOT match the given matcher (or exact value) given.
- `encodedJson(M)` - value must be a string which, when decoded as JSON, matches the given matcher or
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,11 +239,11 @@ function isCallable(x: any): x is ((...args: any[]) => any) {
}

/**
* Do a glob-like pattern match (which only supports *s)
* Do a glob-like pattern match (which only supports *s). Supports multiline strings.
*/
export function stringLike(pattern: string): PropertyMatcher {
// Replace * with .* in the string, escape the rest and brace with ^...$
const regex = new RegExp(`^${pattern.split('*').map(escapeRegex).join('.*')}$`);
const regex = new RegExp(`^${pattern.split('*').map(escapeRegex).join('.*')}$`, 'm');

return annotateMatcher({ $stringContaining: pattern }, (value: any, failure: InspectionFailure) => {
if (typeof value !== 'string') {
Expand Down
36 changes: 35 additions & 1 deletion packages/@aws-cdk/assert-internal/test/have-resource.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
import { ABSENT, arrayWith, exactValue, expect as cdkExpect, haveResource, haveResourceLike, Capture, anything } from '../lib/index';
import {
ABSENT,
arrayWith,
exactValue,
expect as cdkExpect,
haveResource,
haveResourceLike,
Capture,
anything,
stringLike,
} from '../lib/index';
import { mkResource, mkStack } from './cloud-artifact';

test('support resource with no properties', () => {
Expand Down Expand Up @@ -156,6 +166,30 @@ describe('property absence', () => {
}).toThrowError(/Array did not contain expected element/);
});

test('can use matcher to test stringLike on single-line strings', () => {
const synthStack = mkResource({
Content: 'something required something',
});

expect(() => {
cdkExpect(synthStack).to(haveResource('Some::Resource', {
Content: stringLike('*required*'),
}));
}).not.toThrowError();
});

test('can use matcher to test stringLike on multi-line strings', () => {
const synthStack = mkResource({
Content: 'something\nrequired\nsomething',
});

expect(() => {
cdkExpect(synthStack).to(haveResource('Some::Resource', {
Content: stringLike('*required*'),
}));
}).not.toThrowError();
});

test('arrayContaining must match all elements in any order', () => {
const synthStack = mkResource({
List: ['a', 'b'],
Expand Down

0 comments on commit 52577b0

Please sign in to comment.