From 1fa2cdf8a805a07b96eb131f6996a9c054fe6a08 Mon Sep 17 00:00:00 2001 From: Mathias Vorreiter Pedersen Date: Thu, 29 Aug 2024 14:43:21 +0100 Subject: [PATCH] Add testcases with functions. --- .../controlflow/graph/Cfg.expected | 83 +++++++++++++++++++ .../controlflow/graph/functions.ps1 | 53 ++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 powershell/ql/test/library-tests/controlflow/graph/functions.ps1 diff --git a/powershell/ql/test/library-tests/controlflow/graph/Cfg.expected b/powershell/ql/test/library-tests/controlflow/graph/Cfg.expected index 306b95b03ac..070e99fce3c 100644 --- a/powershell/ql/test/library-tests/controlflow/graph/Cfg.expected +++ b/powershell/ql/test/library-tests/controlflow/graph/Cfg.expected @@ -1,3 +1,86 @@ +functions.ps1: +# 1| enter functions.ps1 +#-----| -> functions.ps1 + +# 1| functions.ps1 + +# 1| enter {...} +#-----| -> {...} + +# 1| {...} + +# 11| enter {...} +#-----| -> {...} + +# 11| exit {...} + +# 11| exit {...} (normal) +#-----| -> exit {...} + +# 11| {...} +#-----| -> {...} + +# 11| {...} +#-----| -> exit {...} (normal) + +# 13| enter {...} +#-----| -> {...} + +# 13| {...} + +# 22| enter {...} +#-----| -> {...} + +# 22| {...} +#-----| -> sum + +# 28| sum +#-----| -> 0 + +# 28| ...=... + +# 28| 0 +#-----| -> ...=... + +# 28| 0 +#-----| -> 0 + +# 36| enter {...} +#-----| -> {...} + +# 36| {...} +#-----| -> sum + +# 41| {...} +#-----| -> sum + +# 42| sum +#-----| -> 0 + +# 42| ...=... +#-----| -> {...} + +# 42| 0 +#-----| -> ...=... + +# 42| 0 +#-----| -> 0 + +# 44| {...} +#-----| -> sum + +# 46| sum +#-----| -> _ + +# 46| ...=... +#-----| -> {...} + +# 46| _ +#-----| -> ...=... + +# 46| _ +#-----| -> _ + global.ps1: # 1| {...} #-----| -> c diff --git a/powershell/ql/test/library-tests/controlflow/graph/functions.ps1 b/powershell/ql/test/library-tests/controlflow/graph/functions.ps1 new file mode 100644 index 00000000000..e9020634925 --- /dev/null +++ b/powershell/ql/test/library-tests/controlflow/graph/functions.ps1 @@ -0,0 +1,53 @@ +Function Add-Numbers-Arguments { + # We take in two numbers + param( + [int] $number1, + [int] $number2 + ) + # We add them together + $number1 + $number2 +} + +function foo() { param($a) } + +Function Default-Arguments { + param( + [int] $name0, + [int] $name1 = 0, + [int] $name2 = $name1 + 1 + ) + $name + $name2 +} + +Function Add-Numbers-From-Array { + # We take in a list of numbers + param( + [int[]] $numbers + ) + + $sum = 0 + foreach ($number in $numbers) { + # We add each number to the sum + $sum += $number + } + $sum +} + +Function Add-Numbers-From-Pipeline { + # We take in a list of numbers + param( + [int[]] $numbers + ) + Begin { + $sum = 0 + } + Process { + # We add each number to the sum + $sum += $_ + } + End { + # We return the sum + $sum + } +} +