📄 test_urwid.py
字号:
#!/usr/bin/python# -*- coding: utf-8 -*-## Urwid unit testing .. ok, ok, ok# Copyright (C) 2004-2007 Ian Ward## This library is free software; you can redistribute it and/or# modify it under the terms of the GNU Lesser General Public# License as published by the Free Software Foundation; either# version 2.1 of the License, or (at your option) any later version.## This library is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU# Lesser General Public License for more details.## You should have received a copy of the GNU Lesser General Public# License along with this library; if not, write to the Free Software# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA## Urwid web site: http://excess.org/urwid/from __future__ import nested_scopesimport urwidimport unittestfrom test import test_supportclass DecodeOneTest(unittest.TestCase): def gwt(self, ch, exp_ord, exp_pos): o, pos = urwid.str_util.decode_one(ch,0) assert o==exp_ord, " got:"+`o`+" expected:"+`exp_ord` assert pos==exp_pos, " got:"+`pos`+" expected:"+`exp_pos` def test1byte(self): self.gwt("ab", ord("a"), 1) self.gwt("\xc0a", ord("?"), 1) # error def test2byte(self): self.gwt("\xc2", ord("?"), 1) # error self.gwt("\xc0\x80", ord("?"), 1) # error self.gwt("\xc2\x80", 0x80, 2) self.gwt("\xdf\xbf", 0x7ff, 2) def test3byte(self): self.gwt("\xe0", ord("?"), 1) # error self.gwt("\xe0\xa0", ord("?"), 1) # error self.gwt("\xe0\x90\x80", ord("?"), 1) # error self.gwt("\xe0\xa0\x80", 0x800, 3) self.gwt("\xef\xbf\xbf", 0xffff, 3) def test4byte(self): self.gwt("\xf0", ord("?"), 1) # error self.gwt("\xf0\x90", ord("?"), 1) # error self.gwt("\xf0\x90\x80", ord("?"), 1) # error self.gwt("\xf0\x80\x80\x80", ord("?"), 1) # error self.gwt("\xf0\x90\x80\x80", 0x10000, 4) self.gwt("\xf3\xbf\xbf\xbf", 0xfffff, 4)class CalcWidthTest(unittest.TestCase): def wtest(self, desc, s, exp): result = urwid.calc_width( s, 0, len(s)) assert result==exp, desc+" got:"+`result`+" expected:"+`exp` def test1(self): urwid.set_encoding("utf-8") self.wtest("narrow", "hello", 5) self.wtest("wide char", '\xe6\x9b\xbf', 2) self.wtest("invalid", '\xe6', 1) self.wtest("zero width", '\xcc\x80', 0) self.wtest("mixed", 'hello\xe6\x9b\xbf\xe6\x9b\xbf', 9) def test2(self): urwid.set_encoding("euc-jp") self.wtest("narrow", "hello", 5) self.wtest("wide", "\xA1\xA1\xA1\xA1", 4) self.wtest("invalid", "\xA1", 1)class ConvertDecSpecialTest(unittest.TestCase): def ctest(self, desc, s, exp, expcs): urwid.set_encoding('ascii') c = urwid.Text(s).render((5,)) result = c._text[0] assert result==exp, desc+" got:"+`result`+" expected:"+`exp` resultcs = c._cs[0] assert resultcs==expcs, desc+" got:"+`resultcs`+" expected:"+`expcs` def test1(self): self.ctest("no conversion", "hello", "hello", [(None,5)]) self.ctest("only special", u"拢拢拢拢拢", "}}}}}", [("0",5)]) self.ctest("mix left", u"拢拢abc", "}}abc", [("0",2),(None,3)]) self.ctest("mix right", u"abc拢拢", "abc}}", [(None,3),("0",2)]) self.ctest("mix inner", u"a拢拢bc", "a}}bc", [(None,1),("0",2),(None,2)] ) self.ctest("mix well", u"拢a拢b拢", "}a}b}", [("0",1),(None,1),("0",1),(None,1),("0",1)] ) class WithinDoubleByteTest(unittest.TestCase): def setUp(self): urwid.set_encoding("euc-jp") def wtest(self, str, ls, pos, expected, desc): result = urwid.within_double_byte(str, ls, pos) assert result==expected, desc+" got:"+`result`+" expected:"+`expected` def test1(self): self.wtest("mnopqr",0,2,0,'simple no high bytes') self.wtest("mn\xA1\xA1qr",0,2,1,'simple 1st half') self.wtest("mn\xA1\xA1qr",0,3,2,'simple 2nd half') self.wtest("m\xA1\xA1\xA1\xA1r",0,3,1,'subsequent 1st half') self.wtest("m\xA1\xA1\xA1\xA1r",0,4,2,'subsequent 2nd half') self.wtest("mn\xA1@qr",0,3,2,'simple 2nd half lo') self.wtest("mn\xA1\xA1@r",0,4,0,'subsequent not 2nd half lo') self.wtest("m\xA1\xA1\xA1@r",0,4,2,'subsequent 2nd half lo') def test2(self): self.wtest("\xA1\xA1qr",0,0,1,'begin 1st half') self.wtest("\xA1\xA1qr",0,1,2,'begin 2nd half') self.wtest("\xA1@qr",0,1,2,'begin 2nd half lo') self.wtest("\xA1\xA1\xA1\xA1r",0,2,1,'begin subs. 1st half') self.wtest("\xA1\xA1\xA1\xA1r",0,3,2,'begin subs. 2nd half') self.wtest("\xA1\xA1\xA1@r",0,3,2,'begin subs. 2nd half lo') self.wtest("\xA1@\xA1@r",0,3,2,'begin subs. 2nd half lo lo') self.wtest("@\xA1\xA1@r",0,3,0,'begin subs. not 2nd half lo') def test3(self): self.wtest("abc \xA1\xA1qr",4,4,1,'newline 1st half') self.wtest("abc \xA1\xA1qr",4,5,2,'newline 2nd half') self.wtest("abc \xA1@qr",4,5,2,'newline 2nd half lo') self.wtest("abc \xA1\xA1\xA1\xA1r",4,6,1,'newl subs. 1st half') self.wtest("abc \xA1\xA1\xA1\xA1r",4,7,2,'newl subs. 2nd half') self.wtest("abc \xA1\xA1\xA1@r",4,7,2,'newl subs. 2nd half lo') self.wtest("abc \xA1@\xA1@r",4,7,2,'newl subs. 2nd half lo lo') self.wtest("abc @\xA1\xA1@r",4,7,0,'newl subs. not 2nd half lo')class CalcTextPosTest(unittest.TestCase): def ctptest(self, text, tests): for s,e,p, expected in tests: got = urwid.calc_text_pos( text, s, e, p ) assert got == expected, `s,e,p`+" got: "+`got`+" expected:"+`expected` def test1(self): text = "hello world out there" tests = [ (0,21,0, (0,0)), (0,21,5, (5,5)), (0,21,21, (21,21)), (0,21,50, (21,21)), (2,15,50, (15,13)), (6,21,0, (6,0)), (6,21,3, (9,3)), ] self.ctptest(text, tests) def test2_wide(self): urwid.set_encoding("euc-jp") text = "hel\xA1\xA1 world out there" tests = [ (0,21,0, (0,0)), (0,21,4, (3,3)), (2,21,2, (3,1)), (2,21,3, (5,3)), (6,21,0, (6,0)), ] self.ctptest(text, tests) def test3_utf8(self): urwid.set_encoding("utf-8") text = "hel\xc4\x83 world \xe2\x81\x81 there" tests = [ (0,21,0, (0,0)), (0,21,4, (5,4)), (2,21,1, (3,1)), (2,21,2, (5,2)), (2,21,3, (6,3)), (6,21,7, (15,7)), (6,21,8, (16,8)), ] self.ctptest(text, tests) def test4_utf8(self): urwid.set_encoding("utf-8") text = "he\xcc\x80llo \xe6\x9b\xbf world" tests = [ (0,15,0, (0,0)), (0,15,1, (1,1)), (0,15,2, (4,2)), (0,15,4, (6,4)), (8,15,0, (8,0)), (8,15,1, (8,0)), (8,15,2, (11,2)), (8,15,5, (14,5)), ] self.ctptest(text, tests) class CalcBreaksTest(unittest.TestCase): def cbtest(self, width, exp): result = urwid.default_layout.calculate_text_segments( self.text, width, self.mode ) assert len(result) == len(exp), `result, exp` for l,e in zip(result, exp): end = l[-1][-1] assert end == e, `result,exp` def test(self): for width, exp in self.do: self.cbtest( width, exp ) class CalcBreaksCharTest(CalcBreaksTest): mode = 'any' text = "abfghsdjf askhtrvs\naltjhgsdf ljahtshgf" # tests do = [ ( 100, [18,38] ), ( 6, [6, 12, 18, 25, 31, 37, 38] ), ( 10, [10, 18, 29, 38] ), ]class CalcBreaksDBCharTest(CalcBreaksTest): def setUp(self): urwid.set_encoding("euc-jp") mode = 'any' text = "abfgh\xA1\xA1j\xA1\xA1xskhtrvs\naltjhgsdf\xA1\xA1jahtshgf" # tests do = [ ( 10, [10, 18, 28, 38] ), ( 6, [5, 11, 17, 18, 25, 31, 37, 38] ), ( 100, [18, 38]), ]class CalcBreaksWordTest(CalcBreaksTest): mode = 'space' text = "hello world\nout there. blah" # tests do = [ ( 10, [5, 11, 22, 27] ), ( 5, [5, 11, 17, 22, 27] ), ( 100, [11, 27] ), ]class CalcBreaksWordTest2(CalcBreaksTest): mode = 'space' text = "A simple set of words, really...." do = [ ( 10, [8, 15, 22, 33]), ( 17, [15, 33]), ( 13, [12, 22, 33]), ]class CalcBreaksDBWordTest(CalcBreaksTest): def setUp(self): urwid.set_encoding("euc-jp") mode = 'space' text = "hel\xA1\xA1 world\nout-\xA1\xA1tre blah" # tests do = [ ( 10, [5, 11, 21, 26] ), ( 5, [5, 11, 16, 21, 26] ), ( 100, [11, 26] ), ]class CalcBreaksUTF8Test(CalcBreaksTest): def setUp(self): urwid.set_encoding("utf-8") mode = 'space' text = '\xe6\x9b\xbf\xe6\xb4\xbc\xe6\xb8\x8e\xe6\xba\x8f\xe6\xbd\xba' do = [ (4, [6, 12, 15] ), (10, [15] ), (5, [6, 12, 15] ), ]class SubsegTest(unittest.TestCase): def setUp(self): urwid.set_encoding("euc-jp") def st(self, seg, text, start, end, exp): s = urwid.LayoutSegment(seg) result = s.subseg( text, start, end ) assert result == exp, "Expected %s, got %s"%(`exp`,`result`) def test1_padding(self): self.st( (10, None), "", 0, 8, [(8, None)] ) self.st( (10, None), "", 2, 10, [(8, None)] ) self.st( (10, 0), "", 3, 7, [(4, 0)] ) self.st( (10, 0), "", 0, 20, [(10, 0)] ) def test2_text(self): self.st( (10, 0, "1234567890"), "", 0, 8, [(8,0,"12345678")] ) self.st( (10, 0, "1234567890"), "", 2, 10, [(8,0,"34567890")] ) self.st( (10, 0, "12\xA1\xA156\xA1\xA190"), "", 2, 8, [(6, 0, "\xA1\xA156\xA1\xA1")] ) self.st( (10, 0, "12\xA1\xA156\xA1\xA190"), "", 3, 8, [(5, 0, " 56\xA1\xA1")] ) self.st( (10, 0, "12\xA1\xA156\xA1\xA190"), "", 2, 7, [(5, 0, "\xA1\xA156 ")] ) self.st( (10, 0, "12\xA1\xA156\xA1\xA190"), "", 3, 7, [(4, 0, " 56 ")] ) self.st( (10, 0, "12\xA1\xA156\xA1\xA190"), "", 0, 20, [(10, 0, "12\xA1\xA156\xA1\xA190")] ) def test3_range(self): t = "1234567890" self.st( (10, 0, 10), t, 0, 8, [(8, 0, 8)] ) self.st( (10, 0, 10), t, 2, 10, [(8, 2, 10)] ) self.st( (6, 2, 8), t, 1, 6, [(5, 3, 8)] ) self.st( (6, 2, 8), t, 0, 5, [(5, 2, 7)] ) self.st( (6, 2, 8), t, 1, 5, [(4, 3, 7)] ) t = "12\xA1\xA156\xA1\xA190" self.st( (10, 0, 10), t, 0, 8, [(8, 0, 8)] ) self.st( (10, 0, 10), t, 2, 10, [(8, 2, 10)] ) self.st( (6, 2, 8), t, 1, 6, [(1, 3), (4, 4, 8)] ) self.st( (6, 2, 8), t, 0, 5, [(4, 2, 6), (1, 6)] ) self.st( (6, 2, 8), t, 1, 5, [(1, 3), (2, 4, 6), (1, 6)] ) class CalcTranslateTest(unittest.TestCase): def test1_left(self): result = urwid.default_layout.layout( self.text, self.width, 'left', self.mode) assert result == self.result_left, result def test2_right(self): result = urwid.default_layout.layout( self.text, self.width, 'right', self.mode) assert result == self.result_right, result def test3_center(self): result = urwid.default_layout.layout( self.text, self.width, 'center', self.mode) assert result == self.result_center, resultclass CalcTranslateCharTest(CalcTranslateTest): text = "It's out of control!\nYou've got to" mode = 'any' width = 15 result_left = [ [(15, 0, 15)], [(5, 15, 20), (0, 20)], [(13, 21, 34), (0, 34)]] result_right = [ [(15, 0, 15)], [(10, None), (5, 15, 20), (0,20)], [(2, None), (13, 21, 34), (0,34)]] result_center = [ [(15, 0, 15)], [(5, None), (5, 15, 20), (0,20)], [(1, None), (13, 21, 34), (0,34)]]class CalcTranslateWordTest(CalcTranslateTest): text = "It's out of control!\nYou've got to" mode = 'space' width = 14 result_left = [ [(11, 0, 11), (0, 11)], [(8, 12, 20), (0, 20)], [(13, 21, 34), (0, 34)]] result_right = [ [(3, None), (11, 0, 11), (0, 11)], [(6, None), (8, 12, 20), (0, 20)], [(1, None), (13, 21, 34), (0, 34)]] result_center = [ [(2, None), (11, 0, 11), (0, 11)], [(3, None), (8, 12, 20), (0, 20)], [(1, None), (13, 21, 34), (0, 34)]]class CalcTranslateWordTest2(CalcTranslateTest): text = "It's out of control!\nYou've got to " mode = 'space' width = 14 result_left = [ [(11, 0, 11), (0, 11)], [(8, 12, 20), (0, 20)], [(14, 21, 35), (0, 35)]] result_right = [ [(3, None), (11, 0, 11), (0, 11)], [(6, None), (8, 12, 20), (0, 20)], [(14, 21, 35), (0, 35)]] result_center = [ [(2, None), (11, 0, 11), (0, 11)], [(3, None), (8, 12, 20), (0, 20)], [(14, 21, 35), (0, 35)]]class CalcTranslateWordTest3(CalcTranslateTest): def setUp(self): urwid.set_encoding('utf-8') text = '\xe6\x9b\xbf\xe6\xb4\xbc\n\xe6\xb8\x8e\xe6\xba\x8f\xe6\xbd\xba' width = 10 mode = 'space' result_left = [ [(4, 0, 6), (0, 6)], [(6, 7, 16), (0, 16)]] result_right = [ [(6, None), (4, 0, 6), (0, 6)], [(4, None), (6, 7, 16), (0, 16)]] result_center = [ [(3, None), (4, 0, 6), (0, 6)], [(2, None), (6, 7, 16), (0, 16)]] class CalcTranslateWordTest4(CalcTranslateTest): text = ' Die Gedank' width = 3 mode = 'space' result_left = [ [(0, 0)], [(3, 1, 4), (0, 4)], [(3, 5, 8)], [(3, 8, 11), (0, 11)]]
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -