Exemplo n.º 1
0
        def test_update_keeps_new_order(self):
            AST_A = asttree.AbstractSyntaxTree()
            AST_A.data = [FakeNode('1'), FakeNode('2')]

            AST_B = asttree.AbstractSyntaxTree()
            AST_B.data = [FakeNode('2'), FakeNode('1')]

            AST_A.update(AST_B)
            assert AST_A.data == [FakeNode('2'), FakeNode('1')]
Exemplo n.º 2
0
        def test_update_adds_nodes(self):
            AST_A = asttree.AbstractSyntaxTree()
            AST_A.data = [FakeNode('1'), FakeNode('2')]

            AST_B = asttree.AbstractSyntaxTree()
            AST_B.data = [FakeNode('1'), FakeNode('2'), FakeNode('3')]

            AST_A.update(AST_B)
            assert AST_A.data == [FakeNode('1'), FakeNode('2'), FakeNode('3')]
Exemplo n.º 3
0
        def test_update_performs_nodeupdate(self):
            AST_A = asttree.AbstractSyntaxTree()
            AST_A.data = [FakeNode('1')]

            AST_B = asttree.AbstractSyntaxTree()
            AST_B.data = [FakeNode('1')]

            AST_A.update(AST_B)
            assert AST_A[0].update.called_with(AST_B[0])
Exemplo n.º 4
0
    def test_produces_ast(self):
        # input
        data = [
            {
                '_id': 'C5ED1030425A436DABE94E0FCCCE76D6',
                'type': 'section',
                'name': 'home',
                'indent': 0,
                'parent': None,
                'data': {},
            },
        ]
        fd = get_iostream(json.dumps(data))
        AST = parsers.parse(fd, lexers.LexerTypes.mtask)

        # expects
        expected_AST = asttree.AbstractSyntaxTree([
            astnode.Node(
                _id='C5ED1030425A436DABE94E0FCCCE76D6',
                ntype=astnode.NodeType.section,
                name='home',
            ),
        ])

        assert AST == expected_AST
Exemplo n.º 5
0
    def _get_mtaskfile_ast(self, filepath):
        if not os.path.isfile(filepath):
            return asttree.AbstractSyntaxTree()

        with open(filepath, 'r') as fd_src:
            fd = iostream.FileDescriptor(fd_src)
            AST = parsers.parse(fd, 'mtask')
        return AST
Exemplo n.º 6
0
 def test_raises_typeerror_if_data_not_iterable(self):
     node = astnode.Node(
         _id=None,
         name='abc',
         ntype=astnode.NodeType.section,
     )
     with pytest.raises(TypeError):
         asttree.AbstractSyntaxTree(node)
Exemplo n.º 7
0
        def test_only_completed_taskchains_archived(self):
            data = [mock.Mock(), mock.Mock(), mock.Mock()]
            completed = [data[1]]

            AST = asttree.AbstractSyntaxTree()
            AST.data = data[:]
            with mock.patch(
                    '{}.AbstractSyntaxTree.get_completed_taskchains'.format(
                        asttree.__name__),
                    return_value=completed):
                archive_ast = AST.archive_completed()

            assert archive_ast.data == [data[1]]
            assert AST.data == [data[0], data[2]]
Exemplo n.º 8
0
        def test_filters_incompleted_child_taskchains(self):
            """ Retrieve all top-level task-nodes whose self/children are all completed.
            """
            complete_node = mock.Mock()
            complete_node.is_complete = mock.Mock(return_value=True)

            incomplete_node = mock.Mock()
            incomplete_node.is_complete = mock.Mock(return_value=False)

            AST = asttree.AbstractSyntaxTree()
            AST.data = [complete_node, incomplete_node]

            completed = AST.get_completed_taskchains()
            assert completed.data == [complete_node]
Exemplo n.º 9
0
 def test_render_invalid_renderer(self):
     tree = asttree.AbstractSyntaxTree([
         astnode.Node(
             _id='',
             ntype='task',
             name='task A',
             data={
                 'status': 'todo',
                 'created': None,
                 'finished': False,
                 'modified': None,
             },
         )
     ])
     with pytest.raises(TypeError):
         tree.render(Not_A_Renderer)
Exemplo n.º 10
0
        def test_render_valid(self, renderer):
            tree = asttree.AbstractSyntaxTree([
                astnode.Node(
                    _id='',
                    ntype='task',
                    name='task A',
                    data={
                        'status': 'todo',
                        'created': None,
                        'finished': False,
                        'modified': None,
                    },
                )
            ])
            tree.render(renderer)

            assert renderer.render_calls == 1
