test_tokenutil.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. """Tests for tokenutil"""
  2. # Copyright (c) IPython Development Team.
  3. # Distributed under the terms of the Modified BSD License.
  4. import nose.tools as nt
  5. from IPython.utils.tokenutil import token_at_cursor, line_at_cursor
  6. def expect_token(expected, cell, cursor_pos):
  7. token = token_at_cursor(cell, cursor_pos)
  8. offset = 0
  9. for line in cell.splitlines():
  10. if offset + len(line) >= cursor_pos:
  11. break
  12. else:
  13. offset += len(line)+1
  14. column = cursor_pos - offset
  15. line_with_cursor = '%s|%s' % (line[:column], line[column:])
  16. nt.assert_equal(token, expected,
  17. "Expected %r, got %r in: %r (pos %i)" % (
  18. expected, token, line_with_cursor, cursor_pos)
  19. )
  20. def test_simple():
  21. cell = "foo"
  22. for i in range(len(cell)):
  23. expect_token("foo", cell, i)
  24. def test_function():
  25. cell = "foo(a=5, b='10')"
  26. expected = 'foo'
  27. # up to `foo(|a=`
  28. for i in range(cell.find('a=') + 1):
  29. expect_token("foo", cell, i)
  30. # find foo after `=`
  31. for i in [cell.find('=') + 1, cell.rfind('=') + 1]:
  32. expect_token("foo", cell, i)
  33. # in between `5,|` and `|b=`
  34. for i in range(cell.find(','), cell.find('b=')):
  35. expect_token("foo", cell, i)
  36. def test_multiline():
  37. cell = '\n'.join([
  38. 'a = 5',
  39. 'b = hello("string", there)'
  40. ])
  41. expected = 'hello'
  42. start = cell.index(expected) + 1
  43. for i in range(start, start + len(expected)):
  44. expect_token(expected, cell, i)
  45. expected = 'hello'
  46. start = cell.index(expected) + 1
  47. for i in range(start, start + len(expected)):
  48. expect_token(expected, cell, i)
  49. def test_multiline_token():
  50. cell = '\n'.join([
  51. '"""\n\nxxxxxxxxxx\n\n"""',
  52. '5, """',
  53. 'docstring',
  54. 'multiline token',
  55. '""", [',
  56. '2, 3, "complicated"]',
  57. 'b = hello("string", there)'
  58. ])
  59. expected = 'hello'
  60. start = cell.index(expected) + 1
  61. for i in range(start, start + len(expected)):
  62. expect_token(expected, cell, i)
  63. expected = 'hello'
  64. start = cell.index(expected) + 1
  65. for i in range(start, start + len(expected)):
  66. expect_token(expected, cell, i)
  67. def test_nested_call():
  68. cell = "foo(bar(a=5), b=10)"
  69. expected = 'foo'
  70. start = cell.index('bar') + 1
  71. for i in range(start, start + 3):
  72. expect_token(expected, cell, i)
  73. expected = 'bar'
  74. start = cell.index('a=')
  75. for i in range(start, start + 3):
  76. expect_token(expected, cell, i)
  77. expected = 'foo'
  78. start = cell.index(')') + 1
  79. for i in range(start, len(cell)-1):
  80. expect_token(expected, cell, i)
  81. def test_attrs():
  82. cell = "a = obj.attr.subattr"
  83. expected = 'obj'
  84. idx = cell.find('obj') + 1
  85. for i in range(idx, idx + 3):
  86. expect_token(expected, cell, i)
  87. idx = cell.find('.attr') + 2
  88. expected = 'obj.attr'
  89. for i in range(idx, idx + 4):
  90. expect_token(expected, cell, i)
  91. idx = cell.find('.subattr') + 2
  92. expected = 'obj.attr.subattr'
  93. for i in range(idx, len(cell)):
  94. expect_token(expected, cell, i)
  95. def test_line_at_cursor():
  96. cell = ""
  97. (line, offset) = line_at_cursor(cell, cursor_pos=11)
  98. assert line == "", ("Expected '', got %r" % line)
  99. assert offset == 0, ("Expected '', got %r" % line)
  100. def test_muliline_statement():
  101. cell = """a = (1,
  102. 3)
  103. int()
  104. map()
  105. """
  106. for c in range(16, 22):
  107. yield lambda: expect_token("int", cell, c)
  108. for c in range(22, 28):
  109. yield lambda: expect_token("map", cell, c)