Skip to content

Commit 37596e6

Browse files
authoredNov 25, 2021
fix(assert): support multiline strings with stringLike() (#17692)
Updates the `RegExp` constructor to support multiline. Resolves #17691 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 39fe11b commit 37596e6

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed
 

‎packages/@aws-cdk/assert-internal/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ The following matchers exist:
103103
back to exact value matching.
104104
- `arrayWith(E, [F, ...])` - value must be an array containing the given elements (or matchers) in any order.
105105
- `stringLike(S)` - value must be a string matching `S`. `S` may contain `*` as wildcard to match any number
106-
of characters.
106+
of characters. Multiline strings are supported.
107107
- `anything()` - matches any value.
108108
- `notMatching(M)` - any value that does NOT match the given matcher (or exact value) given.
109109
- `encodedJson(M)` - value must be a string which, when decoded as JSON, matches the given matcher or

‎packages/@aws-cdk/assert-internal/lib/assertions/have-resource-matchers.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -239,11 +239,11 @@ function isCallable(x: any): x is ((...args: any[]) => any) {
239239
}
240240

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

248248
return annotateMatcher({ $stringContaining: pattern }, (value: any, failure: InspectionFailure) => {
249249
if (typeof value !== 'string') {

‎packages/@aws-cdk/assert-internal/test/have-resource.test.ts

+35-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1-
import { ABSENT, arrayWith, exactValue, expect as cdkExpect, haveResource, haveResourceLike, Capture, anything } from '../lib/index';
1+
import {
2+
ABSENT,
3+
arrayWith,
4+
exactValue,
5+
expect as cdkExpect,
6+
haveResource,
7+
haveResourceLike,
8+
Capture,
9+
anything,
10+
stringLike,
11+
} from '../lib/index';
212
import { mkResource, mkStack } from './cloud-artifact';
313

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

169+
test('can use matcher to test stringLike on single-line strings', () => {
170+
const synthStack = mkResource({
171+
Content: 'something required something',
172+
});
173+
174+
expect(() => {
175+
cdkExpect(synthStack).to(haveResource('Some::Resource', {
176+
Content: stringLike('*required*'),
177+
}));
178+
}).not.toThrowError();
179+
});
180+
181+
test('can use matcher to test stringLike on multi-line strings', () => {
182+
const synthStack = mkResource({
183+
Content: 'something\nrequired\nsomething',
184+
});
185+
186+
expect(() => {
187+
cdkExpect(synthStack).to(haveResource('Some::Resource', {
188+
Content: stringLike('*required*'),
189+
}));
190+
}).not.toThrowError();
191+
});
192+
159193
test('arrayContaining must match all elements in any order', () => {
160194
const synthStack = mkResource({
161195
List: ['a', 'b'],

0 commit comments

Comments
 (0)
Please sign in to comment.