Skip to content

Commit

Permalink
Merge pull request RolandPheasant#162 from pmiossec/fix_search
Browse files Browse the repository at this point in the history
Fix 2 bugs on search feature
  • Loading branch information
RolandPheasant authored Sep 27, 2016
2 parents 2984950 + aa51fad commit c02f124
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ public SearchMetadata Create(string searchText, bool useRegex, int index, bool f
{
if (searchText == null) throw new ArgumentNullException(nameof(searchText));

var withNegation = searchText.WithNegation();
var isExclusion = withNegation.IsNegation;
searchText = withNegation.Text;
var isExclusion = false;
if (!useRegex)
{
var withNegation = searchText.WithNegation();
isExclusion = withNegation.IsNegation;
searchText = withNegation.Text;
}

var association = _textAssociationCollection.Lookup(searchText);
string icon;
Expand All @@ -52,6 +56,5 @@ public SearchMetadata Create(string searchText, bool useRegex, int index, bool f
isGlobal,
isExclusion);
}

}
}
120 changes: 120 additions & 0 deletions Source/TailBlazer.Fixtures/SearchHintEx.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
using FluentAssertions;
using TailBlazer.Domain.FileHandling.Recent;
using Xunit;
using TailBlazer.Views.Searching;

namespace TailBlazer.Fixtures
{
public class SearchHintEx
{
[Fact]
public void ShouldAskForTextWhenTextIsEmpty()
{
var searchRequest = new SearchRequest("", false);

var result = searchRequest.BuildMessage();

result.IsValid.Should().BeTrue();
result.Message.Should().Be("Type to search using plain text");
}

[Fact]
public void ShouldAskForTextWhenRegexIsEmpty()
{
var searchRequest = new SearchRequest("", true);

var result = searchRequest.BuildMessage();

result.IsValid.Should().BeTrue();
result.Message.Should().Be("Type to search using regex");
}

[Fact]
public void ShouldBeValidWhenSearchingPlainText()
{
var searchRequest = new SearchRequest("[inf", false);

var result = searchRequest.BuildMessage();

result.IsValid.Should().BeTrue();
result.Message.Should().Be("Hit enter to search using plain text");
}

[Fact]
public void ShouldBeValidWhenSearchingAValidRegex()
{
var searchRequest = new SearchRequest("[inf]", true);

var result = searchRequest.BuildMessage();

result.IsValid.Should().BeTrue();
result.Message.Should().Be("Hit enter to search using regex");
}

[Fact]
public void ShouldBeInvalidWhenSearchingTooShortRegEx()
{
var searchRequest = new SearchRequest(".", true);

var result = searchRequest.BuildMessage();

result.IsValid.Should().BeFalse();
result.Message.Should().Be("Regex must be at least 2 characters");
}

[Fact]
public void ShouldBeValidWhenSearchingPlainTextExclusion()
{
var searchRequest = new SearchRequest("-[inf", false);

var result = searchRequest.BuildMessage();

result.IsValid.Should().BeTrue();
result.Message.Should().Be("Hit enter to search using plain text");
}

[Fact]
public void ShouldBeInvalidWhenPlainTextExclusionTextIsTooShort()
{
var searchRequest = new SearchRequest("-f", false);

var result = searchRequest.BuildMessage();

result.IsValid.Should().BeFalse();
result.Message.Should().Be("Text must be at least 3 characters");
}

[Fact]
public void ShouldBeInvalidWhenSearchingIrregularRegEx()
{
var searchRequest = new SearchRequest("[inf", true);

var result = searchRequest.BuildMessage();

result.IsValid.Should().BeFalse();
result.Message.Should().Be("Invalid regular expression");
}

[Fact]
public void ShouldBeInvalidWhenPlainTextContainsIllegalCharacter()
{
var searchRequest = new SearchRequest(@"[i\nf", false);

var result = searchRequest.BuildMessage();

result.IsValid.Should().BeFalse();
result.Message.Should().Be("Text contains illegal characters");
}

[Fact]
public void ShouldBeInvalidWhenPlainTextContainsOnlyWhiteSpaces()
{
var searchRequest = new SearchRequest("- \t", false);

var result = searchRequest.BuildMessage();

result.IsValid.Should().BeFalse();
result.Message.Should().Be("Text contains illegal characters");
}
}
}
1 change: 1 addition & 0 deletions Source/TailBlazer.Fixtures/TailBlazer.Fixtures.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
<Compile Include="FileDropFixture.cs" />
<Compile Include="IconDescriptionFixture.cs" />
<Compile Include="IconProviderFixture.cs" />
<Compile Include="SearchHintEx.cs" />
<Compile Include="StartFromFixture.cs" />
<Compile Include="FileSearchFixture.cs" />
<Compile Include="FileSegmentFixture.cs" />
Expand Down
15 changes: 9 additions & 6 deletions Source/TailBlazer/Views/Searching/SearchHintEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ public static SearchHintMessage BuildMessage(this SearchRequest source)
if ((!source.UseRegEx && source.Text.Contains(@"\")) || (source.TextWithoutExclusion.Trim().Length == 0))
return new SearchHintMessage(false, "Text contains illegal characters");

try
if (source.UseRegEx)
{
var test = new Regex(source.TextWithoutExclusion);
}
catch (Exception)
{
return new SearchHintMessage(false, "Invalid regular expression");
try
{
var test = new Regex(source.Text);
}
catch (Exception)
{
return new SearchHintMessage(false, "Invalid regular expression");
}
}

var message = $"Hit enter to search using {(source.UseRegEx ? "regex" : "plain text")}";
Expand Down

0 comments on commit c02f124

Please sign in to comment.