// // Copyright (c) Microsoft. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. // using System.Threading.Tasks; using Microsoft.SqlTools.ServiceLayer.Connection; using Microsoft.SqlTools.ServiceLayer.LanguageServices; using Microsoft.SqlTools.ServiceLayer.Workspace.Contracts; using Microsoft.SqlTools.Test.Utility; using Xunit; namespace Microsoft.SqlTools.ServiceLayer.Test.LanguageServices { /// /// Tests for the ServiceHost Language Service tests /// public class LanguageServiceTests { #region "Diagnostics tests" /// /// Verify that the SQL parser correctly detects errors in text /// [Fact] public void ParseSelectStatementWithoutErrors() { // sql statement with no errors const string sqlWithErrors = "SELECT * FROM sys.objects"; // get the test service LanguageService service = TestObjects.GetTestLanguageService(); // parse the sql statement var scriptFile = new ScriptFile(); scriptFile.SetFileContents(sqlWithErrors); ScriptFileMarker[] fileMarkers = service.GetSemanticMarkers(scriptFile); // verify there are no errors Assert.Equal(0, fileMarkers.Length); } /// /// Verify that the SQL parser correctly detects errors in text /// [Fact] public void ParseSelectStatementWithError() { // sql statement with errors const string sqlWithErrors = "SELECT *** FROM sys.objects"; // get test service LanguageService service = TestObjects.GetTestLanguageService(); // parse sql statement var scriptFile = new ScriptFile(); scriptFile.SetFileContents(sqlWithErrors); ScriptFileMarker[] fileMarkers = service.GetSemanticMarkers(scriptFile); // verify there is one error Assert.Equal(1, fileMarkers.Length); // verify the position of the error Assert.Equal(9, fileMarkers[0].ScriptRegion.StartColumnNumber); Assert.Equal(1, fileMarkers[0].ScriptRegion.StartLineNumber); Assert.Equal(10, fileMarkers[0].ScriptRegion.EndColumnNumber); Assert.Equal(1, fileMarkers[0].ScriptRegion.EndLineNumber); } /// /// Verify that the SQL parser correctly detects errors in text /// [Fact] public void ParseMultilineSqlWithErrors() { // multiline sql with errors const string sqlWithErrors = "SELECT *** FROM sys.objects;\n" + "GO\n" + "SELECT *** FROM sys.objects;\n"; // get test service LanguageService service = TestObjects.GetTestLanguageService(); // parse sql var scriptFile = new ScriptFile(); scriptFile.SetFileContents(sqlWithErrors); ScriptFileMarker[] fileMarkers = service.GetSemanticMarkers(scriptFile); // verify there are two errors Assert.Equal(2, fileMarkers.Length); // check position of first error Assert.Equal(9, fileMarkers[0].ScriptRegion.StartColumnNumber); Assert.Equal(1, fileMarkers[0].ScriptRegion.StartLineNumber); Assert.Equal(10, fileMarkers[0].ScriptRegion.EndColumnNumber); Assert.Equal(1, fileMarkers[0].ScriptRegion.EndLineNumber); // check position of second error Assert.Equal(9, fileMarkers[1].ScriptRegion.StartColumnNumber); Assert.Equal(3, fileMarkers[1].ScriptRegion.StartLineNumber); Assert.Equal(10, fileMarkers[1].ScriptRegion.EndColumnNumber); Assert.Equal(3, fileMarkers[1].ScriptRegion.EndLineNumber); } #endregion #region "Autocomplete Tests" /// /// Verify that the SQL parser correctly detects errors in text /// [Fact] public async Task AutocompleteTest() { // TODO Re-enable this test once we have a way to hook up the right auto-complete and connection services. // Probably need a service provider channel so that we can mock service access. Otherwise everything accesses // static instances and cannot be properly tested. //var autocompleteService = TestObjects.GetAutoCompleteService(); //var connectionService = TestObjects.GetTestConnectionService(); //ConnectParams connectionRequest = TestObjects.GetTestConnectionParams(); //var connectionResult = connectionService.Connect(connectionRequest); //var sqlConnection = connectionService.ActiveConnections[connectionResult.ConnectionId]; //await autocompleteService.UpdateAutoCompleteCache(sqlConnection); await Task.Run(() => { return; }); } #endregion } }