1import unittest
2
3import urwid
4
5
6class PaddingTest(unittest.TestCase):
7    def ptest(self, desc, align, width, maxcol, left, right,min_width=None):
8        p = urwid.Padding(None, align, width, min_width)
9        l, r = p.padding_values((maxcol,),False)
10        assert (l,r)==(left,right), "%s expected %s but got %s"%(
11            desc, (left,right), (l,r))
12
13    def petest(self, desc, align, width):
14        self.assertRaises(urwid.PaddingError, lambda:
15            urwid.Padding(None, align, width))
16
17    def test_create(self):
18        self.petest("invalid pad",6,5)
19        self.petest("invalid pad type",('bad',2),5)
20        self.petest("invalid width",'center','42')
21        self.petest("invalid width type",'center',('gouranga',4))
22
23    def test_values(self):
24        self.ptest("left align 5 7",'left',5,7,0,2)
25        self.ptest("left align 7 7",'left',7,7,0,0)
26        self.ptest("left align 9 7",'left',9,7,0,0)
27        self.ptest("right align 5 7",'right',5,7,2,0)
28        self.ptest("center align 5 7",'center',5,7,1,1)
29        self.ptest("fixed left",('fixed left',3),5,10,3,2)
30        self.ptest("fixed left reduce",('fixed left',3),8,10,2,0)
31        self.ptest("fixed left shrink",('fixed left',3),18,10,0,0)
32        self.ptest("fixed left, right",
33            ('fixed left',3),('fixed right',4),17,3,4)
34        self.ptest("fixed left, right, min_width",
35            ('fixed left',3),('fixed right',4),10,3,2,5)
36        self.ptest("fixed left, right, min_width 2",
37            ('fixed left',3),('fixed right',4),10,2,0,8)
38        self.ptest("fixed right",('fixed right',3),5,10,2,3)
39        self.ptest("fixed right reduce",('fixed right',3),8,10,0,2)
40        self.ptest("fixed right shrink",('fixed right',3),18,10,0,0)
41        self.ptest("fixed right, left",
42            ('fixed right',3),('fixed left',4),17,4,3)
43        self.ptest("fixed right, left, min_width",
44            ('fixed right',3),('fixed left',4),10,2,3,5)
45        self.ptest("fixed right, left, min_width 2",
46            ('fixed right',3),('fixed left',4),10,0,2,8)
47        self.ptest("relative 30",('relative',30),5,10,1,4)
48        self.ptest("relative 50",('relative',50),5,10,2,3)
49        self.ptest("relative 130 edge",('relative',130),5,10,5,0)
50        self.ptest("relative -10 edge",('relative',-10),4,10,0,6)
51        self.ptest("center relative 70",'center',('relative',70),
52            10,1,2)
53        self.ptest("center relative 70 grow 8",'center',('relative',70),
54            10,1,1,8)
55
56    def mctest(self, desc, left, right, size, cx, innercx):
57        class Inner:
58            def __init__(self, desc, innercx):
59                self.desc = desc
60                self.innercx = innercx
61            def move_cursor_to_coords(self,size,cx,cy):
62                assert cx==self.innercx, desc
63        i = Inner(desc,innercx)
64        p = urwid.Padding(i, ('fixed left',left),
65            ('fixed right',right))
66        p.move_cursor_to_coords(size, cx, 0)
67
68    def test_cursor(self):
69        self.mctest("cursor left edge",2,2,(10,2),2,0)
70        self.mctest("cursor left edge-1",2,2,(10,2),1,0)
71        self.mctest("cursor right edge",2,2,(10,2),7,5)
72        self.mctest("cursor right edge+1",2,2,(10,2),8,5)
73
74    def test_reduced_padding_cursor(self):
75        # FIXME: This is at least consistent now, but I don't like it.
76        # pack() on an Edit should leave room for the cursor
77        # fixing this gets deep into things like Edit._shift_view_to_cursor
78        # though, so this might not get fixed for a while
79
80        p = urwid.Padding(urwid.Edit(u'',u''), width='pack', left=4)
81        self.assertEqual(p.render((10,), True).cursor, None)
82        self.assertEqual(p.get_cursor_coords((10,)), None)
83        self.assertEqual(p.render((4,), True).cursor, None)
84        self.assertEqual(p.get_cursor_coords((4,)), None)
85
86        p = urwid.Padding(urwid.Edit(u'',u''), width=('relative', 100), left=4)
87        self.assertEqual(p.render((10,), True).cursor, (4, 0))
88        self.assertEqual(p.get_cursor_coords((10,)), (4, 0))
89        self.assertEqual(p.render((4,), True).cursor, None)
90        self.assertEqual(p.get_cursor_coords((4,)), None)
91
92
93class FillerTest(unittest.TestCase):
94    def ftest(self, desc, valign, height, maxrow, top, bottom,
95            min_height=None):
96        f = urwid.Filler(None, valign, height, min_height)
97        t, b = f.filler_values((20,maxrow), False)
98        assert (t,b)==(top,bottom), "%s expected %s but got %s"%(
99            desc, (top,bottom), (t,b))
100
101    def fetest(self, desc, valign, height):
102        self.assertRaises(urwid.FillerError, lambda:
103            urwid.Filler(None, valign, height))
104
105    def test_create(self):
106        self.fetest("invalid pad",6,5)
107        self.fetest("invalid pad type",('bad',2),5)
108        self.fetest("invalid width",'middle','42')
109        self.fetest("invalid width type",'middle',('gouranga',4))
110        self.fetest("invalid combination",('relative',20),
111            ('fixed bottom',4))
112        self.fetest("invalid combination 2",('relative',20),
113            ('fixed top',4))
114
115    def test_values(self):
116        self.ftest("top align 5 7",'top',5,7,0,2)
117        self.ftest("top align 7 7",'top',7,7,0,0)
118        self.ftest("top align 9 7",'top',9,7,0,0)
119        self.ftest("bottom align 5 7",'bottom',5,7,2,0)
120        self.ftest("middle align 5 7",'middle',5,7,1,1)
121        self.ftest("fixed top",('fixed top',3),5,10,3,2)
122        self.ftest("fixed top reduce",('fixed top',3),8,10,2,0)
123        self.ftest("fixed top shrink",('fixed top',3),18,10,0,0)
124        self.ftest("fixed top, bottom",
125            ('fixed top',3),('fixed bottom',4),17,3,4)
126        self.ftest("fixed top, bottom, min_width",
127            ('fixed top',3),('fixed bottom',4),10,3,2,5)
128        self.ftest("fixed top, bottom, min_width 2",
129            ('fixed top',3),('fixed bottom',4),10,2,0,8)
130        self.ftest("fixed bottom",('fixed bottom',3),5,10,2,3)
131        self.ftest("fixed bottom reduce",('fixed bottom',3),8,10,0,2)
132        self.ftest("fixed bottom shrink",('fixed bottom',3),18,10,0,0)
133        self.ftest("fixed bottom, top",
134            ('fixed bottom',3),('fixed top',4),17,4,3)
135        self.ftest("fixed bottom, top, min_height",
136            ('fixed bottom',3),('fixed top',4),10,2,3,5)
137        self.ftest("fixed bottom, top, min_height 2",
138            ('fixed bottom',3),('fixed top',4),10,0,2,8)
139        self.ftest("relative 30",('relative',30),5,10,1,4)
140        self.ftest("relative 50",('relative',50),5,10,2,3)
141        self.ftest("relative 130 edge",('relative',130),5,10,5,0)
142        self.ftest("relative -10 edge",('relative',-10),4,10,0,6)
143        self.ftest("middle relative 70",'middle',('relative',70),
144            10,1,2)
145        self.ftest("middle relative 70 grow 8",'middle',('relative',70),
146            10,1,1,8)
147
148    def test_repr(self):
149        repr(urwid.Filler(urwid.Text(u'hai')))
150