Browse Source

Add consistency check for incremental tag soup parser

newNRvisualizers
Daniel Grunwald 14 years ago
parent
commit
06bf2f4c2f
  1. 10
      ICSharpCode.NRefactory.ConsistencyCheck/ICSharpCode.NRefactory.ConsistencyCheck.csproj
  2. 2
      ICSharpCode.NRefactory.ConsistencyCheck/Program.cs
  3. 152
      ICSharpCode.NRefactory.ConsistencyCheck/Xml/TagSoupIncrementalTests.cs
  4. 107
      ICSharpCode.NRefactory.ConsistencyCheck/Xml/TestTextSource.cs
  5. 65
      ICSharpCode.NRefactory.ConsistencyCheck/Xml/TestTextSourceVersion.cs
  6. 7
      ICSharpCode.NRefactory.Xml/AXmlAttribute.cs
  7. 7
      ICSharpCode.NRefactory.Xml/AXmlTag.cs
  8. 12
      ICSharpCode.NRefactory.Xml/AXmlText.cs
  9. 2
      ICSharpCode.NRefactory.Xml/TagReader.cs

10
ICSharpCode.NRefactory.ConsistencyCheck/ICSharpCode.NRefactory.ConsistencyCheck.csproj

@ -66,6 +66,9 @@
<Compile Include="ResolverTest.cs" /> <Compile Include="ResolverTest.cs" />
<Compile Include="RoundtripTest.cs" /> <Compile Include="RoundtripTest.cs" />
<Compile Include="Solution.cs" /> <Compile Include="Solution.cs" />
<Compile Include="Xml\TagSoupIncrementalTests.cs" />
<Compile Include="Xml\TestTextSource.cs" />
<Compile Include="Xml\TestTextSourceVersion.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="app.config" /> <None Include="app.config" />
@ -76,10 +79,17 @@
<Project>{53DCA265-3C3C-42F9-B647-F72BA678122B}</Project> <Project>{53DCA265-3C3C-42F9-B647-F72BA678122B}</Project>
<Name>ICSharpCode.NRefactory.CSharp</Name> <Name>ICSharpCode.NRefactory.CSharp</Name>
</ProjectReference> </ProjectReference>
<ProjectReference Include="..\ICSharpCode.NRefactory.Xml\ICSharpCode.NRefactory.Xml.csproj">
<Project>{DC393B66-92ED-4CAD-AB25-CFEF23F3D7C6}</Project>
<Name>ICSharpCode.NRefactory.Xml</Name>
</ProjectReference>
<ProjectReference Include="..\ICSharpCode.NRefactory\ICSharpCode.NRefactory.csproj"> <ProjectReference Include="..\ICSharpCode.NRefactory\ICSharpCode.NRefactory.csproj">
<Project>{3B2A5653-EC97-4001-BB9B-D90F1AF2C371}</Project> <Project>{3B2A5653-EC97-4001-BB9B-D90F1AF2C371}</Project>
<Name>ICSharpCode.NRefactory</Name> <Name>ICSharpCode.NRefactory</Name>
</ProjectReference> </ProjectReference>
</ItemGroup> </ItemGroup>
<ItemGroup>
<Folder Include="Xml" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.Targets" /> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.Targets" />
</Project> </Project>

2
ICSharpCode.NRefactory.ConsistencyCheck/Program.cs

