~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to tests/UnitTests/MonoDevelop.Core/BacktrackingStringMatcherTests.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
Import upstream version 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// BacktrackingStringMatcherTests.cs
 
3
//
 
4
// Author:
 
5
//       Mike Krüger <mkrueger@xamarin.com>
 
6
//
 
7
// Copyright (c) 2012 Xamarin Inc. (http://xamarin.com)
 
8
//
 
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:
 
15
//
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
//
 
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
 
25
// THE SOFTWARE.
 
26
using System;
 
27
using NUnit.Framework;
 
28
using MonoDevelop.Core.Text;
 
29
using System.Linq;
 
30
 
 
31
namespace MonoDevelop.Core
 
32
{
 
33
        public class BacktrackingStringMatcherTests
 
34
        {
 
35
                [Test()]
 
36
                public void TestStringMatcher ()
 
37
                {
 
38
                        var matcher = StringMatcher.GetMatcher ("typese", true);
 
39
                        Assert.IsTrue (matcher.IsMatch ("TypeSystemService"));
 
40
                }
 
41
 
 
42
                [Test()]
 
43
                public void TestGetMatch ()
 
44
                {
 
45
                        var matcher = StringMatcher.GetMatcher ("typese", true);
 
46
                        var match = matcher.GetMatch("TypeSystemService");
 
47
                        CompareMatch(match, "****------**-----");
 
48
                }
 
49
 
 
50
                [Test()]
 
51
                public void TestGetMatchWithUpperCaseWord ()
 
52
                {
 
53
                        var matcher = StringMatcher.GetMatcher ("myhtmser", true);
 
54
                        var match = matcher.GetMatch("MyFunnyHTMLService");
 
55
                        CompareMatch(match,          "**-----***-***----");
 
56
                }
 
57
 
 
58
                [Test()]
 
59
                public void TestGetMatchWithUpperCaseWordCase2 ()
 
60
                {
 
61
                        var matcher = StringMatcher.GetMatcher ("myhmser", true);
 
62
                        var match = matcher.GetMatch("MyFunnyHTMLMasterService");
 
63
                        CompareMatch(match,          "**-----*---*-----***----");
 
64
                }
 
65
 
 
66
                [Test()]
 
67
                public void TestGetMatchWithunderscoreWord ()
 
68
                {
 
69
                        var matcher = StringMatcher.GetMatcher ("myhtmser", true);
 
70
                        var match = matcher.GetMatch("my_html_Service");
 
71
                        CompareMatch(match,          "**-***--***----");
 
72
                }
 
73
 
 
74
                [Test()]
 
75
                public void TestDigit ()
 
76
                {
 
77
                        var matcher = StringMatcher.GetMatcher ("my12", true);
 
78
                        var match = matcher.GetMatch("my_html_Service_123");
 
79
                        CompareMatch(match,          "**--------------**-");
 
80
                }
 
81
 
 
82
                [Test()]
 
83
                public void TestPunctuation ()
 
84
                {
 
85
                        var matcher = StringMatcher.GetMatcher ("foo:b", true);
 
86
                        var match = matcher.GetMatch("foo:bar");
 
87
                        CompareMatch(match,          "*****--");
 
88
                }
 
89
 
 
90
                [Test()]
 
91
                public void TestUnderscoreAtEnd ()
 
92
                {
 
93
                        var matcher = StringMatcher.GetMatcher ("FB", true);
 
94
                        var match = matcher.GetMatch("foo_");
 
95
                        Assert.AreEqual (null, match);
 
96
                }
 
97
 
 
98
                /// <summary>
 
99
                /// Bug 7659 - Terrible quick search matching 
 
100
                /// </summary>
 
101
                [Test()]
 
102
                public void TestBug7659 ()
 
103
                {
 
104
                        var matcher = StringMatcher.GetMatcher ("MoDr.add", true);
 
105
                        int rank;
 
106
                        Assert.IsTrue (matcher.CalcMatchRank("MonoDevelop.MonoDroid.addin.xml", out rank));
 
107
                }
 
108
 
 
109
                static string GenerateString(int[] match, string str)
 
110
                {
 
111
                        var result = new char[str.Length];
 
112
                        for (int i = 0; i < result.Length;i++) {
 
113
                                result[i] = match.Contains (i) ? '*' : '-';
 
114
                        }
 
115
                        return new string (result);
 
116
                }
 
117
                static void CompareMatch (int[] match, string str)
 
118
                {
 
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.");
 
124
                                }
 
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.");
 
129
                                }
 
130
                        }
 
131
 
 
132
                        foreach (var i in match){
 
133
                                if (str[i] != '*') {
 
134
                                        Console.WriteLine (str);
 
135
                                        Console.WriteLine (GenerateString (match, str));
 
136
                                        Assert.Fail ("Match "+ i +" doesn't match.");
 
137
                                }
 
138
                        }
 
139
                }
 
140
        }
 
141
}
 
142