Esempio n. 1
0
    def test_repeated_ids(self):
        '''
        Tests if the repeated ids raises KeyError
        '''

        json_string1 = '''
        {
            "class": "Text",
            "id": "same_id",
            "markup": "nice"
        }
        '''
        json_string2 = '''
        {
            "class": "Text",
            "id": "same_id",
            "markup": "not nice"
        }
        '''

        w_dict1 = json.loads(json_string1)
        w_dict2 = json.loads(json_string2)
        text1 = ugh.construct(w_dict1)
        with self.assertRaises(KeyError):
            ugh.construct(w_dict2)
Esempio n. 2
0
    def test_multiple_callbacks(self):
        '''
        Tests if multiple callbacks are correctly assigned.
        '''

        json_string = '''
        {
            "class": "Filler",
            "body": {
                "class": "Pile",
                "widget_list": [
                    {
                        "class": "Button",
                        "label": "my callback button",
                        "on_press": "my_callback"
                    }, {
                        "class": "Button",
                        "label": "my callback button",
                        "on_press": "my_callback"
                    }, {
                        "class": "Button",
                        "label": "my callback button",
                        "on_press": "my_callback"
                    }
                ]
            }
        }
        '''

        call = {'my_callback': lambda btn: btn.set_label('nice')}
        w_dict = json.loads(json_string)
        widget = ugh.construct(w_dict, call)
Esempio n. 3
0
    def test_text_attr(self):
        '''
        Test if attributes passed are properly loaded.
        '''

        json_string = '''
        {
            "class": "Text",
            "markup": [ "reversed", "nice" ]
        }
        '''

        w_dict = json.loads(json_string)
        text = ugh.construct(w_dict)

        # type
        self.assertIsInstance(text, urwid.Text)

        # properties
        self.assertEqual(text.text, 'nice')
        # get_text() returns the the text and a tuple with the attribute and
        # size of the string
        attrs = [('reversed', 4)]
        txt = 'nice'
        self.assertEqual(text.get_text(), (txt, attrs))
Esempio n. 4
0
    def test_multiple_text_attrs(self):
        '''
        Tests the widget Text when using multiple markup attributes to it's
        constructor.
        '''

        json_string = '''
        {
            "class": "Text",
            "markup": [
                [ "some_attr", "nice0 " ],
                "nice1 ",
                [ "other_attr", "nice2" ]
            ]
        }
        '''

        w_dict = json.loads(json_string)
        widget = ugh.construct(w_dict)

        text = widget.text
        correct_text = 'nice0 nice1 nice2'
        self.assertEqual(text, correct_text)

        attrs = widget.get_text()
        correct_attrs = ('nice0 nice1 nice2', [('some_attr', 6), (None, 6),
                                               ('other_attr', 5)])
        self.assertEqual(attrs, correct_attrs)
Esempio n. 5
0
    def test_list_widgets(self):
        '''
        Tests the creation of widgets that receive lists of widgets in it's
        constructor.

        For example, the Pile widget. It receives a list of widgets, or tuples,
        with options for each widget.
        '''

        json_string = '''
        {
            "class": "Pile",
            "widget_list": [
                { "class": "Text", "markup": "nice0" },
                { "class": "Text", "markup": "nice1" },
                { "class": "Text", "markup": "nice2" },
                { "class": "Text", "markup": "nice3" }
            ]
        }
        '''

        w_dict = json.loads(json_string)
        pile = ugh.construct(w_dict)

        # types
        for w, _ in pile.contents:
            self.assertIsInstance(w, urwid.Text)

        # properties
        i = 0
        for w, _ in pile.contents:
            self.assertEqual(w.text, f'nice{i}')
            i += 1
