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

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.Tests/CSharp/Parser/ParseSelfTests.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.IO;
 
21
using ICSharpCode.NRefactory.Editor;
 
22
using ICSharpCode.NRefactory.TypeSystem;
 
23
using NUnit.Framework;
 
24
 
 
25
namespace ICSharpCode.NRefactory.CSharp.Parser
 
26
{
 
27
        /// <summary>
 
28
        /// Test fixture that parses NRefactory itself,
 
29
        /// ensures that there are no parser crashes while doing so,
 
30
        /// and that the returned positions are consistent.
 
31
        /// </summary>
 
32
        [TestFixture]
 
33
        public class ParseSelfTests
 
34
        {
 
35
                string[] fileNames;
 
36
                
 
37
                [TestFixtureSetUp]
 
38
                public void SetUp()
 
39
                {
 
40
                        string path = Path.GetFullPath (Path.Combine ("..", ".."));
 
41
                        if (!File.Exists(Path.Combine(path, "NRefactory.sln")))
 
42
                                throw new InvalidOperationException("Test cannot find the NRefactory source code in " + path);
 
43
                        fileNames = Directory.GetFiles(path, "*.cs", SearchOption.AllDirectories);
 
44
                }
 
45
                
 
46
                [Test]
 
47
                public void GenerateTypeSystem()
 
48
                {
 
49
                        IProjectContent pc = new CSharpProjectContent();
 
50
                        CSharpParser parser = new CSharpParser();
 
51
                        parser.GenerateTypeSystemMode = true;
 
52
                        foreach (string fileName in fileNames) {
 
53
                                SyntaxTree syntaxTree;
 
54
                                using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.SequentialScan)) {
 
55
                                        syntaxTree = parser.Parse(fs, fileName);
 
56
                                }
 
57
                                var unresolvedFile = syntaxTree.ToTypeSystem();
 
58
                                foreach (var td in unresolvedFile.GetAllTypeDefinitions()) {
 
59
                                        Assert.AreSame(unresolvedFile, td.UnresolvedFile);
 
60
                                        foreach (var member in td.Members) {
 
61
                                                Assert.AreSame(unresolvedFile, member.UnresolvedFile);
 
62
                                                Assert.AreSame(td, member.DeclaringTypeDefinition);
 
63
                                        }
 
64
                                }
 
65
                                pc = pc.AddOrUpdateFiles(unresolvedFile);
 
66
                        }
 
67
                }
 
68
                
 
69
                #region ParseAndCheckPositions
 
70
                string currentFileName;
 
71
                ReadOnlyDocument currentDocument;
 
72
                
 
73
                [Test, Ignore("Positions still are incorrect in several cases")]
 
74
                public void ParseAndCheckPositions()
 
75
                {
 
76
                        CSharpParser parser = new CSharpParser();
 
77
                        foreach (string fileName in fileNames) {
 
78
                                this.currentDocument = new ReadOnlyDocument(File.ReadAllText(fileName));
 
79
                                SyntaxTree syntaxTree = parser.Parse(currentDocument, fileName);
 
80
                                if (parser.HasErrors)
 
81
                                        continue;
 
82
                                this.currentFileName = fileName;
 
83
                                CheckPositionConsistency(syntaxTree);
 
84
                                CheckMissingTokens(syntaxTree);
 
85
                        }
 
86
                }
 
87
 
 
88
                void PrintNode (AstNode node)
 
89
                {
 
90
                        Console.WriteLine ("Parent:" + node.GetType ());
 
91
                        Console.WriteLine ("Children:");
 
92
                        foreach (var c in node.Children)
 
93
                                Console.WriteLine (c.GetType () +" at:"+ c.StartLocation +"-"+ c.EndLocation + " Role: "+ c.Role);
 
94
                        Console.WriteLine ("----");
 
95
                }
 
96
                
 
97
                void CheckPositionConsistency (AstNode node)
 
98
                {
 
99
                        string comment = "(" + node.GetType ().Name + " at " + node.StartLocation + " in " + currentFileName + ")";
 
100
                        var pred = node.StartLocation <= node.EndLocation;
 
101
                        if (!pred)
 
102
                                PrintNode (node);
 
103
                        Assert.IsTrue(pred, "StartLocation must be before EndLocation " + comment);
 
104
                        var prevNodeEnd = node.StartLocation;
 
105
                        var prevNode = node;
 
106
                        for (AstNode child = node.FirstChild; child != null; child = child.NextSibling) {
 
107
                                bool assertion = child.StartLocation >= prevNodeEnd;
 
108
                                if (!assertion) {
 
109
                                        PrintNode (prevNode);
 
110
                                        PrintNode (node);
 
111
                                        
 
112
                                }
 
113
                                Assert.IsTrue(assertion, currentFileName + ": Child " + child.GetType () +" (" + child.StartLocation  + ")" +" must start after previous sibling " + prevNode.GetType () + "(" + prevNode.StartLocation + ")");
 
114
                                CheckPositionConsistency(child);
 
115
                                prevNodeEnd = child.EndLocation;
 
116
                                prevNode = child;
 
117
                        }
 
118
                        Assert.IsTrue(prevNodeEnd <= node.EndLocation, "Last child must end before parent node ends " + comment);
 
119
                }
 
120
                
 
121
                void CheckMissingTokens(AstNode node)
 
122
                {
 
123
                        if (node is CSharpTokenNode) {
 
124
                                Assert.IsNull(node.FirstChild, "Token nodes should not have children");
 
125
                        } else {
 
126
                                var prevNodeEnd = node.StartLocation;
 
127
                                var prevNode = node;
 
128
                                for (AstNode child = node.FirstChild; child != null; child = child.NextSibling) {
 
129
                                        CheckWhitespace(prevNode, prevNodeEnd, child, child.StartLocation);
 
130
                                        CheckMissingTokens(child);
 
131
                                        prevNode = child;
 
132
                                        prevNodeEnd = child.EndLocation;
 
133
                                }
 
134
                                CheckWhitespace(prevNode, prevNodeEnd, node, node.EndLocation);
 
135
                        }
 
136
                }
 
137
                
 
138
                void CheckWhitespace(AstNode startNode, TextLocation whitespaceStart, AstNode endNode, TextLocation whitespaceEnd)
 
139
                {
 
140
                        if (whitespaceStart == whitespaceEnd || startNode == endNode)
 
141
                                return;
 
142
                        int start = currentDocument.GetOffset(whitespaceStart.Line, whitespaceStart.Column);
 
143
                        int end = currentDocument.GetOffset(whitespaceEnd.Line, whitespaceEnd.Column);
 
144
                        string text = currentDocument.GetText(start, end - start);
 
145
                        bool assertion = string.IsNullOrWhiteSpace(text);
 
146
                        if (!assertion) {
 
147
                                if (startNode.Parent != endNode.Parent)
 
148
                                        PrintNode (startNode.Parent);
 
149
                                PrintNode (endNode.Parent);
 
150
                        }
 
151
                        Assert.IsTrue(assertion, "Expected whitespace between " + startNode.GetType () +":" + whitespaceStart + " and " + endNode.GetType () + ":" + whitespaceEnd
 
152
                                      + ", but got '" + text + "' (in " + currentFileName + " parent:" + startNode.Parent.GetType () +")");
 
153
                }
 
154
                #endregion
 
155
        }
 
156
}