예제 #1
0
class NodeFactoryTest(unittest.TestCase):
    def setUp(self):
        self._node_factory = NodeFactory(DEFAULT_NODE_IMPLS)
        self._node_factory.register({TestClass: TestClassNode})

    def test_render_customized_class(self):
        data = TestClass(1, "test", True)

        node = self._node_factory.create_root_node(data)
        widget = node.get_widget()

        contents = []
        while widget is not None:
            node = widget.get_node()
            if not node.is_expanded():
                node.toggle_expanded()
            widget = node.get_widget()
            contents.append(widget.render((18, )).content())
            widget = widget.next_inorder()

        texts = [[[t[2] for t in row] for row in content]
                 for content in contents]
        # verify
        self.assertEqual(5, len(texts))
        expected = [
            [[B("{                 ")]],
            [[B("   "), B('"id"'),
              B(": "), B('1'),
              B('        ')]],
            [[B("   "), B('"name"'),
              B(": "), B('"test"'),
              B(' ')]],
            [[B("   "), B('"flag"'),
              B(": "), B('true'),
              B('   ')]],
            [[B("}                 ")]],
        ]
        self.assertEqual(expected, texts)
예제 #2
0
class ObjectNodeTest(unittest.TestCase):
    """
    unit tests for :py:class:`pyfx.view.json_lib.object.object_node.ObjectNode`
    """
    def setUp(self):
        self._node_factory = NodeFactory(DEFAULT_NODE_IMPLS)

    def test_empty_object(self):
        """ test rendering of an empty JSON object"""
        data = {}

        # act
        node = self._node_factory.create_root_node(data)
        widget = node.get_widget()

        contents = []
        while widget is not None:
            node = widget.get_node()
            if not node.is_expanded():
                node.toggle_expanded()
            widget = node.get_widget()
            contents.append(widget.render((18, )).content())
            widget = widget.next_inorder()

        # restart and scan from the end widget
        node = self._node_factory.create_root_node(data)
        widget = node.get_end_node().get_widget()
        contents_from_end = []
        while widget is not None:
            contents_from_end.append(widget.render((18, )).content())
            widget = widget.prev_inorder()

        texts = [[[t[2] for t in row] for row in content]
                 for content in contents]
        texts_from_end = [[[t[2] for t in row] for row in content]
                          for content in contents_from_end]
        texts_from_end.reverse()

        # verify
        self.assertEqual(2, len(texts))
        expected = [
            [[B("{                 ")]],
            [[B("}                 ")]],
        ]
        self.assertEqual(expected, texts)
        self.assertEqual(expected, texts_from_end)

    def test_simple_object(self):
        """ test rendering of a not-nested JSON object """
        data = {"k1": "v1", "k2": "v2"}

        # act
        node = self._node_factory.create_root_node(data)
        widget = node.get_widget()

        contents = []
        while widget is not None:
            node = widget.get_node()
            if not node.is_expanded():
                node.toggle_expanded()
            widget = node.get_widget()
            contents.append(widget.render((18, )).content())
            widget = widget.next_inorder()

        texts = [[[t[2] for t in row] for row in content]
                 for content in contents]

        # verify
        self.assertEqual(4, len(texts))
        expected = [
            [[B("{                 ")]],
            [[B("   "), B('"k1"'),
              B(": "), B('"v1"'),
              B('     ')]],
            [[B("   "), B('"k2"'),
              B(": "), B('"v2"'),
              B('     ')]],
            [[B("}                 ")]],
        ]
        self.assertEqual(expected, texts)

    def test_nested_object(self):
        """
        test rendering of a nested JSON object
        """

        data = {"key": {"key": "val"}}

        # act
        node = self._node_factory.create_root_node(data)
        widget = node.get_widget()

        contents = []
        while widget is not None:
            node = widget.get_node()
            if not node.is_expanded():
                node.toggle_expanded()
            widget = node.get_widget()
            contents.append(widget.render((18, )).content())
            widget = widget.next_inorder()

        texts = [[[t[2] for t in row] for row in content]
                 for content in contents]

        # verify
        self.assertEqual(5, len(texts))
        expected = [
            [[B("{                 ")]],
            [[B("   "), B('"key"'),
              B(": "), B("{       ")]],
            [[B("      "), B('"key"'),
              B(": "), B('"val"')]],
            [[B("   "), B("}              ")]],
            [[B("}                 ")]],
        ]
        self.assertEqual(expected, texts)

    def test_object_with_list_child(self):
        """
        test rendering of a JSON object with list child
        """

        data = {"key": [1]}

        # act
        node = self._node_factory.create_root_node(data)
        widget = node.get_widget()

        contents = []
        while widget is not None:
            node = widget.get_node()
            if not node.is_expanded():
                node.toggle_expanded()
            widget = node.get_widget()
            contents.append(widget.render((18, )).content())
            widget = widget.next_inorder()

        texts = [[[t[2] for t in row] for row in content]
                 for content in contents]

        # verify
        self.assertEqual(5, len(texts))
        expected = [
            [[B("{                 ")]],
            [[B("   "), B('"key"'),
              B(": "), B("[       ")]],
            [[B("      "), B('1'), B('           ')]],
            [[B("   "), B("]              ")]],
            [[B("}                 ")]],
        ]
        self.assertEqual(expected, texts)

    def test_prev_order(self):
        data = {"k1": "v1", "k2": "v2"}

        node = self._node_factory.create_root_node(data)
        # start from the end
        widget = node.get_end_node().get_widget()

        contents = []
        while widget is not None:
            contents.append(widget.render((18, )).content())
            widget = widget.prev_inorder()

        texts = [[[t[2] for t in row] for row in content]
                 for content in contents]
        texts.reverse()

        # verify
        self.assertEqual(4, len(texts))
        expected = [
            [[B("{                 ")]],
            [[B("   "), B('"k1"'),
              B(": "), B('"v1"'),
              B('     ')]],
            [[B("   "), B('"k2"'),
              B(": "), B('"v2"'),
              B('     ')]],
            [[B("}                 ")]],
        ]
        self.assertEqual(expected, texts)