Esempio n. 6
0
    def test_simple_widgets(self):
        '''
        Simple test that verifies the creation of simple widgets.

        Simple widgets are widgets that do not need other widgets for it to be
        created. For example: Text and Button widgets. A Filler, List etc.
        widgets need some widget to be passed in the constructor for it to be
        created.
        '''

        json_string = '''
        {
            "text1": {
                "class": "Text",
                "markup": "nice",
                "align": "center"
            },
            "text2": {
                "class": "Text",
                "markup": "not nice",
                "align": "left"
            },
            "button1": {
                "class": "Button",
                "label": "this button"
            },
            "button2": {
                "class": "Button",
                "label": "other button"
            }
        }
        '''
        w_dict = json.loads(json_string)

        for key, item in w_dict.items():
            w_dict[key] = ugh.construct(item)

        # easier reading
        t1 = w_dict['text1']
        t2 = w_dict['text2']
        b1 = w_dict['button1']
        b2 = w_dict['button2']

        # types
        self.assertIsInstance(t1, urwid.Text)
        self.assertIsInstance(t2, urwid.Text)
        self.assertIsInstance(b1, urwid.Button)
        self.assertIsInstance(b2, urwid.Button)

        # properties
        self.assertEqual(t1.text, 'nice')
        self.assertEqual(t1.align, 'center')
        self.assertEqual(t2.text, 'not nice')
        self.assertEqual(t2.align, 'left')
        self.assertEqual(b1.label, 'this button')
        self.assertEqual(b2.label, 'other button')
Esempio n. 7
0
    def test_user_data(self):
        '''
        Tests if the user_data problem raises an exception
        '''

        json_string = '''
        {
            "class": "Button",
            "label": "my callback button",
            "on_press": "my_callback",
            "user_data": [12312312, "any data", {"lol": "what?"}]

        }
        '''

        call = {'my_callback': lambda btn: btn.set_label('nice')}
        w_dict = json.loads(json_string)
        with self.assertRaises(ValueError):
            ugh.construct(w_dict, call)
Esempio n. 8
0
    def test_ids(self):
        '''
        Tests the id management implementation of ugh.

        Every widget with an id key in it's json should be included in the ids
        dictionary.
        '''

        json_string = '''
        {
            "class": "Pile",
            "widget_list": [
                {
                    "class": "Columns",
                    "id": 1,
                    "widget_list": [
                        { "class": "Button", "label": "btn0" },
                        { "class": "Button", "label": "btn1" },
                        { "class": "Button", "label": "btn2" },
                        { "class": "Button", "label": "btn3" },
                        {
                            "class": "Columns",
                            "id": 2,
                            "widget_list": [
                                { "class": "Button", "label": "btn0" },
                                { "class": "Button", "label": "btn1" },
                                { "class": "Button", "label": "btn2" },
                                { "class": "Button", "label": "btn3" }
                            ]
                        }
                    ]
                }
            ]
        }
        '''

        w_dict = json.loads(json_string)
        widget = ugh.construct(w_dict)
        print(ugh.ids)
        self.assertIn(1, ugh.ids)
        self.assertIn(2, ugh.ids)

        i = 0
        for w, _ in ugh.ids[1].contents:
            if not isinstance(w, urwid.Columns):
                self.assertEqual(w.label, f'btn{i}')
                i += 1

        i = 0
        for w, _ in ugh.ids[2].contents:
            self.assertEqual(w.label, f'btn{i}')
            i += 1
Esempio n. 9
0
    def test_callback(self):
        '''
        Tests if a simple callback, with only the button itself, works.
        '''

        json_string = '''
        {
            "class": "Filler",
            "body": {
                "class": "Button",
                "id": "btn",
                "label": "my callback button",
                "on_press": "my_callback"
            }
        }
        '''

        call = {'my_callback': lambda btn: btn.set_label('nice')}
        w_dict = json.loads(json_string)
        widget = ugh.construct(w_dict, call)
