Skip to content

Commit

Permalink
Add percentage calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
GeertvanHorrik committed Aug 31, 2020
1 parent b1c0e16 commit d4712da
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/Squirrel/UpdateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,27 @@ Task<IDisposable> acquireUpdateLock()
});
}

/// <summary>
/// Calculates the total percentage of a specific step that should report within a specific range.
/// <para />
/// If a step needs to report between 50 -> 75 %, this method should be used as CalculateProgress(percentage, 50, 75).
/// </summary>
/// <param name="percentageOfCurrentStep">The percentage of the current step, a value between 0 and 100.</param>
/// <param name="stepStartPercentage">The start percentage of the range the current step represents.</param>
/// <param name="stepEndPercentage">The end percentage of the range the current step represents.</param>
/// <returns>The calculated percentage that can be reported about the total progress.</returns>
internal static int CalculateProgress(int percentageOfCurrentStep, int stepStartPercentage, int stepEndPercentage)
{
// Ensure we are between 0 and 100
percentageOfCurrentStep = Math.Max(Math.Min(percentageOfCurrentStep, 100), 0);

var range = stepEndPercentage - stepStartPercentage;
var singleValue = range / 100d;
var totalPercentage = (singleValue * percentageOfCurrentStep) + stepStartPercentage;

return (int)totalPercentage;
}

static string getApplicationName()
{
var fi = new FileInfo(getUpdateExe());
Expand Down
16 changes: 16 additions & 0 deletions test/UpdateManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,22 @@ public void CurrentlyInstalledVersionTests(string input, string expectedVersion)
Assert.Equal(expected, fixture.CurrentlyInstalledVersion(input));
}
}

[Theory]
[InlineData(0, 0, 25, 0)]
[InlineData(12, 0, 25, 3)]
[InlineData(55, 0, 25, 13)]
[InlineData(100, 0, 25, 25)]
[InlineData(0, 25, 50, 25)]
[InlineData(12, 25, 50, 28)]
[InlineData(55, 25, 50, 38)]
[InlineData(100, 25, 50, 50)]
public void CalculatesPercentageCorrectly(int percentageOfCurrentStep, int stepStartPercentage, int stepEndPercentage, int expectedPercentage)
{
var percentage = UpdateManager.CalculateProgress(percentageOfCurrentStep, stepStartPercentage, stepEndPercentage);

Assert.Equal(expectedPercentage, percentage);
}
}
}
}

0 comments on commit d4712da

Please sign in to comment.