-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
feat(assertions): capture matching value #16426
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would appreciate a more "useful" example here that demonstrates the benefit of using capturing. We can use the assertion found in the pipelines module like
expect(captured.asString().length()).toBe(3)
or something