-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(assertions): capture matching value (#16426)
The `assertions` module now has the ability to capture values during template matching. These captured values can then later be retrieved and used for further processing. This change also adds support for `anyValue()` matcher. This matcher will match any non-nullish targets during template matching. Migrated some tests in `pipelines` module to the `assertions` module, using the new capture and `anyValue()` features. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
- Loading branch information
Niranjan Jayakar
authored
Sep 9, 2021
1 parent
430f50a
commit cc74f92
Showing
17 changed files
with
512 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { Matcher, MatchResult } from './matcher'; | ||
import { Type, getType } from './private/type'; | ||
|
||
/** | ||
* Capture values while matching templates. | ||
* Using an instance of this class within a Matcher will capture the matching value. | ||
* The `as*()` APIs on the instance can be used to get the captured value. | ||
*/ | ||
export class Capture extends Matcher { | ||
public readonly name: string; | ||
private value: any = null; | ||
|
||
constructor() { | ||
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; | ||
} | ||
|
||
/** | ||
* 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.reportIncorrectType('string'); | ||
} | ||
|
||
/** | ||
* Retrieve the captured value as a number. | ||
* 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.reportIncorrectType('number'); | ||
} | ||
|
||
/** | ||
* Retrieve the captured value as a boolean. | ||
* 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.reportIncorrectType('boolean'); | ||
} | ||
|
||
/** | ||
* Retrieve the captured value as an array. | ||
* 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.reportIncorrectType('array'); | ||
} | ||
|
||
/** | ||
* Retrieve the captured value as a JSON object. | ||
* 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.reportIncorrectType('object'); | ||
} | ||
|
||
private checkNotNull(): void { | ||
if (this.value == null) { | ||
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)}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './capture'; | ||
export * from './template'; | ||
export * from './match'; | ||
export * from './matcher'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export type Type = 'string' | 'number' | 'bigint' | 'boolean' | 'symbol' | 'undefined' | 'object' | 'function' | 'array'; | ||
|
||
export function getType(obj: any): Type { | ||
return Array.isArray(obj) ? 'array' : typeof obj; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { Capture, Match } from '../lib'; | ||
|
||
describe('Capture', () => { | ||
test('uncaptured', () => { | ||
const capture = new Capture(); | ||
expect(() => capture.asString()).toThrow(/No value captured/); | ||
}); | ||
|
||
test('nullish', () => { | ||
const capture = new Capture(); | ||
const matcher = Match.objectEquals({ foo: capture }); | ||
|
||
const result = matcher.test({ foo: null }); | ||
expect(result.failCount).toEqual(1); | ||
expect(result.toHumanStrings()[0]).toMatch(/Can only capture non-nullish values/); | ||
}); | ||
|
||
test('asString()', () => { | ||
const capture = new Capture(); | ||
const matcher = Match.objectEquals({ foo: capture }); | ||
|
||
matcher.test({ foo: 'bar' }); | ||
expect(capture.asString()).toEqual('bar'); | ||
|
||
matcher.test({ foo: 3 }); | ||
expect(() => capture.asString()).toThrow(/expected to be string but found number/); | ||
}); | ||
|
||
test('asNumber()', () => { | ||
const capture = new Capture(); | ||
const matcher = Match.objectEquals({ foo: capture }); | ||
|
||
matcher.test({ foo: 3 }); | ||
expect(capture.asNumber()).toEqual(3); | ||
|
||
matcher.test({ foo: 'bar' }); | ||
expect(() => capture.asNumber()).toThrow(/expected to be number but found string/); | ||
}); | ||
|
||
test('asArray()', () => { | ||
const capture = new Capture(); | ||
const matcher = Match.objectEquals({ foo: capture }); | ||
|
||
matcher.test({ foo: ['bar'] }); | ||
expect(capture.asArray()).toEqual(['bar']); | ||
|
||
matcher.test({ foo: 'bar' }); | ||
expect(() => capture.asArray()).toThrow(/expected to be array but found string/); | ||
}); | ||
|
||
test('asObject()', () => { | ||
const capture = new Capture(); | ||
const matcher = Match.objectEquals({ foo: capture }); | ||
|
||
matcher.test({ foo: { fred: 'waldo' } }); | ||
expect(capture.asObject()).toEqual({ fred: 'waldo' }); | ||
|
||
matcher.test({ foo: 'bar' }); | ||
expect(() => capture.asObject()).toThrow(/expected to be object but found string/); | ||
}); | ||
|
||
test('nested within an array', () => { | ||
const capture = new Capture(); | ||
const matcher = Match.objectEquals({ foo: ['bar', capture] }); | ||
|
||
matcher.test({ foo: ['bar', 'baz'] }); | ||
expect(capture.asString()).toEqual('baz'); | ||
}); | ||
}); |
Oops, something went wrong.