diagnostics_test.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #!/usr/bin/env python
  2. #
  3. # Copyright (C) 2013 Strahinja Val Markovic <val@markovic.io>
  4. #
  5. # This file is part of YouCompleteMe.
  6. #
  7. # YouCompleteMe is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # YouCompleteMe is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
  19. from ..server_utils import SetUpPythonPath
  20. SetUpPythonPath()
  21. from .test_utils import Setup, BuildRequest
  22. from webtest import TestApp
  23. from nose.tools import with_setup, eq_
  24. from hamcrest import ( assert_that, contains, contains_string, has_entries,
  25. has_entry, empty )
  26. from ..responses import NoDiagnosticSupport
  27. from .. import handlers
  28. import bottle
  29. import httplib
  30. bottle.debug( True )
  31. @with_setup( Setup )
  32. def Diagnostics_ClangCompleter_ZeroBasedLineAndColumn_test():
  33. app = TestApp( handlers.app )
  34. contents = """
  35. void foo() {
  36. double baz = "foo";
  37. }
  38. // Padding to 5 lines
  39. // Padding to 5 lines
  40. """
  41. event_data = BuildRequest( compilation_flags = ['-x', 'c++'],
  42. event_name = 'FileReadyToParse',
  43. contents = contents,
  44. filetype = 'cpp' )
  45. results = app.post_json( '/event_notification', event_data ).json
  46. assert_that( results,
  47. contains(
  48. has_entries( {
  49. 'text': contains_string( 'cannot initialize' ),
  50. 'ranges': contains( has_entries( {
  51. 'start': has_entries( {
  52. 'line_num': 2,
  53. 'column_num': 15,
  54. } ),
  55. 'end': has_entries( {
  56. 'line_num': 2,
  57. 'column_num': 20,
  58. } ),
  59. } ) ),
  60. 'location': has_entries( {
  61. 'line_num': 2,
  62. 'column_num': 9
  63. } ),
  64. 'location_extent': has_entries( {
  65. 'start': has_entries( {
  66. 'line_num': 2,
  67. 'column_num': 9,
  68. } ),
  69. 'end': has_entries( {
  70. 'line_num': 2,
  71. 'column_num': 12,
  72. } ),
  73. } )
  74. } ) ) )
  75. @with_setup( Setup )
  76. def Diagnostics_ClangCompleter_SimpleLocationExtent_test():
  77. app = TestApp( handlers.app )
  78. contents = """
  79. void foo() {
  80. baz = 5;
  81. }
  82. // Padding to 5 lines
  83. // Padding to 5 lines
  84. """
  85. event_data = BuildRequest( compilation_flags = ['-x', 'c++'],
  86. event_name = 'FileReadyToParse',
  87. contents = contents,
  88. filetype = 'cpp' )
  89. results = app.post_json( '/event_notification', event_data ).json
  90. assert_that( results,
  91. contains(
  92. has_entries( {
  93. 'location_extent': has_entries( {
  94. 'start': has_entries( {
  95. 'line_num': 2,
  96. 'column_num': 2,
  97. } ),
  98. 'end': has_entries( {
  99. 'line_num': 2,
  100. 'column_num': 5,
  101. } ),
  102. } )
  103. } ) ) )
  104. @with_setup( Setup )
  105. def Diagnostics_ClangCompleter_PragmaOnceWarningIgnored_test():
  106. app = TestApp( handlers.app )
  107. contents = """
  108. #pragma once
  109. struct Foo {
  110. int x;
  111. int y;
  112. int c;
  113. int d;
  114. };
  115. """
  116. event_data = BuildRequest( compilation_flags = ['-x', 'c++'],
  117. event_name = 'FileReadyToParse',
  118. contents = contents,
  119. filepath = '/foo.h',
  120. filetype = 'cpp' )
  121. response = app.post_json( '/event_notification', event_data )
  122. assert_that( response.body, empty() )
  123. @with_setup( Setup )
  124. def GetDetailedDiagnostic_ClangCompleter_Works_test():
  125. app = TestApp( handlers.app )
  126. contents = """
  127. struct Foo {
  128. int x // semicolon missing here!
  129. int y;
  130. int c;
  131. int d;
  132. };
  133. """
  134. diag_data = BuildRequest( compilation_flags = ['-x', 'c++'],
  135. line_num = 2,
  136. contents = contents,
  137. filetype = 'cpp' )
  138. event_data = diag_data.copy()
  139. event_data.update( {
  140. 'event_name': 'FileReadyToParse',
  141. } )
  142. app.post_json( '/event_notification', event_data )
  143. results = app.post_json( '/detailed_diagnostic', diag_data ).json
  144. assert_that( results,
  145. has_entry( 'message', contains_string( "expected ';'" ) ) )
  146. @with_setup( Setup )
  147. def GetDetailedDiagnostic_JediCompleter_DoesntWork_test():
  148. app = TestApp( handlers.app )
  149. diag_data = BuildRequest( contents = "foo = 5",
  150. line_num = 1,
  151. filetype = 'python' )
  152. response = app.post_json( '/detailed_diagnostic',
  153. diag_data,
  154. expect_errors = True )
  155. eq_( response.status_code, httplib.INTERNAL_SERVER_ERROR )
  156. assert_that( response.json,
  157. has_entry( 'exception',
  158. has_entry( 'TYPE', NoDiagnosticSupport.__name__ ) ) )