Code Monkey home page Code Monkey logo

Comments (4)

nohwnd avatar nohwnd commented on September 23, 2024 1

This could theoretically be done, but

  1. we need a less generic name to be used in that hashtable to represent that scriptblock.
  2. 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.

dkaszews avatar dkaszews commented on September 23, 2024

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.

dkaszews avatar dkaszews commented on September 23, 2024

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.

dkaszews avatar dkaszews commented on September 23, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.