Last active
May 6, 2023 06:03
-
-
Save atsushifx/caa148eaffd5de7f3af5fbcd82286fec to your computer and use it in GitHub Desktop.
PesterBDD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> Invoke-Pester | |
Starting discovery in 1 files. | |
Discovery found 1 tests in 11ms. | |
Running tests. | |
[-] remove comment from source.function implemntation.should work with an parameter is empty 5ms (3ms|2ms) | |
RuntimeException: No implementation | |
at remove-Comments, \remove-Comments\remove-Comments.ps1:15 | |
at <ScriptBlock>, \remove-Comments\Tests\remove-Comments.Tests.ps1:23 | |
Tests completed in 75ms | |
Tests Passed: 0, Failed: 1, Skipped: 0 NotRun: 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> Invoke-Pester | |
Starting discovery in 1 files. | |
Discovery found 1 tests in 12ms. | |
Running tests. | |
[-] \powershell\remove-Comments\Tests\remove-Comments.Tests.ps1 failed with: | |
CommandNotFoundException: The term '\Powershell\remove-Comments\remove-Comments.ps1' is not recognized as | |
a name of a cmdlet, function, script file, or executable program. | |
Check the spelling of the name, or if a path was included, verify that the path is correct and try again. | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> Invoke-Pester | |
Starting discovery in 1 files. | |
Discovery found 1 tests in 179ms. | |
Running tests. | |
[+] \powershell\remove-Comments\Tests\remove-Comments.Tests.ps1 715ms (114ms|448ms) | |
Tests completed in 733ms | |
Tests Passed: 1, Failed: 0, Skipped: 0 NotRun: 0 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
remove comments from input | |
#> | |
function Remove-Comments() { | |
Throw "No implementation" | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
remove comments from input | |
#> | |
function remove-Comments { | |
Param ( | |
[Parameter(ValueFromPipeline)] | |
[String[]] $text | |
) | |
process { | |
$text | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
remove comments from input | |
#> | |
function remove-Comments { | |
Param ( | |
[Parameter(ValueFromPipeline)] | |
[String[]] $text | |
) | |
process { | |
foreach ($line in $text) { | |
$line = ($line -replace '#[^.]*$', '').Trim() | |
$line | |
} | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
remove comments from input | |
#> | |
function Remove-Comments() { | |
param( | |
# Source code text | |
[Parameter(ValueFromPipeline = $true)] | |
[String[]] | |
$text | |
) | |
process { | |
foreach ($line in $text) { | |
if ($line -match '("[^.]*#[^.]*"[^.]*)') { | |
$line = ($line -replace '("[^.]*#[^.]*"[^.]*)#[^.]*$', "`$1").Trim() | |
} | |
else { | |
$line = ($line -replace '#[^.]*$', '').Trim() | |
} | |
return $line | |
} | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.REMARK | |
# Copyright (c) 2023 Furukawa, Atsushi <atsushifx@aglabo.com> | |
# | |
# This software is released under the MIT License. | |
# https://opensource.org/licenses/MIT | |
#> | |
<# | |
.SYNOPSIS | |
remove comments from input | |
#> | |
function Remove-Comments() { | |
param( | |
# Source code text | |
[Parameter(ValueFromPipeline = $true)] | |
[String[]] | |
$text | |
) | |
begin { | |
$isSkipLine = false | |
} | |
process { | |
foreach ($line in $text) { | |
$line = $line.Trim() | |
if (!$isSkipLine -and ($line -match "^<#[^.]*(?!#>)$")) { | |
$isSkipLine = $true | |
} | |
elseif ( $isSkipLine -and ($line -match "[^.]*#>$")) { | |
$isSkipLine = $false | |
} | |
elseif ($isSkipLine) { | |
# do nothing | |
} | |
else { | |
# remove in-line commeny | |
$line = $line -replace '<#[^.]*#>\s*', '' | |
# remove line comment | |
if ($line -match '("[^.]*#[^.]*"[^.]*)') { | |
$line = ($line -replace '("[^.]*#[^.]*"[^.]*)#[^.]*$', "`$1").Trim() | |
} | |
else { | |
$line = ($line -replace '#[^.]*$', '').Trim() | |
} | |
return $line | |
} | |
} | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# load tests function script | |
# | |
BeforeAll { | |
$scriptDir = (Split-Path -Parent $PSScriptRoot) | |
$script = (Split-Path -Leaf $PSCommandPath).Replace('.Tests', '') | |
$script = $scriptDir + "\" + $script | |
# Write-Host "Script :" , $script | |
. $script | |
} | |
Describe "remove comment from source" { | |
context "check implementation" { | |
It 'should work for an empty string' { | |
@() | Remove-Comments | Should -BeNullOrEmpty | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# load tests function script | |
# | |
BeforeAll { | |
$scriptDir = (Split-Path -Parent $PSScriptRoot) | |
$script = (Split-Path -Leaf $PSCommandPath).Replace('.Tests', '') | |
$script = $scriptDir + "\" + $script | |
# Write-Host "Script :" , $script | |
. $script | |
} | |
Describe "remove comment from source" { | |
context "line comment" { | |
it "line is all comment" { | |
"# line comment" | remove-Comments | Should -BeNullOrEmpty | |
} | |
it "add comment to line end" { | |
"this is code. # this is comment" | remove-Comments | Should -Be "this is code." | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# load tests function script | |
# | |
BeforeAll { | |
$scriptDir = (Split-Path -Parent $PSScriptRoot) | |
$script = (Split-Path -Leaf $PSCommandPath).Replace('.Tests', '') | |
$script = $scriptDir + "\" + $script | |
# Write-Host "Script :" , $script | |
. $script | |
} | |
Describe "remove comment from source" { | |
context "line comment" { | |
it "line is all comment" { | |
"# line comment" | remove-Comments | Should -BeNullOrEmpty | |
} | |
it "add comment to line end" { | |
"this is code. # this is comment" | remove-Comments | Should -Be "this is code." | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# load tests function script | |
# | |
BeforeAll { | |
$scriptDir = (Split-Path -Parent $PSScriptRoot) | |
$script = (Split-Path -Leaf $PSCommandPath).Replace('.Tests', '') | |
$script = $scriptDir + "\" + $script | |
# Write-Host "Script :", $script | |
# load tests function script | |
# | |
BeforeAll { | |
$scriptDir = (Split-Path -Parent $PSScriptRoot) | |
$script = (Split-Path -Leaf $PSCommandPath).Replace('.Tests', '') | |
$script = $scriptDir + "\" + $script | |
# Write-Host "Script :", $script | |
. $script | |
} | |
Describe "remove comment from source" { | |
context "function implemntation" { | |
It "should work with an empty strings" { | |
@() | remove-Comments | Should -BeNullorEmpty | |
} | |
} | |
Context "line comment" { | |
It "line is all comment" { | |
"# line comment" | remove-Comments | Should -BeNullorEmpty | |
} | |
It "add comment to line end" { | |
"this is code. # this is comment" | remove-Comments | Should -Be "this is code." | |
} | |
It "in string" { | |
'" # in string " is code # is comment' | remove-Comments | Should -Be '" # in string " is code' | |
} | |
} | |
} | |
. $script | |
} | |
Describe "remove comment from source" { | |
context "function implemntation" { | |
It "should work with an empty strings" { | |
@() | remove-Comments | Should -BeNullorEmpty | |
} | |
} | |
Context "line comment" { | |
It "line is all comment" { | |
"# line comment" | remove-Comments | Should -BeNullorEmpty | |
} | |
It "add comment to line end" { | |
"this is code. # this is comment" | remove-Comments | Should -Be "this is code." | |
} | |
It "in string" { | |
'" # in string " is code # is comment' | remove-Comments | Should -Be '" # in string " is code' | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# load tests function script | |
# | |
BeforeAll { | |
$scriptDir = (Split-Path -Parent $PSScriptRoot) | |
$script = (Split-Path -Leaf $PSCommandPath).Replace('.Tests', '') | |
$script = $scriptDir + "\" + $script | |
# Write-Host "Script :" , $script | |
. $script | |
} | |
Describe "remove comment from source" { | |
Context "line comment" { | |
it "line is all comment" { | |
"# line comment" | remove-Comments | Should -BeNullOrEmpty | |
} | |
it "add comment to line end" { | |
"this is code. # this is comment" | remove-Comments | Should -Be "this is code." | |
} | |
it "in string" { | |
'" # in string " is code # is comment' | remove-Comments | Should -Be '" # in string " is code' | |
} | |
} | |
Context "inline comment" { | |
It "only inline comment" { | |
"this <# cooment #> is a code." | remove-Comments | Should -Be "this is a code." | |
} | |
It "ignore # in comment" { | |
"this is <# # is ignored #> a code." | remove-Comments | Should -Be "this is a code." | |
} | |
It "ignore # in string" { | |
'this is <# ignore "#" #> a code.' | remove-Comments | Should -Be "this is a code." | |
} | |
Context "multi-line comment" { | |
It "remove multi line comment" { | |
@( | |
"start comment", | |
"<#### start", | |
" # skip this line", | |
' "also skip thisline "', | |
" end ###>", | |
"end comment" | remove-Comments | Should -Be @("start comment", "end comment") | |
) | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment