## Module claire-mocha ################################################# # # A bridge for using Claire in Mocha. # # # Copyright (c) 2013 Quildreen "Sorella" Motta # # Permission is hereby granted, free of charge, to any person # obtaining a copy of this software and associated documentation files # (the "Software"), to deal in the Software without restriction, # including without limitation the rights to use, copy, modify, merge, # publish, distribute, sublicense, and/or sell copies of the Software, # and to permit persons to whom the Software is furnished to do so, # subject to the following conditions: # # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ### -- Dependencies ---------------------------------------------------- { Property } = require 'claire/lib/property' { check } = require 'claire/lib/check' ### -- Aliases --------------------------------------------------------- keys = Object.keys round = Math.round test = global.it # steals from Mocha ### -- Default configuration ------------------------------------------- _default = times : 100 verbose : false ### -- Error handling -------------------------------------------------- #### λ make-error # :internal: # Constructs an error from a name and message # # :: String -> String -> Error make-error = (name, message) --> (Error.call {}, message) <<< { name: name } #### λ failed # :internal: # Constructs a Failure error for a property. # # :: Report -> Error failed = (report) -> make-error 'property-failed', "#report" #### λ abandoned # :internal: # Constructs an Abandoned error for a property. # # :: Report -> Error abandoned = (report) -> make-error 'property-abandoned', "#report" #### λ percentage # :internal: # Computes the percentage of N/Total # # :: Number, Number -> Number percentage = (n, total) -> round ((n / total) * 100) #### λ display # :internal: # Describes the report in the log if it's important to do so. # # :: Bool -> Report -> IO () display = (verbose, report) --> text = (("#report".split /\n/).map (s) -> " #s").join '\n' total = report.all.length ignored = report.ignored.length ignored-pct = percentage ignored, total has-labels = !!(keys report.labels).length if (verbose) or (ignored-pct > 50) or has-labels => console?.log text ### -- Core implementation --------------------------------------------- #### λ fact # Sugar for constructing a new `Property` that can be used in Mocha. # # :: Config -> Property -> () -> IO () fact = (c, p) -->-> do report = check (c.times or 100), p switch report.verdict | \passed => display c.verbose, report | \failed => throw failed report | \abandoned => throw abandoned report #### λ o # A simple sugar to replace `it`. # # :: String -> (() -> Property) -> () o = (label, p) --> do test label, (fact (global.claire-config or _default), p!) ### -- Exports --------------------------------------------------------- module.exports = { fact, o }