diagnostic_interface_test.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. # Copyright (C) 2015-2018 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 import diagnostic_interface
  18. from ycm.tests.test_utils import VimBuffer, MockVimModule, MockVimBuffers
  19. from hamcrest import ( assert_that,
  20. contains_exactly,
  21. equal_to,
  22. has_entries,
  23. has_item )
  24. from unittest import TestCase
  25. MockVimModule()
  26. def SimpleDiagnosticToJson( start_line, start_col, end_line, end_col ):
  27. return {
  28. 'kind': 'ERROR',
  29. 'location': { 'line_num': start_line, 'column_num': start_col },
  30. 'location_extent': {
  31. 'start': {
  32. 'line_num': start_line,
  33. 'column_num': start_col
  34. },
  35. 'end': {
  36. 'line_num': end_line,
  37. 'column_num': end_col
  38. }
  39. },
  40. 'ranges': [
  41. {
  42. 'start': {
  43. 'line_num': start_line,
  44. 'column_num': start_col
  45. },
  46. 'end': {
  47. 'line_num': end_line,
  48. 'column_num': end_col
  49. }
  50. }
  51. ]
  52. }
  53. def SimpleDiagnosticToJsonWithInvalidLineNum( start_line, start_col,
  54. end_line, end_col ):
  55. return {
  56. 'kind': 'ERROR',
  57. 'location': { 'line_num': start_line, 'column_num': start_col },
  58. 'location_extent': {
  59. 'start': {
  60. 'line_num': start_line,
  61. 'column_num': start_col
  62. },
  63. 'end': {
  64. 'line_num': end_line,
  65. 'column_num': end_col
  66. }
  67. },
  68. 'ranges': [
  69. {
  70. 'start': {
  71. 'line_num': 0,
  72. 'column_num': 0
  73. },
  74. 'end': {
  75. 'line_num': 0,
  76. 'column_num': 0
  77. }
  78. },
  79. {
  80. 'start': {
  81. 'line_num': start_line,
  82. 'column_num': start_col
  83. },
  84. 'end': {
  85. 'line_num': end_line,
  86. 'column_num': end_col
  87. }
  88. }
  89. ]
  90. }
  91. def YcmTextPropertyTupleMatcher( start_line, start_col, end_line, end_col ):
  92. return has_item( contains_exactly(
  93. start_line,
  94. start_col,
  95. 'YcmErrorProperty',
  96. has_entries( { 'end_col': end_col, 'end_lnum': end_line } ) ) )
  97. class DiagnosticInterfaceTest( TestCase ):
  98. def test_ConvertDiagnosticToTextProperties( self ):
  99. for diag, contents, result in [
  100. # Error in middle of the line
  101. [
  102. SimpleDiagnosticToJson( 1, 16, 1, 23 ),
  103. [ 'Highlight this error please' ],
  104. YcmTextPropertyTupleMatcher( 1, 16, 1, 23 )
  105. ],
  106. # Error at the end of the line
  107. [
  108. SimpleDiagnosticToJson( 1, 16, 1, 21 ),
  109. [ 'Highlight this warning' ],
  110. YcmTextPropertyTupleMatcher( 1, 16, 1, 21 )
  111. ],
  112. [
  113. SimpleDiagnosticToJson( 1, 16, 1, 19 ),
  114. [ 'Highlight unicøde' ],
  115. YcmTextPropertyTupleMatcher( 1, 16, 1, 19 )
  116. ],
  117. # Non-positive position
  118. [
  119. SimpleDiagnosticToJson( 0, 0, 0, 0 ),
  120. [ 'Some contents' ],
  121. {}
  122. ],
  123. [
  124. SimpleDiagnosticToJson( -1, -2, -3, -4 ),
  125. [ 'Some contents' ],
  126. YcmTextPropertyTupleMatcher( 1, 1, 1, 1 )
  127. ],
  128. ]:
  129. with self.subTest( diag = diag, contents = contents, result = result ):
  130. current_buffer = VimBuffer( 'foo', number = 1, contents = [ '' ] )
  131. target_buffer = VimBuffer( 'bar', number = 2, contents = contents )
  132. with MockVimBuffers( [ current_buffer, target_buffer ],
  133. [ current_buffer, target_buffer ] ):
  134. actual = diagnostic_interface._ConvertDiagnosticToTextProperties(
  135. target_buffer.number,
  136. diag )
  137. print( actual )
  138. assert_that( actual, result )
  139. def test_ConvertDiagnosticWithInvalidLineNum( self ):
  140. for diag, contents, result in [
  141. # Error in middle of the line
  142. [
  143. SimpleDiagnosticToJsonWithInvalidLineNum( 1, 16, 1, 23 ),
  144. [ 'Highlight this error please' ],
  145. YcmTextPropertyTupleMatcher( 1, 16, 1, 23 )
  146. ],
  147. # Error at the end of the line
  148. [
  149. SimpleDiagnosticToJsonWithInvalidLineNum( 1, 16, 1, 21 ),
  150. [ 'Highlight this warning' ],
  151. YcmTextPropertyTupleMatcher( 1, 16, 1, 21 )
  152. ],
  153. [
  154. SimpleDiagnosticToJsonWithInvalidLineNum( 1, 16, 1, 19 ),
  155. [ 'Highlight unicøde' ],
  156. YcmTextPropertyTupleMatcher( 1, 16, 1, 19 )
  157. ],
  158. ]:
  159. with self.subTest( diag = diag, contents = contents, result = result ):
  160. current_buffer = VimBuffer( 'foo', number = 1, contents = [ '' ] )
  161. target_buffer = VimBuffer( 'bar', number = 2, contents = contents )
  162. with MockVimBuffers( [ current_buffer, target_buffer ],
  163. [ current_buffer, target_buffer ] ):
  164. actual = diagnostic_interface._ConvertDiagnosticToTextProperties(
  165. target_buffer.number,
  166. diag )
  167. print( actual )
  168. assert_that( actual, result )
  169. def test_IsValidRange( self ):
  170. for start_line, start_col, end_line, end_col, expect in (
  171. ( 1, 1, 1, 1, True ),
  172. ( 1, 1, 0, 0, False ),
  173. ( 1, 1, 2, 1, True ),
  174. ( 1, 2, 2, 1, True ),
  175. ( 2, 1, 1, 1, False ),
  176. ( 2, 2, 2, 1, False ),
  177. ):
  178. with self.subTest( start=( start_line, start_col ),
  179. end=( end_line, end_col ),
  180. expect = expect ):
  181. assert_that( diagnostic_interface._IsValidRange( start_line,
  182. start_col,
  183. end_line,
  184. end_col ),
  185. equal_to( expect ) )