def test_right_sequence_in_py3(self): red_on_blue = fmtstr('hello', 'red', 'on_blue') blue_on_red = fmtstr('there', fg='blue', bg='red') green_s = fmtstr('!', 'green') full = red_on_blue + ' ' + blue_on_red + green_s self.assertEqual(full, on_blue(red("hello"))+" "+on_red(blue("there"))+green("!")) self.assertEqual(str(full), '\x1b[31m\x1b[44mhello\x1b[49m\x1b[39m \x1b[34m\x1b[41mthere\x1b[49m\x1b[39m\x1b[32m!\x1b[39m')
def test_fmt_strings_remain_unchanged_when_used_to_construct_other_ones(self): a = fmtstr('hi', 'blue') b = fmtstr('there', 'red') c = a + b green(c) self.assertEqual(a.shared_atts['fg'], FG_COLORS['blue']) self.assertEqual(b.shared_atts['fg'], FG_COLORS['red'])
def test_fmt_strings_remain_unchanged_when_used_to_construct_other_ones( self): a = fmtstr("hi", "blue") b = fmtstr("there", "red") c = a + b green(c) self.assertEqual(a.shared_atts["fg"], FG_COLORS["blue"]) self.assertEqual(b.shared_atts["fg"], FG_COLORS["red"])
def test_funny_chars(self): fmtstr('⁇', 'blue') fmtstr(u'⁇', 'blue') fmtstr(u'⁇', 'blue') str(fmtstr('⁇', 'blue')) str(fmtstr(u'⁇', 'blue')) unicode(fmtstr('⁇', 'blue')) unicode(fmtstr(u'⁇', 'blue')) self.assertTrue(True)
def test_multiple_bfs_splice(self): self.assertEqual(fmtstr('a') + blue('b'), on_blue(' '*2).splice(fmtstr('a')+blue('b'), 0, 2)) self.assertEqual(on_red('yo') + on_blue(' '), on_blue(' '*5).splice(on_red('yo'), 0, 2)) self.assertEqual(' ' + on_red('yo') + on_blue(' '), on_blue(' '*6).splice(' ' + on_red('yo'), 0, 3)) self.assertEqual(on_blue("hey") + ' ' + on_red('yo') + on_blue(' '), on_blue(' '*9).splice(on_blue("hey") + ' ' + on_red('yo'), 0, 6)) self.assertEqual(on_blue(' '*5) + on_blue("hey") + ' ' + on_red('yo') + on_blue(' '), on_blue(' '*14).splice(on_blue("hey") + ' ' + on_red('yo'), 5, 11))
def test_simple_escapes(self): self.assertEqual(str(fmtstr('\x1b[33mhello\x1b[0m')), '\x1b[33mhello\x1b[39m') self.assertEqual(str(fmtstr('\x1b[33mhello\x1b[39m')), '\x1b[33mhello\x1b[39m') self.assertEqual(str(fmtstr('\x1b[33mhello')), '\x1b[33mhello\x1b[39m') self.assertEqual(str(fmtstr('\x1b[43mhello\x1b[49m')), '\x1b[43mhello\x1b[49m') self.assertEqual(str(fmtstr('\x1b[43mhello\x1b[0m')), '\x1b[43mhello\x1b[49m') self.assertEqual(str(fmtstr('\x1b[43mhello')), '\x1b[43mhello\x1b[49m') self.assertEqual(str(fmtstr('\x1b[32;1mhello')), '\x1b[32m\x1b[1mhello\x1b[0m\x1b[39m') self.assertEqual(str(fmtstr('\x1b[2mhello')), 'hello') self.assertEqual(str(fmtstr('\x1b[32;2mhello')), '\x1b[32mhello\x1b[39m') self.assertEqual(str(fmtstr('\x1b[33m\x1b[43mhello\x1b[0m')), '\x1b[33m\x1b[43mhello\x1b[49m\x1b[39m')
def test_right_sequence_in_py3(self): red_on_blue = fmtstr("hello", "red", "on_blue") blue_on_red = fmtstr("there", fg="blue", bg="red") green_s = fmtstr("!", "green") full = red_on_blue + " " + blue_on_red + green_s self.assertEqual( full, on_blue(red("hello")) + " " + on_red(blue("there")) + green("!")) self.assertEqual( str(full), "\x1b[31m\x1b[44mhello\x1b[49m\x1b[39m \x1b[34m\x1b[41mthere\x1b[49m\x1b[39m\x1b[32m!\x1b[39m", )
def test_width_at_offset(self): self.assertEqual(fmtstr('abEcdef').width_at_offset(0), 0) self.assertEqual(fmtstr('abEcdef').width_at_offset(2), 2) self.assertEqual(fmtstr('abEcdef').width_at_offset(3), 4) self.assertEqual(fmtstr('a\u0300b\u3000c').width_at_offset(0), 0) self.assertEqual(fmtstr('a\u0300b\u3000c').width_at_offset(1), 1) self.assertEqual(fmtstr('a\u0300b\u3000c').width_at_offset(2), 1) self.assertEqual(fmtstr('a\u0300b\u3000c').width_at_offset(3), 2) self.assertEqual(len(fmtstr('a\u0300')), 2) self.assertEqual(fmtstr('a\u0300').width, 1)
def test_normal_chars(self): fmtstr('a', 'blue') fmtstr(u'a', 'blue') str(fmtstr('a', 'blue')) str(fmtstr(u'a', 'blue')) unicode(fmtstr('a', 'blue')) unicode(fmtstr(u'a', 'blue')) self.assertTrue(True)
def test_len_of_unicode_in_fsarray(self): fsa = FSArray(3, 2) fsa.rows[0] = fsa.rows[0].setslice_with_length(0, 2, '┌─', 2) self.assertEqual(fsa.shape, (3, 2)) fsa.rows[0] = fsa.rows[0].setslice_with_length(0, 2, fmtstr('┌─', 'blue'), 2) self.assertEqual(fsa.shape, (3, 2))
def test_copy_with_new_str(self): # Change string but not attributes a = fmtstr("hello", "blue") b = a.copy_with_new_str("bye") self.assertEqual(a.s, "hello") self.assertEqual(b.s, "bye") self.assertEqual(a.chunks[0].atts, b.chunks[0].atts)
def test_splice_fmtstr_with_end_without_atts(self): a = fmtstr('notion') b = a.splice('te', 2, 6) self.assertEqual(a.s, "notion") self.assertEqual(b.s, "note") self.assertEqual(len(b.basefmtstrs), 2)
def test_copy_with_new_str(self): # Change string but not attributes a = fmtstr('hello', 'blue') b = a.copy_with_new_str('bye') self.assertEqual(a.s, 'hello') self.assertEqual(b.s, 'bye') self.assertEqual(a.basefmtstrs[0].atts, b.basefmtstrs[0].atts)
def test_splice_fmtstr_with_end_without_atts(self): a = fmtstr('notion') b = a.splice('te', 2, 6) self.assertEqual(a.s, "notion") self.assertEqual(b.s, "note") self.assertEqual(len(b.chunks), 2)
def test_copy_with_new_str(self): # Change string but not attributes a = fmtstr('hello', 'blue') b = a.copy_with_new_str('bye') self.assertEqual(a.s, 'hello') self.assertEqual(b.s, 'bye') self.assertEqual(a.chunks[0].atts, b.chunks[0].atts)
def test_len_of_unicode(self): self.assertEqual(len(fmtstr('┌─')), 2) lines = ['┌─', 'an', '┌─'] r = fsarray(lines) self.assertEqual(r.shape, (3, 2)) self.assertEqual(len(fmtstr(fmtstr('┌─'))), len(fmtstr('┌─'))) self.assertEqual(fmtstr(fmtstr('┌─')), fmtstr('┌─'))
def test_len_of_unicode(self): self.assertEqual(len(fmtstr("┌─")), 2) lines = ["┌─", "an", "┌─"] r = fsarray(lines) self.assertEqual(r.shape, (3, 2)) self.assertEqual(len(fmtstr(fmtstr("┌─"))), len(fmtstr("┌─"))) self.assertEqual(fmtstr(fmtstr("┌─")), fmtstr("┌─"))
def test_splice_with_multiple_basefmtstrs(self): a = fmtstr('notion') b = a.splice('te', 2, 6) c = b.splice('de', 0) self.assertEqual(a.s, "notion") self.assertEqual(b.s, "note") self.assertEqual(c.s, "denote") self.assertEqual(len(c.basefmtstrs), 3)
def test_splice_with_multiple_chunks(self): a = fmtstr('notion') b = a.splice('te', 2, 6) c = b.splice('de', 0) self.assertEqual(a.s, "notion") self.assertEqual(b.s, "note") self.assertEqual(c.s, "denote") self.assertEqual(len(c.chunks), 3)
def test_simple_escapes(self): self.assertEqual(str(fmtstr("\x1b[33mhello\x1b[0m")), "\x1b[33mhello\x1b[39m") self.assertEqual(str(fmtstr("\x1b[33mhello\x1b[39m")), "\x1b[33mhello\x1b[39m") self.assertEqual(str(fmtstr("\x1b[33mhello")), "\x1b[33mhello\x1b[39m") self.assertEqual(str(fmtstr("\x1b[43mhello\x1b[49m")), "\x1b[43mhello\x1b[49m") self.assertEqual(str(fmtstr("\x1b[43mhello\x1b[0m")), "\x1b[43mhello\x1b[49m") self.assertEqual(str(fmtstr("\x1b[43mhello")), "\x1b[43mhello\x1b[49m") self.assertEqual(str(fmtstr("\x1b[32;1mhello")), "\x1b[32m\x1b[1mhello\x1b[0m\x1b[39m") self.assertEqual(str(fmtstr("\x1b[2mhello")), "\x1b[2mhello\x1b[0m") self.assertEqual(str(fmtstr("\x1b[32;2mhello")), "\x1b[32m\x1b[2mhello\x1b[0m\x1b[39m") self.assertEqual( str(fmtstr("\x1b[33m\x1b[43mhello\x1b[0m")), "\x1b[33m\x1b[43mhello\x1b[49m\x1b[39m", )
def test_split(self): self.assertEqual(blue('hello there').split(' '), [blue('hello'), blue('there')]) s = blue('hello there') self.assertEqual(s.split(' '), [s[:5], s[6:]]) # split shouldn't create fmtstrs without basefmtstrs self.assertEqual(fmtstr('a').split('a')[0].basefmtstrs, fmtstr('').basefmtstrs) self.assertEqual(fmtstr('a').split('a')[1].basefmtstrs, fmtstr('').basefmtstrs) self.assertEqual((fmtstr('imp') + ' ').split('i'), [fmtstr(''), fmtstr('mp') + ' ']) self.assertEqual(blue('abcbd').split('b'), [blue('a'), blue('c'), blue('d')])
def test_multiple_bfs_splice(self): self.assertEqual( fmtstr("a") + blue("b"), on_blue(" " * 2).splice(fmtstr("a") + blue("b"), 0, 2), ) self.assertEqual( on_red("yo") + on_blue(" "), on_blue(" " * 5).splice(on_red("yo"), 0, 2)) self.assertEqual( " " + on_red("yo") + on_blue(" "), on_blue(" " * 6).splice(" " + on_red("yo"), 0, 3), ) self.assertEqual( on_blue("hey") + " " + on_red("yo") + on_blue(" "), on_blue(" " * 9).splice(on_blue("hey") + " " + on_red("yo"), 0, 6), ) self.assertEqual( on_blue(" " * 5) + on_blue("hey") + " " + on_red("yo") + on_blue(" "), on_blue(" " * 14).splice( on_blue("hey") + " " + on_red("yo"), 5, 11), )
def test_splice_fmtstr_with_end_with_atts(self): # Need to test with fmtstr consisting of multiple chunks # and with attributes a = fmtstr('notion', 'blue') b = a.splice('te', 2, 6) self.assertEqual(a.s, "notion") self.assertEqual(a.chunks[0].atts, {'fg': 34}) self.assertEqual(len(a.chunks), 1) self.assertEqual(b.s, 'note') self.assertEqual(b.chunks[0].atts, {'fg': 34}) self.assertEqual(b.chunks[1].atts, {}) self.assertEqual(len(b.chunks), 2)
def test_splice_fmtstr_with_end_with_atts(self): # Need to test with fmtstr consisting of multiple chunks # and with attributes a = fmtstr("notion", "blue") b = a.splice("te", 2, 6) self.assertEqual(a.s, "notion") self.assertEqual(a.chunks[0].atts, {"fg": 34}) self.assertEqual(len(a.chunks), 1) self.assertEqual(b.s, "note") self.assertEqual(b.chunks[0].atts, {"fg": 34}) self.assertEqual(b.chunks[1].atts, {}) self.assertEqual(len(b.chunks), 2)
def test_splice_fmtstr_with_end_with_atts(self): # Need to test with fmtstr consisting of multiple basefmtstrs # and with attributes a = fmtstr('notion', 'blue') b = a.splice('te', 2, 6) self.assertEqual(a.s, "notion") self.assertEqual(a.basefmtstrs[0].atts, {'fg': 34}) self.assertEqual(len(a.basefmtstrs), 1) self.assertEqual(b.s, 'note') self.assertEqual(b.basefmtstrs[0].atts, {'fg': 34}) self.assertEqual(b.basefmtstrs[1].atts, {}) self.assertEqual(len(b.basefmtstrs), 2)
def test_width_aware_slice(self): self.assertEqual( fmtstr('E').width_aware_slice(slice(None, 1, None)).s, ' ') self.assertEqual( fmtstr('E').width_aware_slice(slice(None, 2, None)).s, 'E') self.assertEqual( fmtstr('HE!', 'blue').width_aware_slice(slice(1, 2, None)), fmtstr(' ', 'blue')) self.assertEqual( fmtstr('HE!', 'blue').width_aware_slice(slice(1, 3, None)), fmtstr('E', 'blue'))
def test_split(self): self.assertEqual( blue("hello there").split(" "), [blue("hello"), blue("there")]) s = blue("hello there") self.assertEqual(s.split(" "), [s[:5], s[6:]]) # split shouldn't create fmtstrs without chunks self.assertEqual(fmtstr("a").split("a")[0].chunks, fmtstr("").chunks) self.assertEqual(fmtstr("a").split("a")[1].chunks, fmtstr("").chunks) self.assertEqual((fmtstr("imp") + " ").split("i"), [fmtstr(""), fmtstr("mp") + " "]) self.assertEqual( blue("abcbd").split("b"), [blue("a"), blue("c"), blue("d")])
def test_width_aware_slice(self): self.assertEqual( fmtstr("E").width_aware_slice(slice(None, 1, None)).s, " ") self.assertEqual( fmtstr("E").width_aware_slice(slice(None, 2, None)).s, "E") self.assertEqual( fmtstr("HE!", "blue").width_aware_slice(slice(1, 2, None)), fmtstr(" ", "blue"), ) self.assertEqual( fmtstr("HE!", "blue").width_aware_slice(slice(1, 3, None)), fmtstr("E", "blue"), )
def test_ljust_rjust(self): """""" b = fmtstr(u'ab', 'blue', 'on_red', 'bold') g = fmtstr(u'cd', 'green', 'on_red', 'bold') s = b + g self.assertEqual(s.ljust(6), b + g + on_red(' ')) self.assertEqual(s.rjust(6), on_red(' ') + b + g) # doesn't add empties to end self.assertEqual(s.ljust(4), b + g) self.assertEqual(s.rjust(4), b + g) # behavior if background different s = on_blue('a') + on_green('b') self.assertEqual(s.ljust(3), fmtstr('ab ')) self.assertEqual(s.rjust(3), fmtstr(' ab')) s = blue(on_blue('a')) + green(on_green('b')) self.assertEqual(s.ljust(3), blue('a') + green('b') + fmtstr(' ')) self.assertEqual(s.rjust(3), fmtstr(' ') + blue('a') + green('b')) #using fillchar self.assertEqual(s.ljust(3, '*'), fmtstr('ab*')) self.assertEqual(s.rjust(3, '*'), fmtstr('*ab'))
def test_ljust_rjust(self): """""" b = fmtstr("ab", "blue", "on_red", "bold") g = fmtstr("cd", "green", "on_red", "bold") s = b + g self.assertEqual(s.ljust(6), b + g + on_red(" ")) self.assertEqual(s.rjust(6), on_red(" ") + b + g) # doesn't add empties to end self.assertEqual(s.ljust(4), b + g) self.assertEqual(s.rjust(4), b + g) # behavior if background different s = on_blue("a") + on_green("b") self.assertEqual(s.ljust(3), fmtstr("ab ")) self.assertEqual(s.rjust(3), fmtstr(" ab")) s = blue(on_blue("a")) + green(on_green("b")) self.assertEqual(s.ljust(3), blue("a") + green("b") + fmtstr(" ")) self.assertEqual(s.rjust(3), fmtstr(" ") + blue("a") + green("b")) # using fillchar self.assertEqual(s.ljust(3, "*"), fmtstr("ab*")) self.assertEqual(s.rjust(3, "*"), fmtstr("*ab"))
def fs_from_match(d): atts = {} if d['fg']: # this isn't according to spec as I understand it if d['fg'].isupper(): d['bold'] = True #TODO figure out why boldness isn't based on presence of \x02 color = cnames[d['fg'].lower()] if color != 'default': atts['fg'] = FG_COLORS[color] if d['bg']: if d['bg'] == 'I': color = colors[(colors.index(color) + (len(colors) // 2)) % len(colors)] # hack for finding the "inverse" else: color = cnames[d['bg'].lower()] if color != 'default': atts['bg'] = BG_COLORS[color] if d['bold']: atts['bold'] = True return fmtstr(d['string'], **atts)
def fs_from_match(d): atts = {} if d["fg"]: # this isn't according to spec as I understand it if d["fg"].isupper(): d["bold"] = True # TODO figure out why boldness isn't based on presence of \x02 color = CNAMES[d["fg"].lower()] if color != "default": atts["fg"] = FG_COLORS[color] if d["bg"]: if d["bg"] == "I": # hack for finding the "inverse" color = INVERSE_COLORS[color] else: color = CNAMES[d["bg"].lower()] if color != "default": atts["bg"] = BG_COLORS[color] if d["bold"]: atts["bold"] = True return fmtstr(d["string"], **atts)
def fs_from_match(d): atts = {} if d['fg']: # this isn't according to spec as I understand it if d['fg'].isupper(): d['bold'] = True # TODO figure out why boldness isn't based on presence of \x02 color = cnames[d['fg'].lower()] if color != 'default': atts['fg'] = FG_COLORS[color] if d['bg']: if d['bg'] == 'I': # hack for finding the "inverse" color = colors[(colors.index(color) + (len(colors) // 2)) % len(colors)] else: color = cnames[d['bg'].lower()] if color != 'default': atts['bg'] = BG_COLORS[color] if d['bold']: atts['bold'] = True return fmtstr(d['string'], **atts)
def test_width_aware_slice(self): self.assertEqual(fmtstr('E').width_aware_slice(slice(None, 1, None)).s, ' ') self.assertEqual(fmtstr('E').width_aware_slice(slice(None, 2, None)).s, 'E') self.assertEqual(fmtstr('HE!', 'blue').width_aware_slice(slice(1, 2, None)), fmtstr(' ', 'blue')) self.assertEqual(fmtstr('HE!', 'blue').width_aware_slice(slice(1, 3, None)), fmtstr('E', 'blue'))
def test_multi_width(self): self.assertEqual(len(fmtstr('a\u0300')), 2) self.assertEqual(fmtstr('a\u0300').width, 1)
def test_doublewide_width(self): self.assertEqual(len(fmtstr('E', 'blue')), 1) self.assertEqual(fmtstr('E', 'blue').width, 2) self.assertEqual(len(fmtstr('hi')), 2) self.assertEqual(fmtstr('hi').width, 4)
def test_unicode_repr(self): repr(Chunk('–')) self.assertEqual(repr(fmtstr('–')), repr_without_leading_u('–'))
def test_noncurtsies_output(self): fmtstr('\x1b[35mx\x1b[m') self.assertEqual(fmtstr('\x1b[Ahello'), 'hello') self.assertEqual(fmtstr('\x1b[20Ahello'), 'hello') self.assertEqual(fmtstr('\x1b[20mhello'), 'hello')
def test_simple_beginning_splice(self): self.assertEqual(fmtstr('abc').splice('d', 0), fmtstr('dabc')) self.assertEqual(fmtstr('abc').splice('d', 0), 'd'+fmtstr('abc'))
def test_funny_chars(self): fmtstr('⁇', 'blue') str(Chunk('⁇', {'fg': 'blue'})) str(fmtstr('⁇', 'blue')) unicode(fmtstr('⁇', 'blue')) self.assertTrue(True)
def test_output_type(self): self.assertEqual(type(str(fmtstr('hello', 'blue'))), str) self.assertEqual(type(unicode(fmtstr('hello', 'blue'))), unicode)
def test_simple_composition(self): a = fmtstr('hello ', 'underline', 'on_blue') b = fmtstr('there', 'red', 'on_blue') c = a + b fmtstr(c, bg='red') self.assertTrue(True)
def test_slice(self): self.assertEqual(fmtstr('Hi!', 'blue')[1:2], fmtstr('i', 'blue')) self.assertEqual(fmtstr('Hi!', 'blue')[1:], fmtstr('i!', 'blue')) s = fmtstr('imp') + ' ' self.assertEqual(s[1:], fmtstr('mp')+' ') self.assertEqual(blue('a\nb')[0:1], blue('a'))
def test_immutibility_of_FmtStr(self): a = fmtstr('hi', 'blue') b = green(a) self.assertEqual(a.shared_atts['fg'], FG_COLORS['blue']) self.assertEqual(b.shared_atts['fg'], FG_COLORS['green'])
def test_add_unicode_to_byte(self): fmtstr('┌') + fmtstr('a') fmtstr('a') + fmtstr('┌') '┌' + fmtstr('┌') '┌' + fmtstr('a') fmtstr('┌') + '┌' fmtstr('a') + '┌'
def test_splice_of_empty_fmtstr(self): self.assertEqual(fmtstr('ab').splice('', 1), fmtstr('ab'))
def test_unicode_slicing(self): self.assertEqual(fmtstr('┌adfs', 'blue')[:2], fmtstr('┌a', 'blue')) self.assertEqual(type(fmtstr('┌adfs', 'blue')[:2].s), type(fmtstr('┌a', 'blue').s)) self.assertEqual(len(fmtstr('┌adfs', 'blue')[:2]), 2)
def test_out_of_order(self): self.assertEqual(str(fmtstr('\x1b[33m\x1b[43mhello\x1b[39m\x1b[49m')), '\x1b[33m\x1b[43mhello\x1b[49m\x1b[39m')
def test_index(self): self.assertEqual(fmtstr('Hi!', 'blue')[0], fmtstr('H', 'blue')) self.assertRaises(IndexError, fmtstr('Hi!', 'blue').__getitem__, 5)