Skip to content

Commit

Permalink
Merge pull request #6 from kachick/refactor-with-unit-tests
Browse files Browse the repository at this point in the history
Refactor with adding unit tests
  • Loading branch information
kachick authored Mar 3, 2024
2 parents 7a0688e + d75588a commit 150977c
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 13 deletions.
4 changes: 2 additions & 2 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ tasks:
test:
cmds:
# pwsh exit with 0 even if ReadLine made errors, the 1+ code is special for an example as parser error
# So testing here with the "silentness"
- pwsh --File ./tools/test-all.ps1 2>&1 | wc --bytes | grep -Fxq '0'
# So returns 1+ with the result by yourself in each cmd
- pwsh --File ./tools/test-all.ps1
- pwsh --Command 'Test-ModuleManifest -Path *.psd1'
fmt:
cmds:
Expand Down
20 changes: 11 additions & 9 deletions src/PSFzfHistory.psm1
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
# Do not add --height option for fzf, it shows nothing in keybind use
function Invoke-FzfHistory ([String]$fuzzy) {
$reversedCommandSet = [ordered]@{}

[Microsoft.PowerShell.PSConsoleReadLine]::GetHistoryItems() |
& Reverse |
ForEach-Object {
if (!$reversedCommandSet.Contains($_.CommandLine)) {
$reversedCommandSet.Add($_.CommandLine, $true) | Out-Null
}
}
Select-Object -ExpandProperty CommandLine |
Reverse |
AsOrderedSet |
fzf --scheme=history --no-sort --no-height --query $fuzzy
}

$reversedCommandSet.Keys | fzf --scheme=history --no-sort --no-height --query $fuzzy
# Avoid System.Collections.Generic.SortedSet from following points
# - the sort order is "index", not the character dictionary order
# - No creation intermediate objects and just used in pipe
function AsOrderedSet {
$set = New-Object System.Collections.Generic.HashSet[string];
$input | Where-Object { $set.Add($_) }
}

function Reverse {
Expand Down
15 changes: 15 additions & 0 deletions tests/PSFzfHistory.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Import-Module -Name .\src\PSFzfHistory.psm1

InModuleScope PSFzfHistory {
Describe 'AsOrderedSet' {
It 'returns unique elements they are keep first appeared order in original' {
("foo", "bar", "foo", "baz", "f", "BAR", "bar") | AsOrderedSet | Should -BeExactly ('foo', 'bar', 'baz', 'f', 'BAR')
}
}

Describe 'Reverse' {
It 'reverses given list' {
(1, 2, 3, 42, 987654321, 42) | Reverse | Should -BeExactly (42, 987654321, 42, 3, 2, 1)
}
}
}
2 changes: 1 addition & 1 deletion tools/lint-fix.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Do NOT use 'Invoke-ScriptAnalyzer -Recurse -Path .' It includes dotfiles as .direnv
# https://github.com/PowerShell/PSScriptAnalyzer/issues/561
Get-ChildItem -Recurse -Path . -Include "*.psm1" |
Get-ChildItem -Recurse -Path src -Include "*.ps*1" |
ForEach-Object {
Write-Output $_.FullName
Invoke-ScriptAnalyzer -Recurse -ReportSummary -EnableExit -Settings CodeFormatting -Fix -Path $_.FullName
Expand Down
6 changes: 5 additions & 1 deletion tools/test-all.ps1
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
Import-Module -Name .\src\PSFzfHistory.psm1
# https://pester.dev/docs/commands/New-PesterConfiguration
$config = New-PesterConfiguration
$config.Run.Exit = $true

Invoke-Pester -Configuration $config

0 comments on commit 150977c

Please sign in to comment.