Skip to content
Open
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
51 changes: 51 additions & 0 deletions src/Spectre.Console.Tests/Unit/Live/Progress/ProgressTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,57 @@ public void Setting_Max_Value_To_Zero_Should_Make_Percentage_OneHundred()
task.Percentage.ShouldBe(100);
}

[Fact]
public void Adding_Task_With_Negative_Max_Value_Should_Throw()
{
// Given
var console = new TestConsole()
.Interactive();

var progress = new Progress(console)
.Columns(new ProgressBarColumn())
.AutoRefresh(false)
.AutoClear(false);

// When
var exception = Record.Exception(() =>
{
progress.Start(ctx =>
{
ctx.AddTask("foo", maxValue: -1);
});
});

// Then
exception.ShouldBeOfType<ArgumentOutOfRangeException>();
}

[Fact]
public void Setting_Max_Value_To_Negative_Value_Should_Throw()
{
// Given
var console = new TestConsole()
.Interactive();

var progress = new Progress(console)
.Columns(new ProgressBarColumn())
.AutoRefresh(false)
.AutoClear(false);

// When
var exception = Record.Exception(() =>
{
progress.Start(ctx =>
{
var task = ctx.AddTask("foo");
task.MaxValue = -1;
});
});

// Then
exception.ShouldBeOfType<ArgumentOutOfRangeException>();
}

[Fact]
public void Setting_Value_Should_Override_Incremented_Value()
{
Expand Down
10 changes: 10 additions & 0 deletions src/Spectre.Console/Live/Progress/ProgressTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ public ProgressTask(int id, string description, double maxValue, bool autoStart
_description = description?.RemoveNewLines()?.Trim() ??
throw new ArgumentNullException(nameof(description));

if (_maxValue < 0)
{
throw new ArgumentOutOfRangeException(nameof(maxValue), "Max value must be zero or greater.");
}

if (string.IsNullOrWhiteSpace(_description))
{
throw new ArgumentException("Task name cannot be empty", nameof(description));
Expand Down Expand Up @@ -221,6 +226,11 @@ private void Update(

if (maxValue != null)
{
if (maxValue.Value < 0)
{
throw new ArgumentOutOfRangeException(nameof(maxValue), "Max value must be zero or greater.");
}

_maxValue = maxValue.Value;
}

Expand Down