Skip to content

Commit

Permalink
added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
balintlaczko committed Oct 2, 2021
1 parent e6bd876 commit 316c7be
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 20 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ pytest.ini
tests/.coverage
tests/.vs/slnx.sqlite
tests/pytest.ini
tests/htmlcov
67 changes: 47 additions & 20 deletions musicalgestures/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,32 +65,59 @@ def adjust_printlength(self):
elif self.could_not_get_terminal_window:
return
else:
current_length = len(self.prefix) + self.length + \
self.decimals + len(self.suffix) + 10
_length_before = self.length
current_length = len(self.prefix) + self.length + self.decimals + len(self.suffix) + 10

# if the length of printed line is longer than the terminal window's width
if current_length > self.tw_width:
diff = current_length - self.tw_width

# if the difference is shorter than the progress bar length
if diff < self.length:
self.length -= diff
self.length -= diff # shorten the progress bar

# if the difference is at least as long as the progress bar or longer
else: # remove suffix
current_length = current_length - len(self.suffix)
diff = current_length - self.tw_width
current_length = current_length - len(self.suffix) # remove suffix
diff = current_length - self.tw_width # recalculate difference

# if the terminal width is long enough without suffix
if diff <= 0:
self.suffix = "" # just remove suffix

# the terminal window is too short even without suffix
# remove suffix and test again
else:
self.suffix = ""
elif diff < self.length:
self.suffix = ""
self.length -= diff
else: # remove prefix
current_length = current_length - len(self.prefix)
diff = current_length - self.tw_width
if diff <= 0:
self.suffix = ""
self.prefix = ""
elif diff < self.length:
self.suffix = ""
self.prefix = ""
self.length -= diff
else: # display only percent
self.display_only_percent = True

# --- SUFFIX IS REMOVED ---

# if the difference is shorter than the progress bar
if diff < self.length:
self.length -= diff # shorten progress bar

# if the difference is longer than the progress bar, remove prefix
else: # remove prefix
current_length = current_length - len(self.prefix)
diff = current_length - self.tw_width

# if the terminal width is long enough without prefix
if diff <= 0:
self.prefix = "" # just remove prefix

# the terminal window is too short even without prefix (and suffix)
# remove prefix and test again
else:
self.prefix = ""

# --- PREFFIX IS REMOVED ---

# if the difference is shorter than the progress bar
if diff < self.length:
self.length -= diff # shorten progress bar

else: # display only percent
self.display_only_percent = True

