Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ way out. A user flicking a toggle back and forth produces one write, not one per
```

They appear in `--help` in registration order, between the tray switches and `--for`. A value handler
that throws `FormatException` or `ArgumentException` turns its message into the usage error, and the
tool exits 2 without starting.
that throws `FormatException`, `ArgumentException` or `OverflowException` (what `int.Parse` throws for a
number out of range) turns its message into the usage error, and the tool exits 2 without starting.

### Adding rows to `--status`

Expand Down
15 changes: 15 additions & 0 deletions TrayApp.Test/TrayAppBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@
}

[TestMethod]
public void Preferences_WithTheLongestDebounce_IsAcceptedAndATimerTakesIt()

Check warning on line 164 in TrayApp.Test/TrayAppBuilderTests.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Add at least one assertion to this test case.

Check warning on line 164 in TrayApp.Test/TrayAppBuilderTests.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Add at least one assertion to this test case.
{
InMemoryPersistenceProvider<string> provider = new();
TrayAppDefinition app = TrayAppBuilder.Create("demo")
Expand Down Expand Up @@ -303,6 +303,21 @@
Assert.AreEqual("'lots' is not a number.", error);
}

[TestMethod]
public void TryApplyOptions_WhenAHandlerParsesAnOutOfRangeNumber_ReportsItAsAUsageError()
{
// int.Parse throws OverflowException, not FormatException, for a number too big for an int, and
// that used to escape as an unhandled exception (#69).
TrayAppDefinition app = TrayAppBuilder.Create("demo")
.Option(["--count"], "n", "How many.", value => _ = int.Parse(value, System.Globalization.CultureInfo.InvariantCulture))
.Build();

Assert.IsTrue(CommandLineParser.TryParse(["--count", "99999999999"], app.Options, out CommandLineOptions options, out _));
Assert.IsFalse(TrayAppBuilder.TryApplyOptions(options, out string error));
string expected = Assert.ThrowsExactly<OverflowException>(() => int.Parse("99999999999", System.Globalization.CultureInfo.InvariantCulture)).Message;
Assert.AreEqual(expected, error);
}

[TestMethod]
public async Task RunAsync_WithHelp_PrintsUsageAndSucceeds()
{
Expand Down
2 changes: 1 addition & 1 deletion TrayApp/TrayAppBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ internal static bool TryApplyOptions(CommandLineOptions options, out string erro
{
match.Option.Apply(match.Value);
}
catch (Exception ex) when (ex is FormatException or ArgumentException)
catch (Exception ex) when (ex is FormatException or ArgumentException or OverflowException)
{
error = ex.Message;
return false;
Expand Down
Loading