1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
//
// TestParser.cs
//
// Author:
// Michael Hutchinson <mhutchinson@novell.com>
//
// Copyright (C) 2008 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Linq;
using System.Collections.Generic;
using MonoDevelop.Ide.Gui.Content;
using NUnit.Framework;
using ICSharpCode.NRefactory.TypeSystem;
namespace MonoDevelop.Xml.StateEngine
{
public class TestParser : Parser
{
List<Error> errors = new List<Error> ();
public TestParser (RootState rootState) : this (rootState, false)
{
}
public TestParser (RootState rootState, bool buildTree) : base (rootState, buildTree)
{
base.ErrorLogged += delegate (Error err) { errors.Add (err); };
}
public new List<Error> Errors { get { return errors; } }
public void Parse (string doc, params Action[] asserts)
{
Parse (doc, '$', asserts);
}
public void Parse (string doc, char trigger, params Action[] asserts)
{
Assert.AreEqual (Position, 0);
int assertNo = 0;
for (int i = 0; i < doc.Length; i++) {
char c = doc[i];
if (c == trigger) {
asserts[assertNo] ();
assertNo++;
} else {
Push (c);
}
}
Assert.AreEqual (Position, doc.Length - assertNo);
}
public string GetPath ()
{
System.Text.StringBuilder sb = new System.Text.StringBuilder ();
foreach (XObject obj in Nodes) {
if (obj is XDocument) {
sb.Insert (0, '/');
break;
}
sb.Insert (0, obj.FriendlyPathRepresentation);
sb.Insert (0, '/');
}
return sb.ToString ();
}
public void AssertPath (string path)
{
Assert.AreEqual (path, GetPath ());
}
public Action PathAssertion (string path)
{
return delegate {
Assert.AreEqual (path, GetPath ());
};
}
public void AssertStateIs<T> () where T : State
{
Assert.IsTrue (CurrentState is T, "Current state is {0} not {1}", CurrentState.GetType ().Name, typeof (T).Name);
}
public void AssertNodeDepth (int depth)
{
Assert.AreEqual (depth, Nodes.Count, "Node depth is {0} not {1}", Nodes.Count, depth);
}
public void AssertNodeIs<T> (int down)
{
XObject n = Nodes.Peek (down);
AssertNodeDepth (down);
Assert.IsTrue (n is T, "Node down {0} is {1}, not {2}", down, n.GetType ().Name, typeof (T).Name);
}
public void AssertNodeIs<T> ()
{
XObject n = Nodes.Peek ();
Assert.IsTrue (n is T, "Node is {0}, not {1}", n.GetType ().Name, typeof (T).Name);
}
public void AssertErrorCount (int count)
{
AssertErrorCount (count, x => true);
}
public void AssertErrorCount (int count, Func<Error,bool> filter)
{
string msg = null;
int actualCount = errors.Where (filter).Count ();
if (actualCount != count) {
System.Text.StringBuilder sb = new System.Text.StringBuilder ();
foreach (Error err in errors)
if (filter (err))
sb.AppendFormat ("{0}@{1}: {2}\n", err.ErrorType, err.Region, err.Message);
msg = sb.ToString ();
}
Assert.AreEqual (count, actualCount, msg);
}
public void AssertAttributes (params string[] nameValuePairs)
{
if ((nameValuePairs.Length % 2) != 0)
throw new ArgumentException ("nameValuePairs");
AssertNodeIs<IAttributedXObject> ();
IAttributedXObject obj = (IAttributedXObject) Nodes.Peek ();
int i = 0;
foreach (XAttribute att in obj.Attributes) {
Assert.IsTrue (i < nameValuePairs.Length);
Assert.AreEqual (nameValuePairs[i], att.Name.FullName);
Assert.AreEqual (nameValuePairs[i + 1], att.Value);
i += 2;
}
Assert.IsTrue (i == nameValuePairs.Length);
}
public void AssertNoErrors ()
{
AssertErrorCount (0);
}
public void AssertEmpty ()
{
AssertNodeDepth (1);
AssertNodeIs<XDocument> ();
}
public void AssertName (string name)
{
AssertName (0, name);
}
public void AssertName (int down, string name)
{
XObject node = Nodes.Peek (down);
Assert.IsTrue (node is INamedXObject);
Assert.AreEqual (name, ((INamedXObject)node).Name.FullName);
}
}
}
|