Beispiel #1
0
    def test_remove_comments_when_nested(self):
        ''' Test what happens when the file has some nested comments.
        '''
        content = [
            'public class Foo {',
            '// private int i = 3; /*',
            'private int j = 4; // */',
            '/* // private int k = 5; */',
            '/* blah blah blah */ private int l = 6;',
            '/* // this is all commented',
            '/*',
            'public Foo() { }',
            '*/',
            '/// Writes a comment.  The comment characters /* */ or // should be included in the comment string',
            '}',
        ]

        expected = [
            'public class Foo {',
            'private int j = 4;',
            'private int l = 6;',
            '}',
        ]
        actual = remove_comments(content)
        self.assertEquals(expected, actual)
Beispiel #2
0
 def test_remove_comments_no_comments(self):
     ''' Test remove_comments when the content has no comments.
     '''
     content = [
         'public class Foo',
         '{',
         '}',
     ]
     expected = content
     actual = remove_comments(content)
     self.assertEquals(expected, actual)
Beispiel #3
0
 def test_simple_remove_comments(self):
     ''' Test to remove some javadocs and some commented lines.
     '''
     content = [
         '/* this is the class javadoc',
         '*',
         '***********/',
         'public class Foo {',
         '// this is the max value',
         'private int maxValue;',
         '}',
     ]
     expected = [
         'public class Foo {',
         'private int maxValue;',
         '}',
     ]
     actual = remove_comments(content)
     self.assertEquals(expected, actual)
Beispiel #4
0
    def test_remove_inline_comments(self):
        ''' Test what happens when the file has comments the same
        line there is code.
        '''
        content = [
            'public class Foo // extends Bar',
            '{',
            'private int j; // = 3;',
            'public Foo() {',
            'if (this.j == 3 /* || this.j == 2 */) {',
            'throw new RuntimeError("foo")',
            '}',
            '}',
            'public/* static */Object getValue() {',
            '/*',
            'if (this.j == 3)',
            'raise RuntimeException("getValue")',
            '*/ }',
            '}',
        ]

        expected = [
            'public class Foo',
            '{',
            'private int j;',
            'public Foo() {',
            'if (this.j == 3 ) {',
            'throw new RuntimeError("foo")',
            '}',
            '}',
            'public Object getValue() {',
            '}',
            '}',
        ]
        actual = remove_comments(content)
        self.assertEquals(expected, actual)
Beispiel #5
0
    def parse_content(self, filepath, content):
        ''' It will parse the content of the file.

        If the file has a compiler directive, then it will be ignore
        it by returning an empty list, because it is very difficult
        to take into account the changes that affect the compiler.


        :parameters:
            filepath: str
                the path and name of the file. The path will
                be realive to where the command was executed.
            content: list(str)
                the content of the file already modified
                by the base parser.

        :returns:
            a list of pywebuml.models.Class with it's
            methods and attributes.
        '''
        res = []
        if not content:
            # the file is empty
            return []
        content = remove_comments(content)

        if not content:
            # the whole file was a comment.
            return []

        content = self.clean_content(content)
        current_line = None
        self.class_parser = self.get_class_parser(content)

        while True:
            current_line = content[self.index]

            if self.should_ignore_line(current_line, content):
                pass
            elif has_any_keyword(current_line, CLASS_KEYWORDS):
                # To take into account that some methods or enums, could
                # be defined before the class.
                self.index = self.parse_class_definition(filepath, content, self.index, res)
            else:
                # iterate the method or enum that is outside the class
                opened = current_line.count('{') - current_line.count('}')
                current_position = self.index
                opened_keys = 0
                while True:
                    line = content[current_position]
                    opened_keys = opened_keys + line.count('{') - line.count('}')
                    current_position += 1

                    if opened_keys == 0:
                        break
                self.index = current_position


            if self.index >= len(content):
                break

        return res