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

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.ConsistencyCheck/PatternMatchingTest.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
// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
 
2
// 
 
3
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
 
4
// software and associated documentation files (the "Software"), to deal in the Software
 
5
// without restriction, including without limitation the rights to use, copy, modify, merge,
 
6
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
 
7
// to whom the Software is furnished to do so, subject to the following conditions:
 
8
// 
 
9
// The above copyright notice and this permission notice shall be included in all copies or
 
10
// substantial portions of the Software.
 
11
// 
 
12
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 
13
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 
14
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
 
15
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 
16
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 
17
// DEALINGS IN THE SOFTWARE.
 
18
 
 
19
using System;
 
20
using System.Linq;
 
21
using ICSharpCode.NRefactory.CSharp;
 
22
using ICSharpCode.NRefactory.PatternMatching;
 
23
 
 
24
namespace ICSharpCode.NRefactory.ConsistencyCheck
 
25
{
 
26
        /// <summary>
 
27
        /// Validates pattern matching:
 
28
        /// Every compilation must match its clone;
 
29
        /// and mutations to the cloned compilation must prevent the match.
 
30
        /// </summary>
 
31
        /// <remarks>
 
32
        /// This test is SLOW! (due to O(n^2) algorithm).
 
33
        /// Expect it to take a whole hour!
 
34
        /// </remarks>
 
35
        public class PatternMatchingTest
 
36
        {
 
37
                public static void RunTest(CSharpFile file)
 
38
                {
 
39
                        AstNode copy = file.SyntaxTree.Clone();
 
40
                        if (!copy.IsMatch(file.SyntaxTree))
 
41
                                throw new InvalidOperationException("Clone must match the compilation itself; in " + file.FileName);
 
42
                        
 
43
                        // Mutate identifiers:
 
44
                        foreach (var id in copy.Descendants.OfType<Identifier>()) {
 
45
                                if (id.Parent is ConstructorDeclaration || id.Parent is DestructorDeclaration)
 
46
                                        continue; // identifier in ctor/dtor isn't relevant for matches
 
47
                                string oldName = id.Name;
 
48
                                id.Name = "mutatedName";
 
49
                                if (copy.IsMatch(file.SyntaxTree))
 
50
                                        throw new InvalidOperationException("Mutation in " + id.StartLocation + " did not prevent the match; in " + file.FileName);
 
51
                                id.Name = oldName;
 
52
                                //if (!copy.IsMatch(file.SyntaxTree))
 
53
                                //      throw new InvalidOperationException("Clone must match the compilation itself after resetting the mutation");
 
54
                        }
 
55
                        // Mutate primitive values:
 
56
                        foreach (var pe in copy.Descendants.OfType<PrimitiveExpression>()) {
 
57
                                object oldVal = pe.Value;
 
58
                                pe.Value = "Mutated " + "Value";
 
59
                                if (copy.IsMatch(file.SyntaxTree))
 
60
                                        throw new InvalidOperationException("Mutation in " + pe.StartLocation + " did not prevent the match; in " + file.FileName);
 
61
                                pe.Value = oldVal;
 
62
                        }
 
63
                        Console.Write('.');
 
64
                }
 
65
        }
 
66
}