예제 #3
0
 def setUp(self):
     self._node_factory = NodeFactory(DEFAULT_NODE_IMPLS)
예제 #4
0
class ArrayNodeTest(unittest.TestCase):
    """
    unit tests for :py:class:`pyfx.view.json_lib.array.array_node.ArrayNode`
    """
    def setUp(self):
        self._node_factory = NodeFactory(DEFAULT_NODE_IMPLS)

    def test_empty_list(self):
        """ test rendering of an empty JSON object"""
        data = []

        # act
        node = self._node_factory.create_root_node(data)
        widget = node.get_widget()

        contents = []
        while widget is not None:
            node = widget.get_node()
            if not node.is_expanded():
                node.toggle_expanded()
            widget = node.get_widget()
            contents.append(widget.render((18, )).content())
            widget = widget.next_inorder()

        # restart and scan from the end
        node = self._node_factory.create_root_node(data)
        widget = node.get_end_node().get_widget()
        contents_from_end = []
        while widget is not None:
            contents_from_end.append(widget.render((18, )).content())
            widget = widget.prev_inorder()

        texts = [[[t[2] for t in row] for row in content]
                 for content in contents]
        texts_from_end = [[[t[2] for t in row] for row in content]
                          for content in contents_from_end]
        texts_from_end.reverse()

        # verify
        self.assertEqual(2, len(texts))
        expected = [
            [[B("[                 ")]],
            [[B("]                 ")]],
        ]
        self.assertEqual(expected, texts)
        self.assertEqual(expected, texts_from_end)

    def test_simple_array(self):
        """
        test rendering a not-nested array
        """

        data = [1, 2, "str"]

        # act
        node = self._node_factory.create_root_node(data)
        widget = node.get_widget()

        contents = []
        while widget is not None:
            node = widget.get_node()
            if not node.is_expanded():
                node.toggle_expanded()
            widget = node.get_widget()
            contents.append(widget.render((18, )).content())
            widget = widget.next_inorder()

        texts = [[[t[2] for t in row] for row in content]
                 for content in contents]

        # verify
        self.assertEqual(5, len(texts))
        expected = [[[B("[                 ")]],
                    [[B("   "), B('1'), B('              ')]],
                    [[B("   "), B('2'), B('              ')]],
                    [[B("   "), B('"str"'),
                      B('          ')]], [[B("]                 ")]]]
        self.assertEqual(expected, texts)

    def test_nested_array(self):
        """
        test rendering a nested array
        """

        data = [1, 2, ["str", True]]

        # act
        node = self._node_factory.create_root_node(data)
        widget = node.get_widget()

        contents = []
        while widget is not None:
            node = widget.get_node()
            if not node.is_expanded():
                node.toggle_expanded()
            widget = node.get_widget()
            contents.append(widget.render((18, )).content())
            widget = widget.next_inorder()

        texts = [[[t[2] for t in row] for row in content]
                 for content in contents]

        # verify
        self.assertEqual(8, len(texts))
        expected = [[[B("[                 ")]],
                    [[B("   "), B('1'), B('              ')]],
                    [[B("   "), B('2'), B('              ')]],
                    [[B("   "), B("[              ")]],
                    [[B("      "), B('"str"'),
                      B('       ')]], [[B("      "),
                                        B('true'),
                                        B('        ')]],
                    [[B("   "), B("]              ")]],
                    [[B("]                 ")]]]
        self.assertEqual(expected, texts)

    def test_array_with_object_child(self):
        """
        test rendering an array with object as a child
        """

        data = [1, 2, {"test": True}]

        # act
        node = self._node_factory.create_root_node(data)
        widget = node.get_widget()

        contents = []
        while widget is not None:
            node = widget.get_node()
            if not node.is_expanded():
                node.toggle_expanded()
            widget = node.get_widget()
            contents.append(widget.render((18, )).content())
            widget = widget.next_inorder()

        texts = [[[t[2] for t in row] for row in content]
                 for content in contents]

        # verify
        self.assertEqual(7, len(texts))
        expected = [[[B("[                 ")]],
                    [[B("   "), B('1'), B('              ')]],
                    [[B("   "), B('2'), B('              ')]],
                    [[B("   "), B("{              ")]],
                    [[B("      "),
                      B('"test"'),
                      B(": "), B("true")]], [[B("   "),
                                              B("}              ")]],
                    [[B("]                 ")]]]
        self.assertEqual(expected, texts)

    def test_prev_order(self):
        data = [1, 2]

        node = self._node_factory.create_root_node(data)
        # start from the end
        widget = node.get_end_node().get_widget()

        contents = []
        while widget is not None:
            contents.append(widget.render((18, )).content())
            widget = widget.prev_inorder()

        texts = [[[t[2] for t in row] for row in content]
                 for content in contents]
        texts.reverse()

        # verify
        self.assertEqual(4, len(texts))
        expected = [[[B("[                 ")]],
                    [[B("   "), B('1'), B('              ')]],
                    [[B("   "), B('2'), B('              ')]],
                    [[B("]                 ")]]]
        self.assertEqual(expected, texts)
