1import unittest 2 3from urwid import text_layout 4from urwid.compat import B 5import urwid 6 7 8class CalcBreaksTest(object): 9 def cbtest(self, width, exp): 10 result = text_layout.default_layout.calculate_text_segments( 11 B(self.text), width, self.mode ) 12 assert len(result) == len(exp), repr((result, exp)) 13 for l,e in zip(result, exp): 14 end = l[-1][-1] 15 assert end == e, repr((result,exp)) 16 17 def test(self): 18 for width, exp in self.do: 19 self.cbtest( width, exp ) 20 21 22class CalcBreaksCharTest(CalcBreaksTest, unittest.TestCase): 23 mode = 'any' 24 text = "abfghsdjf askhtrvs\naltjhgsdf ljahtshgf" 25 # tests 26 do = [ 27 ( 100, [18,38] ), 28 ( 6, [6, 12, 18, 25, 31, 37, 38] ), 29 ( 10, [10, 18, 29, 38] ), 30 ] 31 32 33class CalcBreaksDBCharTest(CalcBreaksTest, unittest.TestCase): 34 def setUp(self): 35 urwid.set_encoding("euc-jp") 36 37 mode = 'any' 38 text = "abfgh\xA1\xA1j\xA1\xA1xskhtrvs\naltjhgsdf\xA1\xA1jahtshgf" 39 # tests 40 do = [ 41 ( 10, [10, 18, 28, 38] ), 42 ( 6, [5, 11, 17, 18, 25, 31, 37, 38] ), 43 ( 100, [18, 38]), 44 ] 45 46 47class CalcBreaksWordTest(CalcBreaksTest, unittest.TestCase): 48 mode = 'space' 49 text = "hello world\nout there. blah" 50 # tests 51 do = [ 52 ( 10, [5, 11, 22, 27] ), 53 ( 5, [5, 11, 17, 22, 27] ), 54 ( 100, [11, 27] ), 55 ] 56 57 58class CalcBreaksWordTest2(CalcBreaksTest, unittest.TestCase): 59 mode = 'space' 60 text = "A simple set of words, really...." 61 do = [ 62 ( 10, [8, 15, 22, 33]), 63 ( 17, [15, 33]), 64 ( 13, [12, 22, 33]), 65 ] 66 67 68class CalcBreaksDBWordTest(CalcBreaksTest, unittest.TestCase): 69 def setUp(self): 70 urwid.set_encoding("euc-jp") 71 72 mode = 'space' 73 text = "hel\xA1\xA1 world\nout-\xA1\xA1tre blah" 74 # tests 75 do = [ 76 ( 10, [5, 11, 21, 26] ), 77 ( 5, [5, 11, 16, 21, 26] ), 78 ( 100, [11, 26] ), 79 ] 80 81 82class CalcBreaksUTF8Test(CalcBreaksTest, unittest.TestCase): 83 def setUp(self): 84 urwid.set_encoding("utf-8") 85 86 mode = 'space' 87 text = '\xe6\x9b\xbf\xe6\xb4\xbc\xe6\xb8\x8e\xe6\xba\x8f\xe6\xbd\xba' 88 do = [ 89 (4, [6, 12, 15] ), 90 (10, [15] ), 91 (5, [6, 12, 15] ), 92 ] 93 94 95class CalcBreaksCantDisplayTest(unittest.TestCase): 96 def test(self): 97 urwid.set_encoding("euc-jp") 98 self.assertRaises(text_layout.CanNotDisplayText, 99 text_layout.default_layout.calculate_text_segments, 100 B('\xA1\xA1'), 1, 'space' ) 101 urwid.set_encoding("utf-8") 102 self.assertRaises(text_layout.CanNotDisplayText, 103 text_layout.default_layout.calculate_text_segments, 104 B('\xe9\xa2\x96'), 1, 'space' ) 105 106 107class SubsegTest(unittest.TestCase): 108 def setUp(self): 109 urwid.set_encoding("euc-jp") 110 111 def st(self, seg, text, start, end, exp): 112 text = B(text) 113 s = urwid.LayoutSegment(seg) 114 result = s.subseg( text, start, end ) 115 assert result == exp, "Expected %r, got %r"%(exp,result) 116 117 def test1_padding(self): 118 self.st( (10, None), "", 0, 8, [(8, None)] ) 119 self.st( (10, None), "", 2, 10, [(8, None)] ) 120 self.st( (10, 0), "", 3, 7, [(4, 0)] ) 121 self.st( (10, 0), "", 0, 20, [(10, 0)] ) 122 123 def test2_text(self): 124 self.st( (10, 0, B("1234567890")), "", 0, 8, [(8,0,B("12345678"))] ) 125 self.st( (10, 0, B("1234567890")), "", 2, 10, [(8,0,B("34567890"))] ) 126 self.st( (10, 0, B("12\xA1\xA156\xA1\xA190")), "", 2, 8, 127 [(6, 0, B("\xA1\xA156\xA1\xA1"))] ) 128 self.st( (10, 0, B("12\xA1\xA156\xA1\xA190")), "", 3, 8, 129 [(5, 0, B(" 56\xA1\xA1"))] ) 130 self.st( (10, 0, B("12\xA1\xA156\xA1\xA190")), "", 2, 7, 131 [(5, 0, B("\xA1\xA156 "))] ) 132 self.st( (10, 0, B("12\xA1\xA156\xA1\xA190")), "", 3, 7, 133 [(4, 0, B(" 56 "))] ) 134 self.st( (10, 0, B("12\xA1\xA156\xA1\xA190")), "", 0, 20, 135 [(10, 0, B("12\xA1\xA156\xA1\xA190"))] ) 136 137 def test3_range(self): 138 t = "1234567890" 139 self.st( (10, 0, 10), t, 0, 8, [(8, 0, 8)] ) 140 self.st( (10, 0, 10), t, 2, 10, [(8, 2, 10)] ) 141 self.st( (6, 2, 8), t, 1, 6, [(5, 3, 8)] ) 142 self.st( (6, 2, 8), t, 0, 5, [(5, 2, 7)] ) 143 self.st( (6, 2, 8), t, 1, 5, [(4, 3, 7)] ) 144 t = "12\xA1\xA156\xA1\xA190" 145 self.st( (10, 0, 10), t, 0, 8, [(8, 0, 8)] ) 146 self.st( (10, 0, 10), t, 2, 10, [(8, 2, 10)] ) 147 self.st( (6, 2, 8), t, 1, 6, [(1, 3), (4, 4, 8)] ) 148 self.st( (6, 2, 8), t, 0, 5, [(4, 2, 6), (1, 6)] ) 149 self.st( (6, 2, 8), t, 1, 5, [(1, 3), (2, 4, 6), (1, 6)] ) 150 151 152class CalcTranslateTest(object): 153 def setUp(self): 154 urwid.set_encoding("utf-8") 155 156 def test1_left(self): 157 result = urwid.default_layout.layout( self.text, 158 self.width, 'left', self.mode) 159 assert result == self.result_left, result 160 161 def test2_right(self): 162 result = urwid.default_layout.layout( self.text, 163 self.width, 'right', self.mode) 164 assert result == self.result_right, result 165 166 def test3_center(self): 167 result = urwid.default_layout.layout( self.text, 168 self.width, 'center', self.mode) 169 assert result == self.result_center, result 170 171 172class CalcTranslateCharTest(CalcTranslateTest, unittest.TestCase): 173 text = "It's out of control!\nYou've got to" 174 mode = 'any' 175 width = 15 176 result_left = [ 177 [(15, 0, 15)], 178 [(5, 15, 20), (0, 20)], 179 [(13, 21, 34), (0, 34)]] 180 result_right = [ 181 [(15, 0, 15)], 182 [(10, None), (5, 15, 20), (0,20)], 183 [(2, None), (13, 21, 34), (0,34)]] 184 result_center = [ 185 [(15, 0, 15)], 186 [(5, None), (5, 15, 20), (0,20)], 187 [(1, None), (13, 21, 34), (0,34)]] 188 189 190class CalcTranslateWordTest(CalcTranslateTest, unittest.TestCase): 191 text = "It's out of control!\nYou've got to" 192 mode = 'space' 193 width = 14 194 result_left = [ 195 [(11, 0, 11), (0, 11)], 196 [(8, 12, 20), (0, 20)], 197 [(13, 21, 34), (0, 34)]] 198 result_right = [ 199 [(3, None), (11, 0, 11), (0, 11)], 200 [(6, None), (8, 12, 20), (0, 20)], 201 [(1, None), (13, 21, 34), (0, 34)]] 202 result_center = [ 203 [(2, None), (11, 0, 11), (0, 11)], 204 [(3, None), (8, 12, 20), (0, 20)], 205 [(1, None), (13, 21, 34), (0, 34)]] 206 207 208class CalcTranslateWordTest2(CalcTranslateTest, unittest.TestCase): 209 text = "It's out of control!\nYou've got to " 210 mode = 'space' 211 width = 14 212 result_left = [ 213 [(11, 0, 11), (0, 11)], 214 [(8, 12, 20), (0, 20)], 215 [(14, 21, 35), (0, 35)]] 216 result_right = [ 217 [(3, None), (11, 0, 11), (0, 11)], 218 [(6, None), (8, 12, 20), (0, 20)], 219 [(14, 21, 35), (0, 35)]] 220 result_center = [ 221 [(2, None), (11, 0, 11), (0, 11)], 222 [(3, None), (8, 12, 20), (0, 20)], 223 [(14, 21, 35), (0, 35)]] 224 225 226class CalcTranslateWordTest3(CalcTranslateTest, unittest.TestCase): 227 def setUp(self): 228 urwid.set_encoding('utf-8') 229 230 text = B('\xe6\x9b\xbf\xe6\xb4\xbc\n\xe6\xb8\x8e\xe6\xba\x8f\xe6\xbd\xba') 231 width = 10 232 mode = 'space' 233 result_left = [ 234 [(4, 0, 6), (0, 6)], 235 [(6, 7, 16), (0, 16)]] 236 result_right = [ 237 [(6, None), (4, 0, 6), (0, 6)], 238 [(4, None), (6, 7, 16), (0, 16)]] 239 result_center = [ 240 [(3, None), (4, 0, 6), (0, 6)], 241 [(2, None), (6, 7, 16), (0, 16)]] 242 243 244class CalcTranslateWordTest4(CalcTranslateTest, unittest.TestCase): 245 text = ' Die Gedank' 246 width = 3 247 mode = 'space' 248 result_left = [ 249 [(0, 0)], 250 [(3, 1, 4), (0, 4)], 251 [(3, 5, 8)], 252 [(3, 8, 11), (0, 11)]] 253 result_right = [ 254 [(3, None), (0, 0)], 255 [(3, 1, 4), (0, 4)], 256 [(3, 5, 8)], 257 [(3, 8, 11), (0, 11)]] 258 result_center = [ 259 [(2, None), (0, 0)], 260 [(3, 1, 4), (0, 4)], 261 [(3, 5, 8)], 262 [(3, 8, 11), (0, 11)]] 263 264 265class CalcTranslateWordTest5(CalcTranslateTest, unittest.TestCase): 266 text = ' Word.' 267 width = 3 268 mode = 'space' 269 result_left = [[(3, 0, 3)], [(3, 3, 6), (0, 6)]] 270 result_right = [[(3, 0, 3)], [(3, 3, 6), (0, 6)]] 271 result_center = [[(3, 0, 3)], [(3, 3, 6), (0, 6)]] 272 273 274class CalcTranslateClipTest(CalcTranslateTest, unittest.TestCase): 275 text = "It's out of control!\nYou've got to\n\nturn it off!!!" 276 mode = 'clip' 277 width = 14 278 result_left = [ 279 [(20, 0, 20), (0, 20)], 280 [(13, 21, 34), (0, 34)], 281 [(0, 35)], 282 [(14, 36, 50), (0, 50)]] 283 result_right = [ 284 [(-6, None), (20, 0, 20), (0, 20)], 285 [(1, None), (13, 21, 34), (0, 34)], 286 [(14, None), (0, 35)], 287 [(14, 36, 50), (0, 50)]] 288 result_center = [ 289 [(-3, None), (20, 0, 20), (0, 20)], 290 [(1, None), (13, 21, 34), (0, 34)], 291 [(7, None), (0, 35)], 292 [(14, 36, 50), (0, 50)]] 293 294class CalcTranslateCantDisplayTest(CalcTranslateTest, unittest.TestCase): 295 text = B('Hello\xe9\xa2\x96') 296 mode = 'space' 297 width = 1 298 result_left = [[]] 299 result_right = [[]] 300 result_center = [[]] 301 302 303class CalcPosTest(unittest.TestCase): 304 def setUp(self): 305 self.text = "A" * 27 306 self.trans = [ 307 [(2,None),(7,0,7),(0,7)], 308 [(13,8,21),(0,21)], 309 [(3,None),(5,22,27),(0,27)]] 310 self.mytests = [(1,0, 0), (2,0, 0), (11,0, 7), 311 (-3,1, 8), (-2,1, 8), (1,1, 9), (31,1, 21), 312 (1,2, 22), (11,2, 27) ] 313 314 def tests(self): 315 for x,y, expected in self.mytests: 316 got = text_layout.calc_pos( self.text, self.trans, x, y ) 317 assert got == expected, "%r got:%r expected:%r" % ((x, y), got, 318 expected) 319 320 321class Pos2CoordsTest(unittest.TestCase): 322 pos_list = [5, 9, 20, 26] 323 text = "1234567890" * 3 324 mytests = [ 325 ( [[(15,0,15)], [(15,15,30),(0,30)]], 326 [(5,0),(9,0),(5,1),(11,1)] ), 327 ( [[(9,0,9)], [(12,9,21)], [(9,21,30),(0,30)]], 328 [(5,0),(0,1),(11,1),(5,2)] ), 329 ( [[(2,None), (15,0,15)], [(2,None), (15,15,30),(0,30)]], 330 [(7,0),(11,0),(7,1),(13,1)] ), 331 ( [[(3, 6, 9),(0,9)], [(5, 20, 25),(0,25)]], 332 [(0,0),(3,0),(0,1),(5,1)] ), 333 ( [[(10, 0, 10),(0,10)]], 334 [(5,0),(9,0),(10,0),(10,0)] ), 335 336 ] 337 338 def test(self): 339 for t, answer in self.mytests: 340 for pos,a in zip(self.pos_list,answer) : 341 r = text_layout.calc_coords( self.text, t, pos) 342 assert r==a, "%r got: %r expected: %r"%(t,r,a) 343