Giter Site home page Giter Site logo

mockingbird's Introduction

This Package is not finished

Who's mocking who now?

This is yet another mocking library. Unlike other mocking libraries, this one allows you to keep your module structure as it is! Just add the {.mockable.} pragma to your exported procs, compile with the --define:mock flag and you're good to go!

The pragma turns any proc annotated with it into a variable that can be assigned to. You can then "mock" procs by simply assigning a different procedure to that variable.

Note: This currently does not work with generics! I'm looking into how to get there but it's hard

Example

# get5Module.nim
proc get5*(): int {.mockable.} = 5 # Can be replaced
# add5Module.nim
import ./get5Module

proc add5*(x: int): int = x + get5()
import ./add5Module
import ./get5Module
import std/[sugar, unittest]


suite "Test add5":  
  # Important for the teardown in order to "reset" the mocks
  let get5Original = get5
  
  teardown:
    get5 = get5Original # "Resets" the mock to provide the normal behaviour again

  test """
    Given get5 returns 5
    When calling add5 with 10
    Then it should return 15
  """:
    check add5(10) == 15

  test """
    Given get5 returns 10
    When calling add5 with 10
    Then it should return 20
  """:
    get5 = () => 10
    check add5(10) == 20

  test """
    Given get5 not being mocked and so it should returns 5
    When calling add5 with 10
    Then it should return 15 again
  """:
    check add5(10) == 15

mockingbird's People

Contributors

philippmdoerner avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

mockingbird's Issues

Make {.mockable.} useable with generics

This currently breaks:

import std/[genasts, macros]

macro mockable*(p: typed): untyped =
  when defined(mock):
    let
      pName = ident($p[0])
      newName = ident($p[0] & "Base")
      oldPrc = p.copyNimTree

    oldPrc[0] = newName
    result = genast(pName, oldPrc, newName):
      oldPrc
      var pName* = newName
  else:
    result = p


proc toString*[T](x: T): string {.mockable.} = $x
/home/philipp/dev/playground/src/playground.nim(18, 35) template/generic instantiation of `mockable` from here
/usr/lib/nim/std/genasts.nim(87, 13) Error: 'toStringBase' doesn't have a concrete type, due to unspecified generic parameters.

Add `testImport` and `mockImport`

The idea is that your source code no longer has to be changed. You don't test your source-code, you test a modified copy that allows mocking.

How so?

All modules imported via mockImport get read in, modified as they would be in mocker.nim (so that the exported procs get the mockable pragma attached) and that modified copy gets written to "testDirectory".
You then import that modified copy in the test.

All modules imported via testImport get read in, modified in the sense that their imports no longer point to the source code, but instead to the modified copies and that modified testImport-ed module gets also written to "testDirectory".
You then import that modified copy in the test.

Tests get written for the procs of the testImport-ed module. This means you are no longer actually testing source-code, but near identical code, that should be similar enough.
If that isn't good enough for the user though, they can instead just use mockable themselves in their source code and work with that.

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.