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

fix(assert): support multiline strings with stringLike() #17692

Merged
merged 3 commits into from
Nov 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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