Permalink
Browse files

Fixed exceptions being thrown when regex contains incomplete information

  • Loading branch information...
1 parent 86df9dc commit 61103415da8ebc1f8d331fd45d2d60f1ac5935f3 @jaredpar committed May 10, 2010
Showing with 67 additions and 7 deletions.
  1. +12 −5 VimCore/CoreInterfaces.fs
  2. +11 −2 VimCore/SearchService.fs
  3. +44 −0 VimCoreTest/SearchServiceTest.cs
View
@@ -611,11 +611,18 @@ type SearchText =
| WholeWord of string
| StraightText of string
with
- member x.RawText =
- match x with
- | Pattern(p) -> p
- | WholeWord(p) -> p
- | StraightText(p) -> p
+ member x.RawText =
+ match x with
+ | Pattern(p) -> p
+ | WholeWord(p) -> p
+ | StraightText(p) -> p
+
+ /// Is this a pattern
+ member x.IsPatternText =
+ match x with
+ | Pattern(_) -> true
+ | WholeWord(_) -> false
+ | StraightText(_) -> false
type SearchData = {
Text : SearchText;
View
@@ -36,7 +36,7 @@ type internal SearchService
let isWrap = SearchKindUtil.IsWrap searchData.Kind
let opts = x.CreateFindOptions searchData.Text searchData.Kind searchData.Options
let findData = FindData(searchData.Text.RawText, tss, opts, nav)
-
+
// Create a function which will give us the next search position
let getNextPoint =
if SearchKindUtil.IsForward searchData.Kind then
@@ -50,7 +50,16 @@ type internal SearchService
// Recursive loop to perform the search "count" times
let rec doFind count position =
- let result = _search.FindNext(position, isWrap, findData) |> NullableUtil.toOption
+
+ let result =
+ try
+ _search.FindNext(position, isWrap, findData) |> NullableUtil.toOption
+ with
+ | :? System.InvalidOperationException ->
+ // If the regular expression has invalid data then don't throw but return a failed match
+ if searchData.Text.IsPatternText then None
+ else reraise()
+
match result,count > 1 with
| Some(span),false -> Some(span)
| Some(span),true ->
@@ -241,7 +241,51 @@ public void FindNextMulitple5()
_factory.Verify();
}
+ [Test]
+ public void BadRegex1()
+ {
+ var tss = MockObjectFactory.CreateTextSnapshot(42).Object;
+ var nav = _factory.Create<ITextStructureNavigator>();
+ _textSearch
+ .Setup(x => x.FindNext(0, true, It.IsAny<FindData>()))
+ .Throws(new InvalidOperationException())
+ .Verifiable();
+ var searchData = new SearchData(SearchText.NewPattern("f("), SearchKind.ForwardWithWrap, SearchOptions.None);
+ var ret = _search.FindNext(searchData, new SnapshotPoint(tss, 0), nav.Object);
+ Assert.IsTrue(ret.IsNone());
+ _factory.Verify();
+ }
+
+
+ [Test]
+ public void BadRegex2()
+ {
+ var tss = MockObjectFactory.CreateTextSnapshot(42).Object;
+ var nav = _factory.Create<ITextStructureNavigator>();
+ _textSearch
+ .Setup(x => x.FindNext(0, true, It.IsAny<FindData>()))
+ .Throws(new InvalidOperationException())
+ .Verifiable();
+ var searchData = new SearchData(SearchText.NewPattern("f("), SearchKind.ForwardWithWrap, SearchOptions.None);
+ var ret = _search.FindNextMultiple(searchData, new SnapshotPoint(tss, 0), nav.Object, 2);
+ Assert.IsTrue(ret.IsNone());
+ _factory.Verify();
+ }
+ [Test]
+ [Description("An InvalidOperationException from a non-regex shouldn't be handled")]
+ public void BadRegex3()
+ {
+ var tss = MockObjectFactory.CreateTextSnapshot(42).Object;
+ var nav = _factory.Create<ITextStructureNavigator>();
+ _textSearch
+ .Setup(x => x.FindNext(0, true, It.IsAny<FindData>()))
+ .Throws(new InvalidOperationException())
+ .Verifiable();
+ var searchData = new SearchData(SearchText.NewStraightText("f("), SearchKind.ForwardWithWrap, SearchOptions.None);
+ Assert.Throws<InvalidOperationException>(() => _search.FindNext(searchData, new SnapshotPoint(tss, 0), nav.Object));
+ _factory.Verify();
+ }
}
}

0 comments on commit 6110341

Please sign in to comment.