1# Copyright (C) 2015-2020 ycmd contributors
2#
3# This file is part of ycmd.
4#
5# ycmd 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# ycmd 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 ycmd.  If not, see <http://www.gnu.org/licenses/>.
17
18from hamcrest import ( assert_that,
19                       calling,
20                       empty,
21                       has_entries,
22                       has_items,
23                       raises )
24from webtest import AppError
25
26from ycmd.tests.cs import PathToTestFile, SharedYcmd, WrapOmniSharpServer
27from ycmd.tests.test_utils import BuildRequest, CompletionEntryMatcher
28from ycmd.utils import ReadFile
29
30
31@SharedYcmd
32def GetCompletions_DefaultToIdentifier_test( app ):
33  filepath = PathToTestFile( 'testy', 'Program.cs' )
34  with WrapOmniSharpServer( app, filepath ):
35    contents = ReadFile( filepath )
36
37    completion_data = BuildRequest( filepath = filepath,
38                                    filetype = 'cs',
39                                    contents = contents,
40                                    line_num = 10,
41                                    column_num = 7 )
42    response_data = app.post_json( '/completions', completion_data ).json
43    print( 'Response: ', response_data )
44    assert_that(
45      response_data,
46      has_entries( {
47        'completion_start_column': 4,
48        'completions': has_items(
49          CompletionEntryMatcher( 'Console', '[ID]' ),
50        ),
51        'errors': empty(),
52      } ) )
53
54
55@SharedYcmd
56def GetCompletions_Basic_test( app ):
57  filepath = PathToTestFile( 'testy', 'Program.cs' )
58  with WrapOmniSharpServer( app, filepath ):
59    contents = ReadFile( filepath )
60
61    completion_data = BuildRequest( filepath = filepath,
62                                    filetype = 'cs',
63                                    contents = contents,
64                                    line_num = 10,
65                                    column_num = 12 )
66    response_data = app.post_json( '/completions', completion_data ).json
67    print( 'Response: ', response_data )
68    assert_that(
69      response_data,
70      has_entries( {
71        'completion_start_column': 12,
72        'completions': has_items(
73          CompletionEntryMatcher( 'CursorLeft',
74                                  'CursorLeft',
75                                  { 'kind': 'Property' } ),
76          CompletionEntryMatcher( 'CursorSize',
77                                  'CursorSize',
78                                  { 'kind': 'Property' } ),
79        ),
80        'errors': empty(),
81      } ) )
82
83
84@SharedYcmd
85def GetCompletions_Unicode_test( app ):
86  filepath = PathToTestFile( 'testy', 'Unicode.cs' )
87  with WrapOmniSharpServer( app, filepath ):
88    contents = ReadFile( filepath )
89
90    completion_data = BuildRequest( filepath = filepath,
91                                    filetype = 'cs',
92                                    contents = contents,
93                                    line_num = 43,
94                                    column_num = 26 )
95    response_data = app.post_json( '/completions', completion_data ).json
96    assert_that( response_data,
97                 has_entries( {
98                    'completion_start_column': 26,
99                    'completions': has_items(
100                      CompletionEntryMatcher( 'DoATest' ),
101                      CompletionEntryMatcher( 'an_int' ),
102                      CompletionEntryMatcher( 'a_unicøde' ),
103                      CompletionEntryMatcher( 'øøø' ),
104                    ),
105                    'errors': empty(),
106                  } ) )
107
108
109@SharedYcmd
110def GetCompletions_MultipleSolution_test( app ):
111  filepaths = [ PathToTestFile( 'testy', 'Program.cs' ),
112                PathToTestFile( 'testy-multiple-solutions',
113                                'solution-named-like-folder',
114                                'testy',
115                                'Program.cs' ) ]
116  for filepath in filepaths:
117    with WrapOmniSharpServer( app, filepath ):
118      contents = ReadFile( filepath )
119
120      completion_data = BuildRequest( filepath = filepath,
121                                      filetype = 'cs',
122                                      contents = contents,
123                                      line_num = 10,
124                                      column_num = 12 )
125      response_data = app.post_json( '/completions',
126                                     completion_data ).json
127
128      print( 'Response: ', response_data )
129      assert_that(
130        response_data,
131        has_entries( {
132          'completion_start_column': 12,
133          'completions': has_items(
134            CompletionEntryMatcher( 'CursorLeft',
135                                    'CursorLeft',
136                                    { 'kind': 'Property' } ),
137            CompletionEntryMatcher( 'CursorSize',
138                                    'CursorSize',
139                                    { 'kind': 'Property' } ),
140          ),
141          'errors': empty(),
142        } ) )
143
144
145@SharedYcmd
146def GetCompletions_PathWithSpace_test( app ):
147  filepath = PathToTestFile( 'неприличное слово', 'Program.cs' )
148  with WrapOmniSharpServer( app, filepath ):
149    contents = ReadFile( filepath )
150
151    completion_data = BuildRequest( filepath = filepath,
152                                    filetype = 'cs',
153                                    contents = contents,
154                                    line_num = 9,
155                                    column_num = 12 )
156    response_data = app.post_json( '/completions', completion_data ).json
157    print( 'Response: ', response_data )
158    assert_that(
159      response_data,
160      has_entries( {
161        'completion_start_column': 12,
162        'completions': has_items(
163          CompletionEntryMatcher( 'CursorLeft',
164                                  'CursorLeft',
165                                  { 'kind': 'Property' } ),
166          CompletionEntryMatcher( 'CursorSize',
167                                  'CursorSize',
168                                  { 'kind': 'Property' } ),
169        ),
170        'errors': empty(),
171      } ) )
172
173
174@SharedYcmd
175def GetCompletions_DoesntStartWithAmbiguousMultipleSolutions_test( app ):
176  filepath = PathToTestFile( 'testy-multiple-solutions',
177                             'solution-not-named-like-folder',
178                             'testy', 'Program.cs' )
179  contents = ReadFile( filepath )
180  event_data = BuildRequest( filepath = filepath,
181                             filetype = 'cs',
182                             contents = contents,
183                             event_name = 'FileReadyToParse' )
184
185  assert_that(
186    calling( app.post_json ).with_args( '/event_notification', event_data ),
187    raises( AppError, 'Autodetection of solution file failed' ),
188    "The Omnisharp server started, despite us not being able to find a "
189    "suitable solution file to feed it. Did you fiddle with the solution "
190    "finding code in cs_completer.py? Hopefully you've enhanced it: you need "
191    "to update this test then :)" )
192
193
194def Dummy_test():
195  # Workaround for https://github.com/pytest-dev/pytest-rerunfailures/issues/51
196  assert True
197