From 2b1b80c0323f89baabc7b71fcb7db549be9cddfc Mon Sep 17 00:00:00 2001 From: kevinlondon Date: Sun, 20 Sep 2015 12:44:59 -0700 Subject: [PATCH] Changed percentage bars to use an explicit assertion error if valid is outside of the expected range. --- glances/outputs/glances_bars.py | 5 +++-- unitest.py | 12 ++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/glances/outputs/glances_bars.py b/glances/outputs/glances_bars.py index 3566c833..f6535368 100644 --- a/glances/outputs/glances_bars.py +++ b/glances/outputs/glances_bars.py @@ -69,8 +69,9 @@ class Bar(object): @percent.setter def percent(self, value): - assert value >= 0 - assert value <= 100 + if value < 0 or value > 100: + raise AssertionError('The percent must be between 0 and 100.') + self.__percent = value @property diff --git a/unitest.py b/unitest.py index f87d31b0..4e9e876b 100755 --- a/unitest.py +++ b/unitest.py @@ -24,6 +24,7 @@ import sys import time import unittest +from glances.outputs.glances_bars import Bar from glances.core.glances_globals import ( appname, is_linux, @@ -186,5 +187,16 @@ class TestGlances(unittest.TestCase): # Check if number of processes in the list equal counter # self.assertEqual(total, len(stats_grab)) + def test_011_output_bars_must_be_between_0_and_100_percent(self): + bar = Bar(size=1) + with self.assertRaises(AssertionError): + bar.percent = -1 + bar.percent = 101 + + # 0 - 100 is an inclusive range, so these should not error. + bar.percent = 0 + bar.percent = 100 + + if __name__ == '__main__': unittest.main()