-
Notifications
You must be signed in to change notification settings - Fork 6
/
Get-OKCommandLength.Tests.ps1
54 lines (53 loc) · 2.23 KB
/
Get-OKCommandLength.Tests.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
49
50
51
52
53
54
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.'
. "$here\$sut"
Write-Host ". ""$here\$sut"""
Describe "Get-OKCommandLength" {
It "Measures length of a single keyword" {
$code = "dir";
Get-OKCommandLength ($code | Get-Token) | Should -Be $code.Length
}
It "Do not ignore leading space" {
$code = " dir";
Get-OKCommandLength ($code | Get-Token) | Should -Be $code.Length
}
It "Do Ignore trailing space (before first comment)" {
$code = "dir ";
Get-OKCommandLength ($code | Get-Token) | Should -Be $code.TrimEnd().Length
}
It "Do not ignore internal space within a command" {
$code = "dir *.*";
Get-OKCommandLength ($code | Get-Token) | Should -Be $code.Length
}
It "Simple Variable, Operator, Number" {
$code = "`$x = 3";
Get-OKCommandLength ($code | Get-Token) | Should -Be $code.Length
}
It "Simple Variable, Operator, Number, Comment" {
$code = "`$x = 3 # Hi!";
#$code | Get-Token | ft | out-host;
Get-OKCommandLength ($code | Get-Token) | Should -Be ($code.Split("#")[0].TrimEnd().Length)
}
It "Complex comment" {
$code = "`$x = 3 <# Hi! #>";
Get-OKCommandLength ($code | Get-Token) | Should -Be ($code.Split("<#")[0].TrimEnd().Length)
}
It "Code with dollar embedded in quotes" {
$code = ".\publish.ps1 `"`$arg`"; # commit and push";
#$code | Get-Token | ft | out-host;
Get-OKCommandLength ($code | Get-Token) | Should -Be ($code.Split("#")[0].TrimEnd().Length)
}
It "Code with comment embedded in quotes" {
$code = "echo `"#`" # commit and push";
$parts = $code.Split("#");
$expected = ($parts[0].Length + "#".Length + $parts[1].TrimEnd().Length)
Get-OKCommandLength ($code | Get-Token) | Should -Be $expected;
}
#". .\profile.ps1" | clipp
#"`". .\profile.ps1`" | clipp"
It "This one fails in the wild" {
$code = "`". .\profile.ps1`" | clipp # dot profile";
#$code | Get-Token | ft | out-host;
Get-OKCommandLength ($code | Get-Token) | Should -Be ($code.Split("#")[0].TrimEnd().Length)
}
}