Skip to content

Instantly share code, notes, and snippets.

@rehno-lindeque
Last active December 18, 2015 20:29
Show Gist options
  • Save rehno-lindeque/5840412 to your computer and use it in GitHub Desktop.
Save rehno-lindeque/5840412 to your computer and use it in GitHub Desktop.
module.exports = _ = require "lodash"
# Define the function you want to test
# (This is a lodash extension: Structured map over an object using a corresponding dictionary of functions)
_.mixin mapStruct: (obj, fns, defaultFn) ->
defaultFn = defaultFn ? if _.isFunction fns then fns else _.identity
_.zipObject (for k,v of obj then [k, (fns?[k] ? defaultFn) v, k])
@rehno-lindeque
Copy link
Author

In test-example.coffee.unit:

# Instrument each function in the `test-example` module with test logging code
t = instrument require "./test-example"

# To test a function... simply call it
t.mapStruct { mulOne: 3, mulTwo: 3, mulFive: 3 },
   mulTwo:  (x) -> x * 2
   mulFive: (x) -> x * 5

The result gets written into test-example.spec.unit file:

mapStruct {"mulOne":3,"mulTwo":3,"mulFive":3} {}
=> {"mulOne":3,"mulTwo":6,"mulFive":15}

The testing framework does git diff test-example.spec.unit and reports an error if there is any differences. To update a test specification, simply git commit test-example.spec.unit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment