Skip to content

Commit

Permalink
Fixed Routine Content throwing exception for empty routine exports. U…
Browse files Browse the repository at this point in the history
…pdated version.
  • Loading branch information
tnunnink committed May 9, 2024
1 parent 85c38f3 commit f7b464b
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 60 deletions.
100 changes: 47 additions & 53 deletions src/.idea/.idea.L5Sharp/.idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 13 additions & 1 deletion src/L5Sharp.Core/Components/Routine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,25 @@ public SheetOrientation? SheetOrientation
/// </remarks>
public LogixContainer<TCode> Content<TCode>() where TCode : LogixCode
{
EnsureContentAdded();

var content = Element.Element(Type.ContentName);

return content is not null
? new LogixContainer<TCode>(content)
: throw Element.L5XError(Type.ContentName);
}


/// <summary>
/// Ensures that the expected content element is added to this routine element. This prevents emprty routines from
/// throwing exceptions when the caller accesses <see cref="Content{TCode}"/>.
/// </summary>
private void EnsureContentAdded()
{
if (Element.Element(Type.ContentName) is not null) return;
UpdateContent(Type);
}

/// <summary>
/// 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 <see cref="RoutineType"/> content.
Expand Down
9 changes: 4 additions & 5 deletions src/L5Sharp.Core/L5Sharp.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
<Title>L5Sharp</Title>
<Authors>Timothy Nunnink</Authors>
<Version>2.3.1</Version>
<AssemblyVersion>2.3.1</AssemblyVersion>
<FileVersion>2.3.1.0</FileVersion>
<Version>2.3.2</Version>
<AssemblyVersion>2.3.2</AssemblyVersion>
<FileVersion>2.3.2.0</FileVersion>
<Description>A library for intuitively interacting with Rockwell's L5X import/export files.</Description>
<RepositoryUrl>https://github.com/tnunnink/L5Sharp</RepositoryUrl>
<PackageTags>csharp allen-bradely l5x logix plc-programming rockwell-automation logix5000</PackageTags>
Expand All @@ -18,8 +18,7 @@
<!--<PackageIcon>icon.png</PackageIcon>-->
<RepositoryType>git</RepositoryType>
<PackageReleaseNotes>
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.
</PackageReleaseNotes>
<Copyright>Copyright (c) Timothy Nunnink 2022</Copyright>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand Down
18 changes: 17 additions & 1 deletion tests/L5Sharp.Tests/Components/RoutineTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FluentAssertions;
using System.Xml.Linq;
using FluentAssertions;

namespace L5Sharp.Tests.Components;

Expand Down Expand Up @@ -51,6 +52,21 @@ public void New_FunctionBlockDiagram_ShouldHaveExpectedValues()
routine.Content<Sheet>().Should().BeEmpty();
}

[Test]
public void New_EmptyRoutineElement_ShouldNotBeNullAndAccessingContentShouldWork()
{
var element = XElement.Parse("<Routine Use=\"Target\" Name=\"Empty\" Type=\"RLL\"/>");
var routine = new Routine(element);

routine.Should().NotBeNull();
routine.Name.Should().Be("Empty");
routine.Type.Should().Be(RoutineType.RLL);
routine.Content<Rung>().Should().NotBeNull();

routine.Content<Rung>().Add(new Rung("XIC(Test);"));
routine.Content<Rung>().Should().HaveCount(1);
}

[Test]
public void New_RungCollection_ShouldHaveExpectedCount()
{
Expand Down

0 comments on commit f7b464b

Please sign in to comment.