Skip to content

Commit

Permalink
Add remaining unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen L. <lrq3000@gmail.com>
  • Loading branch information
lrq3000 committed Jan 31, 2016
1 parent ae858ef commit 1dff8e1
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 27 deletions.
28 changes: 14 additions & 14 deletions tqdm/_tqdm.py
Original file line number Diff line number Diff line change
Expand Up @@ -779,28 +779,28 @@ def set_description(self, desc=None):
def moveto(self, n):
self.fp.write(_unicode('\n' * n + _term_move_up() * -n))

def refresh(self):
def clear(self, nomove=False):
"""
Force refresh the display of this bar
Clear current bar display
"""
self.moveto(self.pos)
# clear up line (can't rely on sp(''))
if not nomove:
self.moveto(self.pos)
# clear up the bar (can't rely on sp(''))
self.fp.write('\r')
self.fp.write(' ' * (self.ncols if self.ncols else 10))
self.fp.write('\r')
# Print current/last bar state
self.fp.write(self.__repr__())
self.moveto(-self.pos)
self.fp.write('\r') # place cursor back at the beginning of line
if not nomove:
self.moveto(-self.pos)

def clear(self):
def refresh(self):
"""
Clear current bar display
Force refresh the display of this bar
"""
self.moveto(self.pos)
# clear up this bar
self.fp.write('\r')
self.fp.write(' ' * (self.ncols if self.ncols else 10))
self.fp.write('\r') # place cursor back at the beginning of line
# clear up this line's content (whatever there was)
self.clear(nomove=True)
# Print current/last bar state
self.fp.write(self.__repr__())
self.moveto(-self.pos)


Expand Down
66 changes: 53 additions & 13 deletions tqdm/tests/tests_tqdm.py
Original file line number Diff line number Diff line change
Expand Up @@ -1045,25 +1045,66 @@ def test_repr():
assert str(t) == ' 0%| | 0/10 [00:00<?, ?it/s]'


@with_setup(pretest, posttest)
def test_clear():
""" Test clearing bar display """
with closing(StringIO()) as our_file:
t1 = tqdm(total=10, file=our_file, desc='pos0 bar',
bar_format='{l_bar}')
t2 = trange(10, file=our_file, desc='pos1 bar',
bar_format='{l_bar}')
before = squash_ctrlchars(our_file.getvalue())
t2.clear()
t1.clear()
after = squash_ctrlchars(our_file.getvalue())
t1.close()
t2.close()
assert before == ['pos0 bar: 0%|', 'pos1 bar: 0%|']
assert after == ['', '']


@with_setup(pretest, posttest)
def test_refresh():
""" Test refresh bar display """
with closing(StringIO()) as our_file:
t1 = tqdm(total=10, file=our_file, desc='pos0 bar',
bar_format='{l_bar}', mininterval=999, miniters=999)
t2 = tqdm(total=10, file=our_file, desc='pos1 bar',
bar_format='{l_bar}', mininterval=999, miniters=999)
t1.update()
t2.update()
before = squash_ctrlchars(our_file.getvalue())
t1.refresh()
t2.refresh()
after = squash_ctrlchars(our_file.getvalue())
t1.close()
t2.close()

# Check that refreshing indeed forced the display to use realtime state
assert before == [u'pos0 bar: 0%|', u'pos1 bar: 0%|']
assert after == [u'pos0 bar: 10%|', u'pos1 bar: 10%|']


@with_setup(pretest, posttest)
def test_write():
""" Test write messages """
# Use regexp because the it and rates can change
#RE_split = re.compile(r'(((\x1b\[A|\r|\n)*((pos\d+) bar:\s+\d+%\|(#|\s){3,6})?)|^(?!\x1b\[A|\r|\n).+?(?=\x1b\[A|\r|\n))') # NOQA

s = "Hello world"
with closing(StringIO()) as our_file:
t1 = tqdm(total=10, file=our_file, desc='pos0 bar', bar_format='{l_bar}',
mininterval=0, miniters=1)
t2 = tqdm(total=10, file=our_file, desc='pos1 bar', bar_format='{l_bar}',
mininterval=0, miniters=1)
t3 = tqdm(total=10, file=our_file, desc='pos2 bar', bar_format='{l_bar}',
mininterval=0, miniters=1)
# Change format to keep only left part w/o bar and it/s rate
t1 = tqdm(total=10, file=our_file, desc='pos0 bar',
bar_format='{l_bar}', mininterval=0, miniters=1)
t2 = trange(10, file=our_file, desc='pos1 bar', bar_format='{l_bar}',
mininterval=0, miniters=1)
t3 = tqdm(total=10, file=our_file, desc='pos2 bar',
bar_format='{l_bar}', mininterval=0, miniters=1)
t1.update()
t2.update()
t3.update()
before = our_file.getvalue()
t1.write(s, file=our_file)

# Write msg and see if bars are correctly redrawn below the msg
t1.write(s, file=our_file) # call as an instance method
tqdm.write(s, file=our_file) # call as a class method
after = our_file.getvalue()

t1.close()
Expand All @@ -1072,6 +1113,5 @@ def test_write():

before_squashed = squash_ctrlchars(before)
after_squashed = squash_ctrlchars(after)
#res_before = [m[0] for m in RE_split.findall('\n'.join(before_squashed))]
#res_after = [m[0] for m in RE_split.findall('\n'.join(after_squashed))]
assert after_squashed == [s] + before_squashed

assert after_squashed == [s, s] + before_squashed

0 comments on commit 1dff8e1

Please sign in to comment.