messages_request_test.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 ycm.tests.test_utils import MockVimModule
  18. MockVimModule()
  19. from hamcrest import assert_that, equal_to
  20. from unittest import TestCase
  21. from unittest.mock import patch, call
  22. from ycm.client.messages_request import _HandlePollResponse
  23. from ycm.tests.test_utils import ExtendedMock
  24. class MessagesRequestTest( TestCase ):
  25. def test_HandlePollResponse_NoMessages( self ):
  26. assert_that( _HandlePollResponse( True, None ), equal_to( True ) )
  27. # Other non-False responses mean the same thing
  28. assert_that( _HandlePollResponse( '', None ), equal_to( True ) )
  29. assert_that( _HandlePollResponse( 1, None ), equal_to( True ) )
  30. assert_that( _HandlePollResponse( {}, None ), equal_to( True ) )
  31. def test_HandlePollResponse_PollingNotSupported( self ):
  32. assert_that( _HandlePollResponse( False, None ), equal_to( False ) )
  33. # 0 is not False
  34. assert_that( _HandlePollResponse( 0, None ), equal_to( True ) )
  35. @patch( 'ycm.client.messages_request.PostVimMessage',
  36. new_callable = ExtendedMock )
  37. def test_HandlePollResponse_SingleMessage( self, post_vim_message ):
  38. assert_that( _HandlePollResponse( [ { 'message': 'this is a message' } ] ,
  39. None ),
  40. equal_to( True ) )
  41. post_vim_message.assert_has_exact_calls( [
  42. call( 'this is a message', warning=False, truncate=True )
  43. ] )
  44. @patch( 'ycm.client.messages_request.PostVimMessage',
  45. new_callable = ExtendedMock )
  46. def test_HandlePollResponse_MultipleMessages( self, post_vim_message ):
  47. assert_that( _HandlePollResponse( [ { 'message': 'this is a message' },
  48. { 'message': 'this is another one' } ] ,
  49. None ),
  50. equal_to( True ) )
  51. post_vim_message.assert_has_exact_calls( [
  52. call( 'this is a message', warning=False, truncate=True ),
  53. call( 'this is another one', warning=False, truncate=True )
  54. ] )
  55. def test_HandlePollResponse_SingleDiagnostic( self ):
  56. diagnostics_handler = ExtendedMock()
  57. messages = [
  58. { 'filepath': 'foo', 'diagnostics': [ 'PLACEHOLDER' ] },
  59. ]
  60. assert_that( _HandlePollResponse( messages, diagnostics_handler ),
  61. equal_to( True ) )
  62. diagnostics_handler.UpdateWithNewDiagnosticsForFile.assert_has_exact_calls(
  63. [
  64. call( 'foo', [ 'PLACEHOLDER' ] )
  65. ] )
  66. def test_HandlePollResponse_MultipleDiagnostics( self ):
  67. diagnostics_handler = ExtendedMock()
  68. messages = [
  69. { 'filepath': 'foo', 'diagnostics': [ 'PLACEHOLDER1' ] },
  70. { 'filepath': 'bar', 'diagnostics': [ 'PLACEHOLDER2' ] },
  71. { 'filepath': 'baz', 'diagnostics': [ 'PLACEHOLDER3' ] },
  72. { 'filepath': 'foo', 'diagnostics': [ 'PLACEHOLDER4' ] },
  73. ]
  74. assert_that( _HandlePollResponse( messages, diagnostics_handler ),
  75. equal_to( True ) )
  76. diagnostics_handler.UpdateWithNewDiagnosticsForFile.assert_has_exact_calls(
  77. [
  78. call( 'foo', [ 'PLACEHOLDER1' ] ),
  79. call( 'bar', [ 'PLACEHOLDER2' ] ),
  80. call( 'baz', [ 'PLACEHOLDER3' ] ),
  81. call( 'foo', [ 'PLACEHOLDER4' ] )
  82. ] )
  83. @patch( 'ycm.client.messages_request.PostVimMessage',
  84. new_callable = ExtendedMock )
  85. def test_HandlePollResponse_MultipleMessagesAndDiagnostics(
  86. self, post_vim_message ):
  87. diagnostics_handler = ExtendedMock()
  88. messages = [
  89. { 'filepath': 'foo', 'diagnostics': [ 'PLACEHOLDER1' ] },
  90. { 'message': 'On the first day of Christmas, my VimScript gave to me' },
  91. { 'filepath': 'bar', 'diagnostics': [ 'PLACEHOLDER2' ] },
  92. { 'message': 'A test file in a Command-T' },
  93. { 'filepath': 'baz', 'diagnostics': [ 'PLACEHOLDER3' ] },
  94. { 'message': 'On the second day of Christmas, my VimScript gave to me' },
  95. { 'filepath': 'foo', 'diagnostics': [ 'PLACEHOLDER4' ] },
  96. { 'message': 'Two popup menus, and a test file in a Command-T' },
  97. ]
  98. assert_that( _HandlePollResponse( messages, diagnostics_handler ),
  99. equal_to( True ) )
  100. diagnostics_handler.UpdateWithNewDiagnosticsForFile.assert_has_exact_calls(
  101. [
  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. ] )