Skip to content

Commit

Permalink
ProgressBar - fixed incorrect throw of out of range exception when se…
Browse files Browse the repository at this point in the history
…tting the MaxValue property. fixes #34
  • Loading branch information
lastunicorn committed Sep 11, 2018
1 parent 091bbb6 commit ba45f2f
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 8 deletions.
23 changes: 17 additions & 6 deletions sources/ConsoleTools/ConsoleTools.Demo.ProgressBar/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -49,15 +50,25 @@ private static void RunDemo()

Task.Run<Task>(async () =>
{
progressBar.Display();
try
{
progressBar.Display();

for (int i = 0; i <= 100; i++)
for (int i = 0; i <= 100; i++)
{
await Task.Delay(50);
progressBar.Value = i;
}
}
catch (Exception ex)
{
await Task.Delay(50);
progressBar.Value = i;
CustomConsole.WriteLine();
CustomConsole.WriteError(ex);
}
finally
{
finishEvent.Set();
}

finishEvent.Set();
});

finishEvent.Wait();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@
<Compile Include="SizeTests\ConstructorTests.cs" />
<Compile Include="SizeTests\InflateWithNumberTests.cs" />
<Compile Include="SizeTests\IsEmptyTests.cs" />
<Compile Include="Spinners\ProgressBarTests\MinValueTests.cs" />
<Compile Include="Spinners\ProgressBarTests\MaxValueTests.cs" />
<Compile Include="TabularData\BorderTemplateTests\GenerateBottomBorderTests.cs" />
<Compile Include="TabularData\BorderTemplateTests\GenerateHorizontalSeparator1Tests.cs" />
<Compile Include="TabularData\BorderTemplateTests\GenerateHorizontalSeparator2Tests.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// ConsoleTools
// Copyright (C) 2017-2018 Dust in the Wind
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

using System;
using DustInTheWind.ConsoleTools.Spinners;
using NUnit.Framework;

namespace DustInTheWind.ConsoleTools.Tests.Spinners.ProgressBarTests
{
[TestFixture]
public class MaxValueTests
{
[Test]
public void MinValue_is_10_set_MaxValue_to_12_succeeds()
{
ProgressBar progressBar = new ProgressBar { MinValue = 10 };

progressBar.MaxValue = 12;

Assert.That(progressBar.MaxValue, Is.EqualTo(12));
}

[Test]
public void MinValue_is_10_set_MaxValue_to_10_succeeds()
{
ProgressBar progressBar = new ProgressBar { MinValue = 10 };

progressBar.MaxValue = 10;

Assert.That(progressBar.MaxValue, Is.EqualTo(10));
}

[Test]
public void MinValue_is_10_set_MaxValue_to_8_throws()
{
ProgressBar progressBar = new ProgressBar { MinValue = 10 };

Assert.Throws<ArgumentOutOfRangeException>(() =>
{
progressBar.MaxValue = 8;
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// ConsoleTools
// Copyright (C) 2017-2018 Dust in the Wind
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

using System;
using DustInTheWind.ConsoleTools.Spinners;
using NUnit.Framework;

namespace DustInTheWind.ConsoleTools.Tests.Spinners.ProgressBarTests
{
[TestFixture]
public class MinValueTests
{
[Test]
public void MaxValue_is_10_set_MinValue_to_8_succeeds()
{
ProgressBar progressBar = new ProgressBar { MaxValue = 10 };

progressBar.MinValue = 8;

Assert.That(progressBar.MinValue, Is.EqualTo(8));
}

[Test]
public void MaxValue_is_10_set_MinValue_to_10_succeeds()
{
ProgressBar progressBar = new ProgressBar { MaxValue = 10 };

progressBar.MinValue = 10;

Assert.That(progressBar.MinValue, Is.EqualTo(10));
}

[Test]
public void MaxValue_is_10_set_MinValue_to_12_throws()
{
ProgressBar progressBar = new ProgressBar { MaxValue = 10 };

Assert.Throws<ArgumentOutOfRangeException>(() =>
{
progressBar.MinValue = 12;
});
}
}
}
9 changes: 7 additions & 2 deletions sources/ConsoleTools/ConsoleTools/Spinners/ProgressBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public int MaxValue
get { return maxValue; }
set
{
if (value > MinValue)
if (value < MinValue)
throw new ArgumentOutOfRangeException(nameof(value), "MaxValue cannot be less than MinValue.");

maxValue = value;
Expand Down Expand Up @@ -322,7 +322,12 @@ private int CalculateValue()

private int CalculateFillLength(int value)
{
return (value - MinValue) * Length / (MaxValue - MinValue);
int valueRange = MaxValue - MinValue;

if (valueRange == 0)
return Length;

return (value - MinValue) * Length / valueRange;
}

/// <summary>
Expand Down

0 comments on commit ba45f2f

Please sign in to comment.