Avoid arbitary code execution
function Calculator { param ( [int]$n1, [ValidateSet("+", "*", "-")] [string]$s, [int]$n2 ) Invoke-Expression ("$n1 $s $n2") }
function Calculator ($n1,$s,$n2 ){Invoke-Expression ("$n1 $s $n2")- function Calculator {
- param (
- [int]$n1,
- [ValidateSet("+", "*", "-")]
- [string]$s,
- [int]$n2
- )
- Invoke-Expression ("$n1 $s $n2")
- }
Describe 'Calculator' { BeforeAll { # Easy breezy tests $test1 = Calculator -n1 4 -n2 5 -s '+' $test2 = Calculator -n1 12 -n2 5 -s '-' $test3 = Calculator -n1 20 -n2 3 -s '*' $test4 = { Calculator "Write-Host" "Hello World" } } Context 'FixedTests' { It "Should pass" { $test1 | Should -Be 9 } It "Should pass" { $test2 | Should -Be 7 } It "Should pass" { $test3 | Should -Be 60 } It "Should throw" { { $test4.Invoke() } | Should -Throw } } Context 'RandomTests' { function ans($n1, $s, $n2 ) { Invoke-Expression ("$n1 $s $n2") } 0..100 | ForEach-Object { $s = '+', '-', '*' | Get-Random $n1 = Get-Random -Maximum 1000 $n2 = Get-Random -Maximum 1000 $ans = ans($n1, $s, $n2) It "Should pass" { Calculator $n1 $s $n2 | Should -be $ans } } } }
# Easy breezy tests$test1 = Calculator -n1 4 -n2 5 -s '+'$test2 = Calculator -n1 12 -n2 5 -s '-'$test3 = Calculator -n1 20 -n2 3 -s '*'Describe 'FixedTests' {It "Should pass" {$test1 | Should be 9}It "Should pass" {$test2 | Should be 7}It "Should pass" {$test3 | Should be 60}}Describe 'RandomTests' {function ans($n1,$s,$n2 ){Invoke-Expression ("$n1 $s $n2")}for($i =0; $i -lt 100; $i++){$s = '+','-','*' | Get-Random$n1 = Get-Random -Maximum 1000$n2 = Get-Random -Maximum 1000$ans = ans($n1,$s,$n2)It "Should pass" {Calculator ($n1,$s,$n2)| Should be $ans- Describe 'Calculator' {
- BeforeAll {
- # Easy breezy tests
- $test1 = Calculator -n1 4 -n2 5 -s '+'
- $test2 = Calculator -n1 12 -n2 5 -s '-'
- $test3 = Calculator -n1 20 -n2 3 -s '*'
- $test4 = { Calculator "Write-Host" "Hello World" }
- }
- Context 'FixedTests' {
- It "Should pass" {
- $test1 | Should -Be 9
- }
- It "Should pass" {
- $test2 | Should -Be 7
- }
- It "Should pass" {
- $test3 | Should -Be 60
- }
- It "Should throw" {
- { $test4.Invoke() } | Should -Throw
- }
- }
}}- Context 'RandomTests' {
- function ans($n1, $s, $n2 ) {
- Invoke-Expression ("$n1 $s $n2")
- }
- 0..100 | ForEach-Object {
- $s = '+', '-', '*' | Get-Random
- $n1 = Get-Random -Maximum 1000
- $n2 = Get-Random -Maximum 1000
- $ans = ans($n1, $s, $n2)
- It "Should pass" {
- Calculator $n1 $s $n2 | Should -be $ans
- }
- }
- }
- }