diff options
author | Iain McCoy <iainmc@mono-cvs.ximian.com> | 2005-07-18 16:07:20 +0400 |
---|---|---|
committer | Iain McCoy <iainmc@mono-cvs.ximian.com> | 2005-07-18 16:07:20 +0400 |
commit | 23029a245bd3e4d971d6c6753bdad72ca8aa2f19 (patch) | |
tree | 430f209c48771bafe1eb5b96ec953891f6f585f6 /mcs/class/PresentationFramework | |
parent | 6529b737cd119309bb564ef59ab598a5bad8bf52 (diff) | |
parent | ceee5da66642296a07cbfdb66a0c7ab0a7c942fc (diff) |
2005-07-18 Iain McCoy <iain@mccoy.id.au>
* Test/CodeWriter.cs: added a bunch of tests for the code generator
* Mono.Windows.Serialization/CodeWriter.cs: fixed a few bugs turned up
by the tests
* Test/CodeWriter.cs, Test/XamlParser.cs: added copyright/license
notices
svn path=/trunk/mcs/; revision=47380
Diffstat (limited to 'mcs/class/PresentationFramework')
5 files changed, 333 insertions, 17 deletions
diff --git a/mcs/class/PresentationFramework/ChangeLog b/mcs/class/PresentationFramework/ChangeLog index a30c26d2d2a..9ee72cc323a 100644 --- a/mcs/class/PresentationFramework/ChangeLog +++ b/mcs/class/PresentationFramework/ChangeLog @@ -1,5 +1,13 @@ 2005-07-18 Iain McCoy <iain@mccoy.id.au> + * Test/CodeWriter.cs: added a bunch of tests for the code generator + * Mono.Windows.Serialization/CodeWriter.cs: fixed a few bugs turned up + by the tests + * Test/CodeWriter.cs, Test/XamlParser.cs: added copyright/license + notices + +2005-07-18 Iain McCoy <iain@mccoy.id.au> + * Mono.Windows.Serialization/XamlParser.cs: some refactoring and improvements in error reporting * Test/XamlParser.cs: added a few tests and improved the readability diff --git a/mcs/class/PresentationFramework/Mono.Windows.Serialization/CodeWriter.cs b/mcs/class/PresentationFramework/Mono.Windows.Serialization/CodeWriter.cs index bf5fc697676..42dabd0dff2 100644 --- a/mcs/class/PresentationFramework/Mono.Windows.Serialization/CodeWriter.cs +++ b/mcs/class/PresentationFramework/Mono.Windows.Serialization/CodeWriter.cs @@ -68,11 +68,13 @@ namespace Mono.Windows.Serialization { } int endNamespaceName = className.LastIndexOf("."); string clrNamespace; - if (endNamespaceName < 0) + if (endNamespaceName < 0) { clrNamespace = "DefaultNamespace"; - else + } else { clrNamespace = className.Substring(0, endNamespaceName); + className = className.Substring(endNamespaceName+1); + } CodeNamespace ns = new CodeNamespace(clrNamespace); ((CodeCompileUnit)objects[0]).Namespaces.Add(ns); @@ -111,10 +113,9 @@ namespace Mono.Windows.Serialization { if (!nameClashes.ContainsKey(varName)) nameClashes[varName] = 0; - else { - nameClashes[varName] = 1 + (int)nameClashes[varName]; - varName += (int)nameClashes[varName]; - } + + nameClashes[varName] = 1 + (int)nameClashes[varName]; + varName += (int)nameClashes[varName]; if (isDefaultName) { @@ -289,10 +290,8 @@ namespace Mono.Windows.Serialization { if (!nameClashes.ContainsKey(varName)) nameClashes[varName] = 0; - else { - nameClashes[varName] = 1 + (int)nameClashes[varName]; - varName += (int)nameClashes[varName]; - } + nameClashes[varName] = 1 + (int)nameClashes[varName]; + varName += (int)nameClashes[varName]; if (isDefaultName) { diff --git a/mcs/class/PresentationFramework/PresentationFramework_test.dll.sources b/mcs/class/PresentationFramework/PresentationFramework_test.dll.sources index 1a4ffb83b0f..56fd45e2566 100644 --- a/mcs/class/PresentationFramework/PresentationFramework_test.dll.sources +++ b/mcs/class/PresentationFramework/PresentationFramework_test.dll.sources @@ -1 +1,2 @@ XamlParser.cs +CodeWriter.cs diff --git a/mcs/class/PresentationFramework/Test/CodeWriter.cs b/mcs/class/PresentationFramework/Test/CodeWriter.cs new file mode 100644 index 00000000000..8c030e4bedc --- /dev/null +++ b/mcs/class/PresentationFramework/Test/CodeWriter.cs @@ -0,0 +1,281 @@ +// +// CodeWriter.cs - NUnit Test Cases for the xaml code generator +// +// Author: +// Iain McCoy (iain@mccoy.id.au) +// +// (C) 2005 Iain McCoy +// +// 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 NUnit.Framework; + +using System; +using System.Diagnostics; +using System.IO; +using System.Xml; +using System.Reflection; +using System.Windows; +using System.CodeDom.Compiler; +using Mono.Windows.Serialization; +using Xaml.TestVocab.Console; + +namespace MonoTests.System.Windows.Serialization +{ + +[TestFixture] +public class CodeWriterTest { + CodeWriter cw; + StringWriter w; + + [SetUp] + public void GetReady() + { + ICodeGenerator generator = (new Microsoft.CSharp.CSharpCodeProvider()).CreateGenerator(); + w = new StringWriter(); + cw = new CodeWriter(generator, w, false); + } + + [TearDown] + public void Clean() {} + + [Test] + public void TestTopLevel() + { + cw.CreateTopLevel(typeof(ConsoleApp), null); + cw.EndObject(); + cw.Finish(); + compare( + "namespace DefaultNamespace {\n"+ + " public class derivedConsoleApp: Xaml.TestVocab.Console.ConsoleApp {\n" + + " private derivedConsoleApp() {\n"+ + " }\n" + + " }\n" + + "}" + ); + } + + [Test] + public void TestTopLevelWithClassName() + { + cw.CreateTopLevel(typeof(ConsoleApp), "MyConsoleApp"); + cw.EndObject(); + cw.Finish(); + compare( + "namespace DefaultNamespace {\n"+ + " public class MyConsoleApp: Xaml.TestVocab.Console.ConsoleApp {\n" + + " private MyConsoleApp() {\n"+ + " }\n" + + " }\n" + + "}" + ); + } + + [Test] + public void TestTopLevelWithClassNameAndNamespace() + { + cw.CreateTopLevel(typeof(ConsoleApp), "Test.Thing.MyConsoleApp"); + cw.EndObject(); + cw.Finish(); + compare( + "namespace Test.Thing {\n"+ + " public class MyConsoleApp: Xaml.TestVocab.Console.ConsoleApp {\n" + + " private MyConsoleApp() {\n"+ + " }\n" + + " }\n" + + "}" + ); + } + + [Test] + public void TestSimplestAddChild() + { + cw.CreateTopLevel(typeof(ConsoleApp), null); + cw.CreateObject(typeof(ConsoleWriter), null); + cw.EndObject(); + cw.EndObject(); + cw.Finish(); + compare( + "namespace DefaultNamespace {\n"+ + " public class derivedConsoleApp: Xaml.TestVocab.Console.ConsoleApp {\n" + + " private derivedConsoleApp() {\n"+ + " Xaml.TestVocab.Console.ConsoleWriter consoleWriter1 = new Xaml.TestVocab.Console.ConsoleWriter();\n"+ + " this.AddChild(consoleWriter1);\n" + + " }\n" + + " }\n" + + "}" + ); + } + + [Test] + public void TestSimplestAddChildAndText() + { + cw.CreateTopLevel(typeof(ConsoleApp), null); + cw.CreateObject(typeof(ConsoleWriter), null); + cw.CreateElementText("Hello"); + cw.EndObject(); + cw.EndObject(); + cw.Finish(); + compare( + "namespace DefaultNamespace {\n"+ + " public class derivedConsoleApp: Xaml.TestVocab.Console.ConsoleApp {\n" + + " private derivedConsoleApp() {\n"+ + " Xaml.TestVocab.Console.ConsoleWriter consoleWriter1 = new Xaml.TestVocab.Console.ConsoleWriter();\n"+ + " this.AddChild(consoleWriter1);\n" + + " consoleWriter1.AddText(\"Hello\");\n" + + " }\n" + + " }\n" + + "}" + ); + } + + [Test] + public void TestTextProperty() + { + cw.CreateTopLevel(typeof(ConsoleApp), null); + cw.CreateObject(typeof(ConsoleWriter), null); + cw.CreateProperty(typeof(ConsoleWriter).GetProperty("Text")); + cw.CreatePropertyText("Hello", typeof(ConsoleValue)); + cw.EndProperty(); + cw.EndObject(); + cw.EndObject(); + cw.Finish(); + compare( + "namespace DefaultNamespace {\n"+ + " public class derivedConsoleApp: Xaml.TestVocab.Console.ConsoleApp {\n" + + " private derivedConsoleApp() {\n"+ + " Xaml.TestVocab.Console.ConsoleWriter consoleWriter1 = new Xaml.TestVocab.Console.ConsoleWriter();\n"+ + " this.AddChild(consoleWriter1);\n" + + " consoleWriter1.Text = ((Xaml.TestVocab.Console.ConsoleValue)(System.ComponentModel.TypeDescriptor.GetConverter(typeof(Xaml.TestVocab.Console.ConsoleValue)).ConvertFromString(\"Hello\")));\n" + + " }\n" + + " }\n" + + "}" + ); + } + + [Test] + public void TestDependencyProperty() + { + cw.CreateTopLevel(typeof(ConsoleApp), null); + cw.CreateObject(typeof(ConsoleWriter), null); + cw.CreateDependencyProperty(typeof(ConsoleApp), "Repetitions", typeof(int)); + cw.CreateDependencyPropertyText("3", typeof(int)); + cw.EndDependencyProperty(); + cw.EndObject(); + cw.EndObject(); + cw.Finish(); + compare( + "namespace DefaultNamespace {\n"+ + " public class derivedConsoleApp: Xaml.TestVocab.Console.ConsoleApp {\n" + + " private derivedConsoleApp() {\n"+ + " Xaml.TestVocab.Console.ConsoleWriter consoleWriter1 = new Xaml.TestVocab.Console.ConsoleWriter();\n"+ + " this.AddChild(consoleWriter1);\n" + + " int temp0;\n" + + " temp0 = ((int)(System.ComponentModel.TypeDescriptor.GetConverter(typeof(int)).ConvertFromString(\"3\")));\n" + + " Xaml.TestVocab.Console.ConsoleApp.SetRepetitions(consoleWriter1, temp0);\n" + + + " }\n" + + " }\n" + + "}" + ); + } + + [Test] + public void TestObjectAsPropertyValue() + { + cw.CreateTopLevel(typeof(ConsoleApp), null); + cw.CreateObject(typeof(ConsoleReader), null); + cw.CreateProperty(typeof(ConsoleReader).GetProperty("Prompt")); + cw.CreatePropertyObject(typeof(ConsoleWriter), null); + cw.EndPropertyObject(typeof(ConsoleWriter)); + cw.EndProperty(); + cw.EndObject(); + cw.EndObject(); + cw.Finish(); + compare( + "namespace DefaultNamespace {\n"+ + " public class derivedConsoleApp: Xaml.TestVocab.Console.ConsoleApp {\n" + + " private derivedConsoleApp() {\n"+ + " Xaml.TestVocab.Console.ConsoleReader consoleReader1 = new Xaml.TestVocab.Console.ConsoleReader();\n"+ + " this.AddChild(consoleReader1);\n" + + " Xaml.TestVocab.Console.ConsoleWriter consoleWriter1 = new Xaml.TestVocab.Console.ConsoleWriter();\n"+ + " consoleReader1.Prompt = consoleWriter1;\n" + + + " }\n" + + " }\n" + + "}" + ); + } + + + + private void compare(string expected) + { + int i, j; + string[] actualLines = w.ToString().Split('\n'); + for (i = 0; i < actualLines.Length; i++) { + if (actualLines[i].StartsWith("//")) { + actualLines[i] = null; + continue; + } + + j = 0; + while (j < actualLines[i].Length && + Char.IsWhiteSpace(actualLines[i][j])) { + j++; + } + if (j == actualLines[i].Length) { + actualLines[i] = null; + continue; + } + } + j = 0; + for (i = 0; i < actualLines.Length; i++) { + if (actualLines[i] != null) + actualLines[j++] = actualLines[i]; + } + string actual = String.Join("\n", actualLines, 0, j); + + string[] expectedLines = expected.Split('\n'); + for (i = 0; i < expectedLines.Length; i++) { + expectedLines[i] = replaceTabsAtLineStart(expectedLines[i]); + } + expected = String.Join("\n", expectedLines); + + Assert.AreEqual(expected, actual); + } + + string replaceTabsAtLineStart(string s) + { + if (s == "" || !Char.IsWhiteSpace(s[0])) + return s; + string remainder = s.Substring(1); + if (s[0] == '\t') + return " " + replaceTabsAtLineStart(remainder); + else + return remainder; + } + + +} + +} diff --git a/mcs/class/PresentationFramework/Test/XamlParser.cs b/mcs/class/PresentationFramework/Test/XamlParser.cs index 161beef041f..b2e7ffca3a1 100644 --- a/mcs/class/PresentationFramework/Test/XamlParser.cs +++ b/mcs/class/PresentationFramework/Test/XamlParser.cs @@ -1,9 +1,33 @@ +// // XamlParser.cs - NUnit Test Cases for the xaml parser // -// Iain McCoy (iain@mccoy.id.au) +// Author: +// Iain McCoy (iain@mccoy.id.au) +// +// (C) 2005 Iain McCoy // -// (C) iain@mccoy.id.au +// 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. +// +// +// +// // // As you may be aware, testing a parser is something of a beastly job. The // approach taken by these tests is to feed a file to XamlParser and see if it @@ -279,7 +303,7 @@ public class XamlParserTest : Assertion { new CreateTopLevelHappening(typeof(ConsoleApp), null), new CreateObjectHappening(typeof(ConsoleWriter), null), new CreateDependencyPropertyHappening(typeof(ConsoleApp), "Repetitions", typeof(int)), - new CreateDependencyPropertyTextHappening("3"), + new CreateDependencyPropertyTextHappening("3", typeof(int)), new EndDependencyPropertyHappening(), new EndObjectHappening(), // ConsoleWriter new EndObjectHappening(), // ConsoleApp @@ -298,7 +322,7 @@ public class XamlParserTest : Assertion { new CreateTopLevelHappening(typeof(ConsoleApp), null), new CreateObjectHappening(typeof(ConsoleWriter), null), new CreateDependencyPropertyHappening(typeof(ConsoleApp), "Repetitions", typeof(int)), - new CreateDependencyPropertyTextHappening("3"), + new CreateDependencyPropertyTextHappening("3", typeof(int)), new EndDependencyPropertyHappening(), new EndObjectHappening(), // ConsoleWriter new EndObjectHappening(), // ConsoleApp @@ -319,7 +343,7 @@ public class XamlParserTest : Assertion { new CreateTopLevelHappening(typeof(ConsoleApp), null), new CreateObjectHappening(typeof(ConsoleWriter), null), new CreateDependencyPropertyHappening(typeof(ConsoleApp), "Repetitions", typeof(int)), - new CreateDependencyPropertyTextHappening("3"), + new CreateDependencyPropertyTextHappening("3", typeof(int)), new EndDependencyPropertyHappening(), new EndObjectHappening(), // ConsoleWriter new EndObjectHappening(), // ConsoleApp @@ -340,7 +364,7 @@ public class XamlParserTest : Assertion { new CreateTopLevelHappening(typeof(ConsoleApp), null), new CreateObjectHappening(typeof(ConsoleWriter), null), new CreateDependencyPropertyHappening(typeof(ConsoleApp), "Repetitions", typeof(int)), - new CreateDependencyPropertyTextHappening("3"), + new CreateDependencyPropertyTextHappening("3", typeof(int)), new EndDependencyPropertyHappening(), new EndObjectHappening(), // ConsoleWriter new EndObjectHappening(), // ConsoleApp @@ -520,8 +544,10 @@ class CreateDependencyPropertyHappening : Happening class CreateDependencyPropertyTextHappening : Happening { public string text; - public CreateDependencyPropertyTextHappening(string text) { + public Type propertyType; + public CreateDependencyPropertyTextHappening(string text, Type propertyType) { this.text = text; + this.propertyType = propertyType; } } @@ -679,6 +705,7 @@ class ParserTester : XamlWriter { CreateDependencyPropertyTextHappening h = (CreateDependencyPropertyTextHappening)getHappening(); Assert.AreEqual(h.text, text); + Assert.AreEqual(h.propertyType, propertyType); } public void EndDependencyProperty(){ |