Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I've now come up with a somewhat elaborate solution which is not exactly ideal from an OO- POV because it requires that the implementer of the test knows how the mock is implemented internally but I think that is still somewhat acceptable as no assumption can be made about how else to specify an open array argument expectation anyway (at least none that would compile).</p> <p>So, this is what the implementation of my mocked method now looks like:</p> <pre><code>function TImplementationProviderMock.GetInstance( const AIID: TGUID; out AInstance; const AArgs: array of const; const AContextID: TImplContextID): Boolean; var lCall: TMockMethod; lArgs: TOpenArray; begin lArgs := ConcatArrays([ArgsToArray([@AIID]), ArgsToArray(AArgs), ArgsToArray([AContextID])]); lCall := AddCall('GetInstance').WithParams(lArgs); Pointer(AInstance) := FindVarData(lCall.OutParams[0]).VPointer; Result := lCall.ReturnValue; end; </code></pre> <p>As you can see the heart of my solution was to construct my own <code>array of TVarRec</code> (aka <code>TOpenArray</code>) which I could then pass to the <code>WithParams</code>-method. I wrote a couple of utility routines that allowed me to merge the explicit arguments with the open array arguments into a single new array. </p> <p>Here is the implementation of <code>ConcatArrays</code>:</p> <pre><code>type TOpenArray = array of TVarRec; function ConcatArrays(const AArrays: array of TOpenArray): TOpenArray; var lLength: Integer; lArray: TOpenArray; lIdx: Integer; lElem: TVarRec; begin lLength := 0; for lArray in AArrays do Inc(lLength, Length(lArray)); SetLength(Result, lLength); lIdx := -1; for lArray in AArrays do for lElem in lArray do begin Inc(lIdx); Result[lIdx] := lElem; end; end; </code></pre> <p>I do have a strong suspicion that these routines could probably be massively optimized by someone with a deeper understanding of how Delphi handles dynamic and open arrays internally.</p> <p>Anyway, with this solution in place at the test site I now have to ignore the fact that there even is an open array parameter in the mocked method. I simply specify the expectation as such:</p> <pre><code>FMock.Expects('GetInstance').WithParams([@IMyIntf, 1, 2, 3, lContextID]).ReturnsOutParam(lDummy).Returns(True); </code></pre> <p>... where the <code>1, 2, 3</code>-bit is really the expected open array argument.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload