mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-13 17:23:02 -05:00
Add tests for the language service diagnostics
This commit is contained in:
@@ -106,6 +106,13 @@ namespace Microsoft.SqlTools.EditorServices
|
|||||||
|
|
||||||
#region Constructors
|
#region Constructors
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Add a default constructor for testing
|
||||||
|
/// </summary>
|
||||||
|
public ScriptFile()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new ScriptFile instance by reading file contents from
|
/// Creates a new ScriptFile instance by reading file contents from
|
||||||
/// the given TextReader.
|
/// the given TextReader.
|
||||||
@@ -433,11 +440,11 @@ namespace Microsoft.SqlTools.EditorServices
|
|||||||
return new BufferRange(startPosition, endPosition);
|
return new BufferRange(startPosition, endPosition);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
/// <summary>
|
||||||
|
/// Set the script files contents
|
||||||
#region Private Methods
|
/// </summary>
|
||||||
|
/// <param name="fileContents"></param>
|
||||||
private void SetFileContents(string fileContents)
|
public void SetFileContents(string fileContents)
|
||||||
{
|
{
|
||||||
// Split the file contents into lines and trim
|
// Split the file contents into lines and trim
|
||||||
// any carriage returns from the strings.
|
// any carriage returns from the strings.
|
||||||
@@ -451,6 +458,10 @@ namespace Microsoft.SqlTools.EditorServices
|
|||||||
this.ParseFileContents();
|
this.ParseFileContents();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Private Methods
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Parses the current file contents to get the AST, tokens,
|
/// Parses the current file contents to get the AST, tokens,
|
||||||
/// and parse errors.
|
/// and parse errors.
|
||||||
|
|||||||
116
test/ServiceHost.Test/LanguageServer/LanguageServiceTests.cs
Normal file
116
test/ServiceHost.Test/LanguageServer/LanguageServiceTests.cs
Normal file
@@ -0,0 +1,116 @@
|
|||||||
|
//
|
||||||
|
// Copyright (c) Microsoft. All rights reserved.
|
||||||
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||||
|
//
|
||||||
|
|
||||||
|
using Microsoft.SqlTools.EditorServices;
|
||||||
|
using Microsoft.SqlTools.EditorServices.Session;
|
||||||
|
using Microsoft.SqlTools.LanguageSupport;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace Microsoft.SqlTools.Test.LanguageServer
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Tests for the ServiceHost Language Service tests
|
||||||
|
/// </summary>
|
||||||
|
public class LanguageServiceTests
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Create a test language service instance
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
private LanguageService CreateTestService()
|
||||||
|
{
|
||||||
|
return new LanguageService(new SqlToolsContext(null, null));
|
||||||
|
}
|
||||||
|
|
||||||
|
#region "Diagnostics tests"
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Verify that the SQL parser correctly detects errors in text
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
public void ParseSelectStatementWithoutErrors()
|
||||||
|
{
|
||||||
|
// sql statement with no errors
|
||||||
|
const string sqlWithErrors = "SELECT * FROM sys.objects";
|
||||||
|
|
||||||
|
// get the test service
|
||||||
|
LanguageService service = CreateTestService();
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Verify that the SQL parser correctly detects errors in text
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
public void ParseSelectStatementWithError()
|
||||||
|
{
|
||||||
|
// sql statement with errors
|
||||||
|
const string sqlWithErrors = "SELECT *** FROM sys.objects";
|
||||||
|
|
||||||
|
// get test service
|
||||||
|
LanguageService service = CreateTestService();
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Verify that the SQL parser correctly detects errors in text
|
||||||
|
/// </summary>
|
||||||
|
[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 = CreateTestService();
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user