Skip to content
This repository has been archived by the owner on Dec 5, 2019. It is now read-only.

Commit

Permalink
documentation in README, updated screenshot
Browse files Browse the repository at this point in the history
  • Loading branch information
dominictarr committed Jan 24, 2011
1 parent e73124b commit acfc853
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 28 deletions.
151 changes: 150 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,156 @@

#It-Is#

##a terse assertion DSL##
a terse assertion DSL, inheriting from node's assert module, but enhanced with the power of functional programming & colours!

# Basic Usage#

var it = require('it-is')

it(actual).equal(10)

# High-level Usuage #

`every` applies an assertion function to every item in a list.

it(arrayOfNumbers)
.every(it.typeof('number'))


`has` applies assertion functions to leaves of a tree.

it({a:1, b: 3})
.has({
a: it.typeof('number').notEqual(3)
, b: it.equal(3)
})

huh?

if you call `it` with an argument `it(actual)` assertions chained will be applied immediately.

it(actual).equal(expected)

is that same as

assert.equal(actual,expected)

if you don't provide an argument, but just start chaining `it.equal(expected)`, it returns a function which makes that assertion.

it.typeof('number').notEqual(unexpected)

returns a function like this:

function (actual){
assert.typeof(actual,'number') //not in node's assert. added in it-is
assert.notEqual(actual,unexpected)
}

pass these functions into It-Is's every and has for terse assertion easyness!

then glance at the error messages which are highlighted to show the exact point the assertion failed:

<img src="https://github.com/dominictarr/it-is/raw/master/screenshot.png" border = "0"/>

#Assertion Methods#

node's assert module methods:

`ok`,`equal`,`notEqual`,`deepEqual`,`notDeepEqual`,`strictEqual`,`notStrictEqual`,`throws`,`doesNotThrow`,`ifError`

and also:

##typeof##
assert type, expected can be 'string', 'number', 'boolean', 'object', 'function', or 'undefined'

it(actual).typeof(type)

##instanceof##
assert instanceof, expected should be a constructor function

it(actual).instanceof(constructor)

example:

it([]).instanceof(Array)

##primitive##
assert is not an object or a function

it(7).primitive()

##complex##
assert is an object or a function

it({}).complex()

##function##
assert is a function


it(function(){}).function()

##matches##
assert matches a regular expresson

it(actual).matches(regex)

example:

it('asdf@asdf.com').matches(/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/)

##like##
assert two strings match, but ignoring case, white space and whether quotes are " or '. (is configurable)

it(actual).like(expected, options)

options object is optional! but should be this format:

{case: boolean,whitespace: boolean, quotes: boolean}

##every##

apply assertions to every item in an array

it(array).every(assertion)

example:

it([1,2,3,4,5]).every(it.typeof('number').notEqual(0))

assertion is just a function

it([
[1,1.0]
, [1,1e0]
, [1,7/7]
]).every(function (line){
assert.equal(line[0],line[1])
})
##has##

apply asssertions to properties of an object, checking that properties and actually there first.

it(actual).has(properties)

if a property is primitive, it's checked for equality.
if a property is a function, it's called with actual's corrisponding property as the argument.

example:

it({
a: 1
, b: 2
, c: { x: true }
, d: [1,2,3,4,5,true,'string']
})
.has({
a: it.notEqual(0)
, b: 2 //values are treated like it.equal(value)
, c: it.complex()
, d: it.every(it.primitive())
})


enjoy!
54 changes: 27 additions & 27 deletions examples.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
var it = require('it-is') //defaults to colour

exports = module.exports =
{
'numbers': function (){
it(10000).equal(10001)
},
'strings': function (){
it('check string equality').equal('check string smameness')
},
'like (case/whitespace insensitive)': function (){
it('check STRING like').like('check\n string\n like S ')
},
'{} instanceof Array': function (){
it({}).instanceof(Array)
},
'function x(){} is primitive ': function (){
it(function x(){}).primitive()
},
'every one of 1,2,3,\'X\',4 is a number': function (){
it([1,2,3,'X',4]).every(it.typeof('number'))
},
'object {a: {b: 1}, z: 2} has .a.b which is equal to 10': function (){
it({a: {b: 1}, z: 2}).has({a: {b: it.equal(10) } })
},
'object {a: {b: 1}, z: 2} has .a.b which is equal to 10': function (){
it({a: 1, b: {} }).has( {b:{c: {} } } )
}
{ 'numbers':
"it(10000).equal(10001)"

, 'strings':
"it('check string equality').equal('check string smameness')"

, 'like (case/whitespace insensitive)':
"it('check STRING like').like('check\\n string\\n like S ')"

, '{} instanceof Array':
"it({}).instanceof(Array)"

, 'function x(){} is primitive ':
"it(function x(){}).primitive()"

, 'every one of 1,2,3,\'X\',4 is a number':
"it([1,2,3,'X',4]).every(it.typeof('number'))"

, 'object {a: {b: 1}, z: 2} has .a.b which is equal to 10':
"it({a: {b: 1}, z: 2}).has({a: {b: it.equal(10) } })"

, 'object {a: {b: 1}, z: 2} has .a.b which is equal to 10':
"it({a: 1, b: {} }).has( {b:{c: {} } } )"

}


Expand All @@ -36,9 +35,10 @@ if(require.main == module)
for (var i in exports){
Error.stackTraceLimit = 1
try{
exports[i]()
eval(exports[i])
} catch (error){
console.log('~',i)

console.log("> " + exports[i])
console.log()
console.log(error.stack)
console.log()
Expand Down
Binary file modified screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit acfc853

Please sign in to comment.