## Preface

In other languages, testing libraries exist which allow you to "mock" a 
function (see <https://jestjs.io/docs/mock-functions>).

Mocking a function means swapping out a function your program would normally 
call, with another function. E.g. it would find all invocations of `foo()` and 
swap it out with a test function you've written `fooMock()`

This is very powerful for testing purposes, and is why I am trying to replicate 
this behavior.

Elegantbeef and I have been talking about this in chat for a while, and have 
been testing solutions such as using (or abusing) TRMs with little success.

## Examples of what I'd like to have

### `strgen.nim`
    
    
    import std/[random]
    
    randomize()
    
    proc genString*(length: int): string =
      for i in 0 ..< length:
        result &= rand('a'..'z')
    
    
    Run

### `strgen_test.nim`
    
    
    import strgen
    
    mock(strgen.genString, proc(length: int): string = "foobar")
    assert strgen(12).len == 6
    
    mock(strgen.genString, proc(length: int): string = "foobarbaz")
    assert strgen(12).len == 9
    
    
    Run

You'll notice that `strgen` would normally output strings with a length of `12` 
here, but that changes because of the mocks. These are also able to be declared 
and changed at runtime.

I'm hoping some members of the community could help brainstorm a way for this 
to be achieved. Some of the solutions we've thought of so far have greatly 
restricted the desired flexibility of "mocking", or would require compiler 
changes.

Reply via email to