@ -46,6 +46,8 @@ namespace ICSharpCode.NRefactory.ConsistencyCheck
public static void Main(string[] args) public static void Main(string[] args)
{ {
TagSoupIncrementalTests.Run("c:\\temp\\ClosedXML.xml");
using (new Timer("Loading solution... ")) { using (new Timer("Loading solution... ")) {
solution = new Solution(SolutionFile); solution = new Solution(SolutionFile);
} }

152
ICSharpCode.NRefactory.ConsistencyCheck/Xml/TagSoupIncrementalTests.cs

@ -0,0 +1,152 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team
//
// 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.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using ICSharpCode.NRefactory.ConsistencyCheck.Xml;
using ICSharpCode.NRefactory.Editor;
using ICSharpCode.NRefactory.Xml;
namespace ICSharpCode.NRefactory.ConsistencyCheck
{
/// <summary>
/// Tests incremental tag soup parser.
/// </summary>
public class TagSoupIncrementalTests
{
static Random sharedRnd = new Random();
public static void Run(string fileName)
{
Run(new StringTextSource(File.ReadAllText(fileName)));
}
public static void Run(ITextSource originalXmlFile)
{
int seed;
lock (sharedRnd) {
seed = sharedRnd.Next();
}
Random rnd = new Random(seed);
TagSoupParser parser = new TagSoupParser();
StringBuilder b = new StringBuilder(originalXmlFile.Text);
IncrementalParserState parserState = null;
TestTextSourceVersion version = new TestTextSourceVersion();
for (int iteration = 0; iteration < 100; iteration++) {
var textSource = new TextSourceWithVersion(new StringTextSource(b.ToString()), version);
var incrementalResult = parser.ParseIncremental(parserState, textSource, out parserState);
var nonIncrementalResult = parser.Parse(textSource);
CompareResults(incrementalResult, nonIncrementalResult);
// Randomly mutate the file:
List<TextChangeEventArgs> changes = new List<TextChangeEventArgs>();
int modifications = rnd.Next(0, 10);
for (int i = 0; i < modifications; i++) {
int offset = rnd.Next(0, b.Length);
int originalOffset = rnd.Next(0, originalXmlFile.TextLength);
int insertionLength;
int removalLength;
switch (rnd.Next(0, 21) / 20) {
case 0:
removalLength = 0;
insertionLength = rnd.Next(0, Math.Min(50, originalXmlFile.TextLength - originalOffset));
break;
case 1:
removalLength = rnd.Next(0, Math.Min(10, b.Length - offset));
insertionLength = rnd.Next(0, Math.Min(20, originalXmlFile.TextLength - originalOffset));
break;
default:
removalLength = rnd.Next(0, b.Length - offset);
insertionLength = rnd.Next(0, originalXmlFile.TextLength - originalOffset);
break;
}
string removedText = b.ToString(offset, removalLength);
b.Remove(offset, removalLength);
string insertedText = originalXmlFile.GetText(originalOffset, insertionLength);
b.Insert(offset, insertedText);
changes.Add(new TextChangeEventArgs(offset, removedText, insertedText));
}
version = new TestTextSourceVersion(version, changes);
}
}
static void CompareResults(IList<AXmlObject> result1, IList<AXmlObject> result2)
{
if (result1.Count != result2.Count)
throw new InvalidOperationException();
for (int i = 0; i < result1.Count; i++) {
CompareResults(result1[i], result2[i]);
}
}
static void CompareResults(AXmlObject obj1, AXmlObject obj2)
{
if (obj1.GetType() != obj2.GetType())
throw new InvalidOperationException();
if (obj1.StartOffset != obj2.StartOffset)
throw new InvalidOperationException();
if (obj1.EndOffset != obj2.EndOffset)
throw new InvalidOperationException();
if (obj1.MySyntaxErrors.Count() != obj2.MySyntaxErrors.Count())
throw new InvalidOperationException();
foreach (var pair in obj1.MySyntaxErrors.Zip(obj2.MySyntaxErrors, (a,b) => new { a, b })) {
if (pair.a.StartOffset != pair.b.StartOffset)
throw new InvalidOperationException();
if (pair.a.EndOffset != pair.b.EndOffset)
throw new InvalidOperationException();
if (pair.a.Description != pair.b.Description)
throw new InvalidOperationException();
}
if (obj1 is AXmlText) {
var a = (AXmlText)obj1;
var b = (AXmlText)obj2;
if (a.ContainsOnlyWhitespace != b.ContainsOnlyWhitespace)
throw new InvalidOperationException();
if (a.Value != b.Value)
throw new InvalidOperationException();
} else if (obj1 is AXmlTag) {
var a = (AXmlTag)obj1;
var b = (AXmlTag)obj2;
if (a.OpeningBracket != b.OpeningBracket)
throw new InvalidOperationException();
if (a.ClosingBracket != b.ClosingBracket)
throw new InvalidOperationException();
if (a.Name != b.Name)
throw new InvalidOperationException();
} else if (obj1 is AXmlAttribute) {
var a = (AXmlAttribute)obj1;
var b = (AXmlAttribute)obj2;
if (a.Name != b.Name)
throw new InvalidOperationException();
if (a.Value != b.Value)
throw new InvalidOperationException();
} else {
throw new NotSupportedException();
}
CompareResults(obj1.Children, obj2.Children);
}
}
}

107
ICSharpCode.NRefactory.ConsistencyCheck/Xml/TestTextSource.cs

@ -0,0 +1,107 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team
//
// 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 ICSharpCode.NRefactory.Editor;
namespace ICSharpCode.NRefactory.ConsistencyCheck.Xml
{
public class TextSourceWithVersion : ITextSource
{
readonly ITextSource textSource;
readonly ITextSourceVersion version;
public TextSourceWithVersion(ITextSource textSource, ITextSourceVersion version)
{
this.textSource = textSource;
this.version = version;
}
ITextSourceVersion ITextSource.Version {
get { return version; }
}
int ITextSource.TextLength {
get { return textSource.TextLength; }
}
string ITextSource.Text {
get { return textSource.Text; }
}
ITextSource ITextSource.CreateSnapshot()
{
throw new NotImplementedException();
}
ITextSource ITextSource.CreateSnapshot(int offset, int length)
{
throw new NotImplementedException();
}
System.IO.TextReader ITextSource.CreateReader()
{
throw new NotImplementedException();
}
System.IO.TextReader ITextSource.CreateReader(int offset, int length)
{
throw new NotImplementedException();
}
char ITextSource.GetCharAt(int offset)
{
return textSource.GetCharAt(offset);
}
string ITextSource.GetText(int offset, int length)
{
return textSource.GetText(offset, length);
}
string ITextSource.GetText(ISegment segment)
{
return textSource.GetText(segment);
}
int ITextSource.IndexOf(char c, int startIndex, int count)
{
return textSource.IndexOf(c, startIndex, count);
}
int ITextSource.IndexOfAny(char[] anyOf, int startIndex, int count)
{
return textSource.IndexOfAny(anyOf, startIndex, count);
}
int ITextSource.IndexOf(string searchText, int startIndex, int count, StringComparison comparisonType)
{
return textSource.IndexOf(searchText, startIndex, count, comparisonType);
}
int ITextSource.LastIndexOf(char c, int startIndex, int count)
{
return textSource.LastIndexOf(c, startIndex, count);
}
int ITextSource.LastIndexOf(string searchText, int startIndex, int count, StringComparison comparisonType)
{
return textSource.LastIndexOf(searchText, startIndex, count, comparisonType);
}
}
}

65
ICSharpCode.NRefactory.ConsistencyCheck/Xml/TestTextSourceVersion.cs

@ -0,0 +1,65 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team
//
// 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.Collections.Generic;
using ICSharpCode.NRefactory.Editor;
namespace ICSharpCode.NRefactory.ConsistencyCheck.Xml
{
public class TestTextSourceVersion : ITextSourceVersion
{
readonly TestTextSourceVersion baseVersion;
readonly IEnumerable<TextChangeEventArgs> changesFromBaseVersionToThis;
public TestTextSourceVersion()
{
}
public TestTextSourceVersion(TestTextSourceVersion baseVersion, IEnumerable<TextChangeEventArgs> changesFromBaseVersionToThis)
{
this.baseVersion = baseVersion;
this.changesFromBaseVersionToThis = changesFromBaseVersionToThis;
}
public bool BelongsToSameDocumentAs(ITextSourceVersion other)
{
TestTextSourceVersion o = (TestTextSourceVersion)other;
return o == this || o == baseVersion || o.baseVersion == this;
}
public int CompareAge(ITextSourceVersion other)
{
throw new NotImplementedException();
}
public IEnumerable<TextChangeEventArgs> GetChangesTo(ITextSourceVersion other)
{
TestTextSourceVersion o = (TestTextSourceVersion)other;
if (o.baseVersion == this)
return o.changesFromBaseVersionToThis;
else
throw new NotImplementedException();
}
public int MoveOffsetTo(ITextSourceVersion other, int oldOffset, AnchorMovementType movement)
{
throw new NotImplementedException();
}
}
}

7
ICSharpCode.NRefactory.Xml/AXmlAttribute.cs

@ -17,6 +17,7 @@
// DEALINGS IN THE SOFTWARE. // DEALINGS IN THE SOFTWARE.
using System; using System;
using System.Globalization;
using ICSharpCode.NRefactory.Editor; using ICSharpCode.NRefactory.Editor;
namespace ICSharpCode.NRefactory.Xml namespace ICSharpCode.NRefactory.Xml
@ -57,5 +58,11 @@ namespace ICSharpCode.NRefactory.Xml
{ {
visitor.VisitAttribute(this); visitor.VisitAttribute(this);
} }
/// <inheritdoc/>
public override string ToString()
{
return string.Format(CultureInfo.InvariantCulture, "[{0} '{1}={2}']", base.ToString(), this.Name, this.Value);
}
} }
} }

