composable object testing
npm install ruler
ruler is meant for just simple, top-down testing; there is not need or desire to have a nested trees of "child" conditions, etc. You can build your tests via progamatically, or an array of objects with
value properties. The later of the two is ugly, but might be handy if you need to store the "rules" in a database or something.
Build assertions programatically:
var ruler = require'ruler'assert = require'assert';var engine = ruleris'name.first' 'john'not'name.last' 'buzz'contains'company' 'red'gte'age' 21;var result = enginetestname:first: 'john'last: 'doe'age: 21company: 'redventures';assertequalresult true;
Pass in an arrray of key/value rules:
var ruler = require'ruler'assert = require'assert';var obj =name:first: 'john'last: 'doe'age: 21company: 'redventures';var filters =cmp: 'is' path: 'name.first' value: 'john'cmp: 'not' path: 'name.last' value: 'buzz'cmp: 'contains' path: 'company' value: 'red'cmp: 'gte' path: 'age' value: 21;var engine = rulerfilters;var result = enginetestobj;assertequalresult true;
Rules sets can be created seperately for their own use, but then composed together in order to create more complex rules without having to create duplicated rules.
var ruler = require'ruler'assert = require'assert';var names = ruleris'name.first' 'john'not'name.last' 'buzz'var info = rulercontains'company' 'red'gte'age' 21;var all = namesandinfo;var result = alltestname:first: 'john'last: 'doe'age: 21company: 'redventures';assertequalresult true;
You can plugin custom "comparators" if the baked in one's do not fit your needs.
return actual * 2 == total;var engine = rulerusedoubled 'price' 40;enginetestprice: 20;
every()is probably slow.
Copyright 2012 Red Ventures
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this work except in compliance with the License. You may obtain a copy of the License in the LICENSE file, or at:
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.