Add text utils unit tests and parameter comments. (#192)

* Add unit tests for PositionOfCursor

* Add unit tests for PositionOfCursor
This commit is contained in:
Karl Burtram
2016-12-15 14:22:39 -08:00
committed by GitHub
parent 5d2bf7fbe4
commit 0d101cf598
2 changed files with 49 additions and 9 deletions

View File

@@ -11,17 +11,17 @@ namespace Microsoft.SqlTools.ServiceLayer.Utility
/// Find the position of the cursor in the SQL script content buffer and return previous new line position /// Find the position of the cursor in the SQL script content buffer and return previous new line position
/// </summary> /// </summary>
/// <param name="sql"></param> /// <param name="sql"></param>
/// <param name="startRow"></param> /// <param name="startRow">parameter is 0-based</param>
/// <param name="startColumn"></param> /// <param name="startColumn">parameter is 0-based</param>
/// <param name="prevNewLine"></param> /// <param name="prevNewLine">parameter is 0-based</param>
public static int PositionOfCursor(string sql, int startRow, int startColumn, out int prevNewLine) public static int PositionOfCursor(string sql, int startRow, int startColumn, out int prevNewLine)
{ {
prevNewLine = 0; prevNewLine = 0;
if (string.IsNullOrWhiteSpace(sql)) if (string.IsNullOrWhiteSpace(sql))
{ {
return 1; return 1;
} }
for (int i = 0; i < startRow; ++i) for (int i = 0; i < startRow; ++i)
{ {
while (prevNewLine < sql.Length && sql[prevNewLine] != '\n') while (prevNewLine < sql.Length && sql[prevNewLine] != '\n')
@@ -39,8 +39,8 @@ namespace Microsoft.SqlTools.ServiceLayer.Utility
/// SQL Parser may have similar functionality in which case we'll delete this method. /// SQL Parser may have similar functionality in which case we'll delete this method.
/// </summary> /// </summary>
/// <param name="sql"></param> /// <param name="sql"></param>
/// <param name="startRow"></param> /// <param name="startRow">parameter is 0-based</param>
/// <param name="startColumn"></param> /// <param name="startColumn">parameter is 0-based</param>
/// <param name="tokenText"></param> /// <param name="tokenText"></param>
public static int PositionOfPrevDelimeter(string sql, int startRow, int startColumn) public static int PositionOfPrevDelimeter(string sql, int startRow, int startColumn)
{ {
@@ -67,8 +67,8 @@ namespace Microsoft.SqlTools.ServiceLayer.Utility
/// Find the position of the next delimeter for autocomplete token replacement. /// Find the position of the next delimeter for autocomplete token replacement.
/// </summary> /// </summary>
/// <param name="sql"></param> /// <param name="sql"></param>
/// <param name="startRow"></param> /// <param name="startRow">parameter is 0-based</param>
/// <param name="startColumn"></param> /// <param name="startColumn">parameter is 0-based</param>
public static int PositionOfNextDelimeter(string sql, int startRow, int startColumn) public static int PositionOfNextDelimeter(string sql, int startRow, int startColumn)
{ {
int prevNewLine; int prevNewLine;

View File

@@ -0,0 +1,40 @@
//
// 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.ServiceLayer.Utility;
using Xunit;
namespace Microsoft.SqlTools.Test.Utility
{
/// <summary>
/// Tests for the TextUtilitiesTests class
/// </summary>
public class TextUtilitiesTests
{
[Fact]
public void PositionOfCursorFirstLine()
{
string sql = "EXEC sys.fn_isrolemember ";
int prevNewLine;
int cursorPosition = TextUtilities.PositionOfCursor(sql, 0, sql.Length, out prevNewLine);
Assert.Equal(prevNewLine, 0);
Assert.Equal(cursorPosition, sql.Length);
}
[Fact]
public void PositionOfCursorSecondLine()
{
string sql = "--lineone\nEXEC sys.fn_isrolemember ";
int prevNewLine;
int cursorPosition = TextUtilities.PositionOfCursor(sql, 1, 15, out prevNewLine);
Assert.Equal(prevNewLine, 10);
Assert.Equal(cursorPosition, 25);
}
}
}