forked from vexx32/PSKoans
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AboutOrderOfOperations.Koans.ps1
48 lines (43 loc) · 1.73 KB
/
AboutOrderOfOperations.Koans.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using module PSKoans
[Koan(Position = 118)]
param()
<#
Order of Operations
In PowerShell, the order of operations needs to be taken into account,
sometimes even more so than other languages. Because all native functions
and cmdlets take space-separated arguments, any expressions needing
evaluation need to be explicitly separated with parentheses in many cases.
In general, PowerShell follows PEMDAS, although it has no native exponential
operator. Parentheses always get evaluated first (from inner to outer if
nested), but expressions cannot be passed as arguments without being forced
to evaluate first.
#>
Describe "Order of Operations" {
It "requires parameter argument expressions to be enclosed in parentheses" {
function Add-Numbers {
param(
[int]
$Number1,
[int]
$Number2
)
return $Number1 + $Number2
}
# Be wary of open spaces when passing an expression as a function argument.
$Sum = Add-Numbers (4 + 1) 18
__ | Should -Be $Sum
Add-Numbers 3 * 4 7 | Should -Be 19 # Add parentheses to the function call to make this true.
}
It "will evaluate an entire expression if it is the first element in a pipeline" {
# A pipe character evaluates everything before it on the line before
# passing along the value(s).
__ + 7 | Should -Be 11
__ * 3 + 11 | Should -Be 35
}
It "otherwise follows standard mathematical rules" {
# Although PowerShell doesn't have a native exponentiation operator,
# we do have [Math]::Pow($base, $power)
$Value = 3 + 4 / [Math]::Pow(2, 3)
__ | Should -Be $Value
}
}