diff --git a/Code/Classes/classes.ps1 b/Code/Classes/classes.psm1 similarity index 89% rename from Code/Classes/classes.ps1 rename to Code/Classes/classes.psm1 index 8a8bad9..8c023fa 100644 --- a/Code/Classes/classes.ps1 +++ b/Code/Classes/classes.psm1 @@ -9,6 +9,13 @@ class nodeutility { return $x } + [node[]] static ParseString ([string[]]$String) { + $ParsedFile = [Parser]::ParseInput($String, [ref]$null, [ref]$Null) + $RawAstDocument = $ParsedFile.FindAll({$args[0] -is [Ast]}, $false) + $x=$RawAstDocument | ForEach-Object{if ( $null -eq $_.parent.parent.parent ) { $t = [nodeutility]::SetNode($_); if ( $null -ne $t) { $t} } } + return $x + } + [node] static SetNode ([object]$e) { $node = $null Switch ( $e ) { @@ -52,8 +59,27 @@ class nodeutility { ) } + [String] static SetDefaultShape ([String]$e) { + $Shape = $Null + Switch ( $e ) { + "If" { $Shape = "diamond" } + "ElseIf" { $Shape = "diamond" } + "Foreach" { $Shape = "parallelogram" } + "While" { $Shape = "parallelogram" } + "DoWhile" { $Shape = "parallelogram" } + "DoUntil" { $Shape = "parallelogram" } + "For" { $Shape = "parallelogram" } + Defaut { $Shape = "box" } + + } + return $Shape + } + } +## Ajouter un noeud qu'on pourrait appeler CodeNode, par exemple dans un if , si il n y a rien dedans ... +## Pour le flowchart comme ça on peut dire ce que le if fait +## coup est ce que il faudrait pas que le else et elseif ne soient pas ua meme niveau ...?! class node { [string]$Type [string]$Statement @@ -62,6 +88,7 @@ class node { [node]$Parent [int]$Depth $File + hidden $DefaultShape hidden $Nodeid hidden $code hidden $NewContent @@ -76,6 +103,7 @@ class node { $this.file = $e.extent.file $this.SetDepth() $this.Guid() + $this.DefaultShape = [nodeutility]::SetDefaultShape($this.Type) } node ([Ast]$e,[node]$f) { @@ -84,6 +112,7 @@ class node { $this.file = $e.extent.file $this.SetDepth() $this.Guid() + $this.DefaultShape = [nodeutility]::SetDefaultShape($this.Type) } ## override with parent, for sublevels diff --git a/Code/Functions/New-NodeGraph.ps1 b/Code/Functions/New-NodeGraph.ps1 index 6a0fcc8..1981c69 100644 --- a/Code/Functions/New-NodeGraph.ps1 +++ b/Code/Functions/New-NodeGraph.ps1 @@ -3,7 +3,8 @@ function New-NodeGraph { param ( [node[]]$node, [switch]$UseDescription, - [switch]$GroupAffiliatedNodes + [switch]$GroupAffiliatedNodes, + [Switch]$UseFlowShapes ) begin { @@ -14,6 +15,12 @@ function New-NodeGraph { $labelscript = { $args[0].Statement } } + If ( $PSBoundParameters["UseFlowShapes"].isPresent ) { + $Shapes = { $args[0].DefaultShape } + } else { + $Shapes = { "Box" } + } + } process { @@ -27,9 +34,9 @@ function New-NodeGraph { graph -Name "lol" @{rankdir='LR'} { for ( $i =0 ; $i -lt $arrayofnodes.count; $i++ ) { subgraph _$i { - node -name $arrayofnodes[$i].NodeId -attributes @{label=$labelscript.Invoke($arrayofnodes[$i])} + node -name $arrayofnodes[$i].NodeId -attributes @{label=$labelscript.Invoke($arrayofnodes[$i]);shape=$Shapes.invoke($arrayofnodes[$i])} foreach ( $n in $arrayofnodes[$i].GetChildren($true) ) { - node -name $n.NodeId -attributes @{label=$labelscript.Invoke($n)} + node -name $n.NodeId -attributes @{label=$labelscript.Invoke($n);shape=$Shapes.invoke($n)} edge -From $n.parent.NodeId -to $n.NodeId } } @@ -43,11 +50,11 @@ function New-NodeGraph { graph -Name "lol" -attributes @{rankdir='LR'} { $arrayofnodes.foreach({ - node $_.NodeId -attributes @{label=$labelscript.Invoke($_)} + node $_.NodeId -attributes @{label=$labelscript.Invoke($_);shape=$Shapes.invoke($arrayofnodes[$i])} }) $arrayofnodes.GetChildren($True).foreach({ - node $_.NodeId -attributes @{label=$labelscript.Invoke($_)} + node $_.NodeId -attributes @{label=$labelscript.Invoke($_);shape=$Shapes.invoke($arrayofnodes[$i])} }) for ( $i=0;$i -lt $x.count ; $i++ ) { diff --git a/Tests/NodeUtility.Tests.Ps1 b/Tests/NodeUtility.Tests.Ps1 new file mode 100644 index 0000000..b2ddabe --- /dev/null +++ b/Tests/NodeUtility.Tests.Ps1 @@ -0,0 +1,497 @@ +using module ..\Code\Classes\classes.psm1 +$TestsPath = Split-Path $MyInvocation.MyCommand.Path + +$SampleLocation = Join-Path -Path $TestsPath -ChildPath 'scripts_samples' + +$ItemSampleLocation = Get-Item -Path $SampleLocation + +Push-Location -Path $ItemSampleLocation.FullName + +set-location -Path $ItemSampleLocation.FullName + +#InModuleScope -ModuleName classes -ScriptBlock { + +Describe '[nodeutility] Test IfNode interpretation'{ + Context "Test If wihtout children"{ + $IfSamplePath = (Get-Item -Path './ifsampleWithoutChild.ps1').FullName + $Result = [nodeutility]::ParseFile($IfSamplePath) + It "Testing object type"{ + $Result -is [Node[]] | Should be $true + $Result[0] -is [IfNode] | Should be $true + $Result[0].Children -is [System.Collections.Generic.List[Node]] | Should be $true + } + + It "Testing object property"{ + $Result[0].Statement -eq 'If ( $x -le 1 )' | Should be $true + $Result[0].Depth -eq 1 | Should be $true + $Result[0].Children.Count -eq 0 + } + } + + Context "Test If with children"{ + $IfSamplePath = (Get-Item -Path './ifsampleWithChild.ps1').FullName + $Result = [nodeutility]::ParseFile($IfSamplePath) + It "[FirstIf]Testing parent object type"{ + $Result -is [Node[]] | Should be $true + $Result[0] -is [IfNode] | Should be $true + $Result[0].Children -is [System.Collections.Generic.List[Node]] | Should be $true + } + + It "[FirstIf]Testing parent object property"{ + $Result[0].Statement -eq 'If ( $x -le 1 )' | Should be $true + $Result[0].Depth -eq 1 | Should be $true + $Result[0].Children.Count -eq 1 + } + + It "[FirstIf]Testing child If object type"{ + $Result[0].Children[0] -is [IfNode] | Should be $true + $Result[0].Children[0].Children -is [System.Collections.Generic.List[Node]] | Should be $true + } + + It "[FirstIf]Testing child If object property"{ + $Result[0].Children[0].Statement -eq "If ( `$PWD -like 'C:\Windows' )" | Should be $true + $Result[0].Children[0].Depth -eq 2 | Should be $true + $Result[0].Children[0].Children.Count -eq 0 + } + + It "[SecondIf]Testing parent object type"{ + $Result -is [Node[]] | Should be $true + $Result[1] -is [IfNode] | Should be $true + $Result[1].Children -is [System.Collections.Generic.List[Node]] | Should be $true + } + + It "[SecondIf]Testing parent If object property"{ + $Result[1].Statement -eq 'If ( $x -le 2 )' | Should be $true + $Result[1].Depth -eq 1 | Should be $true + $Result[1].Children.Count -eq 1 + } + + It "[SecondIf]Testing child If object type"{ + $Result[1].Children[0] -is [IfNode] | Should be $true + $Result[1].Children[0].Children -is [System.Collections.Generic.List[Node]] | Should be $true + } + + It "[SecondIf]Testing child If object property"{ + $Result[1].Children[0].Statement -eq "If ( `$PWD -like 'C:\Windows' )" | Should be $true + $Result[1].Children[0].Depth -eq 2 | Should be $true + $Result[1].Children[0].Children.Count -eq 0 + } + + It "[SecondIf]Testing child foreach object type"{ + $Result[1].Children[1] -is [ForeachNode] | Should be $true + $Result[1].Children[1].Children -is [System.Collections.Generic.List[Node]] | Should be $true + } + + It "[SecondIf]Testing child foreach object property"{ + $Result[1].Children[1].Statement -eq "Foreach ( `$u in `$All )" | Should be $true + $Result[1].Children[1].Depth -eq 2 | Should be $true + $Result[1].Children[1].Children.Count -eq 0 + } + } + +}# end of Describe block + +<# +Describe '[node]-[Constructors]'{ + +It '[node]-[Constructor] - Parameterless should Not Throw' { + +# -- Arrange + +# -- Act + +# -- Assert + +{[node]::New()} | Should Not Throw + +}# end of it block + + +It '[node]-[Constructor]([Ast]e) should Not Throw' { + +# -- Arrange + + +[Ast]$e='' + +# -- Act + +# -- Assert + +{[node]::New($e)} | Should Not Throw + +}# end of it block + + +It '[node]-[Constructor]([Ast]e,[node]f) should Not Throw' { + +# -- Arrange + + +[Ast]$e='' + + +[node]$f='' + +# -- Act + +# -- Assert + +{[node]::New($e,$f)} | Should Not Throw + +}# end of it block + + +}# end of Describe block +Describe '[IfNode]-[Constructors]'{ + +It '[IfNode]-[Constructor]([Ast]e) should Not Throw' { + +# -- Arrange + + +[Ast]$e='' + +# -- Act + +# -- Assert + +{[IfNode]::New($e)} | Should Not Throw + +}# end of it block + + +It '[IfNode]-[Constructor]([Ast]e,[node]f) should Not Throw' { + +# -- Arrange + + +[Ast]$e='' + + +[node]$f='' + +# -- Act + +# -- Assert + +{[IfNode]::New($e,$f)} | Should Not Throw + +}# end of it block + + +}# end of Describe block +Describe '[ElseNode]-[Constructors]'{ + +It '[ElseNode]-[Constructor]([Ast]e,[node]f,[string]d) should Not Throw' { + +# -- Arrange + + +[Ast]$e='' + + +[node]$f='' + + +[string]$d='' + +# -- Act + +# -- Assert + +{[ElseNode]::New($e,$f,$d)} | Should Not Throw + +}# end of it block + + +}# end of Describe block +Describe '[ElseIfNode]-[Constructors]'{ + +It '[ElseIfNode]-[Constructor]([Ast]e,[node]j,[string]d,[Ast]f) should Not Throw' { + +# -- Arrange + + +[Ast]$e='' + + +[node]$j='' + + +[string]$d='' + + +[Ast]$f='' + +# -- Act + +# -- Assert + +{[ElseIfNode]::New($e,$j,$d,$f)} | Should Not Throw + +}# end of it block + + +}# end of Describe block +Describe '[SwitchNode]-[Constructors]'{ + +It '[SwitchNode]-[Constructor]([Ast]e) should Not Throw' { + +# -- Arrange + + +[Ast]$e='' + +# -- Act + +# -- Assert + +{[SwitchNode]::New($e)} | Should Not Throw + +}# end of it block + + +It '[SwitchNode]-[Constructor]([Ast]e,[node]f) should Not Throw' { + +# -- Arrange + + +[Ast]$e='' + + +[node]$f='' + +# -- Act + +# -- Assert + +{[SwitchNode]::New($e,$f)} | Should Not Throw + +}# end of it block + + +}# end of Describe block +Describe '[SwitchCaseNode]-[Constructors]'{ + +It '[SwitchCaseNode]-[Constructor]([Ast]e,[node]j,[string]d,[Ast]f) should Not Throw' { + +# -- Arrange + + +[Ast]$e='' + + +[node]$j='' + + +[string]$d='' + + +[Ast]$f='' + +# -- Act + +# -- Assert + +{[SwitchCaseNode]::New($e,$j,$d,$f)} | Should Not Throw + +}# end of it block + + +}# end of Describe block +Describe '[ForeachNode]-[Constructors]'{ + +It '[ForeachNode]-[Constructor]([Ast]e) should Not Throw' { + +# -- Arrange + + +[Ast]$e='' + +# -- Act + +# -- Assert + +{[ForeachNode]::New($e)} | Should Not Throw + +}# end of it block + + +It '[ForeachNode]-[Constructor]([Ast]e,[node]f) should Not Throw' { + +# -- Arrange + + +[Ast]$e='' + + +[node]$f='' + +# -- Act + +# -- Assert + +{[ForeachNode]::New($e,$f)} | Should Not Throw + +}# end of it block + + +}# end of Describe block +Describe '[WhileNode]-[Constructors]'{ + +It '[WhileNode]-[Constructor]([Ast]e) should Not Throw' { + +# -- Arrange + + +[Ast]$e='' + +# -- Act + +# -- Assert + +{[WhileNode]::New($e)} | Should Not Throw + +}# end of it block + + +It '[WhileNode]-[Constructor]([Ast]e,[node]f) should Not Throw' { + +# -- Arrange + + +[Ast]$e='' + + +[node]$f='' + +# -- Act + +# -- Assert + +{[WhileNode]::New($e,$f)} | Should Not Throw + +}# end of it block + + +}# end of Describe block +Describe '[ForNode]-[Constructors]'{ + +It '[ForNode]-[Constructor]([Ast]e) should Not Throw' { + +# -- Arrange + + +[Ast]$e='' + +# -- Act + +# -- Assert + +{[ForNode]::New($e)} | Should Not Throw + +}# end of it block + + +It '[ForNode]-[Constructor]([Ast]e,[node]f) should Not Throw' { + +# -- Arrange + + +[Ast]$e='' + + +[node]$f='' + +# -- Act + +# -- Assert + +{[ForNode]::New($e,$f)} | Should Not Throw + +}# end of it block + + +}# end of Describe block +Describe '[DoUntilNode]-[Constructors]'{ + +It '[DoUntilNode]-[Constructor]([Ast]e) should Not Throw' { + +# -- Arrange + + +[Ast]$e='' + +# -- Act + +# -- Assert + +{[DoUntilNode]::New($e)} | Should Not Throw + +}# end of it block + + +It '[DoUntilNode]-[Constructor]([Ast]e,[node]f) should Not Throw' { + +# -- Arrange + + +[Ast]$e='' + + +[node]$f='' + +# -- Act + +# -- Assert + +{[DoUntilNode]::New($e,$f)} | Should Not Throw + +}# end of it block + + +}# end of Describe block +Describe '[DoWhileNode]-[Constructors]'{ + +It '[DoWhileNode]-[Constructor]([Ast]e) should Not Throw' { + +# -- Arrange + + +[Ast]$e='' + +# -- Act + +# -- Assert + +{[DoWhileNode]::New($e)} | Should Not Throw + +}# end of it block + + +It '[DoWhileNode]-[Constructor]([Ast]e,[node]f) should Not Throw' { + +# -- Arrange + + +[Ast]$e='' + + +[node]$f='' + +# -- Act + +# -- Assert + +{[DoWhileNode]::New($e,$f)} | Should Not Throw + +}# end of it block + + +}# end of Describe block + +}#End InModuleScope + + +#> \ No newline at end of file diff --git a/Tests/scripts_samples/ifsampleWithChild.ps1 b/Tests/scripts_samples/ifsampleWithChild.ps1 new file mode 100644 index 0000000..7c1c62d --- /dev/null +++ b/Tests/scripts_samples/ifsampleWithChild.ps1 @@ -0,0 +1,19 @@ +# Depth 1 +if($x -le 1) { + Write-Output 'Ok' + If ( $PWD -like 'C:\Windows' ){ + Write-Output "You are in the good way!" + } +} + +# Depth 2 +if($x -le 2) { + Write-Output 'Ok' + if ($PWD -like 'C:\Windows'){ + Write-Output "You are in the good way!" + } + + foreach ($u in $All){ + Write-Output "In foreach Depth 2" + } +} \ No newline at end of file diff --git a/Tests/scripts_samples/ifsampleWithoutChild.ps1 b/Tests/scripts_samples/ifsampleWithoutChild.ps1 new file mode 100644 index 0000000..f21bfe3 --- /dev/null +++ b/Tests/scripts_samples/ifsampleWithoutChild.ps1 @@ -0,0 +1,3 @@ +if($x -le 1) { + Write-Output 'Ok' +} \ No newline at end of file diff --git a/Tests/scripts_samples/small.ps1 b/Tests/scripts_samples/small.ps1 new file mode 100644 index 0000000..3946c1c --- /dev/null +++ b/Tests/scripts_samples/small.ps1 @@ -0,0 +1,9 @@ +if ( $i -eq 10) { + Foreach ( $a in $b) { + ##do something + ## ajouter un noeud nodecode + } +} Else { + ## Whathever + ## ajouter un noeud nodecode +} \ No newline at end of file diff --git a/Tests/tests.ps1 b/Tests/tests.ps1 index 897f8d0..7a5438c 100644 --- a/Tests/tests.ps1 +++ b/Tests/tests.ps1 @@ -9,6 +9,13 @@ class nodeutility { return $x } + [node[]] static ParseString ([string[]]$String) { + $ParsedFile = [Parser]::ParseInput($String, [ref]$null, [ref]$Null) + $RawAstDocument = $ParsedFile.FindAll({$args[0] -is [Ast]}, $false) + $x=$RawAstDocument | ForEach-Object{if ( $null -eq $_.parent.parent.parent ) { $t = [nodeutility]::SetNode($_); if ( $null -ne $t) { $t} } } + return $x + } + [node] static SetNode ([object]$e) { $node = $null Switch ( $e ) { @@ -419,4 +426,4 @@ Class DoWhileNode : node { ## Exampple -$x=[nodeutility]::ParseFile("C:\users\lx\gitperso\PSScriptDiagram\sample.ps1") \ No newline at end of file +$x=[nodeutility]::ParseFile("$PWD\Tests\scripts_samples\sample.ps1") \ No newline at end of file