From 0f95a49a19292333bdbe9703a06f0bc33258e885 Mon Sep 17 00:00:00 2001 From: gumme Date: Mon, 26 May 2014 14:35:44 +0200 Subject: [PATCH] Added MarkupExtensionModelTests for testing various strings being used with both shorthand and element style markup extensions. Fixed a bug in MarkupExtensionPrinter to make the tests pass; it now handles spaces and backslashes correctly when printing shorthand markup extensions. --- .../Designer/MarkupExtensionModelTests.cs | 132 ++++++++++++++++++ .../Tests/WpfDesign.Tests.csproj | 1 + .../Project/MarkupExtensionPrinter.cs | 16 ++- .../WpfDesign.XamlDom/Project/XamlObject.cs | 2 +- 4 files changed, 148 insertions(+), 3 deletions(-) create mode 100644 src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Tests/Designer/MarkupExtensionModelTests.cs diff --git a/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Tests/Designer/MarkupExtensionModelTests.cs b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Tests/Designer/MarkupExtensionModelTests.cs new file mode 100644 index 0000000000..4d51eb94c8 --- /dev/null +++ b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Tests/Designer/MarkupExtensionModelTests.cs @@ -0,0 +1,132 @@ +// Copyright (c) 2014 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.Linq; +using System.Text; +using System.Windows.Controls; +using System.Windows.Markup; +using NUnit.Framework; + +namespace ICSharpCode.WpfDesign.Tests.Designer +{ + [TestFixture] + public class MarkupExtensionModelTests : ModelTestHelper + { + private const string PathWithCommasAndSpaces = "C:\\Folder A\\Sub,Folder,A\\SubFolderB\\file,with,commas and spaces.txt"; + private const string Simple = "AbcDef"; + + [Test] + public void ElementMarkupExtensionWithSimpleString() + { + TestMarkupExtensionPrinter(Simple, true); + } + + [Test] + public void ShorthandMarkupExtensionWithSimpleString() + { + TestMarkupExtensionPrinter(Simple, false); + } + + [Test] + public void ElementMarkupExtensionWithFilePathString() + { + TestMarkupExtensionPrinter(PathWithCommasAndSpaces, true); + } + + [Test] + public void ShorthandMarkupExtensionWithFilePathString() + { + TestMarkupExtensionPrinter(PathWithCommasAndSpaces, false); + } + + private void TestMarkupExtensionPrinter(string s, bool useElementStyle) + { + var checkBoxItem = CreateCanvasContext(""); + var tagProp = checkBoxItem.Properties["Tag"]; + + tagProp.SetValue(new DataExtension()); + tagProp.Value.Properties["Data"].SetValue(s); + + string expectedXaml; + + if (useElementStyle) { + // Setting this should force element style + tagProp.Value.Properties["Object"].SetValue(new ExampleClass()); + + expectedXaml = @" + + + + + + + +"; + } else { + StringBuilder sb = new StringBuilder(""); + + expectedXaml = sb.ToString(); + } + + AssertCanvasDesignerOutput(expectedXaml, checkBoxItem.Context); + AssertLog(""); + + // The following tests that the official XamlReader is parsing the resulting xaml into the + // same string that we are testing, regardless if element or shorthand style is being used. + + string xaml = expectedXaml.Insert(" + diff --git a/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/MarkupExtensionPrinter.cs b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/MarkupExtensionPrinter.cs index a1a849ad2d..d7b1b1949e 100644 --- a/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/MarkupExtensionPrinter.cs +++ b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/MarkupExtensionPrinter.cs @@ -93,8 +93,20 @@ namespace ICSharpCode.WpfDesign.XamlDom sb.Append("="); var value = property.PropertyValue; - if (value is XamlTextValue) { - sb.Append((value as XamlTextValue).Text); + var textValue = value as XamlTextValue; + if (textValue != null) { + string text = textValue.Text; + bool containsSpace = text.Contains(' '); + + if(containsSpace) { + sb.Append('\''); + } + + sb.Append(text.Replace("\\", "\\\\")); + + if(containsSpace) { + sb.Append('\''); + } } else if (value is XamlObject) { sb.Append(Print(value as XamlObject)); } diff --git a/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/XamlObject.cs b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/XamlObject.cs index 5af9e0188b..e8275a34e4 100644 --- a/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/XamlObject.cs +++ b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/XamlObject.cs @@ -223,7 +223,7 @@ namespace ICSharpCode.WpfDesign.XamlDom foreach(XamlObject propXamlObject in holder.Properties.Where((prop) => prop.IsSet).Select((prop) => prop.PropertyValue).OfType()) { XamlObject innerHolder; bool updateResult = propXamlObject.UpdateXmlAttribute(true, out innerHolder); - Debug.Assert(updateResult); + Debug.Assert(updateResult || innerHolder == null); if (propXamlObject == this) isThisUpdated = true;