mirror of
				https://github.com/nsnail/spectre.console.git
				synced 2025-11-04 18:40:50 +08:00 
			
		
		
		
	ProgressTask.GetPercentage() returns 100 when max value is 0 (#1694)
This commit is contained in:
		@@ -224,6 +224,11 @@ public sealed class ProgressTask : IProgress<double>
 | 
			
		||||
 | 
			
		||||
    private double GetPercentage()
 | 
			
		||||
    {
 | 
			
		||||
        if (MaxValue == 0)
 | 
			
		||||
        {
 | 
			
		||||
            return 100;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        var percentage = (Value / MaxValue) * 100;
 | 
			
		||||
        percentage = Math.Min(100, Math.Max(0, percentage));
 | 
			
		||||
        return percentage;
 | 
			
		||||
 
 | 
			
		||||
@@ -118,6 +118,31 @@ public sealed class ProgressTests
 | 
			
		||||
        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()
 | 
			
		||||
    {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user