1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
|
#!/usr/bin/python
# -*- coding:Utf-8 -*-
from baron.spliter import split, UntreatedError
from baron.utils import python_version
import pytest
def test_empty():
assert split("") == []
def test_print():
assert split("print") == ["print"]
def test_print_space():
assert split("print ") == ['print', ' ']
def test_import():
assert split("import pouet") == ["import", " ", "pouet"]
def test_from_import():
assert split(" from zob import pouet ") == [" ", "from", " ", "zob", " ", "import", " ", "pouet", " "]
def test_from_import_as():
assert split("from a import b as c") == ["from", " ", "a", " ", "import", " ", "b", " ", "as", " ", "c"]
def test_underscore_variable():
assert split("some_variable") == ["some_variable"]
def test_different_case():
assert split("AbCd cDeF") == ["AbCd", " ", "cDeF"]
def test_decorator():
assert split("@pouet") == ["@", "pouet"]
def test_tab_n_space():
assert split(" ") == [" "]
def test_several_spaces():
assert split(" ") == [" "]
def test_numbers():
assert split("1234") == ["1234"]
def test_several_numbers():
assert split("12 34") == ["12", " ", "34"]
def test_comma():
assert split(",") == [","]
def test_comma_with_words():
assert split("a, b") == ["a", ",", " ", "b"]
def test_dot():
assert split(".") == ["."]
def test_dot_with_word():
assert split("a.b") == ["a", ".", "b"]
def test_dot_with_words():
assert split("a.b.c") == ["a", ".", "b", ".", "c"]
def test_colon():
assert split(";") == [";"]
def test_colon_word():
assert split("pouet;") == ["pouet", ";"]
def test_assign():
assert split("a = b") == ["a", " ", "=", " ", "b"]
def test_call():
assert split("function()") == ["function", "(", ")"]
def test_call_with_arg():
assert split("function(a)") == ["function", "(", "a", ")"]
def test_call_with_args():
assert split("function(a, b, c)") == ["function", "(", "a", ",", " ", "b", ",", " ", "c", ")"]
def test_call_with_kwarg():
assert split("function(a=b)") == ["function", "(", "a", "=", "b", ")"]
def test_call_with_kwargs():
assert split("function(a=b, c= d)") == ["function", "(", "a", "=", "b", ",", " ", "c", "=", " ", "d", ")"]
def test_call_with_start_args():
assert split("function(*args)") == ["function", "(", "*", "args", ")"]
def test_call_with_start_kwargs():
assert split("function(**kwargs)") == ["function", "(", "*", "*", "kwargs", ")"]
def test_function_def():
assert split("def pouet(): pass") == ["def", " ", "pouet", "(", ")", ":", " ", "pass"]
def test_addition():
assert split("a + 2") == ["a", " ", "+", " ", "2"]
def test_substraction():
assert split("a - 2") == ["a", " ", "-", " ", "2"]
def test_multiplication():
assert split("a * 2") == ["a", " ", "*", " ", "2"]
def test_division():
assert split("a/2") == ["a", "/", "2"]
def test_power():
assert split("a**2") == ["a", "*", "*", "2"]
def test_modulo():
assert split("a % 2") == ["a", " ", "%", " ", "2"]
def test_binary_stuff():
assert split("a^2") == ["a", "^", "2"]
assert split("a&2") == ["a", "&", "2"]
assert split("a|2") == ["a", "|", "2"]
assert split("a << 2") == ["a", " ", "<", "<", " ", "2"]
assert split("a >> 2") == ["a", " ", ">", ">", " ", "2"]
def test_operator_assign():
assert split("a += 2") == ["a", " ", "+", "=", " ", "2"]
assert split("a -= 2") == ["a", " ", "-", "=", " ", "2"]
assert split("a *= 2") == ["a", " ", "*", "=", " ", "2"]
assert split("a /= 2") == ["a", " ", "/", "=", " ", "2"]
assert split("a %= 2") == ["a", " ", "%", "=", " ", "2"]
assert split("a &= 2") == ["a", " ", "&", "=", " ", "2"]
assert split("a |= 2") == ["a", " ", "|", "=", " ", "2"]
assert split("a ^= 2") == ["a", " ", "^", "=", " ", "2"]
assert split("a //= 2") == ["a", " ", "/", "/", "=", " ", "2"]
assert split("a **= 2") == ["a", " ", "*", "*", "=", " ", "2"]
assert split("a <<= 2") == ["a", " ", "<", "<", "=", " ", "2"]
assert split("a >>= 2") == ["a", " ", ">", ">", "=", " ", "2"]
def test_factor():
assert split("~a") == ["~", "a"]
def test_del_pouet():
assert split("del pouet") == ["del", " ", "pouet"]
def test_pass():
assert split("pass") == ["pass"]
def test_break():
assert split("break") == ["break"]
def test_continue():
assert split("continue") == ["continue"]
def test_return():
assert split("return") == ["return"]
def test_return_stuff():
assert split("return stuff") == ["return", " ", "stuff"]
def test_yield():
assert split("yield") == ["yield"]
def test_yield_stuff():
assert split("yield stuff") == ["yield", " ", "stuff"]
def test_raise():
assert split("raise") == ["raise"]
def test_raise_stuff():
assert split("raise Exception()") == ["raise", " ", "Exception", "(", ")"]
def test_global_stuff():
assert split("global stuff") == ["global", " ", "stuff"]
def test_exec():
assert split("exec") == ["exec"]
def test_exec_stuff():
assert split("exec stuff") == ["exec", " ", "stuff"]
def test_assert():
assert split("assert") == ["assert"]
def test_assert_stuff():
assert split("assert stuff") == ["assert", " ", "stuff"]
def test_line_end():
assert split("\n") == ["\n"]
def test_line_end_windows():
assert split("\r\n") == ["\r", "\n"]
def test_if():
assert split("if ab: pass") == ["if", " ", "ab", ":", " ", "pass"]
def test_if_elif_else():
assert split("if a:\n pass\nelif b:\n pass\nelse: \n pass") == ["if", " ", "a", ":", "\n", " ", "pass", "\n", "elif", " ", "b", ":", "\n", " ", "pass", "\n", "else", ":", " ", "\n", " ", "pass"]
def test_while():
assert split("while a: pass") == ["while", " ", "a", ":", " ", "pass"]
def test_lambda():
assert split("lambda x: x + 1") == ["lambda", " ", "x", ":", " ", "x", " ", "+", " ", "1"]
def test_for():
assert split("for a in b: pass") == ["for", " ", "a", " ", "in", " ", "b", ":", " ", "pass"]
def test_empty_list():
assert split("[]") == ["[", "]"]
def test_list():
assert split("[a, b, c]") == ["[", "a", ",", " ", "b", ",", " ", "c", "]"]
def test_empty_dict():
assert split("{}") == ["{", "}"]
def test_dict():
assert split("{a: b, c: d}") == ["{", "a", ":", " ", "b", ",", " ", "c", ":", " ", "d", "}"]
def test_not():
assert split("!a") == ["!", "a"]
def test_not_equal():
assert split("a != b") == ["a", " ", "!", "=", " ", "b"]
def test_backquote():
assert split("`a`") == ["`", "a", "`"]
def test_number_in_var():
assert split("a1") == ["a1"]
def test_comment():
assert split("# a b c d") == ["# a b c d"]
def test_comments():
assert split("# a b c d\n# pouet") == ["# a b c d", "\n", "# pouet"]
def test_empty_string():
assert split("''") == ["''"]
def test_string():
assert split("'pouet pouet'") == ["'pouet pouet'"]
def test_empty_string_other_quotes():
assert split('"pouet pouet"') == ['"pouet pouet"']
def test_multi_string():
assert split("'''pouet pouet'''") == ["'''pouet pouet'''"]
def test_multi_string_other_quotes():
assert split('"""pouet pouet"""') == ['"""pouet pouet"""']
def test_missing_quote_yields_error():
with pytest.raises(UntreatedError):
split("'")
with pytest.raises(UntreatedError):
split("'''")
with pytest.raises(UntreatedError):
split('"')
with pytest.raises(UntreatedError):
split('"""')
def test_escape():
assert split("\\\\") == ["\\", "\\"]
def test_escape_in_string():
assert split("'\\\\'") == ["'\\\\'"]
def test_other_escape_string():
assert split("'\\\\'") == ["'\\\\'"]
def test_hexa():
assert split("0x7F") == ["0x7F"]
def test_multi_string_with_same_quotes_in():
assert split('"""pouet " "" pouet"""') == ['"""pouet " "" pouet"""']
def test_comment_backslash():
assert split('# pouet \\\npouet') == ["# pouet \\", "\n", "pouet"]
def test_backslash_in_comment():
assert split("# pouet \\t pouet\npouet") == ["# pouet \\t pouet", "\n", "pouet"]
def test_regression():
assert split("(r'[\"\\'](.|\n|\r)*[\"\\']', 'STRING'),") == ["(", "r", "'[\"\\'](.|\n|\r)*[\"\\']'", ",", " ", "'STRING'", ")", ","]
# TODO: make this test pass in python3 also
# requires to remove dependency on ast.py
if python_version == 2:
def test_remove_crap():
assert split("\x0c\xef\xbb\xbf") == []
def test_split_float_notation():
assert split("a._") == ["a", ".", "_"]
|