|
| 1 | +from ast import Name |
| 2 | + |
| 3 | +import pytest |
| 4 | +from full_match import match |
| 5 | + |
| 6 | +from metacode import ParsedComment, build, insert |
| 7 | + |
| 8 | + |
| 9 | +def test_run_build_with_wrong_key_or_action(): |
| 10 | + with pytest.raises(ValueError, match=match('The key must be valid Python identifier.')): |
| 11 | + build(ParsedComment( |
| 12 | + key='123', |
| 13 | + command='action', |
| 14 | + arguments=[], |
| 15 | + )) |
| 16 | + |
| 17 | + with pytest.raises(ValueError, match=match('The command must be valid Python identifier.')): |
| 18 | + build(ParsedComment( |
| 19 | + key='key', |
| 20 | + command='123', |
| 21 | + arguments=[], |
| 22 | + )) |
| 23 | + |
| 24 | + |
| 25 | +def test_build_ast(): |
| 26 | + with pytest.raises(TypeError, match=match('AST nodes are read-only and cannot be written to.')): |
| 27 | + build(ParsedComment( |
| 28 | + key='key', |
| 29 | + command='command', |
| 30 | + arguments=[Name()], |
| 31 | + )) |
| 32 | + |
| 33 | + |
| 34 | +def test_create_simple_comment(): |
| 35 | + assert build(ParsedComment( |
| 36 | + key='key', |
| 37 | + command='command', |
| 38 | + arguments=[], |
| 39 | + )) == '# key: command' |
| 40 | + |
| 41 | + |
| 42 | +def test_create_difficult_comment(): |
| 43 | + assert build(ParsedComment( |
| 44 | + key='key', |
| 45 | + command='command', |
| 46 | + arguments=[1], |
| 47 | + )) == '# key: command[1]' |
| 48 | + |
| 49 | + assert build(ParsedComment( |
| 50 | + key='key', |
| 51 | + command='command', |
| 52 | + arguments=[1, 2, 3], |
| 53 | + )) == '# key: command[1, 2, 3]' |
| 54 | + |
| 55 | + assert build(ParsedComment( |
| 56 | + key='key', |
| 57 | + command='command', |
| 58 | + arguments=['build'], |
| 59 | + )) == '# key: command[build]' |
| 60 | + |
| 61 | + assert build(ParsedComment( |
| 62 | + key='key', |
| 63 | + command='command', |
| 64 | + arguments=['build', 'build'], |
| 65 | + )) == '# key: command[build, build]' |
| 66 | + |
| 67 | + assert build(ParsedComment( |
| 68 | + key='key', |
| 69 | + command='command', |
| 70 | + arguments=['lol-kek'], |
| 71 | + )) == "# key: command['lol-kek']" |
| 72 | + |
| 73 | + assert build(ParsedComment( |
| 74 | + key='key', |
| 75 | + command='command', |
| 76 | + arguments=['lol-kek', 'lol-kek-chedurek'], |
| 77 | + )) == "# key: command['lol-kek', 'lol-kek-chedurek']" |
| 78 | + |
| 79 | + assert build(ParsedComment( |
| 80 | + key='key', |
| 81 | + command='command', |
| 82 | + arguments=[...], |
| 83 | + )) == "# key: command[...]" |
| 84 | + |
| 85 | + assert build(ParsedComment( |
| 86 | + key='key', |
| 87 | + command='command', |
| 88 | + arguments=[..., ...], |
| 89 | + )) == "# key: command[..., ...]" |
| 90 | + |
| 91 | + assert build(ParsedComment( |
| 92 | + key='key', |
| 93 | + command='command', |
| 94 | + arguments=[1.5], |
| 95 | + )) == "# key: command[1.5]" |
| 96 | + |
| 97 | + assert build(ParsedComment( |
| 98 | + key='key', |
| 99 | + command='command', |
| 100 | + arguments=[1.5, 3.0], |
| 101 | + )) == "# key: command[1.5, 3.0]" |
| 102 | + |
| 103 | + assert build(ParsedComment( |
| 104 | + key='key', |
| 105 | + command='command', |
| 106 | + arguments=[5j], |
| 107 | + )) == "# key: command[5j]" |
| 108 | + |
| 109 | + assert build(ParsedComment( |
| 110 | + key='key', |
| 111 | + command='command', |
| 112 | + arguments=[None], |
| 113 | + )) == "# key: command[None]" |
| 114 | + |
| 115 | + assert build(ParsedComment( |
| 116 | + key='key', |
| 117 | + command='command', |
| 118 | + arguments=[True], |
| 119 | + )) == "# key: command[True]" |
| 120 | + |
| 121 | + assert build(ParsedComment( |
| 122 | + key='key', |
| 123 | + command='command', |
| 124 | + arguments=[False], |
| 125 | + )) == "# key: command[False]" |
| 126 | + |
| 127 | + assert build(ParsedComment( |
| 128 | + key='key', |
| 129 | + command='command', |
| 130 | + arguments=[1, 2, 3, 1.5, 3.0, 5j, 1000j, 'build', 'build2', 'lol-kek', 'lol-kek-chedurek', None, True, False, ...], |
| 131 | + )) == "# key: command[1, 2, 3, 1.5, 3.0, 5j, 1000j, build, build2, 'lol-kek', 'lol-kek-chedurek', None, True, False, ...]" |
| 132 | + |
| 133 | + |
| 134 | +def test_insert_to_strange_comment(): |
| 135 | + with pytest.raises(ValueError, match=match('The existing part of the comment should start with a #.')): |
| 136 | + insert(ParsedComment(key='key', command='command', arguments=[]), 'kek', at_end=True) |
| 137 | + |
| 138 | + with pytest.raises(ValueError, match=match('The existing part of the comment should start with a #.')): |
| 139 | + insert(ParsedComment(key=' key', command='command', arguments=[]), 'kek', at_end=True) |
| 140 | + |
| 141 | + with pytest.raises(ValueError, match=match('The existing part of the comment should start with a #.')): |
| 142 | + insert(ParsedComment(key=' key', command='command', arguments=[]), 'kek') |
| 143 | + |
| 144 | + with pytest.raises(ValueError, match=match('The existing part of the comment should start with a #.')): |
| 145 | + insert(ParsedComment(key='key', command='command', arguments=[]), 'kek') |
| 146 | + |
| 147 | + |
| 148 | +def test_insert_at_begin_to_empty(): |
| 149 | + comment = ParsedComment( |
| 150 | + key='key', |
| 151 | + command='command', |
| 152 | + arguments=['build'], |
| 153 | + ) |
| 154 | + |
| 155 | + assert insert(comment, '') == build(comment) |
| 156 | + |
| 157 | + |
| 158 | +def test_insert_at_end_to_empty(): |
| 159 | + comment = ParsedComment( |
| 160 | + key='key', |
| 161 | + command='command', |
| 162 | + arguments=['build'], |
| 163 | + ) |
| 164 | + |
| 165 | + assert insert(comment, '', at_end=True) == build(comment) |
| 166 | + |
| 167 | + |
| 168 | +def test_insert_at_begin_to_not_empty(): |
| 169 | + comment = ParsedComment( |
| 170 | + key='key', |
| 171 | + command='command', |
| 172 | + arguments=['build'], |
| 173 | + ) |
| 174 | + |
| 175 | + assert insert(comment, '# kek') == build(comment) + ' # kek' |
| 176 | + assert insert(comment, ' # kek') == build(comment) + ' # kek' |
| 177 | + assert insert(comment, build(comment)) == build(comment) + ' ' + build(comment) |
| 178 | + |
| 179 | + |
| 180 | +def test_insert_at_end_to_not_empty(): |
| 181 | + comment = ParsedComment( |
| 182 | + key='key', |
| 183 | + command='command', |
| 184 | + arguments=['build'], |
| 185 | + ) |
| 186 | + |
| 187 | + assert insert(comment, '# kek', at_end=True) == '# kek ' + build(comment) |
| 188 | + assert insert(comment, '# kek ', at_end=True) == '# kek ' + build(comment) |
| 189 | + assert insert(comment, build(comment), at_end=True) == build(comment) + ' ' + build(comment) |
0 commit comments