Browse Source

Added unit tests for XmlParser getting attribute names with prefixes. Added GetQualifiedAttributeName and GetQualifiedAttributeNameAtIndex methods which return a QualifiedName for an attribute that includes its prefix if any.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@2571 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Matt Ward 18 years ago
parent
commit
1e3e02a082
  1. 58
      src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlParser.cs
  2. 44
      src/AddIns/DisplayBindings/XmlEditor/Test/Parser/AttributeNameTestFixture.cs
  3. 53
      src/AddIns/DisplayBindings/XmlEditor/Test/Parser/AttributeNameUnderCursorTests.cs

58
src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlParser.cs

@ -219,6 +219,18 @@ namespace ICSharpCode.XmlEditor @@ -219,6 +219,18 @@ namespace ICSharpCode.XmlEditor
return isNamespace;
}
/// <summary>
/// Gets the attribute name and any prefix. The namespace
/// is not determined.
/// </summary>
/// <returns><see langword="null"/> if no attribute name can
/// be found.</returns>
public static QualifiedName GetQualifiedAttributeName(string xml, int index)
{
string name = GetAttributeName(xml, index);
return GetQualifiedName(name);
}
/// <summary>
/// Gets the name of the attribute inside but before the specified
/// index.
@ -230,10 +242,19 @@ namespace ICSharpCode.XmlEditor @@ -230,10 +242,19 @@ namespace ICSharpCode.XmlEditor
}
index = GetCorrectedIndex(xml.Length, index);
return GetAttributeName(xml, index, true, true, true);
}
/// <summary>
/// Gets the name of the attribute and its prefix at the specified index. The index
/// can be anywhere inside the attribute name or in the attribute value.
/// </summary>
public static QualifiedName GetQualifiedAttributeNameAtIndex(string xml, int index)
{
string name = GetAttributeNameAtIndex(xml, index);
return GetQualifiedName(name);
}
/// <summary>
/// Gets the name of the attribute at the specified index. The index
/// can be anywhere inside the attribute name or in the attribute value.
@ -523,17 +544,7 @@ namespace ICSharpCode.XmlEditor @@ -523,17 +544,7 @@ namespace ICSharpCode.XmlEditor
name = xml.Substring(1);
}
QualifiedName qualifiedName = new QualifiedName();
int prefixIndex = name.IndexOf(':');
if (prefixIndex > 0) {
qualifiedName.Prefix = name.Substring(0, prefixIndex);
qualifiedName.Name = name.Substring(prefixIndex + 1);
} else {
qualifiedName.Name = name;
}
return qualifiedName;
return GetQualifiedName(name);
}
/// <summary>
@ -658,7 +669,7 @@ namespace ICSharpCode.XmlEditor @@ -658,7 +669,7 @@ namespace ICSharpCode.XmlEditor
--currentIndex;
}
if (!invalidString) {
name = ReverseString(reversedAttributeName.ToString());
}
@ -683,5 +694,26 @@ namespace ICSharpCode.XmlEditor @@ -683,5 +694,26 @@ namespace ICSharpCode.XmlEditor
}
return null;
}
/// <summary>
/// Returns a name and its prefix.
/// </summary>
static QualifiedName GetQualifiedName(string name)
{
if (name.Length == 0) {
return null;
}
QualifiedName qualifiedName = new QualifiedName();
int prefixIndex = name.IndexOf(':');
if (prefixIndex > 0) {
qualifiedName.Prefix = name.Substring(0, prefixIndex);
qualifiedName.Name = name.Substring(prefixIndex + 1);
} else {
qualifiedName.Name = name;
}
return qualifiedName;
}
}
}

44
src/AddIns/DisplayBindings/XmlEditor/Test/Parser/AttributeNameTestFixture.cs

@ -22,76 +22,98 @@ namespace XmlEditor.Tests.Parser @@ -22,76 +22,98 @@ namespace XmlEditor.Tests.Parser
public void SuccessTest1()
{
string text = " foo='a";
Assert.AreEqual("foo", XmlParser.GetAttributeName(text, text.Length), "Should have retrieved the attribute name 'foo'");
QualifiedName expectedName = new QualifiedName("foo", String.Empty);
QualifiedName name = XmlParser.GetQualifiedAttributeName(text, text.Length);
Assert.AreEqual(expectedName, name, "Should have retrieved the attribute name 'foo'");
}
[Test]
public void SuccessTest2()
{
string text = " foo='";
Assert.AreEqual("foo", XmlParser.GetAttributeName(text, text.Length), "Should have retrieved the attribute name 'foo'");
QualifiedName expectedName = new QualifiedName("foo", String.Empty);
QualifiedName name = XmlParser.GetQualifiedAttributeName(text, text.Length);
Assert.AreEqual(expectedName, name, "Should have retrieved the attribute name 'foo'");
}
[Test]
public void SuccessTest3()
{
string text = " foo=";
Assert.AreEqual("foo", XmlParser.GetAttributeName(text, text.Length), "Should have retrieved the attribute name 'foo'");
QualifiedName expectedName = new QualifiedName("foo", String.Empty);
QualifiedName name = XmlParser.GetQualifiedAttributeName(text, text.Length);
Assert.AreEqual(expectedName, name, "Should have retrieved the attribute name 'foo'");
}
[Test]
public void SuccessTest4()
{
string text = " foo=\"";
Assert.AreEqual("foo", XmlParser.GetAttributeName(text, text.Length), "Should have retrieved the attribute name 'foo'");
QualifiedName expectedName = new QualifiedName("foo", String.Empty);
QualifiedName name = XmlParser.GetQualifiedAttributeName(text, text.Length);
Assert.AreEqual(expectedName, name, "Should have retrieved the attribute name 'foo'");
}
[Test]
public void SuccessTest5()
{
string text = " foo = \"";
Assert.AreEqual("foo", XmlParser.GetAttributeName(text, text.Length), "Should have retrieved the attribute name 'foo'");
QualifiedName expectedName = new QualifiedName("foo", String.Empty);
QualifiedName name = XmlParser.GetQualifiedAttributeName(text, text.Length);
Assert.AreEqual(expectedName, name, "Should have retrieved the attribute name 'foo'");
}
[Test]
public void SuccessTest6()
{
string text = " foo = '#";
Assert.AreEqual("foo", XmlParser.GetAttributeName(text, text.Length), "Should have retrieved the attribute name 'foo'");
QualifiedName expectedName = new QualifiedName("foo", String.Empty);
QualifiedName name = XmlParser.GetQualifiedAttributeName(text, text.Length);
Assert.AreEqual(expectedName, name, "Should have retrieved the attribute name 'foo'");
}
[Test]
public void FailureTest1()
{
string text = "foo=";
Assert.AreEqual(String.Empty, XmlParser.GetAttributeName(text, text.Length), "Should have retrieved the attribute name 'foo'");
Assert.IsNull(XmlParser.GetQualifiedAttributeName(text, text.Length));
}
[Test]
public void FailureTest2()
{
string text = "foo=<";
Assert.AreEqual(String.Empty, XmlParser.GetAttributeName(text, text.Length), "Should have retrieved the attribute name 'foo'");
Assert.IsNull(XmlParser.GetQualifiedAttributeName(text, text.Length));
}
[Test]
public void FailureTest3()
{
string text = "a";
Assert.AreEqual(String.Empty, XmlParser.GetAttributeName(text, text.Length));
Assert.IsNull(XmlParser.GetQualifiedAttributeName(text, text.Length));
}
[Test]
public void FailureTest4()
{
string text = " a";
Assert.AreEqual(String.Empty, XmlParser.GetAttributeName(text, text.Length));
Assert.IsNull(XmlParser.GetQualifiedAttributeName(text, text.Length));
}
[Test]
public void EmptyString()
{
Assert.AreEqual(String.Empty, XmlParser.GetAttributeName(String.Empty, 10));
Assert.IsNull(XmlParser.GetQualifiedAttributeName(String.Empty, 10));
}
[Test]
public void AttributeWithPrefix()
{
string text = " a:test=";
QualifiedName expectedName = new QualifiedName("test", String.Empty, "a");
QualifiedName name = XmlParser.GetQualifiedAttributeName(text, text.Length);
Assert.AreEqual(expectedName, name);
}
}
}

53
src/AddIns/DisplayBindings/XmlEditor/Test/Parser/AttributeNameUnderCursorTests.cs

@ -18,63 +18,98 @@ namespace XmlEditor.Tests.Parser @@ -18,63 +18,98 @@ namespace XmlEditor.Tests.Parser
public void SuccessTest1()
{
string text = "<a foo";
Assert.AreEqual("foo", XmlParser.GetAttributeNameAtIndex(text, text.Length));
QualifiedName expectedName = new QualifiedName("foo", String.Empty);
QualifiedName name = XmlParser.GetQualifiedAttributeNameAtIndex(text, text.Length);
Assert.AreEqual(expectedName, name);
}
[Test]
public void SuccessTest2()
{
string text = "<a foo";
Assert.AreEqual("foo", XmlParser.GetAttributeNameAtIndex(text, text.IndexOf("foo")));
QualifiedName expectedName = new QualifiedName("foo", String.Empty);
QualifiedName name = XmlParser.GetQualifiedAttributeNameAtIndex(text, text.IndexOf("foo"));
Assert.AreEqual(expectedName, name);
}
[Test]
public void SuccessTest3()
{
string text = "<a foo";
Assert.AreEqual("foo", XmlParser.GetAttributeNameAtIndex(text, text.IndexOf("oo")));
QualifiedName expectedName = new QualifiedName("foo", String.Empty);
QualifiedName name = XmlParser.GetQualifiedAttributeNameAtIndex(text, text.IndexOf("oo"));
Assert.AreEqual(expectedName, name);
}
[Test]
public void SuccessTest4()
{
string text = "<a foo";
Assert.AreEqual("foo", XmlParser.GetAttributeNameAtIndex(text, text.Length - 2));
QualifiedName expectedName = new QualifiedName("foo", String.Empty);
QualifiedName name = XmlParser.GetQualifiedAttributeNameAtIndex(text, text.Length - 2);
Assert.AreEqual(expectedName, name);
}
[Test]
public void SuccessTest5()
{
string text = "<a foo=";
Assert.AreEqual("foo", XmlParser.GetAttributeNameAtIndex(text, 3));
QualifiedName expectedName = new QualifiedName("foo", String.Empty);
QualifiedName name = XmlParser.GetQualifiedAttributeNameAtIndex(text, 3);
Assert.AreEqual(expectedName, name);
}
[Test]
public void SuccessTest6()
{
string text = "<a foo=";
Assert.AreEqual("foo", XmlParser.GetAttributeNameAtIndex(text, text.Length));
QualifiedName expectedName = new QualifiedName("foo", String.Empty);
QualifiedName name = XmlParser.GetQualifiedAttributeNameAtIndex(text, text.Length);
Assert.AreEqual(expectedName, name);
}
[Test]
public void SuccessTest7()
{
string text = "<a foo='";
Assert.AreEqual("foo", XmlParser.GetAttributeNameAtIndex(text, text.Length));
QualifiedName expectedName = new QualifiedName("foo", String.Empty);
QualifiedName name = XmlParser.GetQualifiedAttributeNameAtIndex(text, text.Length);
Assert.AreEqual(expectedName, name);
}
[Test]
public void SuccessTest8()
{
string text = "<a type='a";
Assert.AreEqual("type", XmlParser.GetAttributeNameAtIndex(text, text.Length));
QualifiedName expectedName = new QualifiedName("type", String.Empty);
QualifiedName name = XmlParser.GetQualifiedAttributeNameAtIndex(text, text.Length);
Assert.AreEqual(expectedName, name);
}
[Test]
public void SuccessTest9()
{
string text = "<a type='a'";
Assert.AreEqual("type", XmlParser.GetAttributeNameAtIndex(text, text.Length - 1));
QualifiedName expectedName = new QualifiedName("type", String.Empty);
QualifiedName name = XmlParser.GetQualifiedAttributeNameAtIndex(text, text.Length - 1);
Assert.AreEqual(expectedName, name);
}
[Test]
public void AttributeNameWithPrefix()
{
string text = "<a x:test=";
QualifiedName expectedName = new QualifiedName("test", String.Empty, "x");
QualifiedName name = XmlParser.GetQualifiedAttributeNameAtIndex(text, text.Length);
Assert.AreEqual(expectedName, name);
}
[Test]
public void AttributeNameWithPrefix2()
{
string text = "<a xab:test=";
QualifiedName expectedName = new QualifiedName("test", String.Empty, "xab");
QualifiedName name = XmlParser.GetQualifiedAttributeNameAtIndex(text, text.IndexOf("xa"));
Assert.AreEqual(expectedName, name);
}
}
}

Loading…
Cancel
Save