Skip to content

Commit c38c911

Browse files
Add additional tests (#4)
* Add additional tests - Add tests for regex matching Name, Namespace, and FullName for types - Add test for invoking Find-Member without input, finding nonpublic members, unwrapping ByRef and Array types during matching, and only retrieving members for unique types from piped objects. * Change to more reliable namespace test
1 parent b6a0147 commit c38c911

File tree

3 files changed

+73
-1
lines changed

3 files changed

+73
-1
lines changed

test/Find-Member.Tests.ps1

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,29 @@ Describe 'Find-Member cmdlet tests' {
1818
}
1919

2020
It 'filters passed members' {
21-
$source = [type] | Find-Member
21+
$source = [powershell] | Find-Member -Force
2222
$results = $source | Find-Member -Static
2323

2424
$results | ShouldAll { $_ -in $source }
2525
$results.Count | Should -Not -Be $source.Count
2626
}
2727
}
2828

29+
It 'gets all members with no input' {
30+
$results = Find-Member
31+
$results.Count | Should -BeGreaterThan 10000
32+
}
33+
34+
It 'matches nonpublic members with Force' {
35+
$result = $ExecutionContext.SessionState | Find-Member -Force
36+
37+
# A nonpublic member is present
38+
$result | ShouldAny { $_.Name -eq 'Internal' -and 'Property' -eq $_.MemberType }
39+
40+
# A public member is still present
41+
$result | ShouldAny { $_.Name -eq 'Module' -and 'Property' -eq $_.MemberType }
42+
}
43+
2944
It 'matches with FilterScript' {
3045
$result = [powershell] |
3146
Find-Member -FilterScript { $_.Name -eq 'Create' -and $_.GetParameters().Count -eq 0 }
@@ -58,6 +73,31 @@ Describe 'Find-Member cmdlet tests' {
5873
ShouldAny { $_.Name -eq 'Create' }
5974
}
6075

76+
It 'return type matches constructors' {
77+
[System.Collections.Generic.List[string]] |
78+
Find-Member -ReturnType System.Collections.Generic.List[string] |
79+
ShouldAny { 'Constructor' -eq $_.MemberType }
80+
}
81+
82+
It 'unwraps target types with elements' {
83+
$result = [System.Management.Automation.Language.Parser] |
84+
Find-Member -ParameterType System.Management.Automation.Language.Token
85+
86+
$result | ShouldAny {
87+
($parameters = $_.GetParameters()) -and
88+
$parameters[1].ParameterType.IsByRef -and
89+
$parameters[1].ParameterType.GetElementType().IsArray
90+
}
91+
}
92+
93+
It 'return type matches fields' {
94+
$contextType = Find-Type ExecutionContext -Namespace *Automation -Force
95+
$result = $ExecutionContext | Find-Member -ReturnType $contextType -Force
96+
97+
$result.MemberType | Should -Be Field
98+
$result.FieldType | Should -Be $contextType
99+
}
100+
61101
It 'filters to virtual members' {
62102
$results = [runspace] | Find-Member -Virtual
63103

@@ -89,4 +129,15 @@ Describe 'Find-Member cmdlet tests' {
89129
$results | ShouldAll { $_.Name -ne 'AddScript' }
90130
$results | ShouldAny { $_.Name -eq 'Create' }
91131
}
132+
133+
It 'gets members for passed objects only once per type' {
134+
$powershells = [powershell]::Create(), [powershell]::Create()
135+
try {
136+
$result = $powershells | Find-Member
137+
} finally {
138+
$powershells.ForEach('Dispose')
139+
}
140+
141+
$result.Where{ $_.Name -eq 'Create' }.Count | Should -Be 3
142+
}
92143
}

test/Find-Type.Tests.ps1

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,22 @@ Describe 'Find-Type tests' {
3535
It 'matches by namespace' {
3636
Find-Type -Namespace System.Timers | ShouldAll { $_.Namespace -eq 'System.Timers' }
3737
}
38+
It 'matches by full name' {
39+
Find-Type -FullName System.Management.Automation.ProxyCommand |
40+
Should -Be ([System.Management.Automation.ProxyCommand])
41+
}
42+
It 'matches by name with regex' {
43+
Find-Type "Runspace(Factory|ConnectionInfo)" -RegularExpression |
44+
Should -Be ([runspacefactory], [System.Management.Automation.Runspaces.RunspaceConnectionInfo])
45+
}
46+
It 'matches by namespace with regex' {
47+
Find-Type MethodAttributes -Namespace 'System\.(?!Reflection).+' -RegularExpression |
48+
Should -Be ([System.Management.Automation.Language.MethodAttributes])
49+
}
50+
It 'matches by full name with regex' {
51+
Find-Type -FullName 'System\.(?!Threading)\w+\.Timer$' -RegularExpression |
52+
Should -Be ([System.Timers.Timer])
53+
}
3854
It 'matches by base class' {
3955
$results = Find-Type -InheritsType System.Management.Automation.Language.Ast
4056

test/Get-Parameter.Tests.ps1

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,9 @@ Describe 'Get-Parameter tests' {
1010

1111
$results | ShouldAny { $_.Name -eq 'asyncResult' }
1212
}
13+
14+
It 'returns nothing without input' {
15+
Get-Parameter | Should -BeNullOrEmpty
16+
$null | Get-Parameter | Should -BeNullOrEmpty
17+
}
1318
}

0 commit comments

Comments
 (0)