| 31 | |
| 32 | |
| 33 | def test_comment_picker_location() -> None: |
| 34 | # multiple "before" comments |
| 35 | source = ( |
| 36 | '#: comment before assignment1\n#:\n#: comment before assignment2\na = 1 + 1\n' |
| 37 | ) |
| 38 | parser = Parser(source) |
| 39 | parser.parse() |
| 40 | assert parser.comments == { |
| 41 | ('', 'a'): 'comment before assignment1\n\ncomment before assignment2' |
| 42 | } |
| 43 | |
| 44 | # before and after comments |
| 45 | source = '#: comment before assignment\na = 1 + 1 #: comment after assignment\n' |
| 46 | parser = Parser(source) |
| 47 | parser.parse() |
| 48 | assert parser.comments == {('', 'a'): 'comment after assignment'} |
| 49 | |
| 50 | # after comment and next line string |
| 51 | source = 'a = 1 + 1\n #: comment after assignment\n"""string on next line"""\n' |
| 52 | parser = Parser(source) |
| 53 | parser.parse() |
| 54 | assert parser.comments == {('', 'a'): 'string on next line'} |
| 55 | |
| 56 | # before comment and next line string |
| 57 | source = '#: comment before assignment\na = 1 + 1\n"""string on next line"""\n' |
| 58 | parser = Parser(source) |
| 59 | parser.parse() |
| 60 | assert parser.comments == {('', 'a'): 'string on next line'} |
| 61 | |
| 62 | # before comment, after comment and next line string |
| 63 | source = ( |
| 64 | '#: comment before assignment\n' |
| 65 | 'a = 1 + 1 #: comment after assignment\n' |
| 66 | '"""string on next line"""\n' |
| 67 | ) |
| 68 | parser = Parser(source) |
| 69 | parser.parse() |
| 70 | assert parser.comments == {('', 'a'): 'string on next line'} |
| 71 | |
| 72 | # inside __init__ method |
| 73 | source = ( |
| 74 | 'class Foo(object):\n' |
| 75 | ' def __init__(self):\n' |
| 76 | ' #: comment before assignment\n' |
| 77 | ' self.attr1 = None\n' |
| 78 | ' self.attr2 = None #: comment after assignment\n' |
| 79 | '\n' |
| 80 | ' #: comment for attr3(1)\n' |
| 81 | ' self.attr3 = None #: comment for attr3(2)\n' |
| 82 | ' """comment for attr3(3)"""\n' |
| 83 | ) |
| 84 | parser = Parser(source) |
| 85 | parser.parse() |
| 86 | assert parser.comments == { |
| 87 | ('Foo', 'attr1'): 'comment before assignment', |
| 88 | ('Foo', 'attr2'): 'comment after assignment', |
| 89 | ('Foo', 'attr3'): 'comment for attr3(3)', |
| 90 | } |