Exemplo n.º 11
0
    class Test_write:
        ast_tree = asttree.AbstractSyntaxTree([
            astnode.Node(
                _id='533C54F7A87A4AAB99296D213314FB2D',
                ntype='task',
                name='task A',
                data={
                    'status': 'todo',
                    'modified': current_dt,
                    'created': current_dt,
                    'finished': False,
                },
            )
        ])
        mtask_tree = [
            {
                "indent": 0,
                "_id":   "533C54F7A87A4AAB99296D213314FB2D",
                "name":  "task A",
                "parent": None,
                "type": "task",
                "data": {"status": "todo",
                         "finished": False,
                         "modified": "1970-01-01T00:00:00+00:00",
                         "created": "1970-01-01T00:00:00+00:00"},
            }
        ]
        current_dt = current_dt

        def test(self):
            """ This is a very evil test.. I need to figure out how to clean this up.
            """
            tempdir = tempfile.mkdtemp()
            filepath = '{}/file.mtask'.format(tempdir)
            taskfile = taskfiles.TaskFile(filepath)
            try:
                mock_open = mock.mock_open()
                with mock.patch(ns + '.open', mock_open, create=True):
                    taskfile.write(self.ast_tree)
                    written_data = mock_open().write.call_args[0][0]
                    data = json.loads(written_data)
                    assert data == self.mtask_tree
            finally:
                if os.path.isdir(tempdir):
                    shutil.rmtree(tempdir)
Exemplo n.º 12
0
    def parse(self, touch=False):
        """ Parses lexer into a nexted list of Nodes.

        Args:
            touch (bool):
                If True, updates last-modified timestamp, adds
                id if none present, etc.

        Returns:

            .. code-block:: python

                [
                    Node('name':'home', 'type':'section', ... children=[
                        Node('name':'laundry', ... children=[]),
                        Node('name':'dishes', ... children=[]),
                        ...
                    ]),
                    Node('name':'taskA', ...children=[])
                ]

        """
        # create dictionary of id:node
        allnodes = {}  # {id:node}
        for token in self.__lexer.data:
            allnodes[token['_id']] = astnode.Node(
                _id=token['_id'],
                ntype=token['type'],
                name=token['name'],
                data=token['data'],
            )

        # create AST
        AST = asttree.AbstractSyntaxTree()
        for token in self.__lexer.data:
            if token['parent']:
                parent = allnodes[token['parent']]
                child = allnodes[token['_id']]
                child.parent = parent
                parent.children.append(child)
            else:
                node = allnodes[token['_id']]
                AST.append(node)

        return AST
Exemplo n.º 13
0
def parse(iostream, lexer):
    """ Parse text from `iostream` using `lexer` .

    Args:
        iostream (taskmage2.parser.iostream.IOStream):
            stream object containing text

        lexer (taskmage2.parser.LexerTypes, str):
            enum/value-str of the lexer you'd like to parse `iostream` with.

    Returns:
        taskmage2.asttree.AbstractSyntaxTree:
            AbstractSyntaxTree built from `iostream`
    """
    lexer = lexers.get_lexer(iostream, lexer)
    lexer.read()

    # create dictionary of id:node
    allnodes = {}  # {id:node}
    for token in lexer.data:
        allnodes[token['_id']] = astnode.Node(
            _id=token['_id'],
            ntype=token['type'],
            name=token['name'],
            data=token['data'],
        )

    # create AST
    AST = asttree.AbstractSyntaxTree()
    for token in lexer.data:
        if token['parent']:
            parent = allnodes[token['parent']]
            child = allnodes[token['_id']]
            child.parent = parent
            parent.children.append(child)
        else:
            node = allnodes[token['_id']]
            AST.append(node)

    return AST
Exemplo n.º 14
0
        def test_children_run_touch(self):
            AST = asttree.AbstractSyntaxTree()
            AST.data = [mock.Mock(), mock.Mock()]

            AST.touch()
            assert all([node.touch.called for node in AST])
Exemplo n.º 15
0
 def test_raises_typeerror_on_invalid_data(self):
     with pytest.raises(TypeError):
         asttree.AbstractSyntaxTree(['a', 'b'])
Exemplo n.º 16
0
        def test_children_run_finalize(self):
            AST = asttree.AbstractSyntaxTree()
            AST.data = [mock.Mock(), mock.Mock()]

            AST.finalize()
            assert all([node.finalize.called for node in AST])