Skip to content

Commit

Permalink
🔧 🎨 Fix Outstanding koan issues (#233)
Browse files Browse the repository at this point in the history
* 🔧 Fix #227 AboutHashtables
Remove redundant assertion
Improve comments

* Fix #218 AboutStringOperators
Split koan into two parts to clarify how the pattern works
Add comment to clarify
Credit to @IISResetMe for the koan design

* 🔧 Fix #206 AboutArrays
Reframe the assertion so the error message makes sense
Use an empty space in the array & add comma to indicate the placeholder

* 🔧 Fix #186 AboutLists
Clarify comments
Remove extra, misleading blank slots
Reframe lambdas a bit to clean up syntax
Fix blank formats

* 🔧 Fix #184 AboutModules
Address errors
Fix inconsistencies, remove fragile parts that checked random values

* 🔧 Fix #182 AboutGroupObject

* 🔧 Fix #176 AboutTeeObject
Variables are not created till the completion of the pipeline
Reframe some assertions appropriately
Add some comments clarifying a few things
Reformat blanks to current standard

* 📝 Fix #175 AboutLoopsAndPipelines
Fix comments

* 🔧 Fix #171 AboutDiscovery
Fix problematic assertions

* 🔧 Fix #195 AboutRedirection
Use correct line indexes for error demonstration

* 🔧 Fix issues in AboutErrorHandling

* 📝 Update AboutOutCmdlets
Formatting revision

* 🔧 Fix #163 AboutComparison
Update comments
Reframe confusing assertions
Update style to match present standard

* 🎨 Fix misplaced indent in AboutDiscovery

* 🎨 Minor updates and fixes
Mostly whitespace/comment adjustments
  • Loading branch information
vexx32 committed Sep 3, 2019
1 parent 5cd6b86 commit adbd525
Show file tree
Hide file tree
Showing 13 changed files with 287 additions and 185 deletions.
38 changes: 21 additions & 17 deletions PSKoans/Koans/Cmdlets 1/AboutDiscovery.Koans.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ Describe 'Get-Help' {

It 'gives exhaustive information for cmdlets and functions' {
$HelpInfo = Get-Help 'Get-Help'
$GetHelpParams = $HelpInfo.Parameters.Parameter.Name
$GetHelpParams = $HelpInfo.Parameters.Parameter.Name | Sort-Object

# Using the information from Get-Help, fill in the missing parameters in alphabetical order.
$GetHelpParams | Should -Be @(
$ParamNames = @(
'Category'
'Component'
'__'
Expand All @@ -44,13 +44,14 @@ Describe 'Get-Help' {
'Role'
'__'
)
$ParamNames | Should -Be $GetHelpParams
}

It 'can give detailed information on specific parameters' {
# You can also query specific parameters for more detailed information on what they can do.
# For instance, does the Path parameter for Get-Help support pipeline input?
$ParameterInfo = Get-Help 'Get-Help' -Parameter Path
__ | Should -Be $ParameterInfo.PipelineInput
$____ | Should -Be $ParameterInfo.PipelineInput
}
<#
Remember: if 'Get-Help Cmdlet-Name' doesn't show you all you need, try -Full.
Expand Down Expand Up @@ -88,15 +89,15 @@ Describe 'Get-Member' {
Which property of the above string has the expected value?
#>
$PropertyName = '__'
$PropertyName = '____'
$String.$PropertyName | Should -Be 6
}

It 'can also find useful methods' {
$String = "Methods are handy!"

# Methods can be accessed just like properties, but have parentheses and often parameters!
$String.EndsWith('__') | Should -BeTrue
$String.EndsWith('____') | Should -BeTrue
<#
If you have trouble figuring out which parameters a method requires, you can check the
OverloadDefinitions by calling the method name without the parentheses. For the above
Expand All @@ -115,8 +116,8 @@ Describe 'Get-Member' {
be used at a time, and usually the number of arguments and type of the arguments
determine the overload that PowerShell will apply when calling the method.
#>
$MethodName = '__'
$MethodArguments = @('__', '__')
$MethodName = '____'
$MethodArguments = @('____', '____')
# The ForEach-Object cmdlet can be used to call methods as well.
'7', '8', '9', '10' |
ForEach-Object -MemberName $MethodName -ArgumentList $MethodArguments |
Expand All @@ -137,7 +138,7 @@ Describe 'Get-Member' {
}

# Which method can be used to remove these files?
$MethodName = '__'
$MethodName = '____'
$TempFiles | ForEach-Object $MethodName

$TempFiles | Test-Path | Should -BeFalse
Expand All @@ -146,7 +147,7 @@ Describe 'Get-Member' {
It 'actually returns objects itself' {
$MemberData = 'string' | Get-Member
# We can all betray our own selves.
$MemberData | Should -BeOfType __
$MemberData | Should -BeOfType ____
}
}
}
Expand All @@ -171,42 +172,45 @@ Describe 'Get-Command' {
# Try calling Get-Command in a PowerShell console to see the typical output!
$CommandCount = Get-Command | Measure-Object | Select-Object -ExpandProperty Count
__ | Should -Be $CommandCount
Get-Command | Select-Object -First 1 -ExpandProperty Name | Should -Be '__'
Get-Command | Select-Object -First 1 -ExpandProperty Name | Should -Be '____'
}

It 'indicates the type of command' {
$CommandType = Get-Command | Select-Object -Skip 3 -First 1 -ExpandProperty CommandType

'__' | Should -Be $CommandType
'____' | Should -Be $CommandType
}

It 'can filter the output by keywords' {
$Command = Get-Command -Name "*-Child*"
$CimCommand = Get-Command -Name '__'

$Command.CommandType | Should -Be 'Cmdlet'
'__' | Should -Be $Command.Name
'____' | Should -Be $Command.Name

$CimCommand = Get-Command -Name '____'
$CimCommand.Name | Should -Be 'Get-CimClass'
}

It 'can look for commands by verb' {
$GetCommands = Get-Command -Verb 'Get'
__ | Should -Be $GetCommands.Count

'__' | Should -Be $GetCommands[4].Name
'____' | Should -Be $GetCommands[4].Name
}

It 'can look for commands by noun' {
$DateCommands = Get-Command -Noun 'Date'

__ | Should -Be $DateCommands.Count
'__' | Should -Be $DateCommands[0].Name
'____' | Should -Be $DateCommands[0].Name
}

It 'can look for commands by module' {
$KoanCommands = Get-Command -Module 'PSKoans'
$KoanCommands = Get-Command -Module 'PSKoans' |
Sort-Object -Property Name
$First4Commands = $KoanCommands | Select-Object -First 4

__ | Should -Be $KoanCommands.Count
$KoanCommands.Name | Should -Be @('__', '__', '__', '__')
@('____', '____', '____', '____') | Should -Be $KoanCommands.Name
}
}
2 changes: 1 addition & 1 deletion PSKoans/Koans/Cmdlets 1/AboutGroupObject.Koans.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Describe 'Group-Object' {
Name (Property that they are grouped by)
Group (All the objects in the group)
#>
'__' | Should -Be $Extensions[3].Group.Extension
'____' | Should -Be $Extensions[3].Name
__ | Should -Be $Extensions[2].Count
}

Expand Down
88 changes: 53 additions & 35 deletions PSKoans/Koans/Cmdlets 1/AboutModules.Koans.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -25,40 +25,46 @@ Describe 'Get-Module' {
the session, or to search available modules on the computer.
#>
It 'returns a list of modules in the current session' {
# To list all installed modules, use the -ListAvailable switch.
<#
By default, Get-Module returns modules currently in use.
To get a list of all modules on the system, you can use the -ListAvailable switch.
#>
$Modules = Get-Module | Sort-Object -Property Name -Unique

$Modules = Get-Module
$FirstThreeModules = @('__', '__', '__')
$VersionOfThirdModule = '__'
$TypeOf6thModule = '__'
$FirstThreeModules = $Modules | Select-Object -First 3
$VersionOfThirdModule = $FirstThreeModules[2].Version
$TypeOf6thModule = $Modules[5].ModuleType

$FirstThreeModules | Should -Be $Modules[0..2].Name
$VersionOfThirdModule | Should -Be $Module[2].Version
$TypeOf6thModule | Should -Be $Modules[5].ModuleType
@('____', '____', '____') | Should -Be $FirstThreeModules.Name
'____' | Should -Be $VersionOfThirdModule
'____' | Should -Be $TypeOf6thModule
}

It 'can filter by module name' {
$Pester = Get-Module -Name 'Pester'
$PesterCommands = $Pester.ExportedCommands.Values.Name

$ThreeExportedCommands = @('__', '__', '__')

$ThreeExportedCommands | Should -BeIn $Pester.ExportedCommands.Values.Name
@('____', '____', '____') | Should -BeIn $PesterCommands
}

It 'can list nested modules' {
$AllModules = Get-Module -All

$Axioms = $AllModules | Where-Object Name -eq 'Axiom'
$AxiomsCommands = $Axioms.ExportedCommands.Values.Name

$Commands = @('__', '__', '__')
$Commands | Should -BeIn $Axioms.ExportedCommands.Values.Name
$Commands = @('____', '____', '____')
$Commands | Should -BeIn $AxiomsCommands
<#
Despite us being able to 'see' this module, we cannot use its commands as it
is only available in Pester's module scope.
Despite us being able to 'see' this module, we cannot use its commands;
it is only available within the scope of the Pester module itself,
since it's a nested module within Pester.
#>
{if ($Commands[1] -in $Axioms.ExportedCommands.Values.Name) {
{
if ($Commands[1] -in $Axioms.ExportedCommands.Values.Name) {
& $Commands[1]
}} | Should -Throw
}
} | Should -Throw
}
}

Expand All @@ -77,31 +83,31 @@ Describe 'Find-Module' {
Mock Find-Module {
Get-Module -ListAvailable -Name $Name |
Select-Object -Property Version, Name, @{
Name = 'Repository'
Expression = {'PSGallery'}
}, Description
Name = 'Repository'
Expression = {'PSGallery'}
}, Description
}

$Module = Find-Module -Name 'Pester' | Select-Object -First 1
}

It 'finds modules that can be installed' {
'__' | Should -Be $Module.Name
'____' | Should -Be $Module.Name
}

It 'lists the latest version of the module' {
'__' | Should -Be $Module.Version
'____' | Should -Be $Module.Version
}

It 'indicates which repository stores the module' {
<#
Unless an additional repository has been configured, all modules
from Find-Module will come from the PowerShell Gallery.
#>
'__' | Should -Be $Module.Repository
'____' | Should -Be $Module.Repository
}
It 'gives a brief description of the module' {
'__' | Should -Match $Module.Description
'____' | Should -Match $Module.Description
}
}

Expand All @@ -110,19 +116,28 @@ Describe 'New-Module' {
New-Module is rarely used in practice, but is capable of dynamically generating
a module in-memory without needing a file on disk.
#>
BeforeAll {
$Module = New-Module -Name 'PSKoans_TestModule' -ScriptBlock {} -ArgumentList
}

It 'creates a dynamic module object' {
$Module = New-Module -Name 'PSKoans_TestModule' -ScriptBlock {}
$Module | Should -Not -BeNullOrEmpty
}

It 'has many properties with $null defaults' {
# For most modules, ModuleBase lists their primary storage location
__ | Should -Be $Module.ModuleBase
<#
RootModule is usually the script file or .dll that contains the main portion of the module's code.
For a module like this, that property is meaningless -- there is no such file.
#>
$____ | Should -Be $Module.RootModule
}

It 'supplies the module with a GUID based random path' {
'__' | Should -Be $Module.Path
It 'uses an existing filesystem location as the ModuleBase' {
<#
In most cases, the ModuleBase for a dynamically generated module will be the current location.
This may not be accurate in all cases, depending on scoping.
#>
'____' | Should -Be $Module.ModuleBase
}

AfterAll {
Expand All @@ -137,7 +152,7 @@ Describe 'Import-Module' {
#>
Context 'Importing Installed Modules' {
BeforeAll {
$Module = New-Module -Name 'PSKoans_ImportModuleTest' {}
$Module = New-Module -Name 'PSKoans_ImportModuleTest' { }
}

It 'does not produce output' {
Expand All @@ -148,15 +163,15 @@ Describe 'Import-Module' {
$ImportedModule = Get-Module -Name 'PSKoans_ImportModuleTest'

$ImportedModule.ExportedCommands.Keys | Should -BeNull
'__' | Should -Be $ImportedModule.Name
'____' | Should -Be $ImportedModule.Name
}
}

Context 'Importing Module From File' {
BeforeAll {
$ModuleScript = {
function Test-ModuleFunction {
__ # Fill in the correct value here
'____' # Fill in the correct value here
}
Export-ModuleMember 'Test-ModuleFunction'
}
Expand All @@ -168,12 +183,15 @@ Describe 'Import-Module' {
}

It 'imports the module into the current session' {
$ImportedModule = Get-Module -Name '__'
$ImportedModule = Get-Module -Name '____'
$ImportedModule | Should -Not -BeNullOrEmpty
}

It 'makes the module''s exported commands available for use' {
@('__') | Should -Be $ImportedModule.ExportedCommands.Keys
It 'makes the exported commands from the module available for use' {
$ImportedModule = Get-Module -Name '____'
@('____') | Should -Be $ImportedModule.ExportedCommands.Values.Name

Test-ModuleFunction | Should -BeExactly 'A successful test.'
}
}

Expand Down
17 changes: 10 additions & 7 deletions PSKoans/Koans/Cmdlets 1/AboutTeeObject.Koans.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,24 @@ Describe 'Tee-Object' {

It 'passes the object on through the pipeline' {
# The value passed to -Variable is created as a new variable
1..10 | Tee-Object -Variable __ | Should -Be $n
$Value = 10
__ | Tee-Object -Variable Tee | Should -Be $Value
}

It 'can store the object(s) into a variable' {
# Note the variable name is passed as a string, without the $!
'alpha', 'beta', 'gamma' | Tee-Object -Variable 'Numbers' | Should -Be $Numbers
'__' | Should -Be $Numbers[1]
# Note the variable name is given as a string, without the $ prefix.
$Values = 'alpha', 'beta', 'gamma'
@('____', '____', '____', '____') | Tee-Object -Variable 'Numbers' | Should -Be $Values
'____' | Should -Be $Numbers[1]
}

It 'can also store the object(s) into a file' {
$File = New-TemporaryFile

$Output = 1..5 | ForEach-Object {"{0:N2}" -f (1 / $_)} | Tee-Object -FilePath $File.FullName
$Stored = $File | Get-Content
$Output = 1..5 | ForEach-Object { "{0:N2}" -f (1 / $_) } | Tee-Object -FilePath $File.FullName
$Stored = Get-Content -Path $File

__ | Should -Be $Stored
# Text files can only store string data, so be careful storing arbitrary data to files like this.
@('__', '__', '3', '__', '__') | Should -Be $Stored
}
}
20 changes: 10 additions & 10 deletions PSKoans/Koans/Cmdlets 2/AboutOutCmdlets.Koans.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ Context 'Out-* Cmdlets' {

'Stored knowledge is of little value until it is used.' | Out-File -FilePath $Path -NoNewline

'__' | Should -Be (Get-Content -Path $Path)
'____' | Should -Be (Get-Content -Path $Path)
}
}

Expand All @@ -103,7 +103,7 @@ Context 'Out-* Cmdlets' {
# Out-Null discards any input sent to it, making it very useful for suppressing output.

It 'does absolutely nothing with output sent to it' {
$String = '__'
$String = '____'
$String | Should -Be 'What can you learn to do today that you have never ben able to do before?'

$String | Out-Null | Should -BeNullOrEmpty
Expand All @@ -118,19 +118,19 @@ Context 'Out-* Cmdlets' {

It 'creates string representations of data' {
#Create a hashtable that pipes into Out-String
$String = @{ } | Out-String

@"
$ExpectedString = @{ ____ = '____'; Spectrum = '____' } | Out-String

Name Value
---- -----
$ActualString = @"
Name Value
---- -----
Color Blue
Spectrum Ultraviolet
Spectrum Ultraviolet
"@ | Should -Be $String
# Mind the indentations; here-strings have to terminate at the very beginning of a line.
"@ # Mind the indentations; here-strings have to terminate at the very beginning of a line.
$ActualString | Should -Be $ExpectedString
}
}
}

0 comments on commit adbd525

Please sign in to comment.