
Hi guys,
have an issue with the programmatic upward search in the Document Snapshot. Below please find some UnitTests which show the issue.
Please see the SearchUp2() and SearchUp5() which are not producing the expected results.
Could you please investigate this behavior ?
Regards
Sven
using System;
using System.Linq;
using ActiproSoftware.Text;
using ActiproSoftware.Text.Implementation;
using ActiproSoftware.Text.Searching;
using ActiproSoftware.Windows.Controls.SyntaxEditor;
using ActiproSoftware.Windows.Controls.SyntaxEditor.Implementation;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Tests
{
[TestClass]
public class SearchTests
{
[TestMethod]
public void SearchUp1() // working
{
#region Arrange
const string searchString = "77";
const bool searchUp = true;
const int searchStartOffset = 25;
const int searchEndOffset = 0;
ITextSnapshot snapshot = GetSampleTextSnapshot();
ISearchOptions searchOptions = GetSearchOptionsFromSearchSettings(searchString, searchUp);
#endregion
#region Act
ISearchResultSet searchResultSet = snapshot.FindNext(searchOptions, searchStartOffset, false, new TextRange(searchEndOffset, searchStartOffset));
#endregion
#region Assert
Assert.IsNotNull(searchResultSet);
Assert.AreEqual(1, searchResultSet.Results.Count);
ISearchCapture firstCapture = searchResultSet.Results.First().Captures.First();
Assert.IsNotNull(firstCapture);
Assert.AreEqual(searchStartOffset-searchString.Length, firstCapture.TextRange.FirstOffset);
Assert.AreEqual(searchStartOffset, firstCapture.TextRange.LastOffset);
#endregion
}
[TestMethod]
public void SearchUp2() // not working
{
#region Arrange
const string searchString = "77";
const bool searchUp = true;
const int searchStartOffset = 75;
const int searchEndOffset = 0;
ITextSnapshot snapshot = GetSampleTextSnapshot();
ISearchOptions searchOptions = GetSearchOptionsFromSearchSettings(searchString, searchUp);
#endregion
#region Act
// the first part "77" of "777" should be found here! but it isnt!
ISearchResultSet searchResultSet = snapshot.FindNext(searchOptions, searchStartOffset, false, new TextRange(searchStartOffset, searchEndOffset));
#endregion
#region Assert
Assert.IsNotNull(searchResultSet);
Assert.AreEqual(1, searchResultSet.Results.Count);
ISearchCapture firstCapture = searchResultSet.Results.First().Captures.First();
Assert.IsNotNull(firstCapture);
Assert.AreEqual(searchStartOffset - searchString.Length, firstCapture.TextRange.FirstOffset);
Assert.AreEqual(searchStartOffset, firstCapture.TextRange.LastOffset);
#endregion
}
[TestMethod]
public void SearchUp3() // working
{
#region Arrange
const string searchString = "77";
const bool searchUp = true;
const int searchStartOffset = 76;
const int searchEndOffset = 0;
ITextSnapshot snapshot = GetSampleTextSnapshot();
ISearchOptions searchOptions = GetSearchOptionsFromSearchSettings(searchString, searchUp);
#endregion
#region Act
ISearchResultSet searchResultSet = snapshot.FindNext(searchOptions, searchStartOffset, false, new TextRange(searchStartOffset, searchEndOffset));
#endregion
#region Assert
Assert.IsNotNull(searchResultSet);
Assert.AreEqual(1, searchResultSet.Results.Count);
ISearchCapture firstCapture = searchResultSet.Results.First().Captures.First();
Assert.IsNotNull(firstCapture);
Assert.AreEqual(searchStartOffset - searchString.Length, firstCapture.TextRange.FirstOffset);
Assert.AreEqual(searchStartOffset, firstCapture.TextRange.LastOffset);
#endregion
}
[TestMethod]
public void SearchUp4() // working
{
#region Arrange
const string searchString = "77";
const bool searchUp = true;
const int searchStartOffset = 101;
const int searchEndOffset = 0;
ITextSnapshot snapshot = GetSampleTextSnapshot();
ISearchOptions searchOptions = GetSearchOptionsFromSearchSettings(searchString, searchUp);
#endregion
#region Act
ISearchResultSet searchResultSet = snapshot.FindNext(searchOptions, searchStartOffset, false, new TextRange(searchStartOffset, searchEndOffset));
#endregion
#region Assert
Assert.IsNotNull(searchResultSet);
Assert.AreEqual(1, searchResultSet.Results.Count);
ISearchCapture firstCapture = searchResultSet.Results.First().Captures.First();
Assert.IsNotNull(firstCapture);
Assert.AreEqual(searchStartOffset - searchString.Length, firstCapture.TextRange.FirstOffset);
Assert.AreEqual(searchStartOffset, firstCapture.TextRange.LastOffset);
#endregion
}
[TestMethod]
public void SearchUp5() // not working
{
#region Arrange
const string searchString = "77";
const bool searchUp = true;
ITextSnapshot snapshot = GetSampleTextSnapshot();
ISearchOptions searchOptions = GetSearchOptionsFromSearchSettings(searchString, searchUp, int.MaxValue);
#endregion
#region Act
// there should be 4 occurences of "77" when searchin upwards, but only 3 are found (seems to be the case, that SearchUp is ignored)
ISearchResultSet searchResultSet = snapshot.FindAll(searchOptions, new TextRange(snapshot.Length, 0));
#endregion
#region Assert
Assert.IsNotNull(searchResultSet);
Assert.AreEqual(4, searchResultSet.Results.Count);
#endregion
}
private ITextSnapshot GetSampleTextSnapshot()
{
string sampleText = " " + Environment.NewLine +
" O M 77.0" + Environment.NewLine +
" O M 7.0" + Environment.NewLine +
"" + Environment.NewLine +
" = M 777.0" + Environment.NewLine +
" O M 77.0" + Environment.NewLine +
" = M 100.0" + Environment.NewLine +
" ";
CodeDocument document = new CodeDocument();
document.SetText(sampleText);
return document.CurrentSnapshot;
}
private static ISearchOptions GetSearchOptionsFromSearchSettings(string text, bool searchUp, int maximumResultCount = 1)
{
return new EditorSearchOptions
{
FindText = text,
MatchCase = false,
MatchWholeWord = false,
SearchUp = searchUp,
PatternProvider = SearchPatternProviders.Normal,
Scope = EditorSearchScope.Document,
MaximumResultCount = maximumResultCount
};
}
}
}