From 68b95027d8dda65b35be94e3eaac4e1dbf430a56 Mon Sep 17 00:00:00 2001 From: David Guida <1432872+mizrael@users.noreply.github.com> Date: Fri, 21 Jun 2024 19:48:09 -0400 Subject: [PATCH] added stylename property to controls (#676) --- .../Yaml/DeserializerValidTests.cs | 21 +++++++++++++++++++ .../Yaml/ValidSerializerTests.cs | 15 +++++++++++++ .../BuiltInControl/with-style.pa.yaml | 3 +++ .../BuiltInControl/with-style.pa.yaml | 3 +++ src/Persistence/Models/Control.cs | 2 ++ src/Persistence/Templates/ControlFactory.cs | 9 ++++++-- src/Persistence/Yaml/ControlConverter.cs | 1 + 7 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 src/Persistence.Tests/_TestData/ValidYaml-CI/BuiltInControl/with-style.pa.yaml create mode 100644 src/Persistence.Tests/_TestData/ValidYaml/BuiltInControl/with-style.pa.yaml diff --git a/src/Persistence.Tests/Yaml/DeserializerValidTests.cs b/src/Persistence.Tests/Yaml/DeserializerValidTests.cs index 77b55d6d..0a5e34e0 100644 --- a/src/Persistence.Tests/Yaml/DeserializerValidTests.cs +++ b/src/Persistence.Tests/Yaml/DeserializerValidTests.cs @@ -515,4 +515,25 @@ public void Deserialize_ShouldParse_Lists_of_Controls(string path, bool isContro list.First().Should().BeOfType(expectedType); list.First().Name.Should().Be(expectedName); } + + [TestMethod] + [DataRow(@"_TestData/ValidYaml{0}/BuiltInControl/with-style.pa.yaml", true, "this is my style")] + [DataRow(@"_TestData/ValidYaml{0}/BuiltInControl/with-style.pa.yaml", false, "this is my style")] + public void Deserialize_ShouldParse_StyleName( + string path, + bool isControlIdentifiers, + string expectedStyleName) + { + // Arrange + var deserializer = CreateDeserializer(isControlIdentifiers); + using var yamlStream = File.OpenRead(GetTestFilePath(path, isControlIdentifiers)); + using var yamlReader = new StreamReader(yamlStream); + + // Act + var control = deserializer.Deserialize(yamlReader); + + // Assert + control.ShouldNotBeNull(); + control.StyleName.Should().Be(expectedStyleName); + } } diff --git a/src/Persistence.Tests/Yaml/ValidSerializerTests.cs b/src/Persistence.Tests/Yaml/ValidSerializerTests.cs index 7ec26c3d..4b5c0c1a 100644 --- a/src/Persistence.Tests/Yaml/ValidSerializerTests.cs +++ b/src/Persistence.Tests/Yaml/ValidSerializerTests.cs @@ -385,4 +385,19 @@ public void Serialize_ShouldCreateValidYamlForComponentInstance(string expectedP var expectedYaml = File.ReadAllText(GetTestFilePath(expectedPath, isControlIdentifiers)).NormalizeNewlines(); serializedGraph.Should().Be(expectedYaml); } + + [TestMethod] + [DataRow(@"_TestData/ValidYaml{0}/BuiltInControl/with-style.pa.yaml", true)] + [DataRow(@"_TestData/ValidYaml{0}/BuiltInControl/with-style.pa.yaml", false)] + public void Serialize_ShouldWriteStyle(string expectedPath, bool isControlIdentifiers) + { + var graph = ControlFactory.Create("myControl1", template: "Button"); + graph.StyleName = "this is my style"; + + var serializer = CreateSerializer(isControlIdentifiers); + + var sut = serializer.SerializeControl(graph).NormalizeNewlines(); + var expectedYaml = File.ReadAllText(GetTestFilePath(expectedPath, isControlIdentifiers)).NormalizeNewlines(); + sut.Should().Be(expectedYaml); + } } diff --git a/src/Persistence.Tests/_TestData/ValidYaml-CI/BuiltInControl/with-style.pa.yaml b/src/Persistence.Tests/_TestData/ValidYaml-CI/BuiltInControl/with-style.pa.yaml new file mode 100644 index 00000000..7939889c --- /dev/null +++ b/src/Persistence.Tests/_TestData/ValidYaml-CI/BuiltInControl/with-style.pa.yaml @@ -0,0 +1,3 @@ +myControl1: + Control: Button + StyleName: this is my style diff --git a/src/Persistence.Tests/_TestData/ValidYaml/BuiltInControl/with-style.pa.yaml b/src/Persistence.Tests/_TestData/ValidYaml/BuiltInControl/with-style.pa.yaml new file mode 100644 index 00000000..e6d832e9 --- /dev/null +++ b/src/Persistence.Tests/_TestData/ValidYaml/BuiltInControl/with-style.pa.yaml @@ -0,0 +1,3 @@ +Control: Button +Name: myControl1 +StyleName: this is my style diff --git a/src/Persistence/Models/Control.cs b/src/Persistence/Models/Control.cs index 4184cf4d..faa9e20c 100644 --- a/src/Persistence/Models/Control.cs +++ b/src/Persistence/Models/Control.cs @@ -84,6 +84,8 @@ public int ZIndex } } + [YamlMember(Order = 60)] + public string StyleName { get; set; } = string.Empty; internal virtual void AfterCreate(Dictionary controlDefinition) { diff --git a/src/Persistence/Templates/ControlFactory.cs b/src/Persistence/Templates/ControlFactory.cs index 2c59ed75..1a3b6348 100644 --- a/src/Persistence/Templates/ControlFactory.cs +++ b/src/Persistence/Templates/ControlFactory.cs @@ -52,6 +52,7 @@ public Control Create(string name, string template, { string? variant = null; string? layout = null; + string? styleName = null; ControlPropertiesCollection? properties = null; IList? children = null; @@ -61,6 +62,7 @@ public Control Create(string name, string template, controlDefinition.TryGetValue(nameof(Control.Properties), out properties); controlDefinition.TryGetValue(nameof(Control.Children), out children); controlDefinition.TryGetValue(nameof(Control.Layout), out layout); + controlDefinition.TryGetValue(nameof(Control.StyleName), out styleName); } if (TryCreateComponent(name, template, componentDefinitionName, componentLibraryUniqueName, variant, properties, children, controlDefinition, out var control)) @@ -70,6 +72,7 @@ public Control Create(string name, string template, { if (TryCreateFirstClassControl(name, controlTemplate.Name, variant ?? string.Empty, properties, children, controlDefinition, out control)) { + control.StyleName = styleName ?? string.Empty; return control; } @@ -77,7 +80,8 @@ public Control Create(string name, string template, { Properties = properties ?? new(), Children = children, - Layout = layout ?? string.Empty + Layout = layout ?? string.Empty, + StyleName = styleName ?? string.Empty, }; } @@ -85,7 +89,8 @@ public Control Create(string name, string template, { Properties = properties ?? new(), Children = children, - Layout = layout ?? string.Empty + Layout = layout ?? string.Empty, + StyleName = styleName ?? string.Empty, }; } diff --git a/src/Persistence/Yaml/ControlConverter.cs b/src/Persistence/Yaml/ControlConverter.cs index c3aa397b..d476db44 100644 --- a/src/Persistence/Yaml/ControlConverter.cs +++ b/src/Persistence/Yaml/ControlConverter.cs @@ -203,6 +203,7 @@ public virtual void OnWriteAfterName(IEmitter emitter, Control control) { emitter.Emit(nameof(Control.Variant), control.Variant); emitter.Emit(nameof(Control.Layout), control.Layout); + emitter.Emit(nameof(Control.StyleName), control.StyleName); if (control.Properties != null && control.Properties.Count > 0) {