9 changed files with 363 additions and 1 deletions
@ -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); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -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); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -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(); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue