我知道如何编写一个从管道输入流的函数。我可以通过阅读函数的源来合理地告诉它是否能正常执行。但是,有没有任何方法可以实际测试以获得正确的行为?
我接受“测试”的任何定义......是我可以运行的一些手动测试或更自动化的。
如果你需要一个例子,假设我有一个将文本分成单词的功能。
PS> Get-Content ./warandpeace.txt | Split-Text
我如何检查它是否从流水线输入流并立即开始拆分?
答案 0 :(得分:2)
您可以编写一个辅助函数,它可以为您提供一些指示,作为传递给它的管道项并由下一个命令处理:
function Print-Pipeline {
param($Name, [ConsoleColor]$Color)
begin {
$ColorParameter = if($PSBoundParameters.ContainsKey('Color')) {
@{ ForegroundColor = $Color }
} else {
@{ }
}
}
process {
Write-Host "${Name}|Before|$_" @ColorParameter
,$_
Write-Host "${Name}|After|$_" @ColorParameter
}
}
假设您有一些要测试的功能:
$Text = 'Some', 'Random', 'Text'
function CharSplit1 { $Input | % GetEnumerator }
filter CharSplit2 { $Input | % GetEnumerator }
你可以像那样测试它们:
PS> $Text |
>>> Print-Pipeline Before` CharSplit1 |
>>> CharSplit1 |
>>> Print-Pipeline After` CharSplit1
Before CharSplit1|Before|Some
Before CharSplit1|After|Some
Before CharSplit1|Before|Random
Before CharSplit1|After|Random
Before CharSplit1|Before|Text
Before CharSplit1|After|Text
After CharSplit1|Before|S
S
After CharSplit1|After|S
After CharSplit1|Before|o
o
After CharSplit1|After|o
After CharSplit1|Before|m
m
After CharSplit1|After|m
After CharSplit1|Before|e
e
After CharSplit1|After|e
After CharSplit1|Before|R
R
After CharSplit1|After|R
After CharSplit1|Before|a
a
After CharSplit1|After|a
After CharSplit1|Before|n
n
After CharSplit1|After|n
After CharSplit1|Before|d
d
After CharSplit1|After|d
After CharSplit1|Before|o
o
After CharSplit1|After|o
After CharSplit1|Before|m
m
After CharSplit1|After|m
After CharSplit1|Before|T
T
After CharSplit1|After|T
After CharSplit1|Before|e
e
After CharSplit1|After|e
After CharSplit1|Before|x
x
After CharSplit1|After|x
After CharSplit1|Before|t
t
After CharSplit1|After|t
PS> $Text |
>>> Print-Pipeline Before` CharSplit2 |
>>> CharSplit2 |
>>> Print-Pipeline After` CharSplit2
Before CharSplit2|Before|Some
After CharSplit2|Before|S
S
After CharSplit2|After|S
After CharSplit2|Before|o
o
After CharSplit2|After|o
After CharSplit2|Before|m
m
After CharSplit2|After|m
After CharSplit2|Before|e
e
After CharSplit2|After|e
Before CharSplit2|After|Some
Before CharSplit2|Before|Random
After CharSplit2|Before|R
R
After CharSplit2|After|R
After CharSplit2|Before|a
a
After CharSplit2|After|a
After CharSplit2|Before|n
n
After CharSplit2|After|n
After CharSplit2|Before|d
d
After CharSplit2|After|d
After CharSplit2|Before|o
o
After CharSplit2|After|o
After CharSplit2|Before|m
m
After CharSplit2|After|m
Before CharSplit2|After|Random
Before CharSplit2|Before|Text
After CharSplit2|Before|T
T
After CharSplit2|After|T
After CharSplit2|Before|e
e
After CharSplit2|After|e
After CharSplit2|Before|x
x
After CharSplit2|After|x
After CharSplit2|Before|t
t
After CharSplit2|After|t
Before CharSplit2|After|Text
答案 1 :(得分:1)
向Zip
函数添加一些Write-Verbose
语句,然后使用Split-Text
参数调用它。你应该实时看到输出。
答案 2 :(得分:0)
PS> 1..10 | %{ Write-Host $_; $_ } | function-under-test
如果你的被测功能是“坏”,你会看到1..10的所有输出两次,比如这个
1
2
3
1
2
3
如果被测功能是从管道中懒洋洋地处理项目,你会看到输出是交错的。
1
1
2
2
3
3