-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
264 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
using Xunit; | ||
using System; | ||
using System.Management.Automation; | ||
using PSGraph.Model; | ||
using FluentAssertions; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace PSGraph.Tests | ||
{ | ||
public class GetGraphTopologicSortCmdletTests : IDisposable | ||
{ | ||
private PowerShell _powershell; | ||
|
||
public GetGraphTopologicSortCmdletTests() | ||
{ | ||
_powershell = PowerShell.Create(); | ||
_powershell.AddCommand("Import-Module") | ||
.AddParameter("Assembly", typeof(PSGraph.Cmdlets.GetGraphTopologicSort).Assembly); | ||
_powershell.Invoke(); | ||
_powershell.Commands.Clear(); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_powershell.Dispose(); | ||
} | ||
|
||
[Fact] | ||
public void GetGraphTopologicSort_ShouldReturnTopologicallySortedVertices() | ||
{ | ||
// Arrange | ||
var graph = GraphTestData.SimpleTestGraph6; // Using real graph data | ||
|
||
// Add the parameters for Get-GraphTopologicSort | ||
_powershell.AddCommand("Get-GraphTopologicSort") | ||
.AddParameter("Graph", graph); | ||
|
||
// Act | ||
var results = _powershell.Invoke(); | ||
|
||
// Assert | ||
results.Should().NotBeNullOrEmpty(); | ||
var sortedVertices = results[0].BaseObject as IEnumerable<PSVertex>; | ||
sortedVertices.Should().NotBeNull(); | ||
|
||
// Check that vertices are sorted topologically. | ||
// In SimpleTestGraph5, A should appear before D and E, D and E before F, etc. | ||
var sortedList = sortedVertices.ToList(); | ||
var vertexA = sortedList.FindIndex(v => v.Label == "A"); | ||
var vertexD = sortedList.FindIndex(v => v.Label == "D"); | ||
var vertexE = sortedList.FindIndex(v => v.Label == "E"); | ||
var vertexF = sortedList.FindIndex(v => v.Label == "F"); | ||
|
||
|
||
vertexA.Should().BeLessThan(vertexD); | ||
vertexA.Should().BeLessThan(vertexE); | ||
vertexD.Should().BeLessThan(vertexF); | ||
vertexE.Should().BeLessThan(vertexF); | ||
} | ||
|
||
[Fact] | ||
public void GetGraphTopologicSort_ShouldReturnEmptyForEmptyGraph() | ||
{ | ||
// Arrange | ||
var emptyGraph = new PsBidirectionalGraph(); // Create an empty graph | ||
|
||
// Add the parameters for Get-GraphTopologicSort | ||
_powershell.AddCommand("Get-GraphTopologicSort") | ||
.AddParameter("Graph", emptyGraph); | ||
|
||
// Act | ||
var results = _powershell.Invoke(); | ||
|
||
// Assert | ||
results.Should().ContainSingle("because the graph contains a single empty element") | ||
.Which.Should().BeOfType<PSObject>() | ||
.Which.BaseObject.Should().BeAssignableTo<IEnumerable<PSVertex>>() | ||
.Which.Should().BeEmpty("because the single result is an empty collection of vertices"); | ||
|
||
|
||
} | ||
|
||
[Fact] | ||
public void GetGraphTopologicSort_ShouldThrowExceptionForCyclicGraph() | ||
{ | ||
// Arrange | ||
var graph = GraphTestData.SimpleTestGraph1; // Using a cyclic graph | ||
|
||
// Add the parameters for Get-GraphTopologicSort | ||
_powershell.AddCommand("Get-GraphTopologicSort") | ||
.AddParameter("Graph", graph); | ||
|
||
// Act | ||
Action act = () => _powershell.Invoke(); | ||
|
||
// Assert | ||
act.Should().Throw<Exception>().WithMessage("*The graph contains at least one cycle*"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System.Management.Automation; | ||
using QuikGraph.Algorithms; | ||
using PSGraph.Model; | ||
|
||
namespace PSGraph.Cmdlets | ||
{ | ||
[Cmdlet(VerbsCommon.Get, "GraphTopologicSort")] | ||
public class GetGraphTopologicSort : PSCmdlet | ||
{ | ||
[Parameter(Mandatory = true)] | ||
[ValidateNotNullOrEmpty] | ||
public PsBidirectionalGraph Graph; | ||
|
||
protected override void EndProcessing() | ||
{ | ||
var res = Graph.TopologicalSort(); | ||
if (res != null) | ||
WriteObject(res); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
BeforeAll { | ||
Import-Module "/workspaces/PSGraph/PSGraph.Tests/bin/Debug/net8.0/PSQuickGraph.psd1" | ||
} | ||
|
||
Describe 'Get-GraphTopologicSort' { | ||
BeforeEach { | ||
# Initialize a new graph before each test | ||
$graph = New-Graph | ||
|
||
# Add vertices | ||
Add-Vertex -Graph $graph -Vertex 'A' | ||
Add-Vertex -Graph $graph -Vertex 'B' | ||
Add-Vertex -Graph $graph -Vertex 'C' | ||
Add-Vertex -Graph $graph -Vertex 'D' | ||
Add-Vertex -Graph $graph -Vertex 'E' | ||
|
||
# Add edges | ||
Add-Edge -From 'A' -To 'B' -Graph $graph | Out-Null | ||
Add-Edge -From 'A' -To 'C' -Graph $graph | Out-Null | ||
Add-Edge -From 'B' -To 'D' -Graph $graph | Out-Null | ||
Add-Edge -From 'C' -To 'D' -Graph $graph | Out-Null | ||
Add-Edge -From 'D' -To 'E' -Graph $graph | Out-Null | ||
} | ||
|
||
It 'Should return a topologically sorted list of vertices' { | ||
$sortedVertices = Get-GraphTopologicSort -Graph $graph | ||
|
||
$sortedVertices | Should -Not -BeNullOrEmpty | ||
|
||
$sortedVertices2 = $sortedVertices | ForEach-Object { $_ } | ||
|
||
|
||
# $sortedVertices2 | Should -BeExactly @('A', 'C', 'B', 'D', 'E') | ||
|
||
# Additional checks to ensure the topological order is valid | ||
# 'A' must come before 'B', 'C', 'D', 'E' | ||
$sortedVertices2.IndexOf('A') | Should -BeLessThan $sortedVertices2.IndexOf('B') | ||
$sortedVertices2.IndexOf('A') | Should -BeLessThan $sortedVertices2.IndexOf('C') | ||
$sortedVertices2.IndexOf('A') | Should -BeLessThan $sortedVertices2.IndexOf('D') | ||
$sortedVertices2.IndexOf('A') | Should -BeLessThan $sortedVertices2.IndexOf('E')ß | ||
|
||
# 'B' must come before 'D' | ||
$sortedVertices2.IndexOf('B') | Should -BeLessThan $sortedVertices2.IndexOf('D') | ||
|
||
# 'C' must come before 'D' | ||
$sortedVertices2.IndexOf('C') | Should -BeLessThan $sortedVertices2.IndexOf('D') | ||
|
||
# 'D' must come before 'E' | ||
$sortedVertices2.IndexOf('D') | Should -BeLessThan $sortedVertices2.IndexOf('E') | ||
|
||
|
||
} | ||
|
||
It 'Should return $null for an empty graph' { | ||
$emptyGraph = New-Graph | ||
$sortedVertices = Get-GraphTopologicSort -Graph $emptyGraph | ||
|
||
$sortedVertices | Should -BeNullOrEmpty | ||
} | ||
|
||
It 'Should throw an error for a cyclic graph' { | ||
# Add a cycle | ||
Add-Edge -From 'E' -To 'A' -Graph $graph | Out-Null | ||
|
||
{ Get-GraphTopologicSort -Graph $graph } | Should -Throw -ErrorId 'QuikGraph.NonAcyclicGraphException,PSGraph.Cmdlets.GetGraphTopologicSort' | ||
} | ||
} |