예제 #5
0
class AtomicNodeTest(unittest.TestCase):
    """
    unit tests for :py:class:`pyfx.view.json_lib.primitive.primitive_node.PrimitiveNode`
    """
    def setUp(self):
        self._node_factory = NodeFactory(DEFAULT_NODE_IMPLS)

    def test_integer_node(self):
        """ test JSON `integer` rendering """
        data = 1

        # act
        node = self._node_factory.create_root_node(data)
        widget = node.get_widget()

        contents = []
        while widget is not None:
            node = widget.get_node()
            if not node.is_expanded():
                widget.keypress((18, ), 'enter')
            widget = node.get_widget()
            contents.append(widget.render((18, )).content())
            widget = widget.next_inorder()

        texts = [[[t[2] for t in row] for row in content]
                 for content in contents]

        # verify
        self.assertEqual(1, len(texts))
        expected = [[[B('1'), B('                 ')]]]
        self.assertEqual(expected, texts)

    def test_numeric_node(self):
        """ test JSON `numeric` rendering """
        data = 1.0

        # act
        node = self._node_factory.create_root_node(data)
        widget = node.get_widget()

        contents = []
        while widget is not None:
            node = widget.get_node()
            if not node.is_expanded():
                widget.keypress((18, ), 'enter')
            widget = node.get_widget()
            contents.append(widget.render((18, )).content())
            widget = widget.next_inorder()

        texts = [[[t[2] for t in row] for row in content]
                 for content in contents]

        # verify
        self.assertEqual(1, len(texts))
        expected = [[[B('1.0'), B('               ')]]]
        self.assertEqual(expected, texts)

    def test_string_node(self):
        """ test JSON `string` rendering """

        data = 'str'

        # act
        node = self._node_factory.create_root_node(data)
        widget = node.get_widget()

        contents = []
        while widget is not None:
            node = widget.get_node()
            if not node.is_expanded():
                widget.keypress((18, ), 'enter')
            widget = node.get_widget()
            contents.append(widget.render((18, )).content())
            widget = widget.next_inorder()

        texts = [[[t[2] for t in row] for row in content]
                 for content in contents]

        # verify
        self.assertEqual(1, len(texts))
        expected = [[[B('"str"'), B('             ')]]]
        self.assertEqual(expected, texts)

    def test_boolean_node(self):
        """ test JSON `boolean` rendering """

        data = True

        # act
        node = self._node_factory.create_root_node(data)
        widget = node.get_widget()

        contents = []
        while widget is not None:
            node = widget.get_node()
            if not node.is_expanded():
                widget.keypress((18, ), 'enter')
            widget = node.get_widget()
            contents.append(widget.render((18, )).content())
            widget = widget.next_inorder()

        texts = [[[t[2] for t in row] for row in content]
                 for content in contents]

        # verify
        self.assertEqual(1, len(texts))
        expected = [[[B('true'), B('              ')]]]
        self.assertEqual(expected, texts)

    def test_null_node(self):
        """ test JSON `null` rendering """

        data = None

        # act
        node = self._node_factory.create_root_node(data)
        widget = node.get_widget()

        contents = []
        while widget is not None:
            node = widget.get_node()
            if not node.is_expanded():
                widget.keypress((18, ), 'enter')
            widget = node.get_widget()
            contents.append(widget.render((18, )).content())
            widget = widget.next_inorder()

        texts = [[[t[2] for t in row] for row in content]
                 for content in contents]

        # verify
        self.assertEqual(1, len(texts))
        expected = [[[B('null'), B('              ')]]]
        self.assertEqual(expected, texts)
예제 #6
0
 def setUp(self):
     self._node_factory = NodeFactory(DEFAULT_NODE_IMPLS)
     self._node_factory.register({TestClass: TestClassNode})