├── test ├── source-map-support.js ├── mocha.opts ├── basic.test.js └── generators.test.js ├── cljs ├── notice.txt └── checkers │ └── itself.cljs ├── .npmignore ├── scripts ├── repl └── repl.clj ├── .gitignore ├── CHANGELOG.md ├── .travis.yml ├── .eslintrc ├── mocha.js ├── package.json ├── project.clj ├── README.md └── LICENSE /test/source-map-support.js: -------------------------------------------------------------------------------- 1 | require('source-map-support').install(); 2 | -------------------------------------------------------------------------------- /cljs/notice.txt: -------------------------------------------------------------------------------- 1 | // This file was generated by the ClojureScript compiler. 2 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | out/ 3 | checkers.js.map 4 | .repl/ 5 | target/ 6 | -------------------------------------------------------------------------------- /test/mocha.opts: -------------------------------------------------------------------------------- 1 | --require test/source-map-support 2 | --slow 1s 3 | --timeout 5s 4 | -------------------------------------------------------------------------------- /scripts/repl: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | rlwrap lein trampoline run -m clojure.main scripts/repl.clj 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | out/ 3 | checkers.js 4 | checkers.js.map 5 | .repl/ 6 | target/ 7 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # 0.10.0 2 | 3 | * Helper for mocha tests 4 | 5 | # 0.9.1 6 | 7 | * Don't swallow exceptions in property checks 8 | * More tests 9 | 10 | # 0.9.0 11 | 12 | * Initial release 13 | -------------------------------------------------------------------------------- /scripts/repl.clj: -------------------------------------------------------------------------------- 1 | (require 2 | '[cljs.repl :as repl] 3 | '[cljs.repl.node :as node]) 4 | 5 | (repl/repl* (node/repl-env) 6 | {:output-dir "out/repl" 7 | :optimizations :none 8 | :cache-analysis true 9 | :source-map true}) 10 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - 4 4 | - node 5 | sudo: false 6 | before_install: 7 | - wget https://raw.githubusercontent.com/technomancy/leiningen/stable/bin/lein -O scripts/lein 8 | - chmod +x scripts/lein 9 | - export PATH=`pwd`/scripts:$PATH 10 | - npm run clean 11 | - npm run build-dev 12 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "node": true 4 | }, 5 | "rules": { 6 | "strict": true, 7 | "quotes": false, 8 | "no-use-before-define": "func", 9 | "no-unused-vars": [2, "all"], 10 | "no-mixed-requires": [1, true], 11 | "max-depth": [1, 5], 12 | "max-len": [1, 80, 4], 13 | "eqeqeq": false, 14 | "no-path-concat": false, 15 | "no-else-return": true, 16 | "no-eq-null": true, 17 | "no-lonely-if": true 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /mocha.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Mocha interop, assumes "it" is a globally defined function. 3 | */ 4 | /*eslint-env mocha */ 5 | 6 | var checkers = require("./"); 7 | 8 | exports = module.exports = checking; 9 | exports.gen = checkers.gen; 10 | 11 | /** 12 | * Generate a mocha example 13 | * @param {string} desc The example description 14 | * @param {array} args List of generators to pass to body 15 | * @param {function} body Function to check, should return true or false 16 | * @param {number} n The number of iterations to check (optional) 17 | * @param {object} options Additional options for check (optional) 18 | */ 19 | function checking(desc, args, body, n, options) { 20 | if (typeof n === 'undefined') { 21 | n = 1000; 22 | options = {}; 23 | } 24 | if (typeof options === 'undefined' && typeof n !== 'number') { 25 | options = n; 26 | n = 1000; 27 | } 28 | it(desc, function() { 29 | checkers.forAll(args, body).check(n, options); 30 | }); 31 | } 32 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "checkers", 3 | "version": "0.13.1", 4 | "description": "Property-based testing for JavaScript via ClojureScript's test.check", 5 | "main": "checkers.js", 6 | "scripts": { 7 | "clean": "lein clean", 8 | "build": "lein cljsbuild once release", 9 | "build-dev": "lein cljsbuild once dev", 10 | "dev": "lein cljsbuild auto dev", 11 | "test": "mocha", 12 | "repl": "./scripts/repl", 13 | "prepublish": "npm run clean && npm run build && npm test" 14 | }, 15 | "repository": { 16 | "type": "git", 17 | "url": "https://github.com/glenjamin/checkers.git" 18 | }, 19 | "keywords": [ 20 | "test", 21 | "testing", 22 | "property-based", 23 | "property", 24 | "quickcheck", 25 | "stochastic", 26 | "fuzz", 27 | "fuzzer", 28 | "proper", 29 | "triq", 30 | "stoch", 31 | "afl", 32 | "checkers" 33 | ], 34 | "author": "Glen Mailer ", 35 | "license": "EPL", 36 | "bugs": { 37 | "url": "https://github.com/glenjamin/checkers/issues" 38 | }, 39 | "homepage": "https://github.com/glenjamin/checkers", 40 | "devDependencies": { 41 | "lodash": "^2.4.1", 42 | "mocha": "^2.1.0", 43 | "source-map-support": "^0.2.9" 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /project.clj: -------------------------------------------------------------------------------- 1 | (defproject checkers "0.9.0-SNAPSHOT" 2 | :description "Property-based testing for JavaScript via ClojureScript's test.check" 3 | :url "https://github.com/glenjamin/checkers" 4 | 5 | :dependencies [[org.clojure/clojure "1.8.0"] 6 | [org.clojure/clojurescript "1.8.40"] 7 | [org.clojure/test.check "0.9.0"]] 8 | 9 | :plugins [[lein-cljsbuild "1.1.3"]] 10 | 11 | :source-paths ["cljs"] 12 | 13 | :clean-targets ["out" "checkers.js" "checkers.js.map"] 14 | 15 | :cljsbuild { 16 | :builds [{:id "dev" 17 | :source-paths ["cljs"] 18 | :compiler {:output-to "checkers.js" 19 | :output-dir "out/dev" 20 | :preamble ["notice.txt"] 21 | :optimizations :simple 22 | :pretty-print true 23 | :cache-analysis true 24 | :source-map "checkers.js.map" 25 | :language-in :ecmascript5 26 | :language-out :ecmascript5}} 27 | {:id "release" 28 | :source-paths ["cljs"] 29 | :compiler {:output-to "checkers.js" 30 | :output-dir "out/release" 31 | :preamble ["notice.txt"] 32 | :optimizations :advanced 33 | :pretty-print true 34 | :cache-analysis true 35 | :language-in :ecmascript5 36 | :language-out :ecmascript5}}]}) 37 | -------------------------------------------------------------------------------- /test/basic.test.js: -------------------------------------------------------------------------------- 1 | /*eslint-env mocha */ 2 | var _ = require('lodash'); 3 | var assert = require('assert'); 4 | 5 | var checkers = require('..'); 6 | var gen = checkers.gen; 7 | 8 | describe("checkers", function() { 9 | it("checks that squaring makes things bigger or the same", function() { 10 | checkers.forAll( 11 | [gen.int], 12 | function(i) { 13 | return i * i >= i; 14 | } 15 | ).check(1000); 16 | }); 17 | it("checks Array.sort is the same as _.sortBy on strings", function() { 18 | checkers.forAll( 19 | [gen.array(gen.int)], 20 | function(arr) { 21 | var lodash = _.sortBy(arr, function(x) { return '' + x; }); 22 | var stdlib = arr.slice(); 23 | stdlib.sort(); 24 | return _.isEqual(stdlib, lodash); 25 | } 26 | ).check(100); 27 | }); 28 | it("returns summary of run", function() { 29 | var summary = checkers.forAll( 30 | [gen.int], 31 | function(n) { 32 | return (n * n) >= n; 33 | } 34 | ).check(100); 35 | assert.equal(summary["num-tests"], 100); 36 | }); 37 | it("errors if property is not satisfied", function() { 38 | try { 39 | checkers.forAll([gen.int], function(i) { 40 | return _.isString(i); 41 | }).check(1); 42 | throw new Error("Shouldn't throw"); 43 | } catch(ex) { 44 | assert.notEqual(ex.message, "Shouldn't throw"); 45 | } 46 | }); 47 | it("treats null as a failure", function() { 48 | try { 49 | checkers.forAll([gen.int], function() { 50 | return null; 51 | }).check(1); 52 | throw new Error("Shouldn't throw"); 53 | } catch(ex) { 54 | assert.notEqual(ex.message, "Shouldn't throw"); 55 | } 56 | }); 57 | it("treats undefined as a failure", function() { 58 | try { 59 | checkers.forAll([gen.int], function() { 60 | return undefined; 61 | }).check(1); 62 | throw new Error("Shouldn't throw"); 63 | } catch(ex) { 64 | assert.notEqual(ex.message, "Shouldn't throw"); 65 | } 66 | }); 67 | it("errors if property checker throws", function() { 68 | try { 69 | checkers.forAll([gen.int], function() { 70 | throw new Error("whoops"); 71 | }).check(1); 72 | throw new Error("Shouldn't throw"); 73 | } catch(ex) { 74 | assert.ok( 75 | /whoops/.test(ex.message), 76 | "expected /whoops/ to match " + ex.message 77 | ); 78 | } 79 | }); 80 | }); 81 | -------------------------------------------------------------------------------- /cljs/checkers/itself.cljs: -------------------------------------------------------------------------------- 1 | (ns checkers.itself 2 | (:require [clojure.test.check :as tc] 3 | [clojure.test.check.properties :as prop] 4 | [clojure.test.check.generators :as gen] 5 | [goog.object])) 6 | 7 | ;; Interop utils 8 | (def ^:private format (aget (js/require "util") "format")) 9 | 10 | (defn- obj-seq 11 | "Seq from enumerable keys of a JS Object" 12 | [obj] 13 | (for [k (goog.object/getKeys obj)] 14 | [k (aget obj k)])) 15 | 16 | (defn- arrayify 17 | "Force a generator's output to be a JS array" 18 | [generator] 19 | (fn [& args] (gen/fmap into-array (apply generator args)))) 20 | 21 | (defn- obj-assoc [obj k v] (doto obj (aset (clj->js k) v))) 22 | (defn- into-object [associative] (reduce-kv obj-assoc #js {} associative)) 23 | (defn- objectify 24 | "Force a generator's output to be a JS object" 25 | [generator] 26 | (fn [& args] (gen/fmap into-object (apply generator args)))) 27 | 28 | ;; Check API 29 | 30 | (defn- format-args [arglist] 31 | (.join (into-array (map #(format "%j" %) arglist)) ",")) 32 | 33 | (defn- generate-message 34 | [{:keys [num-tests fail seed] 35 | {:keys [smallest]} :shrunk}] 36 | (format "Failed after %d test(s)\nInput: %s\nShrunk to: %s\nSeed: %s" 37 | num-tests (format-args fail) (format-args smallest) seed)) 38 | 39 | (defn check 40 | "Wrap up quick-check to take options as a JS object and throw on failure" 41 | [property n & [opts]] 42 | (let [opts (apply concat (js->clj opts :keywordize-keys true)) 43 | {:keys [result] :as r} (apply tc/quick-check n property opts) 44 | summary (clj->js r) 45 | failure (fn [ex] 46 | (aset ex "checkers-result" summary) 47 | (throw ex))] 48 | (cond 49 | (instance? js/Error result) (let [ex result] 50 | (aset ex "message" 51 | (str (.-message ex) "\n" 52 | (generate-message r))) 53 | (failure ex)) 54 | (not result) (let [ex (js/Error. (generate-message r))] 55 | (failure ex)) 56 | :else summary))) 57 | 58 | (aset js/exports "forAll" 59 | (fn [& args] 60 | (let [p (apply prop/for-all* args)] 61 | (aset p "check" #(check p %1 %2)) 62 | p))) 63 | 64 | (aset js/exports "sample" (comp into-array gen/sample)) 65 | 66 | ;; Generator API 67 | 68 | (aset js/exports "gen" #js {}) 69 | 70 | 71 | (aset js/exports "gen" "fmap" gen/fmap) 72 | (aset js/exports "gen" "return" gen/return) 73 | (aset js/exports "gen" "bind" gen/bind) 74 | 75 | ; Combinators & Helpers 76 | (aset js/exports "gen" "resize" gen/resize) 77 | (aset js/exports "gen" "choose" gen/choose) 78 | (aset js/exports "gen" "oneOf" gen/one-of) 79 | (aset js/exports "gen" "frequency" gen/frequency) 80 | (aset js/exports "gen" "pick" gen/elements) 81 | (aset js/exports "gen" "suchThat" gen/such-that) 82 | (aset js/exports "gen" "notEmpty" gen/not-empty) 83 | (aset js/exports "gen" "noShrink" gen/no-shrink) 84 | (aset js/exports "gen" "shrink2" gen/shrink-2) 85 | 86 | ; Data Types 87 | (aset js/exports "gen" "boolean" gen/boolean) 88 | (aset js/exports "gen" "tuple" (arrayify gen/tuple)) 89 | 90 | (aset js/exports "gen" "int" gen/int) 91 | (aset js/exports "gen" "nat" gen/nat) 92 | (aset js/exports "gen" "posInt" gen/s-pos-int) 93 | (aset js/exports "gen" "negInt" gen/s-neg-int) 94 | (aset js/exports "gen" "zeroOrNegInt" gen/neg-int) 95 | 96 | (aset js/exports "gen" "array" (arrayify gen/vector)) 97 | (aset js/exports "gen" "shuffle" (arrayify gen/shuffle)) 98 | 99 | (aset js/exports "gen" "obj" (objectify gen/map)) 100 | (def ^:private gen-hash-map (objectify gen/hash-map)) 101 | (aset js/exports "gen" "object" 102 | (fn [obj] (apply gen-hash-map (apply concat (obj-seq obj))))) 103 | 104 | (aset js/exports "gen" "char" gen/char) 105 | (aset js/exports "gen" "charAscii" gen/char-ascii) 106 | (aset js/exports "gen" "charAlphanum" gen/char-alphanumeric) 107 | (aset js/exports "gen" "charAlpha" gen/char-alpha) 108 | 109 | (aset js/exports "gen" "string" gen/string) 110 | (aset js/exports "gen" "stringAscii" gen/string-ascii) 111 | (aset js/exports "gen" "stringAlphanum" gen/string-alphanumeric) 112 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # checkers 2 | 3 | [![npm version](https://img.shields.io/npm/v/checkers.svg)](https://www.npmjs.com/package/checkers) [![Build Status](https://img.shields.io/travis/glenjamin/checkers/master.svg)](https://travis-ci.org/glenjamin/checkers) 4 | 5 | 6 | 7 | Property-based testing for JavaScript via ClojureScript's [test.check](https://github.com/clojure/test.check). 8 | 9 | test.check is a Clojure property-based testing tool inspired by [QuickCheck](http://www.quviq.com/products/erlang-quickcheck/). The core idea of test.check is that instead of enumerating expected input and output for unit tests, you write properties about your function that should hold true for all inputs. This lets you write concise, powerful tests. 10 | 11 | Checkers brings the power of test.check to plain ol' JavaScript. 12 | 13 | # Install 14 | 15 | npm install checkers --save 16 | 17 | # Usage 18 | 19 | ```js 20 | var checkers = require('checkers'); 21 | var gen = checkers.gen; 22 | 23 | // Property is incorrect 24 | checkers.forAll( 25 | [gen.int], 26 | function(i) { 27 | return i * i > i; 28 | } 29 | ).check(1000); 30 | 31 | // Property is now correct 32 | checkers.forAll( 33 | [gen.int], 34 | function(i) { 35 | return i * i >= i; 36 | } 37 | ).check(1000); 38 | 39 | // Check property with a particular seed 40 | checkers.forAll( 41 | [gen.int], 42 | function(i) { 43 | return i * i >= i; 44 | } 45 | ).check(1000, {seed: 1422111938215}); 46 | ``` 47 | 48 | # Usage with Mocha 49 | 50 | Checkers comes with a helper function to make writing tests for mocha simpler. 51 | 52 | ```js 53 | var checking = require('checkers/mocha'); 54 | var gen = checking.gen; 55 | describe("Addition", function() { 56 | checking("+1", [gen.int], function(i) { 57 | return i + 1 > i; 58 | }, 100, {seed: 1422111938215}); 59 | }); 60 | ``` 61 | 62 | The count is optional, and defaults to 1000. 63 | The extra options are also optional. 64 | 65 | ## Full Documentation 66 | 67 | More coming soon! 68 | 69 | For now you'll have to rely on the examples in the `test` folder. 70 | 71 | ## Building your own generators 72 | 73 | The simplest way to build your own generators is with `gen.fmap`. This lets you apply a function to generated values to produce new ones. 74 | 75 | Here's an example of generating instances of a 3-D `Point` object. 76 | 77 | ```js 78 | var genPoint = gen.fmap( 79 | function(p) { return new Point(p.x, p.y, p.z) }, 80 | gen.object({ x: gen.int, y: gen.int, z: gen.int }) 81 | ); 82 | ``` 83 | 84 | Testing the generator in a Node REPL is also simple. 85 | 86 | ``` 87 | $ # assumes you've already done npm install --save-dev checkers 88 | $ node 89 | > 90 | > var checkers = require('checkers'); 91 | undefined 92 | > 93 | > var gen = checkers.gen; 94 | undefined 95 | > 96 | > var newlog = (x,y,z) => console.log(`new point ${x} ${y} ${z}`); 97 | undefined 98 | > 99 | > var Point = function(x,y,z) { newlog(x,y,z); this.coord=[x,y,z]; return this; } 100 | undefined 101 | > 102 | > var genPoint = gen.fmap( p => new Point(p.x, p.y, p.z) , gen.object({ x: gen.int, y: gen.int, z: gen.int }) ); 103 | undefined 104 | > 105 | > checkers.sample(genPoint); 106 | new point 0 0 0 107 | new point -1 -1 -1 108 | new point -1 -1 2 109 | new point 3 -2 -2 110 | new point 1 -2 2 111 | new point 0 -4 0 112 | new point -4 1 -3 113 | new point -4 -1 -6 114 | new point 3 -2 -1 115 | new point 4 2 -6 116 | [ { coord: [ 0, 0, 0 ] }, 117 | { coord: [ -1, -1, -1 ] }, 118 | { coord: [ -1, -1, 2 ] }, 119 | { coord: [ 3, -2, -2 ] }, 120 | { coord: [ 1, -2, 2 ] }, 121 | { coord: [ 0, -4, 0 ] }, 122 | { coord: [ -4, 1, -3 ] }, 123 | { coord: [ -4, -1, -6 ] }, 124 | { coord: [ 3, -2, -1 ] }, 125 | { coord: [ 4, 2, -6 ] } ] 126 | ``` 127 | 128 | ## TODO 129 | 130 | * Generator tests 131 | * Generator docs 132 | * Sugar for other testing frameworks? 133 | * Tutorial 134 | * Better examples 135 | 136 | ## Development 137 | 138 | See `npm run` or `package.json` for a list of available scripts. 139 | 140 | You will need [leiningen](http://leiningen.org/) in order to build locally. 141 | 142 | ## License 143 | 144 | Distributed under the Eclipse Public License. 145 | 146 | checkers is Copyright © 2015 Glen Mailer and contributors. 147 | 148 | [test.check](https://github.com/clojure/test.check/) is Copyright 149 | Rich Hickey, Reid Draper and contributors. 150 | 151 | -------------------------------------------------------------------------------- /test/generators.test.js: -------------------------------------------------------------------------------- 1 | /*eslint-env mocha */ 2 | var _ = require('lodash'); 3 | 4 | var checkers = require('..'); 5 | var gen = checkers.gen; 6 | 7 | var checking = require('../mocha'); 8 | 9 | describe("checkers.gen", function() { 10 | checking(".boolean", [gen.boolean], _.isBoolean, 10); 11 | checking(".int", [gen.int], function(n) { 12 | return _.isNumber(n) && wholeNumber(n); 13 | }, 100); 14 | checking(".nat", [gen.nat], function(n) { 15 | return _.isNumber(n) && wholeNumber(n) && n >= 0; 16 | }, 100); 17 | checking(".posInt", [gen.posInt], function(n) { 18 | return _.isNumber(n) && wholeNumber(n) && n > 0; 19 | }, 100); 20 | checking(".negInt", [gen.negInt], function(n) { 21 | return _.isNumber(n) && wholeNumber(n) && n < 0; 22 | }, 100); 23 | checking(".zeroOrNegInt", [gen.zeroOrNegInt], function(n) { 24 | return _.isNumber(n) && wholeNumber(n) && n <= 0; 25 | }, 100); 26 | checking(".char", [gen.char], function(c) { 27 | return _.isString(c) && c.length === 1 && 28 | c.charCodeAt(0) < 256 && c.charCodeAt(0) >= 0; 29 | }, 1000); 30 | checking(".charAscii", [gen.charAscii], function(c) { 31 | return _.isString(c) && c.length === 1 && 32 | c.charCodeAt(0) <= 127 && c.charCodeAt(0) >= 0; 33 | }, 100); 34 | checking(".charAlphanum", [gen.charAlphanum], function(c) { 35 | return _.isString(c) && /^\w$/.test(c); 36 | }, 100); 37 | checking(".charAlpha", [gen.charAlpha], function(c) { 38 | return _.isString(c) && /^\w$/.test(c) && !/^\d$/.test(c); 39 | }, 100); 40 | checking(".string", [gen.string], function(c) { 41 | return _.isString(c); 42 | }, 100); 43 | checking(".stringAscii", [gen.stringAscii], function(s) { 44 | return _.isString(s) && _.every(s, function(c) { 45 | return c.charCodeAt(0) <= 127 && c.charCodeAt(0) >= 0; 46 | }); 47 | }, 100); 48 | checking(".stringAlphanum", [gen.stringAlphanum], function(s) { 49 | return _.isString(s) && _.every(s, function(c) { 50 | return /^\w$/.test(c); 51 | }); 52 | }, 100); 53 | describe(".tuple", function() { 54 | checking("pairs of ints", [gen.tuple(gen.int, gen.int)], function(t) { 55 | return _.isArray(t) && t.length == 2 && 56 | _.isNumber(t[0]) && _.isNumber(t[1]); 57 | }, 1000); 58 | checking("pairs of int, char", 59 | [gen.tuple(gen.int, gen.char)], 60 | function(t) { 61 | return _.isArray(t) && t.length == 2 && 62 | _.isNumber(t[0]) && _.isString(t[1]); 63 | }, 64 | 1000 65 | ); 66 | checking("can generate triples", 67 | [gen.tuple(gen.int, gen.int, gen.int)], 68 | function(t) { 69 | return _.isArray(t) && t.length == 3; 70 | }, 71 | 1000 72 | ); 73 | }); 74 | describe(".array", function() { 75 | checking("array of ints", [gen.array(gen.int)], function(arr) { 76 | return _.isArray(arr) && _.every(arr, _.isNumber); 77 | }); 78 | checking("array of strings", [gen.array(gen.string)], function(arr) { 79 | return _.isArray(arr) && _.every(arr, _.isString); 80 | }, 100); 81 | checking("fixed length array", [gen.array(gen.int, 7)], function(arr) { 82 | return _.isArray(arr) && _.every(arr, _.isNumber) && 83 | arr.length === 7; 84 | }); 85 | checking("bounded length array", 86 | [gen.array(gen.int, 3, 17)], 87 | function(arr) { 88 | return _.isArray(arr) && _.every(arr, _.isNumber) && 89 | arr.length >= 3 && arr.length <= 17; 90 | } 91 | ); 92 | }); 93 | describe(".obj", function() { 94 | checking("int -> int", [gen.obj(gen.int, gen.int)], function(o) { 95 | return _.isObject(o) && _.every(o, function(v, k) { 96 | return _.isNumber(v) && _.isString(k) && /^-?\d+$/.test(k); 97 | }); 98 | }, 100); 99 | checking("charAlpha -> int", 100 | [gen.obj(gen.charAlpha, gen.int)], 101 | function(o) { 102 | return _.isObject(o) && _.every(o, function(v, k) { 103 | return _.isString(k) && _.isNumber(v); 104 | }); 105 | }, 106 | 100 107 | ); 108 | }); 109 | checking(".object", 110 | [gen.object({a: gen.int, b: gen.charAlpha, c: gen.nat})], 111 | function(o) { 112 | return _.isEqual(['a', 'b', 'c'], _.keys(o)) && 113 | _.isNumber(o.a) && /[a-z]/i.test(o.b) && 114 | _.isNumber(o.c) && o.c >= 0; 115 | } 116 | ); 117 | checking(".pick", 118 | [gen.pick([1, 'a', false])], 119 | function(x) { 120 | return x == 1 || x == 'a' || x === false; 121 | }, 122 | 10 123 | ); 124 | }); 125 | 126 | function wholeNumber(n) { 127 | return Math.round(n) === n; 128 | } 129 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Eclipse Public License - v 1.0 2 | 3 | THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC 4 | LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM 5 | CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT. 6 | 7 | 1. DEFINITIONS 8 | 9 | "Contribution" means: 10 | 11 | a) in the case of the initial Contributor, the initial code and documentation 12 | distributed under this Agreement, and 13 | b) in the case of each subsequent Contributor: 14 | i) changes to the Program, and 15 | ii) additions to the Program; 16 | 17 | where such changes and/or additions to the Program originate from and are 18 | distributed by that particular Contributor. A Contribution 'originates' 19 | from a Contributor if it was added to the Program by such Contributor 20 | itself or anyone acting on such Contributor's behalf. Contributions do not 21 | include additions to the Program which: (i) are separate modules of 22 | software distributed in conjunction with the Program under their own 23 | license agreement, and (ii) are not derivative works of the Program. 24 | 25 | "Contributor" means any person or entity that distributes the Program. 26 | 27 | "Licensed Patents" mean patent claims licensable by a Contributor which are 28 | necessarily infringed by the use or sale of its Contribution alone or when 29 | combined with the Program. 30 | 31 | "Program" means the Contributions distributed in accordance with this 32 | Agreement. 33 | 34 | "Recipient" means anyone who receives the Program under this Agreement, 35 | including all Contributors. 36 | 37 | 2. GRANT OF RIGHTS 38 | a) Subject to the terms of this Agreement, each Contributor hereby grants 39 | Recipient a non-exclusive, worldwide, royalty-free copyright license to 40 | reproduce, prepare derivative works of, publicly display, publicly 41 | perform, distribute and sublicense the Contribution of such Contributor, 42 | if any, and such derivative works, in source code and object code form. 43 | b) Subject to the terms of this Agreement, each Contributor hereby grants 44 | Recipient a non-exclusive, worldwide, royalty-free patent license under 45 | Licensed Patents to make, use, sell, offer to sell, import and otherwise 46 | transfer the Contribution of such Contributor, if any, in source code and 47 | object code form. This patent license shall apply to the combination of 48 | the Contribution and the Program if, at the time the Contribution is 49 | added by the Contributor, such addition of the Contribution causes such 50 | combination to be covered by the Licensed Patents. The patent license 51 | shall not apply to any other combinations which include the Contribution. 52 | No hardware per se is licensed hereunder. 53 | c) Recipient understands that although each Contributor grants the licenses 54 | to its Contributions set forth herein, no assurances are provided by any 55 | Contributor that the Program does not infringe the patent or other 56 | intellectual property rights of any other entity. Each Contributor 57 | disclaims any liability to Recipient for claims brought by any other 58 | entity based on infringement of intellectual property rights or 59 | otherwise. As a condition to exercising the rights and licenses granted 60 | hereunder, each Recipient hereby assumes sole responsibility to secure 61 | any other intellectual property rights needed, if any. For example, if a 62 | third party patent license is required to allow Recipient to distribute 63 | the Program, it is Recipient's responsibility to acquire that license 64 | before distributing the Program. 65 | d) Each Contributor represents that to its knowledge it has sufficient 66 | copyright rights in its Contribution, if any, to grant the copyright 67 | license set forth in this Agreement. 68 | 69 | 3. REQUIREMENTS 70 | 71 | A Contributor may choose to distribute the Program in object code form under 72 | its own license agreement, provided that: 73 | 74 | a) it complies with the terms and conditions of this Agreement; and 75 | b) its license agreement: 76 | i) effectively disclaims on behalf of all Contributors all warranties 77 | and conditions, express and implied, including warranties or 78 | conditions of title and non-infringement, and implied warranties or 79 | conditions of merchantability and fitness for a particular purpose; 80 | ii) effectively excludes on behalf of all Contributors all liability for 81 | damages, including direct, indirect, special, incidental and 82 | consequential damages, such as lost profits; 83 | iii) states that any provisions which differ from this Agreement are 84 | offered by that Contributor alone and not by any other party; and 85 | iv) states that source code for the Program is available from such 86 | Contributor, and informs licensees how to obtain it in a reasonable 87 | manner on or through a medium customarily used for software exchange. 88 | 89 | When the Program is made available in source code form: 90 | 91 | a) it must be made available under this Agreement; and 92 | b) a copy of this Agreement must be included with each copy of the Program. 93 | Contributors may not remove or alter any copyright notices contained 94 | within the Program. 95 | 96 | Each Contributor must identify itself as the originator of its Contribution, 97 | if 98 | any, in a manner that reasonably allows subsequent Recipients to identify the 99 | originator of the Contribution. 100 | 101 | 4. COMMERCIAL DISTRIBUTION 102 | 103 | Commercial distributors of software may accept certain responsibilities with 104 | respect to end users, business partners and the like. While this license is 105 | intended to facilitate the commercial use of the Program, the Contributor who 106 | includes the Program in a commercial product offering should do so in a manner 107 | which does not create potential liability for other Contributors. Therefore, 108 | if a Contributor includes the Program in a commercial product offering, such 109 | Contributor ("Commercial Contributor") hereby agrees to defend and indemnify 110 | every other Contributor ("Indemnified Contributor") against any losses, 111 | damages and costs (collectively "Losses") arising from claims, lawsuits and 112 | other legal actions brought by a third party against the Indemnified 113 | Contributor to the extent caused by the acts or omissions of such Commercial 114 | Contributor in connection with its distribution of the Program in a commercial 115 | product offering. The obligations in this section do not apply to any claims 116 | or Losses relating to any actual or alleged intellectual property 117 | infringement. In order to qualify, an Indemnified Contributor must: 118 | a) promptly notify the Commercial Contributor in writing of such claim, and 119 | b) allow the Commercial Contributor to control, and cooperate with the 120 | Commercial Contributor in, the defense and any related settlement 121 | negotiations. The Indemnified Contributor may participate in any such claim at 122 | its own expense. 123 | 124 | For example, a Contributor might include the Program in a commercial product 125 | offering, Product X. That Contributor is then a Commercial Contributor. If 126 | that Commercial Contributor then makes performance claims, or offers 127 | warranties related to Product X, those performance claims and warranties are 128 | such Commercial Contributor's responsibility alone. Under this section, the 129 | Commercial Contributor would have to defend claims against the other 130 | Contributors related to those performance claims and warranties, and if a 131 | court requires any other Contributor to pay any damages as a result, the 132 | Commercial Contributor must pay those damages. 133 | 134 | 5. NO WARRANTY 135 | 136 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED ON AN 137 | "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR 138 | IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, 139 | NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each 140 | Recipient is solely responsible for determining the appropriateness of using 141 | and distributing the Program and assumes all risks associated with its 142 | exercise of rights under this Agreement , including but not limited to the 143 | risks and costs of program errors, compliance with applicable laws, damage to 144 | or loss of data, programs or equipment, and unavailability or interruption of 145 | operations. 146 | 147 | 6. DISCLAIMER OF LIABILITY 148 | 149 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR ANY 150 | CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, 151 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION 152 | LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 153 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 154 | ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE 155 | EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY 156 | OF SUCH DAMAGES. 157 | 158 | 7. GENERAL 159 | 160 | If any provision of this Agreement is invalid or unenforceable under 161 | applicable law, it shall not affect the validity or enforceability of the 162 | remainder of the terms of this Agreement, and without further action by the 163 | parties hereto, such provision shall be reformed to the minimum extent 164 | necessary to make such provision valid and enforceable. 165 | 166 | If Recipient institutes patent litigation against any entity (including a 167 | cross-claim or counterclaim in a lawsuit) alleging that the Program itself 168 | (excluding combinations of the Program with other software or hardware) 169 | infringes such Recipient's patent(s), then such Recipient's rights granted 170 | under Section 2(b) shall terminate as of the date such litigation is filed. 171 | 172 | All Recipient's rights under this Agreement shall terminate if it fails to 173 | comply with any of the material terms or conditions of this Agreement and does 174 | not cure such failure in a reasonable period of time after becoming aware of 175 | such noncompliance. If all Recipient's rights under this Agreement terminate, 176 | Recipient agrees to cease use and distribution of the Program as soon as 177 | reasonably practicable. However, Recipient's obligations under this Agreement 178 | and any licenses granted by Recipient relating to the Program shall continue 179 | and survive. 180 | 181 | Everyone is permitted to copy and distribute copies of this Agreement, but in 182 | order to avoid inconsistency the Agreement is copyrighted and may only be 183 | modified in the following manner. The Agreement Steward reserves the right to 184 | publish new versions (including revisions) of this Agreement from time to 185 | time. No one other than the Agreement Steward has the right to modify this 186 | Agreement. The Eclipse Foundation is the initial Agreement Steward. The 187 | Eclipse Foundation may assign the responsibility to serve as the Agreement 188 | Steward to a suitable separate entity. Each new version of the Agreement will 189 | be given a distinguishing version number. The Program (including 190 | Contributions) may always be distributed subject to the version of the 191 | Agreement under which it was received. In addition, after a new version of the 192 | Agreement is published, Contributor may elect to distribute the Program 193 | (including its Contributions) under the new version. Except as expressly 194 | stated in Sections 2(a) and 2(b) above, Recipient receives no rights or 195 | licenses to the intellectual property of any Contributor under this Agreement, 196 | whether expressly, by implication, estoppel or otherwise. All rights in the 197 | Program not expressly granted under this Agreement are reserved. 198 | 199 | This Agreement is governed by the laws of the State of New York and the 200 | intellectual property laws of the United States of America. No party to this 201 | Agreement will bring a legal action under this Agreement more than one year 202 | after the cause of action arose. Each party waives its rights to a jury trial in 203 | any resulting litigation. 204 | 205 | --------------------------------------------------------------------------------