Skip to content

Commit

Permalink
✨ First round of PSObject koans complete!
Browse files Browse the repository at this point in the history
  • Loading branch information
vexx32 committed Jan 7, 2019
1 parent 278fe50 commit 761c36f
Showing 1 changed file with 63 additions and 3 deletions.
66 changes: 63 additions & 3 deletions PSKoans/Koans/Constructs/AboutPSObjects.Koans.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ Describe 'PSObject' {
$Object = @('Stand for nothing; fall for everything.')
}

It 'is accessed via a hidden .PSObject property' {
It "is accessed via a hidden .PSObject property" {
<#
The .PSObject member contains many properties, but the .PSObject property itself does not
actually tab-complete, though its child properties do.
actually register for tab-completion, though its child properties do.
#>
$PSObjectProperties = ($Object.PSObject | Get-Member -MemberType Properties).Name

Expand All @@ -39,7 +39,7 @@ Describe 'PSObject' {
) | Should -Be $PSObjectProperties
}

It 'details the base object properties and methods' {
It "details the base object's properties and methods" {
$PropertyNames = @(
__
'LongLength'
Expand All @@ -56,4 +56,64 @@ Describe 'PSObject' {
__ | Should -Be $Methods.Count
$Methods['__'].Name | Should -Be 'Length'
}

It "can be found on any object in PowerShell" {
# Even an empty collection holds some meaning
$Empty = @()

# Native .NET objects have their standard properties mapped to PSObject properties for easy access
$Empty.PSObject.Properties.Name | Should -Not -BeNullOrEmpty
@('__', '__', '__', '__', 'IsReadOnly', '__', '__', 'IsSynchronised', '__') | Should -Be $Empty.PSObject.Properties.Name
__ | Should -Be $Empty.IsReadOnly
}

It "exposes the object's ETS properties as a collection" {
<#
ETS (Extended Type System) properties are often created by PowerShell's own internal methods for many
pre-existing types, but we can create and add new ones at will.
A PSCustomObject's properties are all ETS NoteProperties.
#>
$Object = [PSCustomObject]@{
Flower = "Rose"
Color = "Red"
Thorns = $true
}

@('__', '__', '__') | Should -Be $Object.PSObject.Properties.Name
# Collections come in many shapes and sizes - what type is this one?
'__' | Should -Be $Object.PSObject.Properties.GetType().Name
'__' | Should -Be $Object.PSObject.Properties['Flower'].MemberType
}

It "details where the properties originate from" {
# Sometimes where it comes from is more important then what it is
$Object = @()
Add-Member -InputObject $Object -MemberType NoteProperty -Name 'TestProperty' -Value __

$Object.TestProperty | Should -Be 12
'__' | Should -Be $Object.PSObject.Properties['TestProperty'].MemberType

$PropertyTypes = $Object.PSObject.Properties |
Group-Object -Property MemberType |
Select-Object -ExpandProperty Name

@( '__', '__' ) | Should -Be $PropertyTypes
}

It "can find derivative properties" {
$Name = @("Julian")
Add-Member -InputObject $Name -MemberType ScriptProperty -Name 'Letters' -Force -Value {
# A ScriptProperty is dynamic, and can reference the object itself using the reserved variable '$this'.
$this | ForEach-Object { $_ -as [char[]] -as [string[]] }
}
Add-Member -InputObject $Name -MemberType NoteProperty -Name 'Surname' -Value 'Sylph'

'__' | Should -Be $Name.Letters[3]

$PropertyTypes = $Name.PSObject.Properties |
Group-Object -Property MemberType |
ForEach-Object -MemberName Name

@( '__', '__', '__') | Should -Be $PropertyTypes
}
}

0 comments on commit 761c36f

Please sign in to comment.