messages_request_test.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. # Copyright (C) 2017 YouCompleteMe Contributors
  2. #
  3. # This file is part of YouCompleteMe.
  4. #
  5. # YouCompleteMe is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # YouCompleteMe is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
  17. from __future__ import unicode_literals
  18. from __future__ import print_function
  19. from __future__ import division
  20. from __future__ import absolute_import
  21. # Not installing aliases from python-future; it's unreliable and slow.
  22. from builtins import * # noqa
  23. from ycm.tests.test_utils import MockVimModule
  24. MockVimModule()
  25. from hamcrest import assert_that, equal_to
  26. from mock import patch, call
  27. from ycm.client.messages_request import _HandlePollResponse
  28. from ycm.tests.test_utils import ExtendedMock
  29. def HandlePollResponse_NoMessages_test():
  30. assert_that( _HandlePollResponse( True, None ), equal_to( True ) )
  31. # Other non-False responses mean the same thing
  32. assert_that( _HandlePollResponse( '', None ), equal_to( True ) )
  33. assert_that( _HandlePollResponse( 1, None ), equal_to( True ) )
  34. assert_that( _HandlePollResponse( {}, None ), equal_to( True ) )
  35. def HandlePollResponse_PollingNotSupported_test():
  36. assert_that( _HandlePollResponse( False, None ), equal_to( False ) )
  37. # 0 is not False
  38. assert_that( _HandlePollResponse( 0, None ), equal_to( True ) )
  39. @patch( 'ycm.client.messages_request.PostVimMessage',
  40. new_callable = ExtendedMock )
  41. def HandlePollResponse_SingleMessage_test( post_vim_message ):
  42. assert_that( _HandlePollResponse( [ { 'message': 'this is a message' } ] ,
  43. None ),
  44. equal_to( True ) )
  45. post_vim_message.assert_has_exact_calls( [
  46. call( 'this is a message', warning=False, truncate=True )
  47. ] )
  48. @patch( 'ycm.client.messages_request.PostVimMessage',
  49. new_callable = ExtendedMock )
  50. def HandlePollResponse_MultipleMessages_test( post_vim_message ):
  51. assert_that( _HandlePollResponse( [ { 'message': 'this is a message' },
  52. { 'message': 'this is another one' } ] ,
  53. None ),
  54. equal_to( True ) )
  55. post_vim_message.assert_has_exact_calls( [
  56. call( 'this is a message', warning=False, truncate=True ),
  57. call( 'this is another one', warning=False, truncate=True )
  58. ] )
  59. def HandlePollResponse_SingleDiagnostic_test():
  60. diagnostics_handler = ExtendedMock()
  61. messages = [
  62. { 'filepath': 'foo', 'diagnostics': [ 'PLACEHOLDER' ] },
  63. ]
  64. assert_that( _HandlePollResponse( messages, diagnostics_handler ),
  65. equal_to( True ) )
  66. diagnostics_handler.UpdateWithNewDiagnosticsForFile.assert_has_exact_calls( [
  67. call( 'foo', [ 'PLACEHOLDER' ] )
  68. ] )
  69. def HandlePollResponse_MultipleDiagnostics_test():
  70. diagnostics_handler = ExtendedMock()
  71. messages = [
  72. { 'filepath': 'foo', 'diagnostics': [ 'PLACEHOLDER1' ] },
  73. { 'filepath': 'bar', 'diagnostics': [ 'PLACEHOLDER2' ] },
  74. { 'filepath': 'baz', 'diagnostics': [ 'PLACEHOLDER3' ] },
  75. { 'filepath': 'foo', 'diagnostics': [ 'PLACEHOLDER4' ] },
  76. ]
  77. assert_that( _HandlePollResponse( messages, diagnostics_handler ),
  78. equal_to( True ) )
  79. diagnostics_handler.UpdateWithNewDiagnosticsForFile.assert_has_exact_calls( [
  80. call( 'foo', [ 'PLACEHOLDER1' ] ),
  81. call( 'bar', [ 'PLACEHOLDER2' ] ),
  82. call( 'baz', [ 'PLACEHOLDER3' ] ),
  83. call( 'foo', [ 'PLACEHOLDER4' ] )
  84. ] )
  85. @patch( 'ycm.client.messages_request.PostVimMessage',
  86. new_callable = ExtendedMock )
  87. def HandlePollResponse_MultipleMessagesAndDiagnostics_test( post_vim_message ):
  88. diagnostics_handler = ExtendedMock()
  89. messages = [
  90. { 'filepath': 'foo', 'diagnostics': [ 'PLACEHOLDER1' ] },
  91. { 'message': 'On the first day of Christmas, my VimScript gave to me' },
  92. { 'filepath': 'bar', 'diagnostics': [ 'PLACEHOLDER2' ] },
  93. { 'message': 'A test file in a Command-T' },
  94. { 'filepath': 'baz', 'diagnostics': [ 'PLACEHOLDER3' ] },
  95. { 'message': 'On the second day of Christmas, my VimScript gave to me' },
  96. { 'filepath': 'foo', 'diagnostics': [ 'PLACEHOLDER4' ] },
  97. { 'message': 'Two popup menus, and a test file in a Command-T' },
  98. ]
  99. assert_that( _HandlePollResponse( messages, diagnostics_handler ),
  100. equal_to( True ) )
  101. diagnostics_handler.UpdateWithNewDiagnosticsForFile.assert_has_exact_calls( [
  102. call( 'foo', [ 'PLACEHOLDER1' ] ),
  103. call( 'bar', [ 'PLACEHOLDER2' ] ),
  104. call( 'baz', [ 'PLACEHOLDER3' ] ),
  105. call( 'foo', [ 'PLACEHOLDER4' ] )
  106. ] )
  107. post_vim_message.assert_has_exact_calls( [
  108. call( 'On the first day of Christmas, my VimScript gave to me',
  109. warning=False,
  110. truncate=True ),
  111. call( 'A test file in a Command-T', warning=False, truncate=True ),
  112. call( 'On the second day of Christmas, my VimScript gave to me',
  113. warning=False,
  114. truncate=True ),
  115. call( 'Two popup menus, and a test file in a Command-T',
  116. warning=False,
  117. truncate=True ),
  118. ] )