Skip to content

Commit

Permalink
Partial unit test
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 27c5068 commit ae858ef
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 2 deletions.
6 changes: 4 additions & 2 deletions tqdm/_tqdm.py
Original file line number Diff line number Diff line change
Expand Up @@ -784,8 +784,9 @@ def refresh(self):
Force refresh the display of this bar
"""
self.moveto(self.pos)
# clear up line (can't rely on sp(''))
self.fp.write('\r')
self.fp.write(' ' * self.ncols) # clear up line (can't rely on sp(''))
self.fp.write(' ' * (self.ncols if self.ncols else 10))
self.fp.write('\r')
# Print current/last bar state
self.fp.write(self.__repr__())
Expand All @@ -796,8 +797,9 @@ def clear(self):
Clear current bar display
"""
self.moveto(self.pos)
# clear up this bar
self.fp.write('\r')
self.fp.write(' ' * self.ncols) # clear up this bar
self.fp.write(' ' * (self.ncols if self.ncols else 10))
self.fp.write('\r') # place cursor back at the beginning of line
self.moveto(-self.pos)

Expand Down
77 changes: 77 additions & 0 deletions tqdm/tests/tests_tqdm.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,51 @@ def progressbar_rate(bar_str):
return float(RE_rate.search(bar_str).group(1))


def squash_ctrlchars(s):
""" Apply control characters in a string just like a terminal display """
# List of supported control codes
ctrlcodes = [r'\r', r'\n', r'\x1b\[A']

# Init variables
curline = 0 # current line in our fake terminal
lines = [''] # state of our fake terminal

# Split input string by control codes
RE_ctrl = re.compile("(%s)" % ("|".join(ctrlcodes)), flags=re.DOTALL)
s_split = RE_ctrl.split(s)
s_split = filter(None, s_split) # filter out empty splits

# For each control character or message
for nextctrl in s_split:
# If it's a control character, apply it
if nextctrl == '\r':
# Carriage return
# Go to the beginning of the line
# simplified here: we just empty the string
lines[curline] = ''
elif nextctrl == '\n':
# Newline
# Go to the next line
if curline < (len(lines) - 1):
# If already exists, just move cursor
curline += 1
else:
# Else the new line is created
lines.append('')
curline += 1
elif nextctrl == '\x1b[A':
# Move cursor up
if curline > 0:
curline -= 1
else:
raise ValueError("Cannot go up, anymore!")
# Else, it is a message, we print it on current line
else:
lines[curline] += nextctrl

return lines


def test_format_interval():
""" Test time interval format """
format_interval = tqdm.format_interval
Expand Down Expand Up @@ -998,3 +1043,35 @@ def test_repr():
with closing(StringIO()) as our_file:
with tqdm(total=10, ascii=True, file=our_file) as t:
assert str(t) == ' 0%| | 0/10 [00:00<?, ?it/s]'


@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)
t1.update()
t2.update()
t3.update()
before = our_file.getvalue()
t1.write(s, file=our_file)
after = our_file.getvalue()

t1.close()
t2.close()
t3.close()

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

0 comments on commit ae858ef

Please sign in to comment.