From f7b464bd5bb5f69f1fecb4e7c04dc35ff90c3cfc Mon Sep 17 00:00:00 2001 From: tnunnink Date: Thu, 9 May 2024 16:07:08 -0500 Subject: [PATCH] Fixed Routine Content throwing exception for empty routine exports. Updated version. --- src/.idea/.idea.L5Sharp/.idea/workspace.xml | 100 ++++++++---------- src/L5Sharp.Core/Components/Routine.cs | 14 ++- src/L5Sharp.Core/L5Sharp.Core.csproj | 9 +- .../L5Sharp.Tests/Components/RoutineTests.cs | 18 +++- 4 files changed, 81 insertions(+), 60 deletions(-) diff --git a/src/.idea/.idea.L5Sharp/.idea/workspace.xml b/src/.idea/.idea.L5Sharp/.idea/workspace.xml index ac5a6015..c5b5e11d 100644 --- a/src/.idea/.idea.L5Sharp/.idea/workspace.xml +++ b/src/.idea/.idea.L5Sharp/.idea/workspace.xml @@ -10,18 +10,8 @@ - - - - - - - - - - - - + + C:\Users\tnunnink\AppData\Roaming\Subversion @@ -440,7 +432,9 @@ - + + + 1677621948966 @@ -785,7 +779,7 @@ - @@ -821,7 +815,6 @@ diff --git a/src/L5Sharp.Core/Components/Routine.cs b/src/L5Sharp.Core/Components/Routine.cs index 2d3b4a02..db6f9c13 100644 --- a/src/L5Sharp.Core/Components/Routine.cs +++ b/src/L5Sharp.Core/Components/Routine.cs @@ -133,13 +133,25 @@ public SheetOrientation? SheetOrientation /// public LogixContainer Content() where TCode : LogixCode { + EnsureContentAdded(); + var content = Element.Element(Type.ContentName); return content is not null ? new LogixContainer(content) : throw Element.L5XError(Type.ContentName); } - + + /// + /// Ensures that the expected content element is added to this routine element. This prevents emprty routines from + /// throwing exceptions when the caller accesses . + /// + private void EnsureContentAdded() + { + if (Element.Element(Type.ContentName) is not null) return; + UpdateContent(Type); + } + /// /// Updates the current routine's content by setting the required Type attribute and adding or replacing the /// child content element with a new element having the name of the provided content. diff --git a/src/L5Sharp.Core/L5Sharp.Core.csproj b/src/L5Sharp.Core/L5Sharp.Core.csproj index bb2aa6c4..afeb3ebe 100644 --- a/src/L5Sharp.Core/L5Sharp.Core.csproj +++ b/src/L5Sharp.Core/L5Sharp.Core.csproj @@ -7,9 +7,9 @@ true L5Sharp Timothy Nunnink - 2.3.1 - 2.3.1 - 2.3.1.0 + 2.3.2 + 2.3.2 + 2.3.2.0 A library for intuitively interacting with Rockwell's L5X import/export files. https://github.com/tnunnink/L5Sharp csharp allen-bradely l5x logix plc-programming rockwell-automation logix5000 @@ -18,8 +18,7 @@ git - Fixed boolean parsing for properties that require 0/1. - Added fix to ToTag for DataType to write StringData correctly. + Fixed bug with Routine Content for emprty routines. Copyright (c) Timothy Nunnink 2022 README.md diff --git a/tests/L5Sharp.Tests/Components/RoutineTests.cs b/tests/L5Sharp.Tests/Components/RoutineTests.cs index 445b9c75..ef629357 100644 --- a/tests/L5Sharp.Tests/Components/RoutineTests.cs +++ b/tests/L5Sharp.Tests/Components/RoutineTests.cs @@ -1,4 +1,5 @@ -using FluentAssertions; +using System.Xml.Linq; +using FluentAssertions; namespace L5Sharp.Tests.Components; @@ -51,6 +52,21 @@ public void New_FunctionBlockDiagram_ShouldHaveExpectedValues() routine.Content().Should().BeEmpty(); } + [Test] + public void New_EmptyRoutineElement_ShouldNotBeNullAndAccessingContentShouldWork() + { + var element = XElement.Parse(""); + var routine = new Routine(element); + + routine.Should().NotBeNull(); + routine.Name.Should().Be("Empty"); + routine.Type.Should().Be(RoutineType.RLL); + routine.Content().Should().NotBeNull(); + + routine.Content().Add(new Rung("XIC(Test);")); + routine.Content().Should().HaveCount(1); + } + [Test] public void New_RungCollection_ShouldHaveExpectedCount() {