Esempio n. 10
0
            "label": "Second line (button)"
        },
        {
            "class": "Columns",
            "widget_list": [
                {
                    "class": "Text",
                    "markup": [ "banner", "third line, part 1/3" ]
                },
                {
                    "class": "Text",
                    "markup": [ "streak", "third line, part 2/3" ]
                },
                {
                    "class": "Text",
                    "markup": [ "banner", "third line, part 3/3" ]
                }
            ]
        }
    ]
}
'''

palette = [('banner', 'black', 'light gray'), ('streak', 'black', 'dark red'),
           ('bg', 'black', 'dark blue')]

w_dict = json.loads(json_string)
t = ugh.construct(w_dict)
loop = urwid.MainLoop(t, palette, unhandled_input=exit_q)
loop.run()
Esempio n. 11
0
    def test_composite_widgets(self):
        '''
        Test the creation of composite widgets.

        Composite widgets are widgets that receive some other widget in it's
        constructor. For example: Filler, WdigetPlaceholder etc.
        '''
        json_string = '''
        {
            "filler": {
                "class": "Filler",
                "body":
                {
                    "class": "AttrMap",
                    "w": {
                        "class": "Text",
                        "markup": "nice",
                        "align": "center"
                    },
                    "attr_map": "reversed"
                }
            },
            "padding": {
                "class": "Padding",
                "w":
                {
                    "class": "AttrMap",
                    "w": {
                        "class": "Text",
                        "markup": "nice",
                        "align": "center"
                    },
                    "attr_map": "reversed"
                }
            },
            "placeholder": {
                "class": "WidgetPlaceholder",
                "original_widget": {
                    "class": "Button",
                    "label": "nice"
                }
            }
        }
        '''
        w_dict = json.loads(json_string)

        filler = ugh.construct(w_dict['filler'])
        filler_text = filler.original_widget.original_widget
        padding = ugh.construct(w_dict['padding'])
        padding_text = padding.original_widget.original_widget
        placeholder = ugh.construct(w_dict['placeholder'])

        # types
        self.assertIsInstance(filler, urwid.Filler)
        self.assertIsInstance(filler.original_widget, urwid.AttrMap)
        self.assertIsInstance(filler_text, urwid.Text)

        self.assertIsInstance(padding, urwid.Padding)
        self.assertIsInstance(padding.original_widget, urwid.AttrMap)
        self.assertIsInstance(padding_text, urwid.Text)

        self.assertIsInstance(placeholder, urwid.WidgetPlaceholder)
        self.assertIsInstance(placeholder.original_widget, urwid.Button)

        # properties
        self.assertEqual(filler_text.text, 'nice')
        self.assertEqual(padding_text.text, 'nice')
        self.assertEqual(placeholder.original_widget.label, 'nice')
Esempio n. 12
0
    def test_composite_list_widgets(self):
        '''
        Tests widgets that have widgets with lists of widgets in it.

        A multi-level test_list_widgets, basically.
        '''

        json_string = '''
        {
            "class": "Pile",
            "widget_list": [
                {
                    "class": "Columns",
                    "widget_list": [
                        {
                            "class": "Text",
                            "markup": [
                                ["attr0", "text0"],
                                "text1",
                                ["attr2", "text2"]
                            ]
                        },
                        {
                            "class": "Button",
                            "label": "button"
                        },
                        {
                            "class": "Columns",
                            "widget_list": [
                                { "class": "Text", "markup": "text0"},
                                { "class": "Text", "markup": "text1"},
                                { "class": "Text", "markup": "text2"},
                                { "class": "Text", "markup": "text3"}
                            ]
                        }
                    ]
                }
            ]
        }
        '''

        w_dict = json.loads(json_string)
        pile = ugh.construct(w_dict)

        cols = pile.contents[0][0]
        self.assertIsInstance(cols, urwid.Columns)

        text = cols.contents[0][0]
        self.assertIsInstance(text, urwid.Text)
        txt = 'text0text1text2'
        self.assertEqual(text.text, txt)
        text_attrs = (txt, [("attr0", 5), (None, 5), ("attr2", 5)])
        self.assertEqual(text.get_text(), text_attrs)

        button = cols.contents[1][0]
        self.assertIsInstance(button, urwid.Button)
        self.assertEqual(button.label, 'button')

        cols = cols.contents[2][0]
        self.assertIsInstance(cols, urwid.Columns)
        for i, t in enumerate(cols.contents):
            self.assertEqual(t[0].text, f'text{i}')