7
ICSharpCode.NRefactory.Xml/AXmlTag.cs

@ -18,6 +18,7 @@
using System; using System;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Globalization;
using ICSharpCode.NRefactory.Editor; using ICSharpCode.NRefactory.Editor;
namespace ICSharpCode.NRefactory.Xml namespace ICSharpCode.NRefactory.Xml
@ -87,5 +88,11 @@ namespace ICSharpCode.NRefactory.Xml
{ {
visitor.VisitTag(this); visitor.VisitTag(this);
} }
/// <inheritdoc/>
public override string ToString()
{
return string.Format(CultureInfo.InvariantCulture, "[{0} '{1}{2}{3}' Attr:{4}]", base.ToString(), this.OpeningBracket, this.Name, this.ClosingBracket, this.Children.Count);
}
} }
} }

12
ICSharpCode.NRefactory.Xml/AXmlText.cs

@ -17,6 +17,7 @@
// DEALINGS IN THE SOFTWARE. // DEALINGS IN THE SOFTWARE.
using System; using System;
using System.Globalization;
namespace ICSharpCode.NRefactory.Xml namespace ICSharpCode.NRefactory.Xml
{ {
@ -30,6 +31,11 @@ namespace ICSharpCode.NRefactory.Xml
{ {
} }
// /// <summary> The type of the text node </summary>
// public TextType Type {
// get { return ((InternalText)internalObject).Type; }
// }
/// <summary> The text with all entity references resloved </summary> /// <summary> The text with all entity references resloved </summary>
public string Value { public string Value {
get { return ((InternalText)internalObject).Value; } get { return ((InternalText)internalObject).Value; }
@ -47,5 +53,11 @@ namespace ICSharpCode.NRefactory.Xml
{ {
visitor.VisitText(this); visitor.VisitText(this);
} }
/// <inheritdoc/>
public override string ToString()
{
return string.Format(CultureInfo.InvariantCulture, "[{0} Text.Length={1}]", base.ToString(), this.Value.Length);
}
} }
} }

2
ICSharpCode.NRefactory.Xml/TagReader.cs

@ -220,7 +220,7 @@ namespace ICSharpCode.NRefactory.Xml
HashSet<string> attributeNames = new HashSet<string>(); HashSet<string> attributeNames = new HashSet<string>();
foreach (var obj in tag.NestedObjects) { foreach (var obj in tag.NestedObjects) {
InternalAttribute attr = obj as InternalAttribute; InternalAttribute attr = obj as InternalAttribute;
if (attr != null && attributeNames.Add(attr.Name)) { if (attr != null && !attributeNames.Add(attr.Name)) {
int attrStart = tagStart + attr.StartRelativeToParent; int attrStart = tagStart + attr.StartRelativeToParent;
OnSyntaxError(attrStart, attrStart + attr.Name.Length, "Attribute with name '{0}' already exists", attr.Name); OnSyntaxError(attrStart, attrStart + attr.Name.Length, "Attribute with name '{0}' already exists", attr.Name);
} }

Loading…
Cancel
Save