From 0d101cf598f1ba551578f2122f13be0ad1395906 Mon Sep 17 00:00:00 2001 From: Karl Burtram Date: Thu, 15 Dec 2016 14:22:39 -0800 Subject: [PATCH] Add text utils unit tests and parameter comments. (#192) * Add unit tests for PositionOfCursor * Add unit tests for PositionOfCursor --- .../Utility/TextUtilities.cs | 18 ++++----- .../Utility/TextUtilitiesTests.cs | 40 +++++++++++++++++++ 2 files changed, 49 insertions(+), 9 deletions(-) create mode 100644 test/Microsoft.SqlTools.ServiceLayer.Test/Utility/TextUtilitiesTests.cs diff --git a/src/Microsoft.SqlTools.ServiceLayer/Utility/TextUtilities.cs b/src/Microsoft.SqlTools.ServiceLayer/Utility/TextUtilities.cs index f06c654c..f4b5cb60 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/Utility/TextUtilities.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/Utility/TextUtilities.cs @@ -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 /// /// - /// - /// - /// + /// parameter is 0-based + /// parameter is 0-based + /// parameter is 0-based public static int PositionOfCursor(string sql, int startRow, int startColumn, out int prevNewLine) - { + { prevNewLine = 0; if (string.IsNullOrWhiteSpace(sql)) { return 1; } - + for (int i = 0; i < startRow; ++i) { 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. /// /// - /// - /// + /// parameter is 0-based + /// parameter is 0-based /// 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. /// /// - /// - /// + /// parameter is 0-based + /// parameter is 0-based public static int PositionOfNextDelimeter(string sql, int startRow, int startColumn) { int prevNewLine; diff --git a/test/Microsoft.SqlTools.ServiceLayer.Test/Utility/TextUtilitiesTests.cs b/test/Microsoft.SqlTools.ServiceLayer.Test/Utility/TextUtilitiesTests.cs new file mode 100644 index 00000000..3d67da8b --- /dev/null +++ b/test/Microsoft.SqlTools.ServiceLayer.Test/Utility/TextUtilitiesTests.cs @@ -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 +{ + /// + /// Tests for the TextUtilitiesTests class + /// + 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); + } + } +}