-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add option input methods * Update README file to include option input methods * Add unit tests for get option method * Add unit tests for get yes/no option method
- Loading branch information
1 parent
3f5ca81
commit 22f6174
Showing
4 changed files
with
148 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using IOUtils.Input; | ||
using NUnit.Framework; | ||
|
||
namespace IOUtils.Tests.InputTests.OptionTests; | ||
|
||
public class OptionTests { | ||
private const string Question = "Example question text"; | ||
|
||
[TestCase("1", new[] {"Option 1", "Option 2"}, "Option 1")] | ||
[TestCase("2", new[] {"Option 1", "Option 2"}, "Option 2")] | ||
[TestCase("3", new[] {"Option 1", "Option 2", "Option 3"}, "Option 3")] | ||
[TestCase("4", new[] {"Option 1", "Option 2", "Option 3", "Option 4"}, "Option 4")] | ||
[TestCase("3", new[] {"Option 1", "Option 2", "Option 3", "Option 4", "Option 5"}, "Option 3")] | ||
public void OptionInput_Options_ValidInput_ReturnsValue(string input, string[] options, string expected) { | ||
MockProvider mockProvider = new(input); | ||
|
||
string actual = OptionInput.GetOption(Question, options, mockProvider); | ||
|
||
Assert.That(actual, Is.EqualTo(expected)); | ||
} | ||
|
||
[TestCase("6", new[] {"Option 1", "Option 2"})] | ||
[TestCase("0", new[] {"Option 1", "Option 2"})] | ||
[TestCase("1.5", new[] {"Option 1", "Option 2"})] | ||
[TestCase("1.0", new[] {"Option 1", "Option 2", "Option 3"})] | ||
[TestCase("0", new[] {"Option 1", "Option 2", "Option 3", "Option 4"})] | ||
[TestCase("-1", new[] {"Option 1", "Option 2", "Option 3"})] | ||
[TestCase("abc", new[] {"Option 1", "Option 2"})] | ||
[TestCase("Yes", new[] {"Option 1", "Option 2", "Option 3"})] | ||
[TestCase("No", new[] {"Option 1", "Option 2"})] | ||
[TestCase("", new[] {"Option 1", "Option 2", "Option 3", "Option 4"})] | ||
public void OptionInput_Options_InvalidInput_ErrorOutput(string input, string[] options) { | ||
MockProvider mockProvider = new(input); | ||
|
||
Assert.Throws<InvalidOperationException>(() => OptionInput.GetOption(Question, options, mockProvider)); | ||
|
||
string expectedQuestion = Question; | ||
|
||
for (int i = 0; i < options.Length; i++) { | ||
expectedQuestion += $"{Environment.NewLine}{i + 1}. {options[i]}"; | ||
} | ||
|
||
string expectedErrorMessage = $"That was not valid, enter a whole number between 1 and {options.Length}"; | ||
|
||
string[] expected = new[] { expectedQuestion, expectedErrorMessage }; | ||
|
||
Assert.That(mockProvider.OutputLines, Is.EquivalentTo(expected)); | ||
} | ||
|
||
[Test] | ||
public void OptionInput_Options_EmptyOptions_ThrowsException() { | ||
Assert.Throws<ArgumentException>(() => OptionInput.GetOption(Question, Array.Empty<string>())); | ||
} | ||
} |
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,45 @@ | ||
using IOUtils.Input; | ||
using NUnit.Framework; | ||
|
||
namespace IOUtils.Tests.InputTests.OptionTests; | ||
|
||
public class YesNoOptionTests { | ||
private const string Question = "Example question text"; | ||
|
||
[TestCase("1", true)] | ||
[TestCase("2", false)] | ||
public void OptionInput_YesNo_ValidInput_ReturnsValue(string input, bool expected) { | ||
MockProvider mockProvider = new(input); | ||
|
||
bool actual = OptionInput.GetYesNoOption(Question, provider: mockProvider); | ||
|
||
Assert.That(actual, Is.EqualTo(expected)); | ||
} | ||
|
||
[TestCase("3")] | ||
[TestCase("1.5")] | ||
[TestCase("1.0")] | ||
[TestCase("0")] | ||
[TestCase("-1")] | ||
[TestCase("abc")] | ||
[TestCase("Yes")] | ||
[TestCase("No")] | ||
[TestCase("")] | ||
[TestCase(null)] | ||
public void OptionInput_YesNo_InvalidInput_ErrorOutput(string input) { | ||
MockProvider mockProvider = new(input); | ||
|
||
Assert.Throws<InvalidOperationException>(() => OptionInput.GetYesNoOption(Question, provider: mockProvider)); | ||
|
||
string[] expected = { | ||
$""" | ||
{Question} | ||
1. Yes | ||
2. No | ||
""", | ||
"That was not valid, enter a whole number between 1 and 2" | ||
}; | ||
|
||
Assert.That(mockProvider.OutputLines, Is.EquivalentTo(expected)); | ||
} | ||
} |
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,30 @@ | ||
using System.Text; | ||
using IOUtils.Providers; | ||
|
||
namespace IOUtils.Input; | ||
|
||
public static class OptionInput { | ||
public static string GetOption(string question, string[] options, IProvider? provider = null) { | ||
int responseIndex = GetOptionIndex(question, options, provider); | ||
|
||
return options[responseIndex]; | ||
} | ||
|
||
public static bool GetYesNoOption(string question, IProvider? provider = null) { | ||
// The index of the "Yes" option is 0 | ||
return GetOptionIndex(question, new[] { "Yes", "No" }, provider) == 0; | ||
} | ||
|
||
public static int GetOptionIndex(string question, string[] options, IProvider? provider = null) { | ||
if (options.Length < 1) | ||
throw new ArgumentException("There must be at least one option to choose from", nameof(options)); | ||
|
||
StringBuilder questionStringBuilder = new(question); | ||
|
||
for (int i = 0; i < options.Length; i++) | ||
questionStringBuilder.Append($"{Environment.NewLine}{i + 1}. {options[i]}"); | ||
|
||
// The index of the selected option is 1 less than the numerical input | ||
return NumericalInput<int>.Get(questionStringBuilder.ToString(), 1, options.Length, provider) - 1; | ||
} | ||
} |
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