1
// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
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:
9
// The above copyright notice and this permission notice shall be included in all copies or
10
// substantial portions of the Software.
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.
21
using ICSharpCode.NRefactory.CSharp;
22
using ICSharpCode.NRefactory.PatternMatching;
24
namespace ICSharpCode.NRefactory.ConsistencyCheck
27
/// Validates pattern matching:
28
/// Every compilation must match its clone;
29
/// and mutations to the cloned compilation must prevent the match.
32
/// This test is SLOW! (due to O(n^2) algorithm).
33
/// Expect it to take a whole hour!
35
public class PatternMatchingTest
37
public static void RunTest(CSharpFile file)
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);
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);
52
//if (!copy.IsMatch(file.SyntaxTree))
53
// throw new InvalidOperationException("Clone must match the compilation itself after resetting the mutation");
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);