def progress(self, iteration):
"""
Expand Down
125 changes: 125 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,126 @@ def test_init(self):
assert pb.length == 30
assert pb.fill == "@"

def test_adjust_printlength_tw_width_0(self):
pb = MgProgressbar()
# spoof terminal window stuff
pb.tw_width = 0
pb.could_not_get_terminal_window = False
assert pb.adjust_printlength() == None

def test_adjust_printlength_no_terminal(self):
pb = MgProgressbar()
# spoof terminal window stuff
pb.tw_width = 1
pb.could_not_get_terminal_window = True
assert pb.adjust_printlength() == None

def test_adjust_printlength_shorten_by_1(self):
pb = MgProgressbar()
_current_length = len(pb.prefix) + pb.length + pb.decimals + len(pb.suffix) + 10
# spoof terminal window stuff
pb.tw_width = _current_length - 1
pb.could_not_get_terminal_window = False
suffix_before = pb.suffix
length_before = pb.length
pb.adjust_printlength()
assert pb.suffix == suffix_before
# shortened it by 1
assert pb.length == length_before - 1

def test_adjust_printlength_shorten_to_1(self):
pb = MgProgressbar()
_current_length = len(pb.prefix) + pb.length + pb.decimals + len(pb.suffix) + 10
# spoof terminal window stuff
pb.tw_width = _current_length - pb.length + 1
pb.could_not_get_terminal_window = False
suffix_before = pb.suffix
pb.adjust_printlength()
assert pb.suffix == suffix_before
assert pb.length == 1

def test_adjust_printlength_only_remove_suffix(self):
my_suffix = "very long testsuffix"
pb = MgProgressbar(suffix=my_suffix, length=len(my_suffix))
_current_length = len(pb.prefix) + pb.length + pb.decimals + len(pb.suffix) + 10
# spoof terminal window stuff
pb.tw_width = _current_length - pb.length
pb.could_not_get_terminal_window = False
length_before = pb.length
pb.adjust_printlength()
assert pb.suffix == ""
assert pb.length == length_before

def test_adjust_printlength_remove_suffix_and_shorten(self):
pb = MgProgressbar(suffix="testsuffix")
_current_length = len(pb.prefix) + pb.length + pb.decimals + len(pb.suffix) + 10
# spoof terminal window stuff
pb.tw_width = _current_length - pb.length - 1
pb.could_not_get_terminal_window = False
length_before = pb.length
pb.adjust_printlength()
assert pb.suffix == ""
assert pb.length < length_before

def test_adjust_printlength_only_remove_prefix_and_suffix(self):
my_suffix = "very long testsuffix"
my_prefix = "very long testprefix"
pb = MgProgressbar(suffix=my_suffix, prefix=my_prefix, length=len(my_suffix))
_current_length = len(pb.prefix) + pb.length + pb.decimals + len(pb.suffix) + 10
# spoof terminal window stuff
pb.tw_width = _current_length - pb.length - len(pb.suffix)
pb.could_not_get_terminal_window = False
length_before = pb.length
pb.adjust_printlength()
assert pb.suffix == ""
assert pb.prefix == ""
assert pb.length == length_before

def test_adjust_printlength_only_remove_prefix_and_suffix_and_shorten(self):
my_suffix = "very long testsuffix"
my_prefix = "very long testprefix"
pb = MgProgressbar(suffix=my_suffix, prefix=my_prefix, length=len(my_suffix))
_current_length = len(pb.prefix) + pb.length + pb.decimals + len(pb.suffix) + 10
# spoof terminal window stuff
pb.tw_width = _current_length - pb.length - len(pb.suffix) - 1
pb.could_not_get_terminal_window = False
length_before = pb.length
pb.adjust_printlength()
assert pb.suffix == ""
assert pb.prefix == ""
assert pb.length == length_before - 1

def test_adjust_printlength_display_only_percent(self):
my_suffix = "very long testsuffix"
my_prefix = "very long testprefix"
pb = MgProgressbar(suffix=my_suffix, prefix=my_prefix, length=len(my_suffix))
_current_length = len(pb.prefix) + pb.length + pb.decimals + len(pb.suffix) + 10
# spoof terminal window stuff
pb.tw_width = _current_length - pb.length - len(pb.suffix) - len(pb.prefix)
pb.could_not_get_terminal_window = False
assert pb.display_only_percent == False
pb.adjust_printlength()
assert pb.suffix == ""
assert pb.prefix == ""
assert pb.display_only_percent == True

def test_progress(self):
pb = MgProgressbar(total=100)
assert pb.progress(1) == None

def test_progress_only_percent(self):
pb = MgProgressbar(total=100)
# spoof terminal window stuff
pb.could_not_get_terminal_window = True
pb.display_only_percent = True
pb.tw_width = 1
assert pb.progress(1) == None
assert pb.display_only_percent == True

def test_repr(self):
pb = MgProgressbar(total=100)
assert print(pb) == None


class Test_roundup:
def test_positive(self):
Expand Down Expand Up @@ -496,3 +616,8 @@ def test_expected_unwrap(self):

def test_expected_no_unwrap(self):
assert unwrap_str("one two") == "one two"


class Test_in_colab:
def test_in_colab(self):
assert in_colab() == False

0 comments on commit 316c7be

Please sign in to comment.