2
// BacktrackingStringMatcherTests.cs
5
// Mike Krüger <mkrueger@xamarin.com>
7
// Copyright (c) 2012 Xamarin Inc. (http://xamarin.com)
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27
using NUnit.Framework;
28
using MonoDevelop.Core.Text;
31
namespace MonoDevelop.Core
33
public class BacktrackingStringMatcherTests
36
public void TestStringMatcher ()
38
var matcher = StringMatcher.GetMatcher ("typese", true);
39
Assert.IsTrue (matcher.IsMatch ("TypeSystemService"));
43
public void TestGetMatch ()
45
var matcher = StringMatcher.GetMatcher ("typese", true);
46
var match = matcher.GetMatch("TypeSystemService");
47
CompareMatch(match, "****------**-----");
51
public void TestGetMatchWithUpperCaseWord ()
53
var matcher = StringMatcher.GetMatcher ("myhtmser", true);
54
var match = matcher.GetMatch("MyFunnyHTMLService");
55
CompareMatch(match, "**-----***-***----");
59
public void TestGetMatchWithUpperCaseWordCase2 ()
61
var matcher = StringMatcher.GetMatcher ("myhmser", true);
62
var match = matcher.GetMatch("MyFunnyHTMLMasterService");
63
CompareMatch(match, "**-----*---*-----***----");
67
public void TestGetMatchWithunderscoreWord ()
69
var matcher = StringMatcher.GetMatcher ("myhtmser", true);
70
var match = matcher.GetMatch("my_html_Service");
71
CompareMatch(match, "**-***--***----");
75
public void TestDigit ()
77
var matcher = StringMatcher.GetMatcher ("my12", true);
78
var match = matcher.GetMatch("my_html_Service_123");
79
CompareMatch(match, "**--------------**-");
83
public void TestPunctuation ()
85
var matcher = StringMatcher.GetMatcher ("foo:b", true);
86
var match = matcher.GetMatch("foo:bar");
87
CompareMatch(match, "*****--");
91
public void TestUnderscoreAtEnd ()
93
var matcher = StringMatcher.GetMatcher ("FB", true);
94
var match = matcher.GetMatch("foo_");
95
Assert.AreEqual (null, match);
99
/// Bug 7659 - Terrible quick search matching
102
public void TestBug7659 ()
104
var matcher = StringMatcher.GetMatcher ("MoDr.add", true);
106
Assert.IsTrue (matcher.CalcMatchRank("MonoDevelop.MonoDroid.addin.xml", out rank));
109
static string GenerateString(int[] match, string str)
111
var result = new char[str.Length];
112
for (int i = 0; i < result.Length;i++) {
113
result[i] = match.Contains (i) ? '*' : '-';
115
return new string (result);
117
static void CompareMatch (int[] match, string str)
119
for (int i = 0; i < str.Length;i++){
120
if (str[i] == '*' && !match.Any(m => m == i)){
121
Console.WriteLine (str);
122
Console.WriteLine (GenerateString (match, str));
123
Assert.Fail ("Match "+ i +" not found match.");
125
if (str[i] == '-' && match.Any(m => m == i)){
126
Console.WriteLine (str);
127
Console.WriteLine (GenerateString (match, str));
128
Assert.Fail ("Match "+ i +" wrongly found.");
132
foreach (var i in match){
134
Console.WriteLine (str);
135
Console.WriteLine (GenerateString (match, str));
136
Assert.Fail ("Match "+ i +" doesn't match.");