Skip to content

Commit

Permalink
ProgressTask.GetPercentage() returns 100 when max value is 0 (#1694)
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankRay78 authored Nov 22, 2024
1 parent be45494 commit 4515d89
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Spectre.Console/Live/Progress/ProgressTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,11 @@ private void Update(

private double GetPercentage()
{
if (MaxValue == 0)
{
return 100;
}

var percentage = (Value / MaxValue) * 100;
percentage = Math.Min(100, Math.Max(0, percentage));
return percentage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,31 @@ public void Setting_Max_Value_Should_Set_The_MaxValue_And_Cap_Value()
task.Value.ShouldBe(20);
}

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

var task = default(ProgressTask);
var progress = new Progress(console)
.Columns(new[] { new ProgressBarColumn() })
.AutoRefresh(false)
.AutoClear(false);

// When
progress.Start(ctx =>
{
task = ctx.AddTask("foo");
task.MaxValue = 0;
});

// Then
task.Value.ShouldBe(0);
task.Percentage.ShouldBe(100);
}

[Fact]
public void Setting_Value_Should_Override_Incremented_Value()
{
Expand Down

0 comments on commit 4515d89

Please sign in to comment.