Comments (4)
This could theoretically be done, but
- we need a less generic name to be used in that hashtable to represent that scriptblock.
- it is not very pleasant to provide that scriptblock for every expansion, and we don't have a more convenient place for it, unless we introduce additional parameter to It, Describe and Context, like --DisplayNameScriptblock.
There is an easy way to get this result, which is simply putting both Expected and AreEqual into the hashtable.
Just for the geeks out there, but I don't recommend using this:
You can also trick the code that does the re-evaluation of the string, by embedding a sub-expression, and have it evaluate any known function or your provided scripblock:
See if you can figure out why it works from the escaping code here:
https://github.com/pester/Pester/blob/main/src/Pester.Runtime.ps1#L392-L405
The only downside there is that you get an extra `
before the expanding, but you can just backspace that 😁
Invoke-pester -container (new-pestercontainer -scriptblock {
BeforeDiscovery {
function Same ($Same){ $Same -eq "same" ? "`bTHE SAME" : "`bSOOO DIFFERENT" }
}
describe "a" {
It '<First> and <Second> return `$(Same <AreSame>) results' -ForEach @(
@{ First = 'Foo'; Second = 'Bar'; AreSame = 'same'; Eval = { param ($a) Write-Host -ForegroundColor Red "-$a-" } }
@{ First = 'Foo'; Second = 'Blergh'; AreSame = 'different' }
) {
# the test
}
It '<First> and <Second> return `$(& <Eval> <AreSame>) results' -ForEach @(
@{ First = 'Foo'; Second = 'Bar'; AreSame = 'same'; Eval = { param ($a) $a -eq "same" ? "`bTHE SAME" : "`bSOOO DIFFERENT" } }
@{ First = 'Foo'; Second = 'Blergh'; AreSame = 'different'; Eval = { param ($a) $a -eq "same" ? "`bTHE SAME" : "`bSOOO DIFFERENT" } }
) {
# the test
}
}
}) -Output Diagnostic
Running tests.
Describing a
[+] Foo and Bar return THE SAME results 6ms (3ms|4ms)
[+] Foo and Blergh return SOOO DIFFERENT results 2ms (1ms|1ms)
[+] Foo and Bar return THE SAME results 6ms (5ms|2ms)
[+] Foo and Blergh return SOOO DIFFERENT results 5ms (3ms|1ms)
Tests completed in 184ms
Tests Passed: 4, Failed: 0, Skipped: 0 NotRun: 0
from pester.
I think you misunderstood, I am proposing to pass the scriptblock into the -Name
parameter, not the -ForEach
hashtable for every case. So the syntax is nearly identical as current templating, just wrapped into braces and double quotes instead of singles:
# Old style
It 'Tests with <First> and <Second>' ...
# New style
It {"Tests with $First and $Second"} ...
No new parameters are required, all Pester has to do is check the type of -Name
(example code, did not look into internals):
- $DisplayName = ProcessTemplate $Name $TestCase
+ if ($Name is [String]) {
+ $DisplayName = Process-Template $Name $TestCase
+ } else {
+ $Display Name = Invoke-Command $Name -ArgumentList $TestCase
+ }
Maybe some other PowerShell magic will be required to pass the arguments without having to create param()
, but you get the idea.
from pester.
An alternative idea with similar syntax is to add a parameter which instead of processing the template will just call $ExecutionContext.InvokeCommand.ExpandString($Name)
. The examples above become then:
It -ExpandName 'Tests with $First and $Second' ...
I find it less elegant, as it requires extra parameter and use of single quotes to delay expansion, which may look weird and unintuitive.
I don't really understand why Pester did not go into one of those approaches in the first place. It limits the templating to just variables and their children, and the hack you have shown indicates that it is possible to break out of this "sandbox" anyways.
from pester.
There is an easy way to get this result, which is simply putting both Expected and AreEqual into the hashtable.
By same argument, you could just write the entire test name into an argument and use -Name '<DisplayName>'
, but that kinda defeats the whole point of templating, doesn't it?
from pester.
Related Issues (20)
- Update SECURITY.md and SUPPORT.md? HOT 1
- Option to disable old assertions HOT 7
- Add trimWhitespace to Should-beString
- Add description to all should-* assertions and remove this todo HOT 1
- Update v5 help links in to versioned URIs? HOT 2
- Should-BeLikeString does not show types of data
- don't return values from assertions except for throw HOT 3
- Remove code specifically for PS 2.0-5.0 HOT 5
- Tests are not included in coverage even when ExcludeTests = $false; HOT 2
- Test discovery on linux fails to find tests in hidden folders HOT 7
- Pester Testing fails to create a container for Azure Automation commands HOT 2
- `Should-BeFals*`/`Should-BeTru*`: The commands are missing named parameter `Expected` HOT 3
- Enable code coverage tests on server when overall code coverage is measured
- `Should-Throw`: It is not possible using positional parameters when also using pipeline input HOT 5
- `Should-Throw`: How to negate the evaluation? HOT 6
- Deduplicate files provided using Run.Path and -Path? HOT 2
- Suggest to add `Should-Match` to Pester v6 HOT 6
- Suggest to add an option to assert the correct number of items in a collection to Pester v6 HOT 5
- Container is reported as Passed/Successful when discovery fails HOT 3
- Calling pester from a class HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pester.