📄 test_urwid.py
字号:
result_right = [ [(3, None), (0, 0)], [(3, 1, 4), (0, 4)], [(3, 5, 8)], [(3, 8, 11), (0, 11)]] result_center = [ [(2, None), (0, 0)], [(3, 1, 4), (0, 4)], [(3, 5, 8)], [(3, 8, 11), (0, 11)]]class CalcTranslateWordTest5(CalcTranslateTest): text = ' Word.' width = 3 mode = 'space' result_left = [[(3, 0, 3)], [(3, 3, 6), (0, 6)]] result_right = [[(3, 0, 3)], [(3, 3, 6), (0, 6)]] result_center = [[(3, 0, 3)], [(3, 3, 6), (0, 6)]]class CalcTranslateClipTest(CalcTranslateTest): text = "It's out of control!\nYou've got to\n\nturn it off!!!" mode = 'clip' width = 14 result_left = [ [(20, 0, 20), (0, 20)], [(13, 21, 34), (0, 34)], [(0, 35)], [(14, 36, 50), (0, 50)]] result_right = [ [(-6, None), (20, 0, 20), (0, 20)], [(1, None), (13, 21, 34), (0, 34)], [(14, None), (0, 35)], [(14, 36, 50), (0, 50)]] result_center = [ [(-3, None), (20, 0, 20), (0, 20)], [(1, None), (13, 21, 34), (0, 34)], [(7, None), (0, 35)], [(14, 36, 50), (0, 50)]] class CalcPosTest(unittest.TestCase): def setUp(self): self.text = "A" * 27 self.trans = [ [(2,None),(7,0,7),(0,7)], [(13,8,21),(0,21)], [(3,None),(5,22,27),(0,27)]] self.mytests = [(1,0, 0), (2,0, 0), (11,0, 7), (-3,1, 8), (-2,1, 8), (1,1, 9), (31,1, 21), (1,2, 22), (11,2, 27) ] def tests(self): for x,y, expected in self.mytests: got = urwid.calc_pos( self.text, self.trans, x, y ) assert got == expected, `x,y`+" got: "+`got`+" expected:"+`expected`class Pos2CoordsTest(unittest.TestCase): pos_list = [5, 9, 20, 26] text = "1234567890" * 3 mytests = [ ( [[(15,0,15)], [(15,15,30),(0,30)]], [(5,0),(9,0),(5,1),(11,1)] ), ( [[(9,0,9)], [(12,9,21)], [(9,21,30),(0,30)]], [(5,0),(0,1),(11,1),(5,2)] ), ( [[(2,None), (15,0,15)], [(2,None), (15,15,30),(0,30)]], [(7,0),(11,0),(7,1),(13,1)] ), ( [[(3, 6, 9),(0,9)], [(5, 20, 25),(0,25)]], [(0,0),(3,0),(0,1),(5,1)] ), ( [[(10, 0, 10),(0,10)]], [(5,0),(9,0),(10,0),(10,0)] ), ] def test(self): for t, answer in self.mytests: for pos,a in zip(self.pos_list,answer) : r = urwid.calc_coords( self.text, t, pos) assert r==a, "%s got: %s expected: %s"%( `t`,`r`,`a`)class CanvasCacheTest(unittest.TestCase): def setUp(self): # purge the cache urwid.CanvasCache._widgets.clear() def cct(self, widget, size, focus, expected): got = urwid.CanvasCache.fetch(widget, urwid.Widget, size, focus) assert expected==got, "got: %s expected: %s"%(got, expected) def test1(self): a = urwid.Text("") b = urwid.Text("") blah = urwid.TextCanvas() blah.finalize(a, (10,1), False) blah2 = urwid.TextCanvas() blah2.finalize(a, (15,1), False) bloo = urwid.TextCanvas() bloo.finalize(b, (20,2), True) urwid.CanvasCache.store(urwid.Widget, blah) urwid.CanvasCache.store(urwid.Widget, blah2) urwid.CanvasCache.store(urwid.Widget, bloo) self.cct(a, (10,1), False, blah) self.cct(a, (15,1), False, blah2) self.cct(a, (15,1), True, None) self.cct(a, (10,2), False, None) self.cct(b, (20,2), True, bloo) self.cct(b, (21,2), True, None) urwid.CanvasCache.invalidate(a) self.cct(a, (10,1), False, None) self.cct(a, (15,1), False, None) self.cct(b, (20,2), True, bloo)class CanvasTest(unittest.TestCase): def ct(self, text, attr, exp_content): c = urwid.TextCanvas(text, attr) content = list(c.content()) assert content == exp_content, "got: "+`content`+" expected: "+`exp_content` def ct2(self, text, attr, left, top, cols, rows, def_attr, exp_content): c = urwid.TextCanvas(text, attr) content = list(c.content(left, top, cols, rows, def_attr)) assert content == exp_content, "got: "+`content`+" expected: "+`exp_content` def test1(self): self.ct(["Hello world"], None, [[(None, None, "Hello world")]]) self.ct(["Hello world"], [[("a",5)]], [[("a", None, "Hello"), (None, None," world")]]) self.ct(["Hi","There"], None, [[(None, None, "Hi ")], [(None, None,"There")]]) def test2(self): self.ct2(["Hello"], None, 0, 0, 5, 1, None, [[(None, None, "Hello")]]) self.ct2(["Hello"], None, 1, 0, 4, 1, None, [[(None, None, "ello")]]) self.ct2(["Hello"], None, 0, 0, 4, 1, None, [[(None, None, "Hell")]]) self.ct2(["Hi","There"], None, 1, 0, 3, 2, None, [[(None, None, "i ")], [(None, None,"her")]]) self.ct2(["Hi","There"], None, 0, 0, 5, 1, None, [[(None, None, "Hi ")]]) self.ct2(["Hi","There"], None, 0, 1, 5, 1, None, [[(None, None, "There")]])class ShardBodyTest(unittest.TestCase): def sbt(self, shards, shard_tail, expected): result = urwid.shard_body(shards, shard_tail, False) assert result == expected, "got: "+`result`+" expected: "+`expected` def sbttail(self, num_rows, sbody, expected): result = urwid.shard_body_tail(num_rows, sbody) assert result == expected, "got: "+`result`+" expected: "+`expected` def sbtrow(self, sbody, expected): result = list(urwid.shard_body_row(sbody)) assert result == expected, "got: "+`result`+" expected: "+`expected` def test1(self): cviews = [(0,0,10,5,None,"foo"),(0,0,5,5,None,"bar")] self.sbt(cviews, [], [(0, None, (0,0,10,5,None,"foo")), (0, None, (0,0,5,5,None,"bar"))]) self.sbt(cviews, [(0, 3, None, (0,0,5,8,None,"baz"))], [(3, None, (0,0,5,8,None,"baz")), (0, None, (0,0,10,5,None,"foo")), (0, None, (0,0,5,5,None,"bar"))]) self.sbt(cviews, [(10, 3, None, (0,0,5,8,None,"baz"))], [(0, None, (0,0,10,5,None,"foo")), (3, None, (0,0,5,8,None,"baz")), (0, None, (0,0,5,5,None,"bar"))]) self.sbt(cviews, [(15, 3, None, (0,0,5,8,None,"baz"))], [(0, None, (0,0,10,5,None,"foo")), (0, None, (0,0,5,5,None,"bar")), (3, None, (0,0,5,8,None,"baz"))]) def test2(self): sbody = [(0, None, (0,0,10,5,None,"foo")), (0, None, (0,0,5,5,None,"bar")), (3, None, (0,0,5,8,None,"baz"))] self.sbttail(5, sbody, []) self.sbttail(3, sbody, [(0, 3, None, (0,0,10,5,None,"foo")), (0, 3, None, (0,0,5,5,None,"bar")), (0, 6, None, (0,0,5,8,None,"baz"))]) sbody = [(0, None, (0,0,10,3,None,"foo")), (0, None, (0,0,5,5,None,"bar")), (3, None, (0,0,5,9,None,"baz"))] self.sbttail(3, sbody, [(10, 3, None, (0,0,5,5,None,"bar")), (0, 6, None, (0,0,5,9,None,"baz"))]) def test3(self): self.sbtrow([(0, None, (0,0,10,5,None,"foo")), (0, None, (0,0,5,5,None,"bar")), (3, None, (0,0,5,8,None,"baz"))], [20]) self.sbtrow([(0, iter("foo"), (0,0,10,5,None,"foo")), (0, iter("bar"), (0,0,5,5,None,"bar")), (3, iter("zzz"), (0,0,5,8,None,"baz"))], ["f","b","z"]) class ShardsTrimTest(unittest.TestCase): def sttop(self, shards, top, expected): result = urwid.shards_trim_top(shards, top) assert result == expected, "got: "+`result`+" expected: "+`expected` def strows(self, shards, rows, expected): result = urwid.shards_trim_rows(shards, rows) assert result == expected, "got: "+`result`+" expected: "+`expected` def stsides(self, shards, left, cols, expected): result = urwid.shards_trim_sides(shards, left, cols) assert result == expected, "got: "+`result`+" expected: "+`expected` def test1(self): shards = [(5, [(0,0,10,5,None,"foo"),(0,0,5,5,None,"bar")])] self.sttop(shards, 2, [(3, [(0,2,10,3,None,"foo"),(0,2,5,3,None,"bar")])]) self.strows(shards, 2, [(2, [(0,0,10,2,None,"foo"),(0,0,5,2,None,"bar")])]) shards = [(5, [(0,0,10,5,None,"foo")]),(3,[(0,0,10,3,None,"bar")])] self.sttop(shards, 2, [(3, [(0,2,10,3,None,"foo")]),(3,[(0,0,10,3,None,"bar")])]) self.sttop(shards, 5, [(3, [(0,0,10,3,None,"bar")])]) self.sttop(shards, 7, [(1, [(0,2,10,1,None,"bar")])]) self.strows(shards, 7, [(5, [(0,0,10,5,None,"foo")]),(2, [(0,0,10,2,None,"bar")])]) self.strows(shards, 5, [(5, [(0,0,10,5,None,"foo")])]) self.strows(shards, 4, [(4, [(0,0,10,4,None,"foo")])]) shards = [(5, [(0,0,10,5,None,"foo"), (0,0,5,8,None,"baz")]), (3,[(0,0,10,3,None,"bar")])] self.sttop(shards, 2, [(3, [(0,2,10,3,None,"foo"), (0,2,5,6,None,"baz")]), (3,[(0,0,10,3,None,"bar")])]) self.sttop(shards, 5, [(3, [(0,0,10,3,None,"bar"), (0,5,5,3,None,"baz")])]) self.sttop(shards, 7, [(1, [(0,2,10,1,None,"bar"), (0,7,5,1,None,"baz")])]) self.strows(shards, 7, [(5, [(0,0,10,5,None,"foo"), (0,0,5,7,None,"baz")]), (2, [(0,0,10,2,None,"bar")])]) self.strows(shards, 5, [(5, [(0,0,10,5,None,"foo"), (0,0,5,5,None,"baz")])]) self.strows(shards, 4, [(4, [(0,0,10,4,None,"foo"), (0,0,5,4,None,"baz")])]) def test2(self): shards = [(5, [(0,0,10,5,None,"foo"),(0,0,5,5,None,"bar")])] self.stsides(shards, 0, 15, [(5, [(0,0,10,5,None,"foo"),(0,0,5,5,None,"bar")])]) self.stsides(shards, 6, 9, [(5, [(6,0,4,5,None,"foo"),(0,0,5,5,None,"bar")])]) self.stsides(shards, 6, 6, [(5, [(6,0,4,5,None,"foo"),(0,0,2,5,None,"bar")])]) self.stsides(shards, 0, 10, [(5, [(0,0,10,5,None,"foo")])]) self.stsides(shards, 10, 5, [(5, [(0,0,5,5,None,"bar")])]) self.stsides(shards, 1, 7, [(5, [(1,0,7,5,None,"foo")])]) shards = [(5, [(0,0,10,5,None,"foo"), (0,0,5,8,None,"baz")]), (3,[(0,0,10,3,None,"bar")])] self.stsides(shards, 0, 15, [(5, [(0,0,10,5,None,"foo"), (0,0,5,8,None,"baz")]), (3,[(0,0,10,3,None,"bar")])]) self.stsides(shards, 2, 13, [(5, [(2,0,8,5,None,"foo"), (0,0,5,8,None,"baz")]), (3,[(2,0,8,3,None,"bar")])]) self.stsides(shards, 2, 10, [(5, [(2,0,8,5,None,"foo"), (0,0,2,8,None,"baz")]), (3,[(2,0,8,3,None,"bar")])]) self.stsides(shards, 2, 8, [(5, [(2,0,8,5,None,"foo")]), (3,[(2,0,8,3,None,"bar")])]) self.stsides(shards, 2, 6, [(5, [(2,0,6,5,None,"foo")]), (3,[(2,0,6,3,None,"bar")])]) self.stsides(shards, 10, 5, [(8, [(0,0,5,8,None,"baz")])]) self.stsides(shards, 11, 3, [(8, [(1,0,3,8,None,"baz")])])class ShardsJoinTest(unittest.TestCase): def sjt(self, shard_lists, expected): result = urwid.shards_join(shard_lists) assert result == expected, "got: "+`result`+" expected: "+`expected` def test(self): shards1 = [(5, [(0,0,10,5,None,"foo"), (0,0,5,8,None,"baz")]), (3,[(0,0,10,3,None,"bar")])] shards2 = [(3, [(0,0,10,3,None,"aaa")]), (5,[(0,0,10,5,None,"bbb")])] shards3 = [(3, [(0,0,10,3,None,"111")]), (2,[(0,0,10,3,None,"222")]), (3,[(0,0,10,3,None,"333")])] self.sjt([shards1], shards1) self.sjt([shards1, shards2], [(3, [(0,0,10,5,None,"foo"), (0,0,5,8,None,"baz"), (0,0,10,3,None,"aaa")]), (2, [(0,0,10,5,None,"bbb")]), (3, [(0,0,10,3,None,"bar")])]) self.sjt([shards1, shards3], [(3, [(0,0,10,5,None,"foo"), (0,0,5,8,None,"baz"), (0,0,10,3,None,"111")]), (2, [(0,0,10,3,None,"222")]), (3, [(0,0,10,3,None,"bar"), (0,0,10,3,None,"333")])]) self.sjt([shards1, shards2, shards3], [(3, [(0,0,10,5,None,"foo"), (0,0,5,8,None,"baz"), (0,0,10,3,None,"aaa"), (0,0,10,3,None,"111")]), (2, [(0,0,10,5,None,"bbb"), (0,0,10,3,None,"222")]), (3, [(0,0,10,3,None,"bar"), (0,0,10,3,None,"333")])])class TagMarkupTest(unittest.TestCase): mytests = [ ("simple one", "simple one", []), (('blue',"john"), "john", [('blue',4)]), (["a ","litt","le list"], "a little list", []), (["mix",('high',[" it ",('ital',"up a")])," little"], "mix it up a little", [(None,3),('high',4),('ital',4)]), ] def test(self): for input, text, attr in self.mytests: restext,resattr = urwid.decompose_tagmarkup( input ) assert restext == text, "got: "+`restext`+" expected: "+`text` assert resattr == attr, "got: "+`resattr`+" expected: "+`attr` def test_bad_tuple(self): try: urwid.decompose_tagmarkup((1,2,3)) except urwid.TagMarkupException, e: pass else: assert 0, "should have thrown exception!" def test_bad_type(self): try: urwid.decompose_tagmarkup(5) except urwid.TagMarkupException, e: pass else: assert 0, "should have thrown exception!" class TextTest(unittest.TestCase): def setUp(self): self.t = urwid.Text("I walk the\ncity in the night") def test1_wrap(self): expected = ["I walk the","city in ","the night "] got = self.t.render((10,))._text assert got == expected, "got: "+`got`+" expected:"+`expected` def test2_left(self): self.t.set_align_mode('left') expected = ["I walk the ","city in the night "] got = self.t.render((18,))._text assert got == expected, "got: "+`got`+" expected:"+`expected` def test3_right(self): self.t.set_align_mode('right') expected = [" I walk the"," city in the night"] got = self.t.render((18,))._text assert got == expected, "got: "+`got`+" expected:"+`expected` def test4_center(self): self.t.set_align_mode('center') expected = [" I walk the "," city in the night"] got = self.t.render((18,))._text assert got == expected, "got: "+`got`+" expected:"+`expected`class EditTest(unittest.TestCase): def setUp(self): self.t1 = urwid.Edit("","blah blah") self.t2 = urwid.Edit("stuff:", "blah blah") self.t3 = urwid.Edit("junk:\n","blah blah\n\nbloo",1) def ktest(self, e, key, expected, pos, desc): got= e.keypress((12,),key) assert got == expected, desc+ ". got: "+`got`+" expected:"+`expected` assert e.edit_pos == pos, desc+ ". pos: "+`e.edit_pos`+" expected pos: "+`pos` def test1_left(self): self.t1.set_edit_pos(0) self.ktest(self.t1,'left','left',0,"left at left edge") self.ktest(self.t2,'left',None,8,"left within text") self.t3.set_edit_pos(10) self.ktest(self.t3,'left',None,9,"left after newline")
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -