diff options
author | Iain McCoy <iainmc@mono-cvs.ximian.com> | 2005-07-17 18:05:44 +0400 |
---|---|---|
committer | Iain McCoy <iainmc@mono-cvs.ximian.com> | 2005-07-17 18:05:44 +0400 |
commit | 3e30285188f0440ede5dba2783fb9edd5aecaf77 (patch) | |
tree | b43e08959cd9e22bc80bbd11991bfea45954a6b5 /mcs | |
parent | e7f6647056372a3664d2aae67a61b6548c76ffc8 (diff) |
2005-07-17 Iain McCoy <iain@mccoy.id.au>
* Test/XamlParser.cs: add a bunch of tests
* Mono.Windows.Serialization/XamlParser.cs: improve error reporting,
don't accept elements without namespaces
svn path=/trunk/mcs/; revision=47365
Diffstat (limited to 'mcs')
3 files changed, 165 insertions, 24 deletions
diff --git a/mcs/class/PresentationFramework/ChangeLog b/mcs/class/PresentationFramework/ChangeLog index 659b77faf47..3061d75b2ec 100644 --- a/mcs/class/PresentationFramework/ChangeLog +++ b/mcs/class/PresentationFramework/ChangeLog @@ -1,3 +1,9 @@ +2005-07-17 Iain McCoy <iain@mccoy.id.au> + + * Test/XamlParser.cs: add a bunch of tests + * Mono.Windows.Serialization/XamlParser.cs: improve error reporting, + don't accept elements without namespaces + 2005-07-16 Iain McCoy <iain@mccoy.id.au> * Test/XamlParser.cs: fix a bug in property tests, add tests for diff --git a/mcs/class/PresentationFramework/Mono.Windows.Serialization/XamlParser.cs b/mcs/class/PresentationFramework/Mono.Windows.Serialization/XamlParser.cs index dbf5ef39c45..b79a7777bf9 100644 --- a/mcs/class/PresentationFramework/Mono.Windows.Serialization/XamlParser.cs +++ b/mcs/class/PresentationFramework/Mono.Windows.Serialization/XamlParser.cs @@ -79,7 +79,7 @@ namespace Mono.Windows.Serialization { { while (reader.Read()) { Debug.WriteLine("NOW PARSING: " + reader.NodeType + "; " + reader.Name + "; " + reader.Value); - if (begun && currentState == null && reader.NodeType != XmlNodeType.Whitespace) + if (begun && currentState == null && reader.NodeType != XmlNodeType.Whitespace && reader.NodeType != XmlNodeType.Comment) throw new Exception("Too far: " + reader.NodeType + ", " + reader.Name); if (currentState != null && currentState.type == CurrentType.Code) { @@ -124,6 +124,8 @@ namespace Mono.Windows.Serialization { void parseElement() { + if (reader.NamespaceURI == "") + throw new Exception("No xml namespace specified."); if (reader.LocalName == "Code" && reader.NamespaceURI == XAML_NAMESPACE) { parseCodeElement(); return; @@ -192,9 +194,7 @@ namespace Mono.Windows.Serialization { PropertyInfo prop = currentType.GetProperty(propertyName); if (prop == null) { - Console.WriteLine("Property " + propertyName + " not found on " + currentType.Name); - return; - // TODO: exception + throw new Exception("Property '" + propertyName + "' not found on '" + currentType.Name + "'."); } push(CurrentType.Property, prop); @@ -227,6 +227,8 @@ namespace Mono.Windows.Serialization { bool isEmpty = reader.IsEmptyElement; parent = mapper.GetType(reader.NamespaceURI, reader.Name); + if (parent == null) + throw new Exception("Class '" + reader.Name + "' not found."); if (parent.GetInterface("System.Windows.Serialization.IAddChild") == null) {} //TODO: throw exception if (currentState == null) { @@ -305,11 +307,8 @@ namespace Mono.Windows.Serialization { PropertyInfo prop = currentType.GetProperty(propertyName); if (parsedAsEventProperty(currentType, propertyName)) return; - if (prop == null) { - Console.WriteLine("Property " + propertyName + " not found on " + currentType.Name); - return; - // TODO: throw exception - } + if (prop == null) + throw new Exception ("Property '" + propertyName + "' not found on '" + currentType.Name + "'."); writer.CreateProperty(prop); @@ -358,7 +357,7 @@ namespace Mono.Windows.Serialization { { FieldInfo propField = typeAttachedTo.GetField(propertyName + "Property"); if (propField == null) - throw new Exception("Property " + propertyName + " does not exist on " + typeAttachedTo.Name); + throw new Exception("Property '" + propertyName + "' does not exist on '" + typeAttachedTo.Name + "'."); return (DependencyProperty)propField.GetValue(null); } diff --git a/mcs/class/PresentationFramework/Test/XamlParser.cs b/mcs/class/PresentationFramework/Test/XamlParser.cs index 79b51025360..7c2d44e0ed5 100644 --- a/mcs/class/PresentationFramework/Test/XamlParser.cs +++ b/mcs/class/PresentationFramework/Test/XamlParser.cs @@ -9,6 +9,7 @@ using NUnit.Framework; using System; using System.Diagnostics; using System.IO; +using System.Xml; using System.Reflection; using System.Windows; using Mono.Windows.Serialization; @@ -33,6 +34,42 @@ public class XamlParserTest : Assertion { [Test] public void TestTopLevel() { + string s = "<ConsoleApp xmlns=\"console\"></ConsoleApp>"; + ParserTester pt = new ParserTester(MAPPING + s, + new CreateTopLevelHappening(typeof(ConsoleApp), null), + new EndObjectHappening(), + new FinishHappening()); + pt.Test(); + } + + [Test] + [ExpectedException(typeof(Exception), "Class 'ConsoleApple' not found.")] + public void TestTopLevelWithIncorrectClassName() + { + string s = "<ConsoleApple xmlns=\"console\"></ConsoleApple>"; + ParserTester pt = new ParserTester(MAPPING + s, + new CreateTopLevelHappening(typeof(ConsoleApp), null), + new EndObjectHappening(), + new FinishHappening()); + pt.Test(); + } + + [Test] + [ExpectedException(typeof(XmlException))] + public void TestTopLevelWithWrongEndingTag() + { + string s = "<ConsoleApp xmlns=\"console\"></ConsoleApple>"; + ParserTester pt = new ParserTester(MAPPING + s, + new CreateTopLevelHappening(typeof(ConsoleApp), null), + new EndObjectHappening(), + new FinishHappening()); + pt.Test(); + } + + [Test] + [ExpectedException(typeof(Exception), "No xml namespace specified.")] + public void TestTopLevelWithoutNamespace() + { string s = "<ConsoleApp></ConsoleApp>"; ParserTester pt = new ParserTester(MAPPING + s, new CreateTopLevelHappening(typeof(ConsoleApp), null), @@ -40,10 +77,11 @@ public class XamlParserTest : Assertion { new FinishHappening()); pt.Test(); } + [Test] - public void TestTopLevelWithClass() + public void TestTopLevelWithClassName() { - string s = "<ConsoleApp xmlns:x=\"http://schemas.microsoft.com/winfx/xaml/2005\" x:Class=\"nnn\">\n"+ + string s = "<ConsoleApp xmlns=\"console\" xmlns:x=\"http://schemas.microsoft.com/winfx/xaml/2005\" x:Class=\"nnn\">\n"+ "</ConsoleApp>"; ParserTester pt = new ParserTester(MAPPING + s, new CreateTopLevelHappening(typeof(ConsoleApp), "nnn"), @@ -53,10 +91,10 @@ public class XamlParserTest : Assertion { } [Test] - [ExpectedException(typeof(Exception))] + [ExpectedException(typeof(Exception), "The XAML Name attribute can not be applied to top level elements\nDo you mean the Class attribute?")] public void TestTopLevelWithName() { - string s = "<ConsoleApp xmlns:x=\"http://schemas.microsoft.com/winfx/xaml/2005\" x:Name=\"nnn\">\n"+ + string s = "<ConsoleApp xmlns=\"console\" xmlns:x=\"http://schemas.microsoft.com/winfx/xaml/2005\" x:Name=\"nnn\">\n"+ "</ConsoleApp>"; ParserTester pt = new ParserTester(MAPPING + s, new CreateTopLevelHappening(typeof(ConsoleApp), "nnn"), // this is a lie, actually we expect @@ -69,7 +107,7 @@ public class XamlParserTest : Assertion { [Test] public void TestSimplestAddChild() { - string s = "<ConsoleApp xmlns:x=\"http://schemas.microsoft.com/winfx/xaml/2005\">\n"+ + string s = "<ConsoleApp xmlns=\"console\" xmlns:x=\"http://schemas.microsoft.com/winfx/xaml/2005\">\n"+ "<ConsoleWriter></ConsoleWriter>" + "</ConsoleApp>"; ParserTester pt = new ParserTester(MAPPING + s, @@ -82,9 +120,26 @@ public class XamlParserTest : Assertion { } [Test] + [ExpectedException(typeof(Exception), "Class 'ConsoleWritttter' not found.")] + public void TestSimplestAddChildWithIncorrectName() + { + string s = "<ConsoleApp xmlns=\"console\" xmlns:x=\"http://schemas.microsoft.com/winfx/xaml/2005\">\n"+ + "<ConsoleWritttter></ConsoleWritttter>" + + "</ConsoleApp>"; + ParserTester pt = new ParserTester(MAPPING + s, + new CreateTopLevelHappening(typeof(ConsoleApp), null), + new CreateObjectHappening(typeof(ConsoleWriter), null), + new EndObjectHappening(), + new EndObjectHappening(), + new FinishHappening()); + pt.Test(); + } + + + [Test] public void TestSimplestAddChildAndText() { - string s = "<ConsoleApp xmlns:x=\"http://schemas.microsoft.com/winfx/xaml/2005\">\n"+ + string s = "<ConsoleApp xmlns=\"console\" xmlns:x=\"http://schemas.microsoft.com/winfx/xaml/2005\">\n"+ "<ConsoleWriter>Hello</ConsoleWriter>" + "</ConsoleApp>"; ParserTester pt = new ParserTester(MAPPING + s, @@ -100,7 +155,7 @@ public class XamlParserTest : Assertion { [Test] public void TestTextProperty() { - string s = "<ConsoleApp xmlns:x=\"http://schemas.microsoft.com/winfx/xaml/2005\">\n"+ + string s = "<ConsoleApp xmlns=\"console\" xmlns:x=\"http://schemas.microsoft.com/winfx/xaml/2005\">\n"+ "<ConsoleWriter Text=\"Hello\" />" + "</ConsoleApp>"; ParserTester pt = new ParserTester(MAPPING + s, @@ -114,10 +169,30 @@ public class XamlParserTest : Assertion { new FinishHappening()); pt.Test(); } + + [Test] + [ExpectedException(typeof(Exception), "Property 'Texxxt' not found on 'ConsoleWriter'.")] + public void TestTextPropertyWithIncorrectName() + { + string s = "<ConsoleApp xmlns=\"console\" xmlns:x=\"http://schemas.microsoft.com/winfx/xaml/2005\">\n"+ + "<ConsoleWriter Texxxt=\"Hello\" />" + + "</ConsoleApp>"; + ParserTester pt = new ParserTester(MAPPING + s, + new CreateTopLevelHappening(typeof(ConsoleApp), null), + new CreateObjectHappening(typeof(ConsoleWriter), null), + new CreatePropertyHappening(typeof(ConsoleWriter).GetProperty("Text")), + new CreatePropertyTextHappening("Hello", typeof(ConsoleValue)), + new EndPropertyHappening(), + new EndObjectHappening(), //ConsoleWriter + new EndObjectHappening(), //ConsoleApp + new FinishHappening()); + pt.Test(); + } + [Test] public void TestTextPropertyAsElement() { - string s = "<ConsoleApp xmlns:x=\"http://schemas.microsoft.com/winfx/xaml/2005\">\n"+ + string s = "<ConsoleApp xmlns=\"console\" xmlns:x=\"http://schemas.microsoft.com/winfx/xaml/2005\">\n"+ "<ConsoleWriter><ConsoleWriter.Text>Hello</ConsoleWriter.Text></ConsoleWriter>\n" + "</ConsoleApp>"; ParserTester pt = new ParserTester(MAPPING + s, @@ -133,9 +208,28 @@ public class XamlParserTest : Assertion { } [Test] - public void testDependencyProperty() + [ExpectedException(typeof(Exception), "Property 'Texxxt' not found on 'ConsoleWriter'.")] + public void TestTextPropertyAsElementWithIncorrectName() { - string s = "<ConsoleApp xmlns:x=\"http://schemas.microsoft.com/winfx/xaml/2005\">\n"+ + string s = "<ConsoleApp xmlns=\"console\" xmlns:x=\"http://schemas.microsoft.com/winfx/xaml/2005\">\n"+ + "<ConsoleWriter><ConsoleWriter.Texxxt>Hello</ConsoleWriter.Text></ConsoleWriter>\n" + + "</ConsoleApp>"; + ParserTester pt = new ParserTester(MAPPING + s, + new CreateTopLevelHappening(typeof(ConsoleApp), null), + new CreateObjectHappening(typeof(ConsoleWriter), null), + new CreatePropertyHappening(typeof(ConsoleWriter).GetProperty("Text")), + new CreatePropertyTextHappening("Hello", typeof(ConsoleValue)), + new EndPropertyHappening(), + new EndObjectHappening(), //ConsoleWriter + new EndObjectHappening(), //ConsoleApp + new FinishHappening()); + pt.Test(); + } + + [Test] + public void TestDependencyProperty() + { + string s = "<ConsoleApp xmlns=\"console\" xmlns:x=\"http://schemas.microsoft.com/winfx/xaml/2005\">\n"+ "<ConsoleWriter ConsoleApp.Repetitions=\"3\" />" + "</ConsoleApp>"; ParserTester pt = new ParserTester(MAPPING + s, @@ -149,11 +243,31 @@ public class XamlParserTest : Assertion { new FinishHappening()); pt.Test(); } + + [Test] + [ExpectedException(typeof(Exception), "Property 'Reps' does not exist on 'ConsoleApp'.")] + public void TestDependencyPropertyWithIncorrectName() + { + string s = "<ConsoleApp xmlns=\"console\" xmlns:x=\"http://schemas.microsoft.com/winfx/xaml/2005\">\n"+ + "<ConsoleWriter ConsoleApp.Reps=\"3\" />" + + "</ConsoleApp>"; + ParserTester pt = new ParserTester(MAPPING + s, + new CreateTopLevelHappening(typeof(ConsoleApp), null), + new CreateObjectHappening(typeof(ConsoleWriter), null), + new CreateDependencyPropertyHappening(typeof(ConsoleApp), "Repetitions", typeof(int)), + new CreateDependencyPropertyTextHappening("3"), + new EndDependencyPropertyHappening(), + new EndObjectHappening(), // ConsoleWriter + new EndObjectHappening(), // ConsoleApp + new FinishHappening()); + pt.Test(); + } + [Test] - public void testDependencyPropertyAsChildElement() + public void TestDependencyPropertyAsChildElement() { - string s = "<ConsoleApp xmlns:x=\"http://schemas.microsoft.com/winfx/xaml/2005\">\n"+ + string s = "<ConsoleApp xmlns=\"console\" xmlns:x=\"http://schemas.microsoft.com/winfx/xaml/2005\">\n"+ "<ConsoleWriter><ConsoleApp.Repetitions>3</ConsoleApp.Repetitions></ConsoleWriter>" + "</ConsoleApp>"; ParserTester pt = new ParserTester(MAPPING + s, @@ -167,9 +281,31 @@ public class XamlParserTest : Assertion { new FinishHappening()); pt.Test(); } - public void testObjectAsPropertyValue() + + [Test] + [ExpectedException(typeof(Exception), "Property 'Reps' does not exist on 'ConsoleApp'.")] + public void TestDependencyPropertyAsChildElementWithIncorrectName() + { + string s = "<ConsoleApp xmlns=\"console\" xmlns:x=\"http://schemas.microsoft.com/winfx/xaml/2005\">\n"+ + "<ConsoleWriter><ConsoleApp.Reps>3</ConsoleApp.Reps></ConsoleWriter>" + + "</ConsoleApp>"; + ParserTester pt = new ParserTester(MAPPING + s, + new CreateTopLevelHappening(typeof(ConsoleApp), null), + new CreateObjectHappening(typeof(ConsoleWriter), null), + new CreateDependencyPropertyHappening(typeof(ConsoleApp), "Repetitions", typeof(int)), + new CreateDependencyPropertyTextHappening("3"), + new EndDependencyPropertyHappening(), + new EndObjectHappening(), // ConsoleWriter + new EndObjectHappening(), // ConsoleApp + new FinishHappening()); + pt.Test(); + } + + + [Test] + public void TestObjectAsPropertyValue() { - string s = "<ConsoleApp xmlns:x=\"http://schemas.microsoft.com/winfx/xaml/2005\">\n"+ + string s = "<ConsoleApp xmlns=\"console\" xmlns:x=\"http://schemas.microsoft.com/winfx/xaml/2005\">\n"+ "<ConsoleReader>\n" + "<ConsoleReader.Prompt><ConsoleWriter /></ConsoleReader.Prompt>\n" + "</ConsoleReader>\n" + |