-
Notifications
You must be signed in to change notification settings - Fork 164
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
MockPromptForString + doctor to work with ValidatePathError. #325
Merged
andreban
merged 11 commits into
GoogleChromeLabs:master
from
chenlevy24:MockPromptForStrings
Sep 10, 2020
Merged
Changes from 5 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
df1c828
Added 'MockPromptForString' class and changed 'doctor' to work with '…
chenlevy24 556dacd
Removed 'ValidatePathError' from 'index.ts'
chenlevy24 fac428d
Fixed a typo in 'MockPromptForStrings'
chenlevy24 acba2ea
Changed the implementation of the class. Now in order to use it we ne…
chenlevy24 b3fc03c
Added 'getNextMessage' function and did some small syntactic changes
chenlevy24 1b4ffdf
Changed the order of the insertion of the messages. it is not in reve…
chenlevy24 2c18f9d
Added tests for the class and changed it's logic
chenlevy24 745c35c
Changed the name of the test to mockPromptSpec.ts
chenlevy24 5897c8e
Removed the old named test
chenlevy24 011f86d
Added tests for all of the functions and changed the classes name(rem…
chenlevy24 6e1020d
United 2 lines into one in 'getNextMessage'
chenlevy24 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,107 @@ | ||
/* | ||
* Copyright 2020 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import {Prompt, ValidateFunction} from '../../lib/Prompt'; | ||
|
||
/** | ||
* A class which used for testing and which mocks user's input. | ||
*/ | ||
export class MockPromptForStrings implements Prompt { | ||
private responses: string[] = []; | ||
|
||
/** | ||
* Sets the next answer of this class to be the given message. | ||
* @param message the message to be returned in the next prompt message. | ||
*/ | ||
addMessage(message: string): void { | ||
this.responses.push(message); | ||
} | ||
|
||
async printMessage(): Promise<void> { | ||
// An empty function for testing. | ||
} | ||
|
||
/** | ||
* Sets the output to be the given message. | ||
* @param message the message to be prompt. Not relevant for tests. | ||
* @param {string | null} defaultValue a default value or null. | ||
* @param {ValidateFunction<T>} validateFunction a function to validate the input. | ||
* @returns {Promise<T>} a {@link Promise} that resolves to the validated loaded message, | ||
* converted to `T` by the `validateFunction`. | ||
*/ | ||
async promptInput<T>(_message: string, | ||
_defaultValue: string | null, | ||
validateFunction: ValidateFunction<T>): Promise<T> { | ||
const nextResponse = this.getNextMessage(); | ||
return (await validateFunction(nextResponse)).unwrap(); | ||
} | ||
|
||
/** | ||
* Sets the output to be the given message. | ||
* @param message the message to be prompt. Not relevant for tests. | ||
* @param {string[]} choices a list of choices. Not relevant for testing. | ||
* @param {string | null} defaultValue a default value or null. | ||
* @param {ValidateFunction<T>} validateFunction a function to validate the input. | ||
* @returns {Promise<T>} a {@link Promise} that resolves to the validated loaded message, | ||
* converted to `T` by the `validateFunction`. | ||
*/ | ||
async promptChoice<T>(_message: string, | ||
_choices: string[], | ||
_defaultValue: string | null, | ||
validateFunction: ValidateFunction<T>): Promise<T> { | ||
const nextResponse = this.getNextMessage(); | ||
return (await validateFunction(nextResponse)).unwrap(); | ||
} | ||
|
||
/** | ||
* Sets the output to be the given message. | ||
* @param message the message to be prompt. Not relevant for tests. | ||
* @param defaultValue the value to be returned | ||
* @returns {Promise<boolean>} a {@link Promise} that resolves to a {@link boolean} value. The | ||
* value will the `true` if the user answers `Yes` and `false` for `No`. | ||
*/ | ||
async promptConfirm(_message: string, defaultValue: boolean): Promise<boolean> { | ||
return defaultValue; | ||
} | ||
|
||
/** | ||
* Sets the output to be the given message. | ||
* @param message the message to be prompt. Not relevant for tests. | ||
* @param {ValidateFunction<T>} validateFunction a function to validate the input. | ||
* @returns {Promise<string>} a {@link Promise} that resolves to the user input validated by | ||
* `validateFunction`. | ||
*/ | ||
async promptPassword(_message: string, validateFunction: ValidateFunction<string>, | ||
): Promise<string> { | ||
const nextResponse = this.getNextMessage(); | ||
return (await validateFunction(nextResponse)).unwrap(); | ||
} | ||
|
||
/** | ||
* Sets the output to be the given message. | ||
* @param {ValidateFunction<T>} validateFunction a function to validate the input. | ||
* @returns {string} which is the next message to be prompted`. | ||
*/ | ||
private getNextMessage(): string { | ||
if (this.responses.length < 0) { | ||
chenlevy24 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
throw new Error('No answer was given. Please use addMessage(NextResponse) before' + | ||
chenlevy24 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
' using this function'); | ||
} | ||
const nextResponse = this.responses[this.responses.length]; | ||
chenlevy24 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this.responses.pop(); | ||
return nextResponse; | ||
} | ||
} |
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
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 just realised this isn't actually "for strings" anymore - it can just be called MockPrompt now.
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.
But the messages are always strings
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 had thought that the "ForStrings" was about the return type of the methods - for example that promptInput would always return a string (as opposed to a Color or something). The version we've got now works if it's used in a place that returns a Color.
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.
The "forStrings" was for the input we accept, and as for now it's only strings
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 had the same impression about
T
always being astring
, which affects thevalidateFunction
input and thePromise<T>
output. ButT
can now have any values and we could dropForStrings
. The fact thataddMessages()
is always a string seems to be more of an implementation detail?