Skip to content

Instantly share code, notes, and snippets.

@atsushifx
Last active May 6, 2023 06:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atsushifx/caa148eaffd5de7f3af5fbcd82286fec to your computer and use it in GitHub Desktop.
Save atsushifx/caa148eaffd5de7f3af5fbcd82286fec to your computer and use it in GitHub Desktop.
PesterBDD
> 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
> 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.
> 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
<#
.SYNOPSIS
remove comments from input
#>
function Remove-Comments() {
Throw "No implementation"
}
<#
.SYNOPSIS
remove comments from input
#>
function remove-Comments {
Param (
[Parameter(ValueFromPipeline)]
[String[]] $text
)
process {
$text
}
}
<#
.SYNOPSIS
remove comments from input
#>
function remove-Comments {
Param (
[Parameter(ValueFromPipeline)]
[String[]] $text
)
process {
foreach ($line in $text) {
$line = ($line -replace '#[^.]*$', '').Trim()
$line
}
}
}
<#
.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
}
}
}
<#
.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
}
}
}
}
# 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
}
}
}
# 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."
}
}
# 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."
}
}
# 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'
}
}
}
# 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